Lab-05
Lab-05
Using Fork, Exec, Wait & Exit System Calls for Creating
Child Processes
Objective:
This lab describes how a program can execute, wait, terminate, and
control children processes using system calls.
Activity Outcomes:
On completion of this lab students will be able to:
• Write programs that uses process creation system call fork ()
• Use exec system call
• Use wait system call
• Use sleep system call
Output:
• A process may suspend for a period of time using the sleep () command:
• Orphan Process: When a parent dies before its child, the child is automatically adopted by
the original “init” process whose PID is 1.
• Zombie Process: A process that terminates cannot leave the system until its parent accepts
its return code. If its parent process is already dead, it’ll already have been adopted by the
“init” process, which always accepts its children’s return codes (orphan). However, if a
process’s parent is alive but never executes a wait ( ), the process’s return code will never be
accepted and the process will remain a zombie.
5
1. execl()
Syntax:
int execl(const char *path, const char *arg, ..., NULL);
• The `execl()` function takes a variable number of arguments.
• Each argument is passed to the new program as separate
parameters.
• Path: Path to the executable.
• arg: The first argument should be the name of the program
(conventionally).
• Other arguments follow and must end with a `NULL` pointer.
2. execv()
Syntax:
int execv(const char *path, char *const argv[]);
• This function is similar to `execl()`, but the arguments are passed
as an array rather than a variable number of arguments.
7
3.execle()
Syntax:
int execle(const char *path, const char *arg, ..., NULL, char *const
envp[]);
• `execle()` works like `execl()`, but also allows you to specify a
custom environment for the new process.
• envp: An array of strings representing the environment variables
for the new process.
8
4. execve()
Syntax:
int execve(const char *path, char *const argv[], char *const envp[]);
• `execve()` is the most fundamental `exec()` function and is the
system call that all other `exec()` functions internally use.
• It takes the path to the executable, the array of arguments, and an
array of environment variables.
5. execlp()
Syntax:
int execlp(const char *file, const char *arg, ..., NULL);
• The `execlp()` function behaves like `execl()`, but instead of
specifying the full path to the executable, the system searches for
the executable in the directories listed in the `PATH` environment
variable.
9
6. execvp()
Syntax:
int execvp(const char *file, char *const argv[]);
• Similar to `execv()`, `execvp()` takes the arguments as an array and
looks for the executable in the `PATH` environment variable.
• The `exec()` system call does not create a new process; it simply
replaces the current process image with a new program.
• After an `exec()` call, the original program ceases to exist, and the
new program runs in the same process.
• The PID of the process remains unchanged.
• If the `exec()` call is successful, it does not return. If it fails (e.g.,
if the executable is not found), the function returns `1` and sets
`errno` to indicate the error.
10
If successful, the `execlp()` call in this example will replace the current
process with the `ls` command, listing the directory contents. The process
does not return to the original program.
Summary of Differences:
|Function|Arguments (Command)|Arguments (Env Variables)|Path Search|
11
Error Handling
1. Fork System Call (`fork()`):
Check Return Value:
• If `fork()` returns a negative value, it indicates an error in creating
a new process.
Handle Success and Failure:
• If `fork()` returns `0`, you are in the child process.
• If `fork()` returns a positive value, you are in the parent
process, and the return value is the child’s process ID.
12
Process Termination
1. Ensure Proper Exit in Child Process:
• Use `exit()`:
• The child process should call `exit()` to terminate properly.
2. Ensure Proper Exit in Parent Process:
• Wait for Child:
• The parent process should wait for the child to complete using
`wait()` or `waitpid()` to avoid premature termination and zombie
processes.
13
Status Codes
1. Check Exit Status:
• Use `WIFEXITED(status)`:
• Check if the child process terminated normally.
2. Retrieve Exit Status:
• Use `WEXITSTATUS(status)`:
• Extract the exit status of the child process if it terminated
normally.
3. Check for Abnormal Termination:
• Use `WIFSIGNALED(status)`:
• Determine if the child process was terminated by a signal.
4. Check for Core Dumps:
• Use `WCOREDUMP(status)`:
• Check if the child process generated a core dump.
I’ve added the references links for more details:
https://www2.it.uu.se/education/course/homepage/os/vt18/module2/exec/
14
Graded Task:
In this activity, you are required to perform tasks given below:
1) Print something and Check id of the parent process [Estimated 5-10
mins]
2) Create a child process and print child process id in parent process.
[ Estimated 10 mins]
3) Create a process and make it an orphan. [Estimated 5 mins]
Hint: To, illustrate this insert a sleep statement into the child’s
code. This ensured that the parent process terminated before its
child.
4) Write a C/C++ program in which a parent process creates a child
process using a fork() system call. The child process takes your age
as input and parent process prints the age. [Estimated 10 mins]
5) Write a C++ program that creates an array of size 1000 and
populates it with random integers between 1 and 100. Now, it
creates two child processes. The first child process finds how many
prime numbers are there among first 500 number while the second
child process finds the number of prime numbers among the
remaining 500 numbers. [Estimated 20 mins]
6) Write a program that sets a custom environment variable (e.g.,
MY_VAR="HelloWorld") and then uses execle to execute
/usr/bin/env (a command that prints all environment variables).
[Estimated 5 mins]
7) Write a program that prompts the user to input a command, and
then uses execvp to execute the input command dynamically.
Example: If the user inputs ps, the program will run execvp("ps", args);.
[Estimated 10 mins]