---
# System prepended metadata

title: Mini Project 0 Answers

---

# Mini Project 0 Answers

## Part 1
Q1. What is the maximum length of the input message?
[DJ] You can assume a maximum length of 4096 characters.

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?)
[DJ] You can use this library for functions like strlen, and strcmp but you are expected to stick to scanf and printf for taking user input and printing output to the user. If there are any other functions, you wish to utilize, please mention them seperately.

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)?
[DJ] The user is supposed to enter this.

Q4. On each run does newfile.txt get cleaned up or the previous runs data is stored and hence printed on PRINT command?
[DJ] Either way is fine. You can mention what you did in a README file (in your repository).

Q5. Are we allowed to use `fgets()` to take input from terminal?
[DJ] No, you can scan a string with spaces using scanf only. There is no need to use fgets.

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?
[DJ] Either way is fine. You can mention what you did in a README file (in your repository). We are not going to be focusing on edge cases for this Mini Project.

Q7.Are we allowed to use getchar() along with scanf()?
[SJ] No , you can use scanf only to take input.


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
```

[DJ] Yes, this is fine.

Q9. Can we use the functions fputs, fputc, and fgetc?
[DJ] No. If there are any specific reasons as to why you want to use these, elaborate further.

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]
[DJ] Yes, this is the correct understanding. "INPUT" reads until it encounters a new line character. Refer to Q.8

Q11. After each run should we clear the data in the file or is it fine if we overwrite the contents? [Refer to Q4]
[DJ] Answered already in Q4.

Q12. Is it necessary to use a single file descriptor, or can we use multiple file descriptors?
[DJ] It's necessary to use one only.
[DJ] Update: We do not care about how many file descriptors you use as long as the program does not crash.

Q13. Can we use strcat to append '\n' to input string to write into the file?
[DJ] You are free to do string manipulation.

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.
~~[DP] Yes. You can use these system calls.~~
[DJ] Please note that you can only use create in this part. You are NOT allowed to use the read and write system call.
![image](https://hackmd.io/_uploads/B1ktcXRFR.png)
Source: Mini Project 0 page on course website.
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
```
[DJ] Yes, this is fine.

Q16. What is the weightage of mini project 0 in course total?
[DJ] This is irrelevant.

Q17. Kindly specify the input format.
[DJ] We have given an example already in the Mini Project. I have also answered 2 doubts regarding this which are acceptable formats (Q8 and Q15). Please don't keep asking redundant questions.

Q18. Are we allowed to use fflush() for this part ?
[DJ] You can only fflush to `stdout` if you are using fflush.

Q19. Are we allowed to use freopen?
[DJ] Yes.

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 ?
[DJ] It has been mentioned clearly several times already, you can only use printf and scanf.

Q21. Are we allowed to use dup and dup2?
[DJ] Yes.

Q22. Are we allowed to use open ?(instruction taught in tut)?
[DJ] Yes.

Q23. Are we allowed to use <sys/stat.h> header file and struct stat from it will mention its use in doc?
[DJ] Yes, that is fine.

Q24. Are we allowed to modify stdout using freopen()?
[DJ] How are you planning to restore stdout???

Q25. Are we allowed to use syscall?
[DJ] No.

Q26. Are we allowed to use fopen and fwrite?
[DJ] fwrite is not allowed. It is recommended to stick to open instead of fopen since it's a lower level function.

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.
[DJ] Go through the tut slides. The required system call is literally mentioned in the list and someone has already asked regarding that here.
`Hint: You are supposed to be duplicating the file descriptors for STDIN and STDOUT.`

Q28. Are we allowed to use lseek() and memset()?
[DJ] Why do you need this? Please elaborate.

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?
[DJ] Sure, there might be some other issues with your code but as long as you are using printf and scanf for I/O, this should be fine.

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?
[IG] lseek is not necessary for that.
     `Hint: Opening/reopening a file places the cursor at beginning.`
     
