Child Process: It is created as it is parent process copy and inherits most of its attribute. This is created by fork() function.
fork(): Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call.
PID: Process Identification Number
getpid(): It returns the process id of the calling process.
getppid(): It returns the process id of the parent of the calling process.
Process0: It is a special type of a procerss that is creted when the system boots, after forking a child process, process0 becomes the swapper process.
pid: When the process started then it is given a unique number called process identification number, this identifies that the process is to the system.
ppid: In addition to the unique process identifcation number, each process is assigned a parent process id that tells which process started it.
cpid: It is the process id of the job that created the shared memory segment.
Code for Child Process using fork() function:
1 st Method
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
int main(){
pid_t pid;
printf("Fork Started");
q = fork();
if(q<0){
printf("Error");
}
else if(q==0){
printf("Child pid: ", getpid());
printf("Parent pid", getppid());
}
else{
printf("Parent pid: ", getpid());
printf("Child pid: ",p);
}
return 0;
}
2 nd Method
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
int main(){
pid_t pid;
char* message;
int n;
printf("Fork Starting...");
pid = fork();
switch(pid){
case -1:
perror("Error);
exit(1);
case 0:
message = "CHILD PROICESS";
n = 3;
break;
default:
message: "PARENT PROCESS";
n = 3;
}
for(;n>0;n--){
printf("%d\n", (int)getpid());
puts(message);
sleep(1);
}
exit(0);
}
LABORATORY-3A
Zombie and Orphan Process
Zombie Process: A process which finished the execution but still has no entry in the process table to report to its parent process is called a zombie process.
Orphan Process: A process whose parent process no more exist i.e. either finished or terminated without waiting for its child process to terminate is called orphan process.
Code for Zombie Process:
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
int main(){
pid_t id,pid,cid;
char* message;
int n;
printf("Fork Starting...");
id = fork();
switch(id){
case -1:
perror("Error);
exit(1);
case 0:
message = "CHILD PROICESS";
n = 2;
cid = id;
break;
default:
message: "PARENT PROCESS";
n = 4;
pid = id;
}
for(;n>0;n--){
printf("%d\t%d\t%d\t", (int)getpid(),p,id);
puts(message);
sleep(1);
if(n==1 and id == cid){
printf("ZOMBIE CHILD EXITING\n");
}
if(n==1 and id == pid){
system("ps -ax");
printf("PARENT EXITING\n");
}
}
}
Code for Orphan Process:
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
int main(){
pid_t pid,ppid,cpid;
char* message;
int n;
printf("Fork Starting...");
pid = fork();
switch(pid){
case -1:
perror("Error);
exit(1);
case 0:
message = "CHILD PROICESS";
n = 2;
cpid = pid;
break;
default:
message: "PARENT PROCESS";
n = 4;
ppid = pid;
}
for(;n>0;n--){
printf("%d\t%d\t%d\t", (int)getpid(),n,id);
puts(message);
sleep(1);
if(n==1 and pid == cpid){
printf("CHILD EXITING\n");
}
if(n==1 and pid == ppid){
system("ps -ax");
printf("PARENT EXITING\n");
}
}
}