0% found this document useful (0 votes)
8 views17 pages

Performa Lab-2

This document is a term work submission for the Operating System Lab (PCS 502) at Graphic Era Hill University, Dehradun. It includes acknowledgments, a student lab report sheet, and several C programming exercises demonstrating various operating system concepts such as process creation, CPU scheduling, and handling of zombie and orphan processes. The document serves as a practical guide for students to understand and implement operating system functionalities.
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)
8 views17 pages

Performa Lab-2

This document is a term work submission for the Operating System Lab (PCS 502) at Graphic Era Hill University, Dehradun. It includes acknowledgments, a student lab report sheet, and several C programming exercises demonstrating various operating system concepts such as process creation, CPU scheduling, and handling of zombie and orphan processes. The document serves as a practical guide for students to understand and implement operating system functionalities.
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/ 17

Term work

on

OPERATING SYSTEM LAB


(PCS 502)
2023-24

Submitted to: Submitted by:

Dr. Pardeep Singh Antariksha Chauhan

Assistant Professor University Roll. No.: 2118267


GEHU, Dehradun Class Roll. No./Section: 14/C

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

GRAPHIC ERA HILL UNIVERSITY, DEHRADUN


ACKNOWLEDGMENT

I would like to particularly thank my Operating System Lab Faculty Dr.

Pardeep Singh for his patience, support and encouragement throughout the

completion of this Term work.

At last but not the least I greatly indebted to all other persons who

directly or indirectly helped me during this course.

Ayush Maheshwari

University. Roll No.- 2018265

B.Tech CSE-C-V Sem

Session: 2021-22

GEHU, Dehradun
DEPARTMENT OF CSE
STUDENT LAB REPORT
SHEET Photograph
Passport Size
Name of Student ……………………………………….…….Mob.No……………………………….…………..

Address Permanent ………………………………………………………………………………………….………..

Father’s Name …………………………Occupation ……………………………MoNo……..…….….………

Mother’s Name ………………… …..Occupation……………………………MoNo…………….…………..

Section …………….Branch……………………Semester……………..Class Roll No.................GradeA B C

Local Address…………………………………………………Email……………….……………………. Marks 5 3 1

S.No. Practical D.O.P. Date of Grade Grade Total Student’s Teacher’s


Submission (Viva) (Report Marks Signature Signature
File) (out of
10)
1

9
10

11
PROGRAM-1

OBJECTIVE: Write a C program to demonstrate the use of fork() system call.

SOURCE CODE:

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

int main() {
printf("working of fork() function\n");

fork();

printf("\nprocess with process ID %d is running\n", getpid());

return 0;
}
OUTPUT:
PROGRAM-2

OBJECTIVE: Write a C program in which parent process computes the


sum of even Numbers and child process computes the sum of odd
number stored in an array using a fork().

SOURCE CODE:

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

int main() {
int n, oddsum = 0, evensum = 0;

printf("enter the size of array: ");


scanf("%d", &n);

int arr[n];

printf("enter the numbers in array: ");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int x = fork();

if (x == 0) {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
evensum = evensum + arr[i];
}
}
printf("sum of even numbers: %d, calculated by child process with process ID %d\n", evensum,
getpid());
}

else {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
oddsum = oddsum + arr[i];
}
}
printf("\nsum of odd numbers: %d, calculated by parent process with process ID %d\n", oddsum,
getpid());
}
return 0;
}
OUTPUT:
PROGRAM-3

OBJECTIVE: Write a C program to demonstrate zombie process and orphan


process.

SOURCE CODE:

ZOMBIE PROCESS:

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

int main() {
int x = fork();

if (x == 0) {
printf("\nchild process executing with process id:%d\n", getpid());
printf("\nchild process is finished\n");
} else {
sleep(10);
printf("\nparent process executing with process id:%d\n", getpid());
printf("\nparent process is finished\n");
}

return 0;
}

ORPHAN PROCESS:

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

int main() {

int x = fork();
if (x == 0) {
sleep(5);
printf("\nchild process is executing with process ID %d\n", getpid());
printf("\nchild process is finished\n");
} else {
printf("\nparent process is executing with process ID %d\n", getpid());
printf("\nparent process is finished\n");
}

return 0;
}
OUTPUT:

ZOMBIE:

ORPHAN:
PROGRAM-4

OBJECTIVE: Write a C program to implement FCFS CPU Scheduling Algorithm.

SOURCE CODE:

#include <stdio.h>

struct Process {

int process_id;

int arrival_time;

int burst_time;

};

void sortProcessesByArrivalTime(struct Process processes[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (processes[j].arrival_time > processes[j + 1].arrival_time) {

struct Process temp = processes[j];

processes[j] = processes[j + 1];

processes[j + 1] = temp;

}
void calculateCompletionTime(struct Process processes[], int n, int completion_time[]) {

completion_time[0] = processes[0].arrival_time + processes[0].burst_time;

for (int i = 1; i < n; i++) {

if (processes[i].arrival_time > completion_time[i - 1]) {

completion_time[i] = processes[i].arrival_time + processes[i].burst_time;

} else {

completion_time[i] = completion_time[i - 1] + processes[i].burst_time;

void calculateAverageTime(struct Process processes[], int n) {

int completion_time[n];

int turnaround_time[n];

int waiting_time[n];

calculateCompletionTime(processes, n, completion_time);

float avg_waiting_time = 0, avg_turnaround_time = 0;

int total_cpu_time = 0;
for (int i = 0; i < n; i++) {

turnaround_time[i] = completion_time[i] - processes[i].arrival_time;

waiting_time[i] = turnaround_time[i] - processes[i].burst_time;

avg_waiting_time += waiting_time[i];

avg_turnaround_time += turnaround_time[i];

total_cpu_time += processes[i].burst_time;

avg_waiting_time /= n;

avg_turnaround_time /= n;

printf("Process\t Arrival Time\t Burst Time\t Completion Time\t Turnaround Time\t Waiting Time\
n");

for (int i = 0; i < n; i++) {

printf("P%d\t %d\t\t %d\t\t %d\t\t\t %d\t\t\t %d\n", processes[i].process_id,


processes[i].arrival_time,

processes[i].burst_time, completion_time[i], turnaround_time[i], waiting_time[i]);

float cpu_utilization = (float)total_cpu_time / completion_time[n - 1] * 100;

float throughput = (float)n / completion_time[n - 1];


printf("Average Waiting Time: %.2f\n", avg_waiting_time);

printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);

printf("CPU Utilization: %.2f%%\n", cpu_utilization);

printf("Throughput: %.2f processes per time unit\n", throughput);

int main() {

int n;

printf("Enter the number of processes: ");

scanf("%d", &n);

struct Process processes[n];

for (int i = 0; i < n; i++) {

processes[i].process_id = i + 1;

printf("Enter arrival time for process P%d: ", i + 1);

scanf("%d", &processes[i].arrival_time);

printf("Enter burst time for process P%d: ", i + 1);

scanf("%d", &processes[i].burst_time);

}
sortProcessesByArrivalTime(processes, n);
calculateAverageTime(processes, n);
return 0;
}
OUTPUT:

You might also like