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

fork-system calls

The document outlines the implementation of the fork(), wait(), exec(), and exit() system calls in a C program. It describes how fork() creates a child process, wait() blocks the parent process until the child exits, exec() replaces the current process with a new one, and exit() terminates the process. An example program demonstrates the use of these system calls to run the 'ls' command in a child process while the parent waits for its completion.

Uploaded by

nandini.alapati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

fork-system calls

The document outlines the implementation of the fork(), wait(), exec(), and exit() system calls in a C program. It describes how fork() creates a child process, wait() blocks the parent process until the child exits, exec() replaces the current process with a new one, and exit() terminates the process. An example program demonstrates the use of these system calls to run the 'ls' command in a child process while the parent waits for its completion.

Uploaded by

nandini.alapati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Implement fork(), wait(), exec(), exit() system calls

AIM: To implement fork(), wait(), exec(), exit() system calls.


DESCRIPTION:
fork() system call use for creates a new process, which is called child process, which
runs concurrently
with process (which process called system call fork) and this process is called parent
process. After a
new child process created, both processes will execute the next instruction following
the fork() system
call. A child process use same pc(program counter), same CPU registers, same open
files which use in
parent process.
It takes no parameters and return integer value. Below are different values returned by
fork().
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly
created child
process.
A call to wait() blocks the calling process until one of its child processes exits or a
signal is received.
After child process terminates, parent continues its execution after wait system call
instruction.
Child process may terminate due to any of these:
It calls exit();
It returns (an int) from main
It receives a signal (from the OS or another process) whose default action is to
terminate.

SYNTAX:

pid_t wait(int *stat_loc);

The exec family of functions replaces the current running process with a new process.
It can be used to
run a C program by using another C program. It comes under the header file unistd.h.
There are many
members in the exec family which are shown below with examples.
The execlp system call duplicates the actions of the shell in searching for an
executable file if the
specified file name does not contain a slash (/) character. The search path is the path
specified in the
environment by the PATH variable. If this variable isn‟t specified, the default path
":/bin:/usr/bin" is
used.
The execlp system call can be used when the number of arguments to the new
program is known at
compile time. If the number of arguments is not known at compile time, use execvp.
SYNTAX:
#include<unistd.h>
intexeclp(constchar *file, constchar *arg, ...);

exit() terminates the process normally.


exit() performs following operations.
* Flushes unwritten buffered data.
* Closes all open files.
* Removes temporary files.
* Returns an integer exit status to the operating system.
SYNTAX:

void exit ( int status );

status: Status value returned to the parent process. Generally, a status value of 0.
ALGORITHM:
Step1: Start the program
Step2: Declare p
Step3: Call the fork system call for p.
Step4: If p is equal to -1 then
4.1: Print “Process creation unsuccessful”
4.2: Terminate using exit system call.
Step5: If p value is 0 then
5.1: Print “Child process starts”
5.2: Load the program in the given path into child process using exec system call.
5.3: If the return value of exec is negative then print the exception and stop.
5.4: Terminate the child process.
Step6: If p value is >0 then
6.1: Suspend parent process until child completes using wait system call
6.2: Print “Child Terminated”
6.3: Terminate the parent process.
Step7: Stop

PROGRAM
#include<unistd.h>
#include<stdio.h>
int main()
{ pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0)
{
printf(“\nfork() failed....”);
return 1;
}
else if (pid == 0)
{
/* child process */
execlp("/bin/ls", "ls", NULL); // A new program(ls executable is loaded into
memory and executed
exit(1);
}
else
{
/* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}

You might also like