You are on page 1of 4

LAB 04

Aim:- WAP to create a process using fork().

Theory:-
fork() creates a new process by duplicating the calling process, The new process, referred to as
child, is an exact duplicate of the calling process, referred to as parent, except for the
following :
1. The child has its own unique process ID, and this PID does not match the ID of any
existing process group.
2. The child’s parent process ID is the same as the parent’s process ID.
3. The child does not inherit its parent’s memory locks and semaphore adjustments.
4. The child does not inherit outstanding asynchronous I/O operations from its parent nor
does it inherit any asynchronous I/O contexts from its parent.
Return value of fork() On success, the PID of the child process is returned in the parent, and 0
is returned in the child. On failure, 1 is returned in the parent, no child process is created.

Procedure:-

Source Code 1:

#include <iostream>
#include <unistd.h>

int main() {
pid_t c_pid = fork();
if (c_pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (c_pid > 0) {
std::cout << "Parent process ID: " << getpid() << std::endl;
}
else {
std::cout << "Child process ID: " << getpid()<< std::endl;
}
return 0;
}

OUTPUT:
Source Code 2:

#include <iostream>
#include <unistd.h>

int main() {
fork();
fork();
std::cout << "OS LAB" << std::endl;
return 0;
}

OUTPUT:

B) AIM: WAP to create a process using exec().

THEORY:
exec() replaces the current process image with a new process image. It loads the program into
the current process space and runs it from the entry point. In summary, fork() starts a new
process which is a copy of the one that calls it, while exec() replaces the current process image
with another (different) one.

Source Code 3:

#include <stdio.h>
#include <unistd.h>

void main(){
int pi_d, pid;
pi_d = fork();
if(pi_d == 0) {
printf("Child Process B:\npid :%d\nppid:%d\n",getpid(),getppid());
}
if(pi_d > 0) {
pid = fork();
if (pid > 0) {
printf("\nParent Process:\npid:%d\nppid :%d\n",getpid(),getppid());
}
else if (pid == 0) {
printf("Child Process A:\npid :%d\nppid:%d\n",getpid(),getppid());
}
}
}
OUTPUT:

Source Code 4:

#include <stdio.h>
#include <unistd.h>

int main() {
int i;
printf("I am EXEC.c called by execv() ");
printf("\n");
return 0;
}
OUTPUT:

Source Code 5:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main() {
char *args[]={"./EXEC",NULL};
execv(args[0],args);
printf("Ending-----");
return 0;
}
OUTPUT:
AIM: WAP to show zombie process.
THEORY:
A process which has finished the execution but still has entry in the process table to report to
its parent process is known as a zombie process. A child process always first becomes a
zombie before being removed from the process table. The parent process reads the exit status
of the child process which reaps off the child process entry from the process table

Source Code 6:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
int pid = fork();
if (pid > 0) {
sleep(20);
printf("pid : %d",pid);}
else {
exit(0);
}
return 0;
}
OUTPUT:

You might also like