Q31. I'm a bit confused with what functions are not allowed. Are read(), write() and syscall() not allowed?
[IG] It has been clearly mentioned in answers for Q26 and Q14 that they are not allowed. Please read the doubt doc to avoid redundant questions.

Q32. Can we use freopen() for restoring stdout and stdin? We can make terminal(/dev/tty) back to stdout again using freopen()?
[IG] Yes,you can but it is not required to solve the question.

Q33. Is it fine if we create the file on typing the command INPUT and not at the beginning?
[IG] It is clearly mentioned in the requirement 1 of the question to create the file on startup.

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()?
[IG] No, it is not allowed.

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?
[DJ]
a. Both
b. You are not allowed to use any functions which allow you to specify the file descriptor in any way. Whether it be dprintf, fprintf, etc.
c. No, but we do not want to focus on extreme test cases in this assignment so you can expect the character limit to be reasonably small (that is, do not worry about it).

Q36. What can be the maximum size of the file in total after all the input messages are written to the file?
[DJ] You can expect it to be reasonably small, such that you will not have to worry about it.

Q37. Is it ok to append the input messages in the same line in the file and printing in a single line?
[DJ] Please include newline characters.

Q38. Are we allowed to use fork for this part
[DJ] No

Q39 . Are we allowed to use getchar function to clear the newline left in the buffer?
[DJ] Sure.

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?
[DJ] I will be honest, we do not care about either ways in both Q12 and Q30. Neither of them defeat the purpose of the assignment. However, I fail to see how they are contradictory. You can open and close a file descriptor in conditional blocks for both INPUT and PRINT. This essentially causes only one of the file descriptor to exist at a time. If you include the saved file descriptor in the count, then you will obviously need more than 1 file descriptor to be open at a time (I have also updated answer to Q12)

Q41. Can we use exec() functions to read the text file?
[DJ] No.

Q42. Can we open and close the file multiple times [refer from : Q30] ?
[DJ] Yes.

Q43. Can we use fork , exec and shell command like cat for this part?
[DJ] No.

Q44. Are we allowed to use scanf() to read text from the file?
[DJ] Yes.

Q45 . Are we allowed to use execlp()?
[DJ] No.

Q46. If we used dup for stdout and stdin, we will need multiple file descriptors to restore them. is that allowed?
[DJ] Yes.

Q47. Can we use feof() for checking the end of file?
[DJ] Yes.

Q48. Are we allowed to use exec and cat
[DJ] No.

Q49. Are we allowed to open and close the file multiple times during reading and writing?
[DJ] Yes.

Q50. Are allowed to use lseek?
[DJ] Yes.

Q51. Are we allowed to use perror?
[DJ] Yes.

Q52. It seems answer to Q.39 and Q.7 are conflicting. So, we can use getchar() right?
[DJ] For taking input? No. But Q39 clearly mentions that they need getchar for clearing buffer which is fine.

Q53. Can we use the STDOUT_FILENO macro to handle opening and closing of stdout?
[IG] Yes.

## Part 2
Q1. How do we do all the tasks in a single file?
[MS]When you read the question , you observe that for single parent , you must create three children.Create a child, perform the task of the child , after the task , you can create another child again and so on.

Q2. In this part are there any restrictions on the usage of system calls like in part-1?
[IG] Yes. Please use only fork, wait, write,open, getpid and exec for this part.

Q3. Can we use the `kill` system call?
[IG] 

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?
[IG] Yes.

### Task 1
Q1. Are we allowed to use `printf()`? Do we have to print the values of $x$ before and after changing?
[MS]Yes you can.Print values as asked in question.

Q2. Can we kill the child process that is created in first task?
[MS]yes.

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?
[MS]yes.

### 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?
[MS]you can do it if you want,But there is no need of converting into string.

Q2. Can we use shell commands like `touch` and `echo` to create a new file while using the `exec` comm
[MS]yes.

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?
[MS]As given in question, with `exec` command you need to create the file and then write it.You can refer to the above question to get the idea.

Q4. Can I use `getppid()` to get PID of parent?
[MS]No.

