Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
1. Operating System [CS-502]
Lab Manual
Session July- 2012
a) FCFS:
AIM: A program to simulate the FCFS CPU scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the needed variables and the number of process.
Step 3: Read the burst time for all process.
Step 4: Read the arrival time (AT).
Step 5: Calculate the waiting time and turn around time and display it.
Step 6: Calculate the average waiting time and average total turnaround time and display it.
Step 7: Stop the program.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
//clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("nPName Arrtime Burtime WaitTime Start TAT Finish");
2. for(i=0;i<n;i++)
{
printf("n%st%3dt%3dt%3dt%3dt%6dt%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("nAverage Waiting time:%f",(float)totwt/n);
printf("nAverage Turn Around Time:%f",(float)tottat/n);
getch();
return 0;
}
OUTPUT:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: p1 2 4
Enter the Process Name, Arrival Time & Burst Time: p2 3 5
Enter the Process Name, Arrival Time & Burst Time: p3 1 6
PName Arrtime Burtime WaitTime Strart TAT Finish
p3 1 6 0 1 6 7
p1 2 4 5 7 9 11
p2 3 5 8 11 13 16
Average Waiting Time: 4.3333333
Average Turn Around Time: 9.33333333
Q.1What is CPU scheduling?
Q.2What is turnaround time?
Q.3What is waiting time?
b) SJF:
AIM: A program to simulate the SJF CPU scheduling algorithm
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variable.
Step 3: Read the value of n.
Step 4: Scan the value of p[i] for i=1 to i
Step 5: Read the value of at and print the value of at.
Step 6: Do step 7 for i=1 to i
Step 7: Do step 8 for j=j+1 to j<=n and increment j value by 1.
Step 8: if p[i]>p[j], then assign t=p[j] and p[j]=p[i],then p[i]=t.
Step 9: Assign w[j]=at,at=at+p[i].
Step 10: Print the value at.
Step 11: Do step 8 for i=1,i<=n and increment value of i by 1.
Step 12: Assign sum=sum+w[i] and print the value of w[i].
Step 13: Assign avg=sum/n then print the value of avg.
Step 14: Print the value of i and p[i],w[i].
Step 15: Assign avg1=sum1/n and print the value of avg1.
3. Step 16: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("nPnametarrivaltimetexecutiontimetwaitingtimettatime");
for(i=0;i<n;i++)
printf("n%st%5dtt%5dtt%5dtt%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("nAverage waiting time is:%f",awt);
printf("nAverage turnaroundtime is:%f",ata);
getch();
}
OUTPUT:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 4 6
4. Enter the Process Name, Arrival Time & Burst Time: 2 5 15
Enter the Process Name, Arrival Time & Burst Time: 3 6 11
Pname arrivaltime executiontime waitingtime tatime
1 4 6 0 6
3 6 11 4 15
2 5 15 16 31
Average Waiting Time: 6.6667
Average Turn Around Time: 17.3333
Q.1What is the use of CPU scheduling?
Q.2What is burst time?
Q.3What is SJF CPU scheduling?
c) Priority:
AIM: A program to simulate the priority CPU scheduling algorithm
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables as required.
Step 3: Get the number of process.
Step 4: Get the burst time for each process.
Step 5: get the ID value for each process.
Step 6: Get the arrival time for each process.
Step 7: Using the read data find the waiting time and turnaround time and average turn around
time and print the result.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
6. d) Round Robin:
AIM: A program to simulate the Round Robin CPU scheduling algorithm
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables as required.
Step 3: Get the number of process.
Step 4: Get the burst time for each process.
Step 5: get the quantum value.
Step 6: Get the arrival time for each process.
Step 7: Using the read data find the waiting time and turnaround time and average turn around time
and print the result.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:");
for(i=0;i<n;i++)
printf("process %d: %sn",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("n %s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else
if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("n %s -> %d",pn[i],et[i]);
et[i]=0;}
}
}
printf("n Total Estimated Time:%d",x);
7. getch();
}
OUTPUT:
Enter the no of processes: 2
Enter the time quantum: 3
Enter the process name & estimated time: p1 12
Enter the process name & estimated time: p2 15
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
Total Estimated Time: 27
Q.1What is Round Robin CPU scheduling?
Q.2What is the advantage of Round robin CPU scheduling?
5-AIM:To write a C program for implementing Producer Consumer problem using Semaphore.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the choice 1 for Producer, 2 for Consumer.
Step 3: If the choice is 1, the produced buffer size is 1, again if the choice is 1 then buffer size
will be increased[produced buffer = 1 + 1 = 2].
Step 4: If the choice is 2, the consumed buffer size is 1.
Step 5: Stop the program.
8. PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//#include<process.h>
#define max 5
int n,produced,consumed,buffer[max];
int result,i;
int producer(int consumed,int result)
{
if((consumed=1)&&(i>=0))
{
result=result+1;
i++;
buffer[i]=result;
produced=1;
printf("Produced bufffer=%dn",buffer[i]);
consumed=0;
return(produced,result);
}
else
printf("Buffer still to be consumedn");
}
int consumer(int produced,int result)
{
if((produced==1)&&(i>0))
{
consumed=1;
printf("Consumed buffer=%dn",buffer[i]);
produced=0;
i--;
return(consumed,result);
}
else
printf("Buffer still to be produced");
}
int main()
{
clrscr();
produced=0;
consumed=1;
result=0;
i=0;
while(1)
{
printf("Enter the choicen");
printf("1.Producer 2.Consumer 3.Exitn");
scanf("%d",&n);
if(n==3)
exit(0);
else if(n==1)
producer(consumed,buffer[i]);
else if(n==2)
consumer(produced,buffer[i]);
else
exit(0);
}
}
SAMPLE OUTPUT:
9. Enter the choice
1.Producer 2.Consumer 3.Exit
1
Produced Buffer = 1
Enter the choice
1.Producer 2.Consumer 3.Exit
1
Produced Buffer = 2
Enter the choice
1.Producer 2.Consumer 3.Exit
2
Consumed Buffer = 2
Enter the choice
1.Producer 2.Consumer 3.Exit
2
Consumed Buffer = 1
Enter the choice
1.Producer 2.Consumer 3.Exit
2
Buffer still to be produced
Enter the choice
1.Producer 2.Consumer 3.Exit
6-aim-wap to implement optimal page replacement algorithm
program:
#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;
flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
14. 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:
Enter the Number of Pages: 12
Enter 12 Page Numbers:
2 3 2 1 5 2 4 5 3 2 5 2
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
Q.1-What is the use of page replacement algorithm?
Q.2- What is page fault?
15. Q.3- Define FIFO page replacement algorithm?
Q.4- What is the advantage of FIFO?
9-AIM: A program to simulate Bankers Algorithm
PROGRAM:
// Bankers Algorithm- Systems Lab - C Program
#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0
int m,n,i,j,count=0,process;
int max[10][10],alloc[10][10],need[10][10],c[10],avail[10],finish[10];
void readtable(int t[10][10])
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&t[i][j]);
}
void printtable(int t[10][10])
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("t%d",t[i][j]);
16. printf("n");
}
}
void readvector(int v[10])
{
for(j=0;j<n;j++)
scanf("%d",&v[j]);
}
void printvector(int v[10])
{
for(j=0;j<n;j++)
printf("t%d",v[j]);
}
void init()
{
printf("enter the number of processn");
scanf("%d",&m);
printf("enter the number of resourcesn");
scanf("%d",&n);
printf("enter the claim tablen");
readtable(max);
printf("enter the allocation tablen");
readtable(alloc);
printf("enter the max units of each resourcen");
readvector(c);
for(i=0;i<n;i++)
finish[i]=false;
}
void findavail()
{
int sum;
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<m;i++)
{
sum=sum+alloc[i][j];
}
avail[j]=c[j]-sum;
}
}
void findneed()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
}
void selectprocess()
{
int flag;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=avail[j])
flag=1;
17. else
{
flag=0;
break;
}
}
if((flag==1)&&(finish[i]==false))
{
process=i;
count++;
break;
}
}
printf("current status isn");
printtable(alloc);
if(flag==0)
{
printf("system is in unsafe staten");
exit(1);
}
printf("system is in safe state");
}
void executeprocess(int p)
{
printf("excuting process is %d",p);
printtable(alloc);
}
void releaseresource()
{
for(j=0;j<n;j++)
avail[j]=avail[j]+alloc[process][j];
for(j=0;j<n;j++)
{
alloc[process][j]=0;
need[process][j]=0;
}
}
Void main()
{
init();
findavail();
findneed();
do
{
selectprocess();
finish[process]=true;
executeprocess(process);
releaseresource();
}while(count<m);
printf("n all proces executed correctly");
}
OUTPUT:
enter the number of process
3
enter the number of resources
2
enter the claim table
0 0 1
1 1 0
18. enter the allocation table
1 1
0 1
1 0
enter the max units of each resource
2 2
current status is
1 1
0 1
1 0
system is in safe state excuting process is 0 1 1
0 1
1 0
current status is
0 0
0 1
1 0
system is in safe state excuting process is 1 0 0
0 1
1 0
current status is
0 0
0 0
1 0
system is in safe state excuting process is 2 0 0
0 0
1 0
all process executed correctly
Q.1- Explain Bankers algorithm
Q.2- What is deadlock.
Q.3- State the necessary condition for deadlock avoidance.