* # Part 1
Q1. What is the maximum length of the input message?
Q2. Are we allowed to use the string.h library?( Especially for asking the user which commmand they want to enter, INPUT, PRINT and so on?)
Q3. Are we supposed to prompt the user for input by printing "INPUT -> " or is that something the user will enter? And if we are supposed to prompt then how do we handle the PRINT command (is "INPUT -> PRINT" the correct way)?
Q4. On each run does newfile.txt get cleaned up or the previous runs data is stored and hence printed on PRINT command?
Q5. Are we allowed to use `fgets()` to take input from terminal?
Q6. What should we do if only `\n` is given as input? Should the program ignore that and wait for the next set of non newline characters to be given or should the file also conntain the newlines that were given as input?
Q7. Are we allowed to use `getchar()` along with `scanf()`?
Q8. So the sample run would be something like this?
```
Enter a command: INPUT
Enter your message: Hello, world!
Enter a command: PRINT
Hello, world!
Enter a command: STOP
```
Q9. Can we use the functions `fputs`, `fputc`, and `fgetc`?
Q10. According to my understanding is it like we enter INPUT and we give an input to the file, we enter PRINT and it prints the contents of the file, we enter STOP and the program ends? Is it correct? *\[Refer to Q3\]*
Q11. After each run should we clear the data in the file or is it fine if we overwrite the contents? *\[Refer to Q4\]*
Q12. Is it necessary to use a single file descriptor, or can we use multiple file descriptors?
Q13. Can we use strcat to append '\n' to input string to write into the file?
Q14. Are we allowed to use write(), creat() and read() function calls in all the questions?I wanted to use this to read and write to the file.
Q15. Is this correct?
```
Enter a command: INPUT
Enter your message: Hello, world!
Enter a command: INPUT
Enter your message: MP0
Enter a command: INPUT
Enter your message:
Enter a command: PRINT
Hello, world!
MP0
Enter a command: STOP
```
Q16)What is the weightage of mini project 0 in course total
Q17) Kindly specify the input format
Q18) Are we allowed to use ```fflush()``` for this part ?
Q19. Are we allowed to use freopen?
Q20) Are we supposed to not use function that takes file descriptor as like syscall, mmap to read and write to a file or we are not allowed to use only read and write commands ?
Q21) Are we allowed to use dup and dup2?
Q22) Are we allowed to use ````open```` ?(instruction taught in tut)?
Q23) Are we allowed to use ````<sys/stat.h>```` header file and struct stat from it will mention its use in doc?
Q24) Are we allowed to modify stdout using freopen()?
Q25) Are we allowed to use syscall?
Q26) Are we allowed to use fopen and fwrite?
Q27) If We are not allowed to use Syscall like read(),write(), Are we suppose to use syscall() or any other library functions? Please Mention Clearly. In Tutorial We have been taught write() function.
Q28) Are we allowed to use lseek() and memset()?
Q29). Can we use `clearerr()` function. I need to use this because when I read from the file, the EOF character comes to the stdin. So, to remove the EOF flag can I use this syscall?
Q30) We will need to use lseek for resetting the cursor after reading it once or after appending the new statements put via input. Is it allowed?
Q31. I'm a bit confused with what functions are not allowed. Are `read()`, `write()` and `syscall()` not allowed?
Q32. Can we use freopen()? for restoring stdout and stdin we can make terminal(/dev/tty) back to stdout again using freopen().
Q33. Is it fine if we create the file on typing the command INPUT and not at the beggining
Q34. Is it fine if we use fgets() or sscanf() just to read the text file (and nowhere else) and return the final output using printf()?
Q35.a) You have stated above that we cannot use read and write to perform I/O. Does this also apply to file operations or to both file operations and I/O?
b) Are we allowed to use mmap, munmap, msync, memcpy, close(fd), fstat, ftruncate, lseek, and dprintf to perform file operations and check file status?
c) Is there a limit to the maximum number of messages that will be stored in the file?
Q36. What can be the maximum size of the file in total after all the input messages are written to the file?
Q37. Is it ok to append the input messages in the same line in the file and printing in a single line?
Q38. Are we allowed to use ```fork``` for this part
Q39 . Are we allowed to use getchar function to clear the newline left in the buffer
Q40. The answers to Q12 and Q30 seem contradictory to me. So I would like to clarify if it is okay to close and reopen the file as long as the integral value of the file descriptor remains the same?
Q41. Can we use exec() functions to read the text file?
Q42. Can we open and close the file multiple times [refer from : Q30] ?
Q43. Can we use ```fork ```, ```exec``` and shell command like ```cat ```for this part?
Q44. Are we allowed to use `scanf()` to read text from the file?
Q45 . Are we allowed to use `execlp()`?
Q46. If we used dup for stdout and stdin, we will need multiple file descriptors to restore them. is that allowed?
Q47. Can we use `feof()` for checking the end of file?
Q48. Are we allowed to use exec and cat
Q49. Are we allowed to open and close the file multiple times during reading and writing?
Q50. Are allowed to use lseek?
Q51. Are we allowed to use `perror`?
Q52. It seems answer to Q.39 and Q.7 are conflicting. So, we can use `getchar()` right?
Q53. Can we use the `STDOUT_FILENO` macro to handle opening and closing of stdout?
# Part 2
Q1. How do we do all the tasks in a single file?
Q2. In this part are there any restrictions on the usage of ssytem calls like in part-1?
Q3. Can we use the`kill` system call?
Q4. Is it alright if we create 3 different functions for each of the tasks instead of creating children for them, so that the code is structured better?
## Task 1
Q1. Are we allowed to use `printf()`? Do we have to print the values of $x$ before and after changing?
Q2. Can we kill the child process that is created in first task?
Q3. Given the context of the commands we can use in this task, can we use *wait()* to let the child complete execution and let the same parent process mange all three tasks sequentially?
## Task 2
Q1. Can we use functions like `sprintf` or `snprintf` to convert an `int` to a string or write a function that does this manually? Can we use it to store or format a command?
Q2. Can we use shell commands like `touch` and `echo` to create a new file while using the `exec` command? If not, what commands or syscalls can we use?
Q3. Since, we have to use the `exec` command, can we have a new file with some C program in it to execute the logic as per the question? or should we find a way to do it in the same C program file using `exec` call?
Q4. Can I use `getppid()` to get PID of parent?
Q5. Can I use `execl()` syscall instead of `exec()` syscall?
Q6. Do we have to create and write using exec, or can we open the file first and then write to it using exec?
Q7. Asking in refernce with the answer given to Q6 - Means we can use write() to write in the file?
Q8. Can we use `fflush()` to forcibly flush the input to the file. Else, the input is being buffered.
Q9. Considering the already answered doubts, are we fine if we do something like, creating the new file using the exec() syscall from the child and then writing in the parent process?
Q10. Can we get more clarity on this task whether we are allowed to make another C program file(file1) which would create a file(newfile.txt) and run this file(file1) using exec from child process of the main file?
Q11. Can we use shell redirection to write directly into the file instead of using the write system call?
Q12. Can we use the `echo` to both create and write to the file or we need to complusory use write function to write to the file ?
Q13.Can we use file descriptor for writing?
Q14. Can I use `execlp()`?
Q15. Can we use system calls `write()`, `open()`, etc. to create and modify the file?
Q16. How can we use write directly after `exec()` in the same child process? Code after `exec` is not read and in someone's query above, you stated that we cannot do the file i/o in the parent process. It is becoming very confusing, please give a hint.
Q17. In the parent process can we use anything to store the pid of the parent and use it in the child process like in parent process write the id to a file and in child process read from that file and delete it or any global variables?
Q18. Can we make multiple child processes for this task?
Q19. Regarding question 6, can i just use `exec` function to run the shell command and echo the pid into the file, without using the ```write()``` syscall explicitly?
Q20. Can we fork once more in the child process, then creating a file using `exec` in child's process and writing to file in the inside parent process of newly created child? If this is not supposed to be done, then can you please give a hint on how to proceed as the statements after the `exec` are not read?
Q21. After using the exec() in the child process to create a newfile.txt it will replace the child process with process of file creation ,are we supposed to create a new child process to write into that file
Q22. Can we use global variable to store the parent pid?
Q23. Can we also use `sh` with `exec` ?
## Task 3
Q1. I have written the first two tasks as separate pieces of code. Could you please clarify how we are supposed to submit all 3 tasks in one file?
Q2.
> Compare the value of the parent process ID which is stored in the file and printed by the third child. What do you observe?
How is this supposed to be a part of our program?
Q3. Can I use getppid() to get PID of parent?
Q4. Isn't it necessary to use getppid() in task 3 because after parent process is killed the child process would be assigned a new parent process?
Q5. Can we use the fact that process id of parent would be constant and print that in child process?
Q6. Should the child process terminate the parent process or parent process should be killed by itself?
Q7. Can the `kill()` command be used?
Q8. In reference to the answer of Q6, the link allows the usage of `sleep()` function. Are we allowed to use that as well?
Q9. Are we supposed to print the process id of the process that created the child (the one that has been terminated) or the process id of the process the orphan process is assigned to after its original parent has been killed?
Q10. If we use `sleep()`, the parent process will be killed atleast 1 sec after forking. Is that allowed?
Q11. If we use sleep() the program is printing the parent id after exiting(just a second after exiting) is that fine? (the reference you have given in Q6 is also printing after exiting)
Q12. After viewing the contents of the link provided in the Q6 the parent process id of the orphan process is said to be the init process id (which is usually 1) but in Q5 it is mentioned that parent process id is not constant?which statement is correct as I am always getting 1 as the ppid of the orphan processs?
Q13. With reference to Q12, is it correct if the parent we are getting later on is not init but some other number(which is different ofc from the initial parent id)?
Q14. Regarding Q13, according to the following link, the adoptive parent need not necessarily be the init process(PID 1) if the child outlives its parent. https://unix.stackexchange.com/questions/194182/orphan-processs-parent-id-is-not-1-when-parent-process-executed-from-gnome-term
# General
Q1. Are we allowed to use `write()`, `creat()` and `read()` function calls in all the questions?
Q2. (while using AI tools)Will it be fine if we just put the screenshot of relevant answer rather than the entire prompt? Are we allowed to share the chat link?
Q3. Can we use ```exit()``` for both the parts? Also can we use ```exit(EXIT_SUCCESS)``` like commands?
Q4. [Regarding AI Tools] How exactly should we cite the use of AI tools. Can we simply add a link to the chat in our readme? Or should we create a pdf with screenshots and add a link to that/add screenshots directly to the readme.
Q5. I currently have a lot of input prompt messages, print statements like ("My child's pid is %d"), or ("The value of x is now %d") and many other such print ouput statments. Is it okay to have such extra statements, or are we just supposed to print the quantity directly?
Q6. Can we add a folder/pdf containing screenshots into the miniproject-0 folder??
Q7. Is it okay to create a zip file containing the programs and a pdf, both separately commited to the parent repo?
Q8. Does sharing the ChatGPT link suffice or are screenshots also necessary?