1a.
FCFS CPU SCHEDULING ALGORITHM
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = 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("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
3
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}
INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000:");
1b. SJF CPU SCHEDULING ALGORITHM
#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = 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("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
{
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}]
INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
1c. ROUND ROBIN CPU SCHEDULING ALGORITHM
#include<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
5
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
getch();
}
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
The Average Turnaround time is – 15.666667
The Average Waiting time is -- 5.666667
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
1 24 6 30
2347
3 3 7 10
1d.
PRIORITY CPU SCHEDULING ALGORITHM
#include<stdio.h>
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];
6
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);
getch();
}
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
2a. MFT MEMORY MANAGEMENT TECHNIQUE
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentation is %d",ef);
getch();
14
}
INPUT
Enter the total memory available (in Bytes) -- 1000
Enter the block size (in Bytes)-- 300
Enter the number of processes – 5
Enter memory required for process 1 (in Bytes) -- 275
Enter memory required for process 2 (in Bytes) -- 400
Enter memory required for process 3 (in Bytes) -- 290
Enter memory required for process 4 (in Bytes) -- 293
Enter memory required for process 5 (in Bytes) -- 100
No. of Blocks available in memory -- 3
OUTPUT
PROCESS MEMORY REQUIRED ALLOCATED INTERNAL FRAGMENTATION
1 275 YES 25
2 400 NO -----
3 290 YES 10
4 293 YES 7
Memory is Full, Remaining Processes cannot be accommodated
Total Internal Fragmentation is 42
Total External Fragmentation is 100
2b. MVT MANAGEMENT TECHNIQUE MEMORY
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
15
getch();
}
INPUT
Enter the total memory available (in Bytes) -- 1000
Enter memory required for process 1 (in Bytes) -- 400
Memory is allocated for Process 1
Do you want to continue(y/n) -- y
Enter memory required for process 2 (in Bytes) -- 275
Memory is allocated for Process 2
Do you want to continue(y/n) -- y
Enter memory required for process 3 (in Bytes) -- 550
OUTPUT
Memory is Full
Total Memory Available -- 1000
PROCESS MEMORY ALLOCATED
1 400
2 275
Total Memory Allocated is 675
Total External Fragmentation is 325
3a. A program to simulate FIFO Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{ if(q>=3) q=0; a[q]=b[i];
q++;
if(q1<3) { q1=q; } }
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{ printf("-->%c",f); m++; }
p=0; for(k=0;k<q1;k++)
{ if(b[i+1]==a[k]) p=1; } }
printf("\nNo of faults:%d",m);
getch();
}
OUTPUT:
Input: Enter the Number of Pages: 12
Enter 12 Page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 531-> F 2 521-> F 4 524-> F 5 524 3 324-> F 2 324 5
354-> F 2 352-> F No of faults: 9
3b. A program to simulate LRU Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n; char f='F';
clrscr();
printf("Enter the number of pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{ if(q>=3) q=0; a[q]=b[i]; q++; if(q1<3)
{ q1=q; //g=1; } }
printf("\n%d",b[i]);
printf("\t"); for(h=0;h<q1;h++)
printf("%d",a[h]); if((p==0)&&(q<=3))
{ printf("-->%c",f); m++; }
p=0; g=0; if(q1==3)
{ for(k=0;k<q1;k++) {
if(b[i+1]==a[k]) p=1; }
for(j=0;j<q1;j++) { u=0; k=i; while(k>=(i-1)&&(k>=0))
if(b[k]==a[j]) u++; k--; }
if(u==0) q=j; } }
else { for(k=0;k<q;k++)
{ if(b[i+1]==a[k]) p=1; } } }
printf("\nNo of faults:%d",m);
getch();
}
OUTPUT:
Input: Enter the Number of Pages: 12 Enter 12 Page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 251-> F 2 251 4 254-> F 5 254 3 354-> F 2 352-> F 5 352
2 352 No of faults: 7
3c. A program to simulate LFU Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
int main()
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
printf("Enter no of frames : ");
scanf("%d",&f);
printf("Enter no of pages : ");
scanf("%d",&p);
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<50;i++)
count[i]=0;
printf("Enter page no : \n");
for(i=0;i<p;i++)
scanf("%d",&pages[i]);
printf("\n");
for(i=0;i<p;i++)
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
if(frame[j]!=-1)
hit++;
flag=0;
frame[j]=pages[i];
break;
if(count[least]>count[frame[j]])
least=frame[j];
if(flag)
minTime=50;
for(j=0;j<f;j++)
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
temp=j;
minTime=time[frame[j]];
}
count[frame[temp]]=0;
frame[temp]=pages[i];
for(j=0;j<f;j++)
printf("%d ",frame[j]);
printf("\n");
printf("Page hit = %d",hit);
return 0;
OUTPUT:
Input: Enter the Number of Pages: 12 Enter 12 Page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 251-> F 2 251 4 254-> F 5 254 3 354-> F 2 352-> F 5 352
2 352 No of faults: 7
4. Write a C program that illustrates two processes communicating using shared
memory.
Program:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
Struct country
{
Char name[30];
Char capital_city [30];
Char currency[30];
Int population;
};
Int main(int argc,char*argv[])
{
Int shm_id;
Char*shm_addr;
Int*countries_num;
Struct country*countries;
Struct shmid_ds shm_desc;
Shm_id=shmget(100,2048,IPC_CREAT|IPC_EXCL\0600);
If(shm_id==-1){
Perror(“main:shmget:”);
Exit(1);
}
Shm_addr=shmat(shm_id,NULL,0);
If(!shm_addr){
Perror(“main:shmat:”);
Exit(1);
}
Countries_num=(int*)shm_addr;
*countries_num=0;
Countries=(struct country*)((void*)shm_addr sizeof(int));
Strcpy(countries[0],name,”U.S.A”);
Strcpy(countries[0],capital_city,”WASHINGTON”);
Strcpy(countries[0],currency,”U.S.DOLLAR”);
Countries[0].population=250000000;
( countries_num) ;
Strcpy(countries[1].name,”israel”);
Strcpy(countries[1].capital_city,”jerushalem”);
Strcpy(countries[1].currency,”NEW ISRAEL SHEKED”);
Countries[1].population=6000000;
(*countries_num) ;
Strcpy(countries[2].name,”France”);
Strcpy(countries[2].capital_city,”paris”);
Strcpy(countries[2].currency,”Frank”);
Countries[2].population=60000000;
(*countries_num) ;
For(i=0;i<(*countries_num);i )
{
Printf(“country%d:\n”,i 1);
Printf(“name:%d:\n”,i 1);
Printf(“currency:%s:\n”,countries[i].currency);
Printf(“population:%d:\n”,countries[i].population);
}
If(shmdt(shm_addr)==-1){
Perror(“main:shmdt:”);
}
If(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
Perror(“main:shmctl:”);
}
return 0;
}
Output:
Student@ubuntu:~$gcc shm.c
Student@ubuntu:~$ ./a.out
Shared memory ID=65537 child pointer 3086680064
Child value =1
Shared memory ID=65537 child pointer 3086680064
Parent value=1
Parent value=42
Child value=42
5. Write a C program to simulate producer-consumer problem using semaphores.
PROGRAM
#include<stdio.h>
void 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 \t3. Exit”);
printf(“\nEnter 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;
}
}
}
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
6.Write a C program to simulate Bankers algorithm for the purpose of deadlock
avoidance.
PROGRAM
#include< stdio.h >
#include< conio.h >
void main()
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf(“Enter the no of processes:\n”);
scanf(“%d”,&p);
printf(“Enter the no of resources:\n”);
scanf(“%d”,&r);
printf(“Enter the claim for each process:”);
for(i=1;i<=p;i++)
printf(“\nFor process %d”,i);
for(j=1;j<=r;j++)
scanf(“%d”,&clm[i][j]);
printf(“Enter the allocation for each process:\n”);
for(i=1;i<=p;i++)
printf(“\nFor process “,i);
for(j=1;j<=r;j++)
scanf(“%d”,&alloc[i][j]);
printf(“Enter total no of each resource:”);
for(j=1;j<=r;j++)
scanf(“%d”,&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
do
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
req[i][j]=clm[i][j]-alloc[i][j];
printf(“\n\nAvailable resorces is:”);
for(j=1;j<=r;j++)
{ printf(” “,avail[j]); }
printf(“\nClaim matrix:\t\tAllocation matrix:\n”);
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
printf(“%d”,clm[i][j]);
}
printf(“\t\t\t”);
for(j=1;j<=r;j++)
printf(“%d”,alloc[i][j]);
printf(“\n”);
prc=0;
for(i=1;i<=p;i++)
if(comp[i]==0)//if not completed
prc=i;
for(j=1;j<=r;j++)
if(avail[j]
prc=0;
break;
if(prc!=0)
break;
if(prc!=0)
{
printf(“\nProcess “,prc,”runs to completion!”);
count++;
for(j=1;j<=r;j++)
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
while(count!=p&&prc!=0);
if(count==p)
printf(“\nThe system is in a safe state!!”);
else
printf(“\nThe system is in an unsafe state!!”);
getch();
OUT PUT:
Enter the no of processes:
Enter the no of resources:
Enter the claim for each process:
For process I : 2 4 5
For process II: 2 5 3
Enter the total no of each resource : 5 5 2
Available resource is :
Claim matrix: allocation matrix:
245 123
253 234
The system is in an unsafe state!!
7.A program to simulate Bankers Algorithm for Deadlock Prevention.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int cl[10][10],al[10][10],av[10],i,j,k,m,n,ne[10][10],flag=0;
clrscr();
printf("\nEnter the matrix"); scanf("%d %d",&m,&n);
printf("\nEnter the claim matrix:");
for(i=0;i<m;i++) { for(j=0;j<n;j++) {
scanf("%d",&cl[i][j]); } }
printf("\nEnter allocated matrix:");
for(i=0;i<m;i++) { for(j=0;j<n;j++) {
scanf("%d",&al[i][j]); } }
printf("\nThe need matrix:\n");
for(i=0;i<m;i++) { for(j=0;j<n;j++)
{ ne[i][j]=cl[i][j]-al[i][j];
printf("\t%d",ne[i][j]); } printf("\n"); }
printf("\nEnter avaliable matrix");
for(i=0;i<n;i++)
scanf("%d",&av[i]);
printf("Claim matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ printf("\t%d",cl[i][j]); }
printf("\n"); }
printf("\n Allocated matrix:\n");
for(i=0;i<m;i++) { for(j=0;j<n;j++)
{ printf("\t%d",al[i][j]); } printf("\n"); }
printf("Available matrix:\n");
for(i=0;i<n;i++) { printf("%d\t",av[i]); } /
/for(k=0;k<m;k++) for(i=0;i<m;i++)
{ for(j=0;j<n;j++) { if(av[j]>=ne[i][j]) flag=1; else flag=0; } }
if(flag==0) printf("Unsafe State");
else printf("Safe State"); getch();
}
OUTPUT:
Input: Enter the claim matrix:3 2 2 6 1 3 3 1 4 4 2 2
Enter allocated matrix:1 0 0 5 1 1 2 1 1 0 0 2
The need matrix: 2 2 2 1 0 2 1 0 3 4 2 0
Enter available matrix1 1 2
Output: Claim matrix: 3 2 2 6 1 3 3 1 4 4 2 2
Allocated matrix: 1 0 0 5 1 1 2 1 1 0 0 2
Available matrix: 1 1 2
Safe State - 21