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

Os Lab - 3

The document discusses 4 important Linux process system calls: fork(), wait(), exec(), and exit(). Fork() creates a new process, wait() suspends the parent process until the child exits, exec() replaces the running process with a new program, and exit() ends the process and releases resources. Examples are provided to demonstrate the functionality and usage of each system call.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Os Lab - 3

The document discusses 4 important Linux process system calls: fork(), wait(), exec(), and exit(). Fork() creates a new process, wait() suspends the parent process until the child exits, exec() replaces the running process with a new program, and exit() ends the process and releases resources. Examples are provided to demonstrate the functionality and usage of each system call.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB-3

Aim: Implementation of fork(), wait(), exec() and exit() process system calls.
1.Name of the system call: fork()
Description:The use of the fork() system call is to create a new process by duplicating the calling process. The
fork() system call is made by the parent process, and if it is successful, a child process is created. The fork() system
call does not accept any parameters.

Syntax: pid_t fork();


Example Program:
#include <stdio.h>
#include <sys/types.h>;
#include <unistd.h>;
int main()
{
// make two process which run same
// program after this instruction
fork();
fork();
printf("Hello mani!\n");
return 0;
}

Output:

2.Name of the System call:Wait ( ):


Description :The parent process may then issue a wait system call, whic suspends the execution of the parent
process while the child executes. When the child process terminates, it returns an exit status to the operating
system, which is then returned to the waiting parent process. The parent process then resumes execution.

Syntax: pid_t wait(int *stat_loc);


Example Program:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
}

Output:

3.Name of the System call:exec ( ):


Description :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.

Syntax: int execvp (const char *file, char *const argv[]);


Example program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
(void) argc
printf("Here I am: %s's PID is %d\n", argv[0], getpid());
if (execl("print-pid", "print-pid", (char *)NULL) == -1)
exit(EXIT_FAILURE); /* execl failed */
printf("Here I go: %s's PID is %d\n", argv[0], getpid());
return EXIT_SUCCESS;
}
Output:

4.Name of the system call:Exit ( )


Description: The exit() is a system call that is used to end program execution. This call indicates that the thread
execution is complete, which is especially useful in multi-threaded environments. The operating system reclaims
resources spent by the process following the use of the exit() system function.

Syntax:

Example Program:
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main(void)
{
printf("START");
exit(0);
// The program is terminated here
// This line is not printed
printf("End of program");
}

Output:

You might also like