---
# System prepended metadata

title: Assignment 0 Answers

---

Q1: Can we get list of all fucntions or function types which are not allowed??
[PD]: You can only use `scanf` and `printf`.

Q2: For Assignment 0, are we supposed to push both the codes in the main branch, or create separate branches for each question on github?
[PD]: Please refer to the document shared. It specifies a file structure.

Q3: Also are we supposed to push the code to github at every few lines of code we write, or just at the end? Is there any rule to it?
[PD]: No restrictions on number of commits.

Q4 & Q5:
[PD] Only use `scanf` and `printf`. (For Q5: It is possible but we'll clarify once if we can allow `getline()` or not)
[PD]: Do not use getline().

Q5: Is getline() allowed for I/O? It seems impossible to read an entire line in INPUT with just scanf(), with a variable number of words in the line.
[SG] Dont under estimate scanf()

Q6: Is fopen allowed?
[PD]: You can use `open`, `close` only for opening or closing files.

Q7: Can we use freopen?
[PD]: Refer to Q6.

Q8: If files are to be opened with open(), then is write() allowed?
[PD]: `write()` is not allowed.

Q9: Can we use freopen for redirecting input output streams though? apart from just opening or closing files?
[PD]: `freopen` is not allowed.

Q10 : Can we use sprintf or itoa function to concert pid from integer to string
[PD]: The assignment has to be done in C and avoid sprintf.

Q11: can we use mkdir()?
[PD]: Yes

Q12: Can we use fputc?
[PD]: No

Q13: The part 2 has 3 tasks. The submission format mentions 2 files processManagement.c and writer.c. While its clear that task 2 executes writer.c code are the tasks suppsed to be written as separate functions in processmanagement.c so they can be executed individually?
[PD]: Yes
[SG]: you have to do all 3 task in a single file
Q14: printf and scanf generally writes to stdin and stdout. To redirect that to a specific file, we need to use freopen(), and then use printf and scanf to write or read from any file. There is no other way to use only printf and scanf to write to a file when it only writes to stdin and stdout. Could you pls confirm that freopen() can not be used for this task only?
[PD]: `freopen()` is not allowed :)

Q15: Can we use ```dup()``` and ```dup2()``` for Part-1?
[PD]: Yes

Q16: In what order do the ```LAST <n>``` lines from content.txt have to appear? latest first or latest last? similarly for the ```LOG <n>``` questions.
[PD]: Latest last

Q17: Can we use ```syscall()```  for part-1 ?
[PD]: No

Q18: Can we pass arguments while executing writer in part-2 task 2?
[SG]sure

Q19: Can we use to_string() to convert integers to strings?
[PD]: Yes (idk if you can in C)

Q20: Can the constraints for n in part-1 be mentioned?
[PD]: Assume it to be 1024

Q21: Can we use `strcat()`?
[PD]: Yes

Q22: In the exec() syscall, are we allowed to utilize any bash command to accomplish a task or are there any restrictions?
[SG] you have to make writer.c , compile it to get "./writer" and use this executable to accomplish the task

Q23: Are we allowed to use <string.h>? functions like strcat, strlen,etc, as required?
[PD]: Yes you can



Q24: As a workaround to the restrictions, can I use `fgetc` and `fputc` to read and write into my files?
[PD]: No

Q26: Can we use system calls like `Create` ,`Open`,`Close` ,`Read` and `Write`?
[PD]: Read the doc (cannot use Read or Write)

Q27: since we are allowed ```dup()``` and ```dup2()```, can we also use ```fflush()``` for writing the data from the stdout buffer to the destination?
[PD]: Yes you can

Q28 : Do the part 1 restrictions also apply to part 2?
[PD]: No

Q29: Are the write() or open() function allowed, since its not a part of the f* function family?
[PD]: You can use `open` but `write` is not allowed.

Q30: Is shell-redirection allowed, in execvp?
[PD]: Could you elaborate?

Q31: For part-1 `INPUT` command, is it necessary to preserve leading spaces if the user inputs them, for e.g `    hello` should be written to the file as `    hello` or is it fine if its written as `hello`?
[PD]: Preserve spaces.

