You are on page 1of 6

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,

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

Q1. Write a program using fork() system call to create two


child of the same process i.e., Parent P having child process P1
and P2.
Answer:

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

int main()

pid_t p;

printf("before fork\n");

p=fork();

if(p==0)

printf("I am child having id %d\n",getpid());

printf("My parent's id is %d\n",getppid());

else{

printf("My child's id is %d\n",p);

printf("I am parent having id %d\n",getpid());

printf("Common\n");

Q2. Write a program using fork() system call to create a


hierarchy of 3 process such that P2 is the child of P1 and P1 is
the child of P.
Answer:

int main()
{
    int pid, pid1, pid2;
pid = fork();

if (pid == 0) {

 sleep(3);

printf("child[1] --> pid = %d and ppid = %d\n",


               getpid(), getppid());
    }
  
    else {
        pid1 = fork();
        if (pid1 == 0) {
            sleep(2);
            printf("child[2] --> pid = %d and ppid = %d\n",
                   getpid(), getppid());
        }
}

 return 0;
}
Q3. How many total process are created with the below code
int main()
{
fork();
fork();
fork();
}
Answer:

Fork #1 creates an additional processes. You now have two


processes.

Fork #2 is executed by two processes, creating two processes, for


a total of four.

Fork #3 is executed by four processes, creating four processes,


for a total of eight. 

Q4. Write a program to create two child process. The parent


process should wait for both the child to finish.
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;
}

You might also like