0% found this document useful (0 votes)
34 views

Operating System Tutorials

This lab covers processes in operating systems through several C programs. Students are asked to download source code files and modify them to: 1) see how fork() creates a new process; 2) observe that the parent and child processes have different process IDs; 3) use execl() to overlay the child's memory with a new program; and 4) use wait() to have the parent wait for the child process to complete before continuing.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Operating System Tutorials

This lab covers processes in operating systems through several C programs. Students are asked to download source code files and modify them to: 1) see how fork() creates a new process; 2) observe that the parent and child processes have different process IDs; 3) use execl() to overlay the child's memory with a new program; and 4) use wait() to have the parent wait for the child process to complete before continuing.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The University of the West Indies, St.

Augustine
COMP 2604 Operating Systems
Semester 2, 2022/2023
Lab #3 - Processes

Objective:
This lab

Download the Necessary Files


You are required to download the following four (4) source code files from the course
website or myElearning:

‣ 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

Overlaying the Code of a Process


To make the creation of a new process by fork() useful, there exists a family of
system calls (exec calls) that can overlay the memory space of the child process with a
new program.

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

Save the file as overlay_child_2.c.


‣ Compile and run overlay_child_2.c.

Waiting on a Process to Complete


It is sometimes desirable to have the parent process allow the child process to
complete before continuing its own execution. This can be achieved by having the
parent go into a blocked stated, and moving back to the ready state only when the
child has completed. The wait() system call can be used to achieve this.

‣ Compile and run wait_on_child.c.

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.

‣ Save a copy of wait_on_child.c in a new file called wait_on_child_2.c

‣ 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.

You might also like