source: SILBERSCHATZ, A., GALVIN, P.B., and GAGNE, G. Operating System Concepts. 9th ed. Hoboken: Wiley, 2013.
TIP: the unistd.h header has some useful functions. You may also want to check out the wait()
function in sys/wait.h and exit()
in stdlib.h.
Write a C-program that prints its own PID on the screen.
fork()
Write a C-program that spawns another process using fork()
. Both parent and child processes announce their existence (through a printf) and their PIDs.
fork()
childrenWrite a C-program that creates 4 child processes using fork()
, where:
fork()
ed the fork()
!Given the following C code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
fork();
printf("Hi class\n");
return 0;
}
How many times will Hi class
be printed? Why? Draw a pstree
tree that visualizes the parent and children to help explain your answer.
fork()
and orderWrite two functions: a main()
that calls another void
function in which you fork()
the process. If the resulting process is the child one, print some text. If it’s the parent process, print another text.
Execute the program from your commandline terminal a few hundred (!) times and carefully inspect the order of the printed messages. Are these always the same? If so, do you think this will always be the case for everyone running your specific hardware? Why (not)? TIP: Who decides the execution order and timing?