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

Lab-1

The document provides an overview of implementing process system calls in C, specifically focusing on fork(), exec(), and wait(). It explains how fork() creates a new process, detailing the behavior of parent and child processes, and includes code examples demonstrating these concepts. Additionally, it covers the exec() function, which replaces the current process with a new process, along with a sample program illustrating its use.

Uploaded by

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

Lab-1

The document provides an overview of implementing process system calls in C, specifically focusing on fork(), exec(), and wait(). It explains how fork() creates a new process, detailing the behavior of parent and child processes, and includes code examples demonstrating these concepts. Additionally, it covers the exec() function, which replaces the current process with a new process, along with a sample program illustrating its use.

Uploaded by

babithaganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Lab-1

Develop a C program to implement the Process system calls(fork(),


exec(), wait())
Fork()
The fork() system call is used in Unix-like operating systems
(including Linux) to
create a new process by duplicating the existing (calling) process.

After the successful call to fork(), two processes are created; the
parent and the child process.

It's important to note that after the fork(), both the parent and
child processes continue executing from the point of the fork()
call.
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
Hello world
}
#include <stdio.h>
int main()
{
printf("Hello world\n");
fork();
return 0;
}
#include <stdio.h>
int main()
{
fork();
printf("Hello world\n");
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Hello world\n");
printf("Hello world!, process_id(pid) = %d \n",getpid());
fork();
printf("Hello world\n");
printf("Hello world!, process_id(pid) = %d \n",getpid());
return 0;
}
Hello world
Hello world!, process_id(pid) = 6298
Hello world
Hello world!, process_id(pid) = 6298
Hello world
Hello world!, process_id(pid) = 6299
The child process is an exact copy of the parent, with its own unique process
identifier (PID).

Fork() function returns the Process ID.


In the child process, the return value is 0 and in the parent process, return
value is the PID of the newly created process.

The program checks the value of child_PID to determine whether it is running


in parent process or child process.

It's important to note that after the fork(), both the parent and child processes
continue executing from the point of the fork() call.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t p = fork();
if(p<0)
{
perror("fork fail");
exit(1);
} if(p==0)
{
printf("It is the child process,process_id(pid) = %d \n",getpid());
}
else
{
printf(“Parent process!, process_id(pid) = %d \n",getpid());
}
return 0;
}
• Parent process!, process_id(pid) = 6357
• It is the child process,process_id(pid) = 6358
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
????????????????
fork();
printf("hello\n");
return 0;
}
Exec()
• 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.

call replaces the image of the current process with a new process image specified by
the path i.e. the current process code gets replaced by a new process code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{ pid_t pid;
printf("Parent process (PID: %d)\n", getpid());
// Create a child process
pid = fork();
if (pid < 0)
{
perror("Fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// Child process
printf("Child process (PID: %d), Parent PID: %d\n", getpid(), getppid());
// Execute a new program in the child process
execl("/bin/ps", "ps", NULL); // The code below will only execute if execl fails
perror("Exec failed");
exit(EXIT_FAILURE);
}
else
{
// Parent process
printf("Parent process, waiting for the child...\n");
// Wait for the child to complete
wait(NULL);
printf("Child process completed.\n");
}
return 0;
}

You might also like