Operating System Tutorials
Operating System Tutorials
Augustine
COMP 2604 Operating Systems
Semester 2, 2022/2023
Lab #3 - Processes
Objective:
This lab
‣ simple_fork.c
‣ child_pid.c
‣ overlay_child.c
‣ wait_on_child.c
Spawning a Process
UNIX operating systems create new processes by using the fork() system call.
Calling fork() at any point within a C program (parent process) will cause a new
process (child process) to be created that will run concurrently with the parent. The
child process is an exact replica of the parent process in terms of program code and
data, but has its own unique process id.
‣ Compile and run simple_fork.c to see how a new process can be spawned.
You will notice that the second statement is printed twice. The reason for this is that at
the point where fork()is called. a duplicate of the parent process is created. Both the
parent and the child processes continue execution at the instruction after the fork()
call.
‣ In place of the second printf() statement, put a for loop that prints a message
5 times. Save the files as simple_fork_2.c
‣ Compile and run simple_fork_2.c
Differences in Process ID
Even though the parent and child processes are running the same code, they are
nevertheless different processes. In particular, they have different process IDs.
In the parent process, the return value of fork() is equal to the process ID of the new
child process (a number > 0), or it may be equal to a number <0, which indicates that
the fork() call failed. In the child process the return value of fork() is equal to
zero (0).
‣ Run child_pid.c to see how the parent and child can go in different directions
after the call to fork().
‣ After the call to fork() delete all of the other lines. Add a statement to print the
value of the captured return value of fork(). Save the files as child_pid_2.c
‣ Compile and run child_pid_2.c
There are six (6) calls in the exec family of system calls. They are, execl, execle,
execlp, execv, execvp and execve. You should read about their differences..
You will now use a program that spawns a child process and then overlays the child’s
code with another program. The program that will be used to overlay is called ps. The
ps program is used to give a report on the status of processes currently running in the
operating system.
‣ To get a feel for the output of the ps program, open a shell window, and type the
command:
ps au -u [ENTER]
‣ Compile and run overlay_child.c, to see how the execl system call can be
used to replace the memory image of a child process. If you look carefully, you will
see the output of the parent as well as the output of the child when the program is
run.
‣ Repeatedly run the overlay_child.c program. Eventually you will see that the
order of printing changes. That is, on some runs the parent will print first, followed
by the child, and on other runs, the child will print first followed by the parent.
‣ In the parent’s section of the code, i.e. where the test for pid > 0 is done, use an
execl() call to make the parent run the equivalent of:
ls -l
The return value of wait() is the process ID of the child process that has just
terminated. wait() also returns the status code of the child process via an integer
pointer parameter.
‣ In the child section, remove all statements and instead write a for loop that prints
an indexed message 100000 times. For example:
Child - 0
Child - 1
...
‣ In the parent section, remove all statements and instead write a for loop that prints
an indexed message 100000 times. For example:
Parent - 0
Parent - 1
...
‣ Compile and run wait_on_child_2.c. You should notice that the output of the
child and parent is intermixed.
‣ Add a wait() call just before the for loop in the parent section.
‣ Compile and run the code again. This time the child’s output should come first.