Executing processes is fundamentally the reason why operating systems
exist. Beyond that, it's an extremely common activity in modern systems- every
time you look at a file, compile a program, or execute a terminal command
you're probably creating at least one process, if not more.
Understanding the fork()
and exec()
mechanisms
provided by the operating system will give you insight to the
process lifecycle in Linux.
In this studio, you will:
getpid()
fork()
wait()
exec()
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to dferry@slu.edu with the phrase Fork and Exec in the subject line.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
Create a new program called fork.c
. Use printf()
and getpid()
to print out the process' PID. Run the program
three times and copy-paste the output as the answer to this exercise.
fork()
-ing and
exec()
-ing. The fork()
step creates a new
process (the "container" for executing a program), and the exec()
step loads and executes a desired program within that new process.
Recall from the lecture that when fork()
is called, the
system creates a near-identical replica of the calling process. After the
fork you will have two processes that both execute the same program. Insert
the call "fork();
" immediately after the start of
main()
in your program fork.c
What happens when you run your program now? Copy and paste the output.
fork()
. This is a unique function
in that you get two return values for each valid call to
fork()
(one in the parent, and another in the child). What are the
return values listed?
fork()
's return values to modify your program so that
the parent and child processes identify themselves as such when they print out
their PIDs. Copy and paste your program output.
sleep()
function causes a program to suspend for a specified length of time before
continuting, and we can use it to pretend that the child process takes a long
time to execute. Insert the following call immediately before your child process
prints out its PID:
sleep(3);
This will cause your child process to go to sleep for about three seconds before printing its output. Now run your program... what happens? Does it behave the way you would expect?
waitpid()
function will halt the parent until
a desired child process returns. Insert the following call immediately before
your parent process prints out its PID:
waitpid( child_PID, NULL, 0 );
(The second and third parameters allow us some extra control over how this function operates, but we don't need them for now.)
Execute your program again. How is the behavior different?
execvp()
function. (There are many
forms of exec()
, but this one is arguably the easiest to get
started with, see the rest at man 3 exec
.)
Before we use execvp()
, you should execute the command we
want to run so that you know what to expect. We're going to have our
child process execute the ls
program in list-mode. Execute the
command "ls -l
" and copy-paste the output as the answer to this
question.
char* child_argv[] = {"ls", "-l", 0};
char* child_prog = child_argv[0];
Notice that child_argv
has the exact same layout as if you were
to try and access argv
in a regular program. Now, in the child
process, insert the call:
execvp( child_prog, child_argv );
If everything is correct, you should see your child process execute the
"ls -l
" command, and then your parent process should wait and
print out its PID. Copy and paste your program output as the answer to this
exercise.
execvp()
function so that
you can call a different program with different arguments.
execvp()
call so that you can execute an
arbitrary program, for example as a result of user input. This is a vital
part of Lab 2, and we will explore it further in a future studio.