System Programming Lab assignment#02 FIP 2A - TPS UdS Question 2: fork() and files Write a C program to simulate the following scenario: 1. The parent opens a file, for example ”file.txt”, using the system call open() 2. The parent creates a child using the system call fork() 3. The parent calls the system call wait() to wait for its child process to terminate 4. The child uses the file descriptor, returned to the parent by open(), to display on the screen the content of ”file.txt”. This task should be done using the read()/write() system calls. 5. The child sleeps for 5 seconds, prints the message ”Child terminating”, sleeps for another 5 seconds and, finally calls the exit(20). 6. The parent, once awake, prints the message ”My child has terminated” and prints the status value returned by the child. 7. The parent terminates Question 3: fork() Write a C program to simulate the following scenario: 1. The parent creates a child process. 2. The parent goes inside an infinite loop. 3. The parent opens the file ”temp.txt” for reading (using fopen()). If successful, the parent closes the file, sleeps for 1 second then opens the file ”temp.txt” for reading again until unsuccessful (file not existant). This can be done using the loop below: 1 while(f=fopen("temp.txt", "r")){ fclose(f); sleep(1); } 4. The parent reads two numbers and a binary operator(+, -, * or /) from the user. 5. The parent creates the file ”temp.txt” and saves the data in the file. This can be done using. The parent closes the file afterwards. fprintf(f, "%f %f %c\n", a, b, op); 6. The parent will sleep for three seconds. 7. The child goes in an infinite loop 8. The child opens the file ”temp.txt” for reading (using fopen()). If not successful, the child sleeps for 1 second then tries again until success. This can be done using the loop below: while(!(f=fopen("temp.txt", "r"))) sleep(1); 9. The child reads two numbers and the operator from ”temp.txt”, closes the file, deletes it using the system call unlink("temp.txt"), then perform the appropriate arithmetic operation. 10. The child sleeps for 1 second. 2