Q5. Can I use `execl()` syscall instead of `exec()` syscall?
[MS]execl() belongs a family of exec() command.Read man page for exec() to get better idea.Yes you can.

Q6. Do we have to create and write using exec, or can we open the file first and then write to it using exec?
[MS]Use exec() to create the file.then write to the file u.You can write normally by opening using file descriptor.

Q7. Asking in refernce with the answer given to Q6 - Means we can use write() to write in the file?
[MS]Yes.here you can use write. In the first part you are not allowed to use.

Q8. Can we use `fflush()` to forcibly flush the input to the file. Else, the input is being buffered.
[MS]You can.

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?
[MS]Read the qestion again in mini project. It is given that child only should create the file and write.

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?
[MS]don't compilcate it.Using exec create the file , then write into the file u.You should only write parent process id.If you want you can.

Q11. Can we use shell redirection to write directly into the file instead of using the write system call?
[MS]~~No.Use write system call~~ yes.

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 ?
[MS] you can use `echo`.

Q13.Can we use file descriptor for writing?
[MS] you can.

Q14. Can I use `execlp()`?
[MS] family of exec() is allowed. SO, execlp() is allowed.

Q15. Can we use system calls `write()`, `open()`, etc. to create and modify the file?
[MS]Yes

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.
[MS]if you can't do in parent process, create a child process to do that.
you can do in approach as question 10.
you can use exec() and create shell image then give commands.

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?
[MS] yes.

Q18. Can we make multiple child processes for this task?
[MS] yes.


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? 
[MS] yes.

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?
[MS] check question 10 and 16.

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 
[MS] you can.

Q22. Can we use global variable to store the parent pid?
[MS] you can.

Q23. Can we also use `sh` with `exec` ?
[MS] you can.



### 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?
[DP] Refer Part 2 Question 1. You can create three separate children performing the tasks sequentially(one after another).

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?
[DP] This is for your learning. You can mention the answer as comments in your code or in a README file.

Q3. Can I use getppid() to get PID of parent?
[MS] Yes.

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?
[MS]YES

Q5. Can we use the fact that process id of parent would be constant and print that in child process?
[MS]The fact is that it changes. It is not constant if you kill parent.

Q6. Should the child process terminate the parent process or parent process should be killed by itself?
[MS]Here you can use both ways but ensure that the parent is killed after forking.You can check this website https://www.scaler.com/topics/operating-system/zombie-and-orphan-process-in-os/ .Observe how they kill or terminate the parent process.

Q7. Can the `kill()` command be used?
[MS] yes.

Q8. In reference to the answer of Q6, the link allows the usage of `sleep()` function. Are we allowed to use that as well?
[MS]yes.

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?
[MS]please read your question again.The parent process is killed.then how will you print it as parent of the child?.Please print the value which is current parent.

Q10. If we use `sleep()`, the parent process will be killed atleast 1 sec after forking. Is that allowed?
[MS] you can.

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)
[MS]fine.

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?
[MS] your first statement is correct.It will be init.Don't confuse with question 5.There constant means before and after killing the parent.

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)?
[MS]No.you should get the root process id or init process id only.


## General
Q1. Are we allowed to use `write()`, `creat()` and `read()` function calls?
[DJ] Please keep such questions in the section for the related part.

Q2. 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?
[AD] Sharing the prompt is a foolproof method to verify the usage of AI. Sharing the screenshots is compulsary (along with the prompt, there will be more guidelines about the same). However, if you want to add a link to the chat (along with the screenshots), it is your personal choice.

Q3. Can we use exit() for both the parts? Also can we use exit(EXIT_SUCCESS) like commands?
[DJ] Yes.

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.
[DJ] Refer to Q2. **Screenshots are compulsory**. Please arrange them in a pdf file.

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?
[DJ] It's a good idea to use a debug flag for these things if you are using it for debugging but it's fine.

Q6. Can we add a folder/pdf containing screenshots into the miniproject-0 folder??
[DJ] Yes, please add all the relevant screenshots in a pdf and push them with your submission before the deadline.