Q32: In Part 2: There are `three tasks` which require three separate `c` files for them to execute properly. Can we submit separately when uploading to github? or does it HAVE to be only `processmanagement.c` and `writer.c` files.
[SG]you have to do all 3 parts in a single file , do them sequentially in order

Q33: In Part 1, can we assume that we will never encounter the same PID - or if not, should we erase the existing data of content.txt and logs.txt?
[PD]: Yes

Q34: can we use snprintf ?
[PD]: No

Q35 Can file manipulation methods like lseek be used in the program
?
[PD]: Yes

Q36 Can we use the exec family of commands?
[PD]: For Part A no for Part B yes

Q37 Is n always positive for FIRST and LAST commands in Part-1? Also can we use getchar() to clear the input buffer?
[PD]: Use `fflush()` and yes `n` is always positive.

Q38 Should we handle the case where n would be greater than the number of commands input?
[PD]: Yes. If you encounter such situation just print all the contents of the file.

Q39 Can we use feof?
[PD]: Yes

Q40 Can there be empty lines in the `INPUT` command for Part-1?, e.g. user enters the INPUT command and on the input prompt just presses enter without any other characters? If yes, then are we to preserve it?
[PD]: You can skip this.

Q41. For Part-2, is it alright if we create a while-based choice system that lets the user choose which task to run? Or should we just hardcode the running of all 3?
[SG] just do all 3 parts in a single file (you can make functions for each part and call these functions sequentially)

Q42: For part-2: what is printing format like shall we only print pids and value of x or we have to print like
"Value of x is :"?
[SG]  "Value of x is :" (this is better , easy to comprehend , write informative print statements)

Q43: For part-1, it is not mentioned in the assignment doc that exec family of commands must not be used and exec commands were taught in the tutorial too, is it okay to use them??
[PD]: Refer to Q36

Q44. Are we allowed to invoke shell while using execvp to redirect the data from stdout to the text files?
[PD]: Refer to Q36

Q45. Is execvp allowed for part 1? (only execvp, no other member).
[PD]: Refer to Q36

Q46. What are the restrictions for part 2 (if any) if the ones for part 1 dont apply here? Are we still not allowed to use write(), snprintf(), freopen() etc..?
[SG] for part2 , you can use these functions

Q47. Will the number of input lines be more than the maximum value of long long int?
[PD]: No

Q48. In part-1, should we handle the case where in a testcase, `FIRST 4` appears before `INPUT text`?
[PD]: Yes in case the text file is empty do not print anything

Q49. I remember sprintf being allowed for int to string conversion in C. However the answer here has changed. Just for clarification, is sprintf still allowed for int to string only?
[PD]: No do not use sprintf. Converting PID to string should be easy.

Q50. If we can use open(), can we also use fopen()
[PD]: open() is a sys call and the aim of the assignment is to be familiar with low level system calls

Q51. Do we have to run all tasks in Part 2 simultaneously or can we run each task separately?
[SG] refer to  Q41

Q53. Citing Q37. fflush seems to work only for output buffer like for stdout and stderr and not for stdin. Given this can we use getchar()?
[PD]: Just use scanf.

Q54. Can we use the system() command for Part-1?
[PD]: No

Q55. In part-2 is it fine if the child process spawned also performs some of the tasks, say Task 1 is performed Now the parent and Child both will perform Task 2 and then perform Task 3? 
[SG]i dont understand how parent and child both are performing task2  , please elaborate

Q56. Are we allowed to use mkdir() to make a new directory in Part-1?
[PD]: Yes

Q57. Are we allowed to use system() function in part-1?
[PD]: Refer to Q54

Q58. Will the correction be done manually or an automated script will run? Basically, can we print some extra lines like "Enter you input here: " for example.
[PD]: TBD and please don't

Q59. Part 1:Can we use clearerr()?
[PD]: Yes

Q60. Is there any restriction on size of input string in part 1?
[PD]: Assume 1024

Q61. Can the number of commands entered exceed the maximum value of LONG LONG INT?
[PD]: No

Q62. Can we use system function in PART-2 of the assignment?
[SG]No

Q66. Does the input in Part 1 have maximum size?
[PD]: Assume it to be 1024

Q69. Could you please elaborate what is meant by "proper error handling" (under Additional Notes subheading on KV github io)? Are we expected to try-except? Do we have to do it for all system calls?
[PD]: Just print something meaningful if you encounter such errors.

Q70. In Part-1, LAST n/FIRST n/LOG n, can n be negative? Are we supposed to print "Unknown Command" if so?
[PD]: n is always +ve

Q71. In Part-1, are "   abcd" and "abcd" considered two different inputs, i.e, do we have to consider spaces at the start of the string?
[PD]: Yes

Q72. In Part-1, are we supposed to consider commands like ```    INPUT``` (leading spaces) or ```INPUT    ``` (trailing spaces) valid?
[PD]: No

Q73. In Part-1, can the input for the command (the user input right after "Enter command") have spaces, do I have to process inputs like "INPUT :D"?
[PD]: No

Q74. In Part-1, are we allowed to use the setvbuf  function?
[PD]: Yes

Q75. Regarding the response of Q13, what do you mean we have to do all 3 tasks in a single file?
[SG] i mean write code for all three parts in a single file

Q76. Regarding response of Q33, 'yes' for what?
[PD]: You do not need to handle such cases you can assume before running the program folder does not exist

Q77. Regarding response for Q39, if we can't use fopen, how can we use feof?
[PD]: Don't use it then

Q78. do we need to compile the writer.c as well or we can just assume that the executable exists? 
[SG] you have to compile it , then only it will run using exec

Q79. can we use fopen, read, write directly in q2(task2)?
[SG] for q2 you can use these functions

Q80. Whats the maximum number of lines we can have? ik some qs were asked before that said it wont be more than long long int, but is there a fixed smaller limit?
[PD]: No

Q81. Can we use fopen() in Part-2?
[SG]check Q79

Q82 Can we use normal puts gets  and posix spawn in part1
[PD]: No

Q83 are we allowed to invoke bash in p1(without using fork or exec functions)
[PD]: No

Q84 in part2, task3, is it okay if the terminal stays open after both the parent and the child exit?
[SG] yep

Q85 In part1, can we use chdir()?
[PD]: Sure

Q86. What are the restrictions for Part - 2? 
[SG]: just follow the specifications mentions , it is pretty clear

Q87 Can we use putc()?
[PD]: No

Q88. In part 1 can we use setvbuf?
[PD]: Why?

Q89. Can we use ```rewind()``` to reset ```stdin``` to beginning of the file in part 1 ?
[PD]: Just close the file and open again.

Q90. In the Task 3 of the part - 2, the "parent PID printed by the child in Task 3 (after orphaning)" is happening but after that particular terminal exits. Is that fine? Basically, the terminal looks like:
C:\osn\task  $ output 1
                output 2
C;\osn\task  $ output 3
This happening cuz there is a delay and that delay is printing the output to the terminal after it exits.
[SG] thats fine

Q91. Is itoa function allowed in both the parts?
[PD]: No you cannot

Q93. do we have to consider errors like some error encountered while using dup2 or some error encountered while making directory? if yes, then should we just print error encountered?
[PD]: Print appropriate errors

Q94/95/96. 
[PD] No

Q97. Are we to wait for the child processes to end before running the next task for Part-2 Task 1 & 2, where we are not required to orphan them?
[SG] what do you think , yes ofc

Q98. Are we allowed to create another child process to compile the writer.c file or should we precompile it for P2T2?
[SG]precompiled is fine



Q100 Are we supposed to declare LLM usage in the README file ?
[PD]: Yes

Q106. In task 3 of part 2 my orphaned process is not being adopted by process id 1 but rather a different process,due to modern subsystem linux architecture,is that a problem?
[SG] that is fine

Q107: Are we allowed to upload the binary files along with the codes?
[VG] Not needed we will anyway compile again.

