CIS307: Homework 2: Using Unix System Services and Pthreads
This homework is given February 6 and is due February 23 by 5pm.
This homework is just a vehicle for using a lot of Unix system services,
thus becoming familiar with them, and for using Pthreads. Look up the Stevens
book and its code to see how system services work. For pthreads, use the man
pages and the class notes. The homeworks requires you to write two separate
programs.
PROGRAM 1
Your program generates three children. Thus we have the following processes
PARENT, CHILD1, CHILD2, and CHILD3.
Your program is called with as parameters the names of six text files,
say, fn1, fn2, fn3, fn4, fn5, fn6.
Fn1 and fn2 already exist. Fn3 and fn4 are created by the second process
(first child). Fn5 and fn6 are created by the third process (second child).
Define a function selfid that prints out the process id of the
current process and of its parent. Define a global integer variable called
"self".
PARENT:
- Call selfid;
- Call atexit with selfid as parameter;
- Print out all the parameters of the program call;
- Print out all the setenv variables;
- self = -1;
- Open for read fn1;
- Spawn the three children processes;
- printf("1. Parent, Self = %d\n", self);
- Close fn1;
- Sleep for 6 seconds;
- Wait for two of its children to terminate and print out
for each child the current time and its process id;
- printf("2. Parent, Self = %d\n", self);
- Exit(0).
CHILD1:
- Call selfid;
- Call atexit with selfid as parameter;
- printf("1. Child 1, Self = %d\n", self);
- self = 1;
- Open for write fn3 and write to fn3 the lines of fn1;
- Close fn1 and fn3;
- Open for read fn2;
- Open for write fn4 and write to fn4 the lines of fn2;
- Close fn2 and fn4;
- printf("2. Child 1, Self = %d\n", self);
- Exit(0).
CHILD2:
- Call selfid;
- Call atexit with selfid as parameter;
- printf("1. Child 2, Self = %d\n", self);
- self = 2;
- Open for write fn5 and write to fn5 the lines of fn1;
- Close fn1 and fn5;
- Open for read fn2;
- Open for write fn6 and write to fn6 the lines of fn2;
- Close fn2 and fn6;
- Sleep for 15 seconds;
- printf("2. Child 2, Self = %d\n", self);
- Exit(0).
CHILD3:
- Call selfid;
- Call atexit with selfid as parameter;
- Execute another program image.
- This program image will:
- prints its own and its parent process id;
- prints the real user id;
- exit(0).
As comment in your program explain:
- Why fn4 and fn6 are each a copy of fn2
while fn3 and fn5 are not each a copy of fn1.
- Explain what happens to the parent CHILD2.
- Why CHILD3 has the same process id and parent process id before and
after the execute statement.
PROGRAM 2
Write a program with a global integer variable "shrd".
The program creates two threads and goes to sleep for RUN_DURATION
seconds.
Thread 1 does the following:
- Do forever:
- Prompt the user to enter a non-zero integer;
- Read an integer and store it in shrd;
Thread 2 does the following:
- Let x, y be integer local variables;
- Do forever:
- x = shrd;
- if x is 0 then sleep for 0.1 seconds
- else
- y = x;
- x = 0;
- printf("Thread2: y = %d\n",y);
EXTRA CREDIT
Determine the time it takes to fork and the time it takes to start a thread.
Do measuraments that are both precise and accurate.
ingargiola@cis.temple.edu