p1 200 |
The second process is defined as follows:
p2 fred |
In other words, the processes look as follows:
Your job is to write the program skipex. It is going to use pthreads to solve Skippy's problem. At all times, it is going to make sure that n experiment processes are running. It only needs two threads. One reads from standard input and one waits for experiment processes to die. The one that reads from standard input simply reads integers -- that is how n is set and changed. You can use scanf() for this one if you want -- nothing fancy. Have skipex die if the scanf() call fails or if the user enters an integer &le 0. When you die, don't worry about the experiment processes that are currently running.
Your program will start with n equal to zero. When n is first set, you create n experiment processes (this is a very simple fork()/execl()). Your second thread monitors when processes die and replaces them. When the user increases n, you create more processes. When the user decreases n, your second thread will not replace processes when they die, until there are only n running.
Make sure you pay attention to race conditions, and remember that if a process has no children, wait() will return instantly.
You can do this with fork()/exec()/pipe()/select()/wait(). You do not have to touch experiment. Explain to me how you do this. I am not requiring code here, although if you want to write skipex.c, feel free to. I want your explanation to describe exactly what you are doing -- when you make each system call, and perhaps a picture of the state when n equals, say, three.
Oh, and you must use wait(), not waitpid(), wait3() or any of that other garbage. No polling allowed either.
Hint: When the write end of a pipe goes away, read returns zero.
#include <stdio.h> #include <stdlib.h> main() { int *ip, *i2, i; char *s; ip = (int *) malloc(sizeof(int)*4); for (i = 0; i < 4; i++) ip[i] = 5+i; printf("0x%lx\n", (unsigned long) ip); s = (char *) malloc(sizeof(char)*61); i2 = (int *) malloc(sizeof(int)*3); for (i = 0; i < 3; i++) i2[i] = 10+i; free(s); s = (char *) malloc(sizeof(char)*9); strcpy(s, "ABCDEFGH"); sleep(10000); } |
Suppose the program prints 0x6100 as its first line. At the point where the program sleeps, I would like you to tell me the contents of every byte of the heap that you know about. You may make the following assumptions:
Addresses | Identity | Value(s) |
0x6100 to 0x6103 | ip[0] | 5 |