4 Processes
4 Processes
Processes
R:Ch 3 p60-77, T:Ch2 pp 71-77
Process management
Interference and shared variables
Reentrant code and Coroutines
Motivation for a process
Fork, join process notation
Process: An object with state
Process
Each process has an ``abstract'' locus of
control
Any exchanges of control are done
automatically and implicitly
Runs in parallel on multiprocessor
process Main
fork A; t1 = b*b; join A
t4 = t1 - t3; t5 = sqrt(t4)
fork B; fork C; t6 = -b - t5; join B; join C
fork D; t10 = t6/t7; join D
end process Main
process A
t2 = 4 * a; t3 = t2 * c
end process A
process B
t7 = 2 * a
end process B
process C
t8 = -b + t5
end process C
process D
t9 = t8/t7
end process D
int main(void) {
int x;
x = 0;
fork();
x = 1;
printf("I am process %ld and my x is %d\n", (long)getpid(), x);
return 0;
}
waitpid:
Allows you to wait for a particular process, or all process if pid is -1.
Important option is NOHANG which will return 0 if there is a
specified child to wait for but it has not yet terminated.