Docsity

CS 2204 Lab 12: Understanding Fork System Call in C Programming, Lab Reports of Computer Science

Toggle sidebar
bg1
CS 2204 Lab 12
your name here (please print):
your student ID number here:
Create a subdirectory called lab12 under your home directory. Perform any necessary work for
this lab assignment in that directory. Ensure that this assignment is performed from within a
bash shell. Revisit the lecture notes of Nov 14 in preparation for this lab assignment.
1. (5 points) Consider the following program:
#include <stdio.h>
main ()
{
int pid;
int i;
for (i=0; i<3; i++){
if ((pid=fork()) <0) {
printf("Sorry, cannot fork\n");
} else if (pid == 0) {
printf("child %d\n", i);
} else {
printf("parent %d\n", i);}}
exit(0);
}
How many processes are involved, in total, in this program? Hint: Merely analyzing the
output of the program will not help you. You need to understand exactly how the fork()
system call works.
2. (3 points) Now analyze the output of the above program and explain exactly why you get
the number of lines that you see.
3. (2 points) Redirect the output of the program into a file. Why does the file contain more
lines than what you saw in the previous question?

Partial preview of the text

Download CS 2204 Lab 12: Understanding Fork System Call in C Programming and more Lab Reports Computer Science in PDF only on Docsity!

CS 2204 Lab 12

your name here (please print):

your student ID number here:

Create a subdirectory called lab12 under your home directory. Perform any necessary work for this lab assignment in that directory. Ensure that this assignment is performed from within a bash shell. Revisit the lecture notes of Nov 14 in preparation for this lab assignment.

  1. (5 points) Consider the following program:

#include <stdio.h> main () { int pid; int i;

for (i=0; i<3; i++){ if ((pid=fork()) <0) { printf("Sorry, cannot fork\n"); } else if (pid == 0) { printf("child %d\n", i); } else { printf("parent %d\n", i);}} exit(0); }

How many processes are involved, in total, in this program? Hint: Merely analyzing the output of the program will not help you. You need to understand exactly how the fork() system call works.

  1. (3 points) Now analyze the output of the above program and explain exactly why you get the number of lines that you see.
  2. (2 points) Redirect the output of the program into a file. Why does the file contain more lines than what you saw in the previous question?