0% found this document useful (0 votes)
15 views5 pages

Dev 6

The document describes three programming exercises related to process management in operating systems. The first demonstrates creating a chain of processes with one parent and one child. The second sorts coins in the parent process and prints the reversed sorted list in the child process. The third demonstrates zombie and orphan process states using fork.

Uploaded by

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

Dev 6

The document describes three programming exercises related to process management in operating systems. The first demonstrates creating a chain of processes with one parent and one child. The second sorts coins in the parent process and prints the reversed sorted list in the child process. The third demonstrates zombie and orphan process states using fork.

Uploaded by

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

[ 2CEIT401 OPERATING SYSTEM]

Practical: 6

AIM- Demonstration of process management system calls for


multiprocessing

Department of Computer Engineering /Information Technology


2CEIT401:Operating Systems Practical-6

1. Create a chain of processes where one parent has exactly one child as per
sample chaingiven by the Instructor. Sequence:1
PROCESS 0 --> PROCESS 1 --> PROCESS 2 --> PROCESS 3 -->
PROCESS 4--> PROCESS 5 --> PROCESS 6
Display PIDs of all processes.
#include <stdio.h>
#include <unistd.h>
int main() {
int levels = 6;
pid_t pid;
printf("PROCESS 0 (PID: %d)\n", getpid());
for (int i = 1; i <= levels; i++) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
printf("--> ");
printf("PROCESS %d (PID: %d)", i, getpid());
} else {
break;
}
}
printf("\n");
return 0;
}

E.No: 22012011099 Page No:


Name: Dev raval
2CEIT401:Operating Systems Practical-6

2. Jack want to sort out coins of various values as per below input. Design
programtosort the coins in the parent process and print the reversely sorted
coins in the child process. For example:
Input : 10, 5, 1, 20, 2
Output :
Parent process:
Sorted coins are : 1, 2, 5, 10, 20
Child process
Reversely sorted coins are: 20, 10,5,2,1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int compare(const void *a, const void *b) {
return (*(int*)b - *(int*)a);
}
int main() {
int coins[] = {10, 5, 1, 20, 2};
int num_coins = sizeof(coins) / sizeof(coins[0]);
int sorted_coins[num_coins];
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
printf("Child process --> ");
printf("Reversely sorted coins are: ");
for (int i = 0; i < num_coins; i++) {
printf("%d", sorted_coins[i]);
if (i != num_coins - 1) {
printf(", ");
}
}
printf("\n");
} else {
printf("Parent process --> ");
printf("Sorted coins are: ");
for (int i = 0; i < num_coins; i++) {

E.No: 22012011099 Page No:


Name: Dev raval
2CEIT401:Operating Systems Practical-6

sorted_coins[i] = coins[i];
}
qsort(sorted_coins, num_coins, sizeof(int), compare);
for (int i = 0; i < num_coins; i++) {
printf("%d", sorted_coins[i]);
if (i != num_coins - 1) {
printf(", ");
}
}
printf("\n");
wait(NULL);
}
return 0;
}

3. Demonstrate Zombi and Orphan state of processes using fork().


#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int id = fork();
if (id > 0)
sleep(50);
else{
printf("Proccess ID of Zombi = %d",getpid());
exit(0);
}
return 0;
}
Orphan :
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int id = fork();
E.No: 22012011099 Page No:
Name: Dev raval
2CEIT401:Operating Systems Practical-6
if (id > 0)
printf("in parent process id = %d\n",getpid());
else if (id == 0)
{
printf("in child process id = %d\n",getpid());
sleep(30);
}
return 0;
}

E.No: 22012011099 Page No:


Name: Dev raval

You might also like