Operating Systems Lab Programs
Operating Systems Lab Programs
Operating Systems Lab Programs
if(pid == 0) {
printf("Child => PID: %d PID: %d\n", getppid(), getpid());
exit(EXIT_SUCCESS);
}
else if(pid > 0) {
printf("Parent => PID: %d\n", getppid());
printf("Waiting for child process to finish.\n");
wait(NULL);
printf("Child process finished.\n");
}
else {
printf("Unable to create child process.\n");
}
return EXIT_SUCCESS;
}
2. fork()
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void childTask() {
printf("Hello World\n");
}
void parentTask() {
printf("Main task.\n");
}
int main(void) {
pid_t pid = fork();
if(pid == 0) {
childTask();
exit(EXIT_SUCCESS);
}
else if(pid > 0) {
wait(NULL);
parentTask();
}
else {
printf("Unable to create child process.");
}
return EXIT_SUCCESS;
}
int main() {
int fd;
ssize_t bytesRead, bytesWritten;
char buffer[100];
return 0;
}
OUTPUT:
HELLO WORLD:
Number of characters read: 12
Number of characters written: 12
2.Write a program to implement named and unnamed pipes.
1.Unnamed Pipe (Parent-Child Communication):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipefd[2];
pid_t pid;
char buffer[100];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
close(pipefd[1]); // Close the write end of the pipe
int bytesRead = read(pipefd[0], buffer, sizeof(buffer));
close(pipefd[0]); // Close the read end of the pipe
printf("Child: Received message from parent: %.*s\n", bytesRead, buffer);
} else {
// Parent process
close(pipefd[0]); // Close the read end of the pipe
char message[] = "Hello from parent!";
write(pipefd[1], message, sizeof(message));
close(pipefd[1]); // Close the write end of the pipe
}
return 0;
}
⮚ S_IRUSR: This constant represents the permission bit for the owner (user) of the file to read the
file. It stands for "read permission for user."
⮚ S_IWUSR: This constant represents the permission bit for the owner (user) of the file to write to
the file. It stands for "write permission for user."
⮚ S_IRGRP: This constant represents the permission bit for the group associated with the file to read
the file. It stands for "read permission for group."
⮚ S_IROTH: This constant represents the permission bit for others (users who are not the owner and
not in the group) to read the file. It stands for "read permission for others."
4a.)
AIM: To write a c program to simulate the CPU scheduling algorithm First Come First Serve (FCFS)
DESCRIPTION:
To calculate the average waiting time using the FCFS algorithm first the waiting time of the first process
is kept zero and the waiting time of the second process is the burst time of the first process and the
waiting time of the third process is the sum of the burst times of the first and the second process and so on.
After calculating all the waiting times the average waiting time is calculated as the average of all the
waiting times. FCFS mainly says first come first serve the algorithm which came first will be served first.
PROGRAM:
#include<stdio.h>
int main()
{
int at[20], bt[20], wt[20], tat[20], ct[20], rt[20], i, n;
float wtavg, tatavg, throughput;
int total_bt = 0;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\nEnter Arrival Time for Process %d -- ", i);
scanf("%d", &at[i]);
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
total_bt += bt[i];
}
wt[0] = 0;
tat[0] = bt[0];
ct[0] = at[0] + bt[0];
rt[0] = wt[0];
wtavg = 0;
tatavg = tat[0];
for(i = 1; i < n; i++)
{
if (ct[i-1] > at[i]) {
wt[i] = ct[i-1] - at[i];
} else {
wt[i] = 0;
}
rt[i] = wt[i];
tat[i] = wt[i] + bt[i];
ct[i] = at[i] + tat[i];
wtavg += wt[i];
tatavg += tat[i];
}
throughput = (float)n / (ct[n-1] - at[0]);
printf("\tPROCESS\tARRIVAL TIME\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\
tCOMPLETION TIME\tRESPONSE TIME\n");
return 0;
}
OUTPUT:
Enter the number of processes -- 4
P0 0 8 0 8 8 0
P1 1 4 7 11 12 7
P2 2 2 10 12 14 10
P3 3 1 11 12 15 11
Average Waiting Time -- 7.000000
Average Turnaround Time -- 10.750000
Throughput -- 0.266667
#include<stdio.h>
#include<string.h>
int main()
{
// Variable and array declarations
int bt[20], at[10], n, i, j, temp, st[10], ft[10], wt[10], tat[10];
int totwt = 0, tottat = 0;
float awt, atat;
char p[10][10], t[10];
return 0;
}
INPUT:
Enter the number of process:3
Enter process name, arrival time& execution time:p1
2
5
Enter process name, arrival time& execution time:p2
5
2
Enter process name, arrival time& execution time:p3
0
9
OUTPUT:
c) PRIORITY:
AIM: To write a c program to simulate the CPU scheduling priority algorithm.
DESCRIPTION:
To calculate the average waiting time in the priority algorithm, sort the burst times according to their
priorities and then calculate the average waiting time of the processes. The waiting time of each process is
obtained by summing up the burst times of all the previous processes.
PROGRAM:
#include<stdio.h>
Int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg,
tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++){
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i); scanf("%d
%d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k]){
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n); printf("\nAverage
Turnaround Time is --- %f",tatavg/n);
}
INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
OUTPUT
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
11101
42516
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000
Average Turnaround Time is --- 12.000000
#include<stdio.h>
int main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
INPUT:
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3
Enter the size of time slice – 3
OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2347
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ------------ 5.666667
int main() {
int buffer[10], bufsize, in, out, produce, consume, choice = 0;
in = 0;
out = 0;
bufsize = 10;
while (choice != 3) {
printf("\n1. Produce \t 2. Consume \t 3. Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if ((in + 1) % bufsize == out) {
printf("\nBuffer is Full");
} else {
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in + 1) % bufsize;
}
break;
case 2:
if (in == out) {
printf("\nBuffer is Empty");
} else {
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out + 1) % bufsize;
}
break;
case 3:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter 1, 2, or 3.\n");
}
}
return 0;
}
OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit
Enter your choice: 3
b) Dining-Philosophers problem.
AIM: To Write a C program to simulate the concept of Dining-Philosophers problem.
DESCRIPTION
The dining-philosophers problem is considered a classic synchronization problem because it is an example
of a large class of concurrency-control problems. It is a simple representation of the need to allocate
several resources among several processes in a deadlock-free and starvation-free manner. Consider five
philosophers who spend their lives thinking and eating. The philosophers share a circular table surrounded
by five chairs,each belonging to one philosopher. In the center of the table is a bowl of rice, and the table
is laid with five single chopsticks. When a philosopher thinks, she does not interact with her colleagues.
From time to time, a philosopher gets hungry and tries to pick up the two chopsticks that are closest to her
(the chopsticks that are between her and her left and right neighbors). A philosopher may pick up only one
chopstick at a time.
Obviously, she cam1ot pick up a chopstick that is already in the hand of a neighbor. When a hungry
philosopher has both her chopsticks at the same time, she eats without releasing her chopsticks. When she
is finished eating, she puts down both of her chopsticks and starts thinking again. The dining-philosophers
Problem may lead to a deadlock situation and hence some rules have to be framed to avoid the occurrence
of deadlock.
PROGRAM
#include <stdio.h>
#include <stdlib.h> // Added for abs() function
void one();
void two();
int main() {
int i;
printf("\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total number of philosophers: ");
scanf("%d", &tph);
do {
printf("\n1. One can eat at a time\n2. Two can eat at a time\n3. Exit\nEnter your choice: ");
scanf("%d", &cho);
switch (cho) {
case 1:
one();
break;
case 2:
two();
break;
case 3:
exit(0);
default:
printf("\nInvalid option.\n");
}
} while (1);
}
return 0;
}
void one() {
int pos = 0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
void two() {
int i, j, s = 0, t, r, x;
printf("\nAllow two philosophers to eat at the same time\n");
SOURCE CODE :
#include<stdio.h>
#include<string.h>
int main() {
int alloc[10][10], max[10][10];
int avail[10], work[10], total[10];
int i, j, k, n, need[10][10];
int m;
int count = 0, c = 0;
char finish[10];
A:
for (i = 0; i < n; i++) {
c = 0;
for (j = 0; j < m; j++)
if ((need[i][j] <= work[j]) && (finish[i] == 'n'))
c++;
if (c == m) {
printf("All resources can be allocated to Process %d\n", i + 1);
printf("Available resources are:");
for (k = 0; k < m; k++) {
work[k] += alloc[i][k];
printf("%4d", work[k]);
}
printf("\n");
finish[i] = 'y';
printf("Process %d executed?: %c\n", i + 1, finish[i]);
count++;
}
}
if (count != n)
goto A;
else
printf("\nSystem is in a safe state.\n");
return 0;
}
OUTPUT
Enter the no. of processes and resources: 4 3
Enter the claim matrix:
322
613
314
422
Enter the allocation matrix:
100
612
211
002
Resource vector:9 3 6
All the resources can be allocated to Process 2 Available resources are: 6 2 3
Process 2 executed?:y
All the resources can be allocated to Process 3 Available resources are: 8 3 4
Process 3 executed?:y
All the resources can be allocated to Process 4 Available resources are: 8 3 6
Process 4 executed?:y
All the resources can be allocated to Process 1 Available resources are: 9 3 6
Process 1 executed?:y
System is in safe mode
The given state is safe state
c)FIRST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
Int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highes
t=0; static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
}
}
frag[i]=highest; bf[ff[i]]=1; highest=0;
}
ff[i]=j; highest=temp;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
11376
24151
PROGRAM
A) FCFS DISK SCHEDULING ALGORITHM
#include<stdio.h>
int main()
{
int t[20], n, I, j, tohm[20], tot=0; float avhm;
printf(“enter the no.of tracks”);
scanf(“%d”,&n);
printf(“enter the tracks to be traversed”);
for(i=2;i<n+2;i++)
scanf(“%d”,&t*i+);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf(“Tracks traversed\tDifference between tracks\n”);
for(i=1;i<n+1;i++)
printf(“%d\t\t\t%d\n”,t*i+,tohm*i+);
printf("\nAverage header movements:%f",avhm);
getch();
}
INPUT
Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
55 58 60 70 18 90 45 3 2 10 52 72 150 160 184 60 10 24
Average header movements:30.888889
}
INPUT
Enter no.of tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
150 160 184 90 70 60 58 55 18 50 10 24 94 20 10 2 3 37
Average header movements: 27.77
INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100
OUTPUT
Tracks traversed Difference Between tracks
150 160 184 18 55 58 60 70 90 50 10 24 240 37 3 2 10 20
Average seek time : 35.7777779
}
OUTPUT:
Enter the starting block & length of file 4 10
4->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk.