Professional Documents
Culture Documents
PESHAWAR
Assignment # 1
Name: Mohsin Ali Khattak
Reg no: 0670
Section: A
Submitted to: Sir Imran Rasheed
Cs-302 operating System
Department: CS&IT
Session – 2018-22
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)
else{
printf("Common\n");
int main()
{
int pid, pid1, pid2;
pid = fork();
if (pid == 0) {
sleep(3);
return 0;
}
Q3. How many total process are created with the below code
int main()
{
fork();
fork();
fork();
}
Answer:
#include <stdio.h>
#include <unistd.h>
void main(){
int pi_d ;
int 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());
}
}
}
Q5. Create a parent-child relationship between two process.
The parent should print two statements:
A) its own PID
B) PID of its child
The child should print two statements:
C) its own PID
D) PID of its parent
Make use of wait() in such a manner that the order of the four
statements A, B, C and D is:
A
C
D
B
You are free to use any other relevant statement/printf as you
desire and their order of execution does not matter.
Answer:
#include <unistd.h>
#include <stdio.h>
int main()
{
int n1 = fork();
int n2 = fork();
if (n1 > 0 && n2 > 0)
{
printf("A\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my childid is %d \n", getppid());
}
else if (n1 == 0 && n2 > 0)
{
printf("C\n");
printf("%d %d \n", n1, n2);
printf("my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
else if (n1 > 0 && n2 == 0)
{
printf("D\n");
printf("%d %d \n", n1, n2);
printf("my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
else {
printf("B\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my childid is %d \n", getppid());
}
return 0;
}