Homework Assignment #3 – Processes and fork() [50 pts]


You are expected to do your own work on all homework assignments. You may (and are encouraged to) engage in general discussions with your classmates regarding the assignments, but specific details of a solution, including the solution itself, must always be your own work. (See the statement of Academic Dishonesty on the Syllabus)

How to turn in?

Assignments need to be turned in via Lamaku. Check the Syllabus for the late assignment policy for the course.

What to turn in?

You should turn in single plain text or PDF file named README.txt, or README.pdf, with your answers to the assignment’s questions. Your file must be readable “as is” and points will be removed if the report is not readable.


Exercise #1 [20 pts]

Consider the following C program (assume that there are no compilation or execution errors of any kind):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int count;

    if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) {
        fprintf(stderr,"Usage: %s <integer>\n", argv[0]);
        exit(1);  
    }   

    pid_t pid1, pid2;
    while (count > 0) {
        pid1 = fork();
        if (pid1 > 0) {
            pid2 = fork();
            count = count - 2;
        } else if (pid1 == 0) {
            count = count - 1;
        } 
    }
    exit(0);
}

Question #1 [2 pts]

If the command-line argument passed to this program is 0, this program creates no process. How many processes does this program create if the command-line argument is 1?

Question #2 [3 pts]

How many processes does this program create if the command-line argument is 2?

Question #3 [12 pts]

Let T(n) be the number of processes this program creates when its input is n (the answer to Question #1 above is thus T(1)).

Write an recursive formula (i.e., a recurrence relation) that gives T(i) as a function of one or more values of T for smaller input (i.e., smaller i). Explain your reasoning.

Feel free to double-check your formula by actually running the program and possibly augmenting it so that it allows you to count processes in whichever way you want.

Question #4 [3 pts]

Say the maximum number of processes that can be created on your machine is 100,000 (you can find out the actual number of your machine with ulimit -u).

What is the smallest value of the command-line argument for which the above program would experience failed fork() calls?


Exercise #2 [30 pts]

Consider the following C program (assuming that there are no compilation or execution errors)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

int main(int argc, char **argv) {
        pid_t pid;
							    
        // Main process's PID=66
        pid = fork(); // creates process with PID=23
        if (pid > 0) {
                pid_t pid2 = fork(); // creates process with PID=101
                if (pid2 == 0) {
                        sleep(10);
                        exit(0);
                }
		sleep(20);
                exit(0);
        } else {
                pid_t pid3 = fork(); // creates process with PID=89
                if (pid3 == 0) {
                        sleep(40);
                        printf("** ONE **\n");
                        exit(0);
                }
                pid_t pid4 = fork(); // creates process with PID=200
                if (pid4 == 0) {
                        sleep(50);
                        exit(0);
                }
                waitpid(pid3, NULL, 0);
        }
	sleep(20);
        printf("** TWO **\n");
        exit(0);
}

Assume that the PID of the main process is 42. Each call to fork() in the code is commented to show the PID of the newly created process (The PIDS are thus 42, 11, 25, 89, and 123). All calls to all functions / systems calls succeed.

In this exercise we assume that all operations take zero time but for sleep() (which sleeps a prescribed number of seconds) and waitpid(), which obviously might block for a while. We also assume that the execution starts at time zero. Therefore, in all the questions below, whenever a time is asked, it’s always a perfect multiple of 10 (since all calls to sleep in the above program are for numbers of seconds that are multiples of 10).

Answer the following questions: