0% found this document useful (0 votes)
51 views9 pages

OS Assignment 1 (Daniyal Aqil 193 (E) )

The document contains code to create a process hierarchy using fork system calls. The code in the first example creates a parent process P that spawns two child processes, P1 and P2, by calling fork twice. The second example modifies the code to create a hierarchy where P1 is spawned from P, and P2 is spawned from P1, resulting in P1 being the parent of P2. The output of the programs is not shown.

Uploaded by

rinim85726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

OS Assignment 1 (Daniyal Aqil 193 (E) )

The document contains code to create a process hierarchy using fork system calls. The code in the first example creates a parent process P that spawns two child processes, P1 and P2, by calling fork twice. The second example modifies the code to create a hierarchy where P1 is spawned from P, and P2 is spawned from P1, resulting in P1 being the parent of P2. The output of the programs is not shown.

Uploaded by

rinim85726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

OS (Lab)

Operating System

Name : Daniyal Aqil


Roll No: 193/BSCS/5th Sem
Section : E
Submitted To : Usama Asif
Question No # 1

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.

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

int main() {
pid_t pid1, pid2;

pid1 = fork();

if (pid1 < 0) {
fprintf(stderr, "Error creating child process P1\n");
return 1;
}
else if (pid1 == 0) {
// Code executed by P1 (first child)
printf("P1 (Child 1) - Process ID: %d\n", getpid());
}
else {
// Code executed by the parent process
// Create the second child process
pid2 = fork();

if (pid2 < 0) {
fprintf(stderr, "Error creating child process P2\n");
2
REPORT TITLE
return 1;
}
else if (pid2 == 0) {
// Code executed by P2 (second child)
printf("P2 (Child 2) - Process ID: %d\n", getpid());
}
else {
// Code executed by the parent process
printf("P (Parent) - Process ID: %d\n", getpid());
}
}

return 0;

3
REPORT TITLE
OUTPUT

4
REPORT TITLE
Question No # 2

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.

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

int main() {
pid_t pid1, pid2;

pid1 = fork();

if (pid1 < 0) {
fprintf(stderr, "Error creating child process P1\n");
return 1;
}
else if (pid1 == 0) {
// Code executed by P1 (first child)
printf("P1 (Child of P) - Process ID: %d\n", getpid());

// Create the second child process (P2)


pid2 = fork();

if (pid2 < 0) {
fprintf(stderr, "Error creating child process P2\n");
return 1;
}
else if (pid2 == 0) {
// Code executed by P2 (second child)
printf("P2 (Child of P1) - Process ID: %d\n", getpid());
}
5
REPORT TITLE
else {
// Code executed by P1
// P1 (Child of P) continues execution
// ... (optional additional code for P1)
}
}
else {
// Code executed by the parent process (P)
printf("P (Parent) - Process ID: %d\n", getpid());
}

return 0;
}

6
REPORT TITLE
Output

7
REPORT TITLE
8
REPORT TITLE
9
REPORT TITLE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy