Os Exp3 Fork
Os Exp3 Fork
Experiment 3
p=fork();
if(p<0){
printf(“error in forking”);
}
}
for ( i = 0 ;i < 2 ;i++ ) {
p1=fork();
if(p1<0){
printf(“error in forking”);
}
printf("Child process ID: %d Parent Process ID: %d\n", getpid(), getppid());
sleep(10);
return 0;
}
2.Demonstrate fork()
Fork()-
The Fork system call is used for creating a new process in Linux, and Unix
systems, which is called the child process,
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process: PID = %d\n", getpid());
} else if (pid > 0) {
// Parent process
printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);
} else {
// Fork failed
printf("Fork failed.\n");
return 1;
}
return 0;
}
3.Demonstrating wait()
Suspends the calling process until any one of its child processes ends. More precisely,
wait() suspends the calling process until the system obtains status information on the
ended child. If the system already has status information on a completed child process
when wait() is called, wait() returns immediately.
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("Child process: PID = %d\n", getpid());
} else if (pid > 0) {
// Parent process
wait(NULL);
printf("Parent process: Child process terminated.\n");
} else {
// Fork failed
printf("Fork failed.\n");
return 1;
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t childPid = fork();
if (childPid == -1) {
perror("fork");
return 1;
}
if (childPid == 0) {
// Child process
printf("Child process (PID: %d) is running with parent PID: %d.\n", getpid(),
getppid());
sleep(3);
// Simulate some work in the child process
printf("Child process (PID: %d) is exiting.\n", getpid());
} else {
// Parent process
printf("Parent process (PID: %d) is going to sleep.\n", getpid());
sleep(1);
// Sleep briefly to let the child process start printf("Parent
process (PID: %d) is exiting.\n", getpid());
// The parent process exits before the child, making the child an orphan.
}
return 0;
}
5.Zombie process:
: 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.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t childPid = fork();
if (childPid == -1)
{
perror("fork");
return 1;
}
if (childPid == 0)
{
printf("[PID: %d] - Child process: running.\n", getpid());
sleep(3);
printf("[PID: %d] - Child process: exiting.\n", getpid());
exit(0);
} else {
printf("[PID: %d] - Parent process: sleep.\n", getpid());
sleep(1);
printf("Parent process (PID: %d) exiting without waiting for the child.\n",
getpid());
}
return 0;
}
CONCLUSION: From this experiment I learned about following system calls like Fork() System
call, Wait() system call, Orphan Process, Zombie process and implemented it in c
language in UBUNTU LINUX and understood it.