char a[100];
main(int argc, char ** argv)
{
int d;
static double b;
char * s = "boo", * p;
p = malloc(300);
return 0;
}
Identify the segment in which each variable resides and indicate if the variable is private to a thread or shared across threads. Be careful.
The array a, the static variable b, and the string
constant "boo" are all in the data segment and are shared across threads.
The arguments argc and argv are in the stack
segment and are private to the thread.
The automatic variables d, s, and p are also
in the stack segment and are private to the thread.
Note that the variable p itself is in the stack
segment (private), but the object it points to is in the data segment which
is a shared region of an address space(that's why the be careful
warning). The contents of s consist of the address of the string "boo"
which happens to be in the data segment (shared).
if(child == 0)
{
The piece of code shown creates two processes. Therefore, we have a
total of three processes, the parent, the first and second
child. Each of these has its own private copy of the variable c. For
the parent, the variable c be 20 before the end of the program.
For the first child (the one created in the first program statement),
the variable c will contain the value 10 before the end of the
program. For the second child (the one created in the else clause),
the variable c will contain the value 15 before the end of the
program.
To solve this problem, we compute the number of
processes that get created by calling the function forkthem(). This can
be given
by the following equation:
n > 0: T(n) = 2 T(n-1) + 1
n = 0: T(0) = 0
where T(n) is the number of processes created
by the function. To see why this is the case, consider what happens when
the
function is called. The first statement calls
the system call fork() which creates a child in addition to the caller.
Both the caller and the
child then execute a recursive call to forkthem()
with an argument set to n-1. Therefore, a call to forkthem() creates one
process of
its own, and then is responsible for all the
children that will get created by the function with n-1.
The solution to the recurrence equation is 2^n-1((2ra\
ised_to_n)
- 1).
Program 1:
main()
{
val = 5;
if(fork())
wait(&val);
val++;
printf("%d\n", val);
return val;
}
Program 2:
main()
{
val = 5;
if(fork())
wait(&val);
else
exit(val);
val++;
printf("%d\n", val);
return val;
}
In the first program, the parent process creates a child and then waits for the child to exit (through the system call "wait"). The child executes and prints out the value of val, which is "6" after the v++ statement. The child then returns the value of val to the parent, which receives it in the argument to "wait" (& val). The parent then prints out the value of val, which is now 7. Note that the parent and child have seperate copies of the variable "val".
Using similar reasoning, you can see that the parent in program 2 waits
for the child to return, and the child exits immediately. In this case
only one value gets printed out which is the number 6 (from the parent
process.)