Sie sind auf Seite 1von 35

Ms.

Manisha Kumari

List of Experiments
Course Name:- Operating System (CSL-373) B.Tech CSE
4th
Sem.

1) Write a C program to implement CPU Scheduling Algorithms FCFS.
2) Writea C program to implement CPU Scheduling Algorithms SJF.
3) Write a C program to implement CPU Scheduling Algorithms Priority.
4) Write a C program to implement CPU Scheduling Algorithms Round Robin .
5) Write a C program to implement MVT.
6) Write a C program to implement MFT.
7) Write a C program to implement memory management using paging technique.
8) Write a C program to implement memory management using segmentation.
9) Write a C program to implement Paging Technique of Memory Management.
10) Write a C program to implement all Page Replacement Algorithms FIFO .
11) Write a C program to implement all Page Replacement Algorithms LRU
12) Write a C program to implement Bankers algorithm for Deadlock Avoidance.
13) Write a C program to implement Bankers algorithm for Deadlock Prevention.
14) Write a C program to create a file.
15) Write a C program to write the data into a file.
16) To create the file, read data from the file, update the file.






1) Write a C program to implement CPU scheduling algorithms
FCFS.

AIM:
To write a C program to implement the CPU scheduling algorithm for FIRST COME FIRST
SERVE.

PROBLEM DESCRIPTION:
Cpu scheduler will decide which process should be given the CPU for its execution.For this it
use different algorithm to choose among the process. one among that algorithm is fcfs algorithm.
In this algorithm the process which arrive first is given the cpu after finishing its request only it
will allow cpu to execute other process.

ALGORITHM:
Step1: Create the number of process.
Step2: Get the ID and Service time for each process.
Step3: Initially, Waiting time of first process is zero and Total time for the first
process is the starting time of that process.
Step4: Calculate the Total time and Processing time for the remaining processes.
Step5: Waiting time of one process is the Total time of the previous process.
Step6: Total time of process is calculated by adding Waiting time and Service time.
Step7: Total waiting time is calculated by adding the waiting time for lack process.
Step8: Total turn around time is calculated by adding all total time of each process.
Step9: Calculate Average waiting time by dividing the total waiting time by total
number of process.
Step10: Calculate Average turn around time by dividing the total time by the
number of process.
Step11: Display the result.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
`char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
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++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[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 Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[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();
}

OUTPUT:
Input:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7
Output:
PName Arrtime Burtime Srart TAT Finish
1 2 3 2 3 5
2 5 6 5 6 4
3 6 7 6 7 10
Average Waiting Time: 3.333
Average Turn Around Time: 7.000
2) Write a C program to implement CPU scheduling algorithms
SJF.
AIM:
To write a C program to implement the CPU scheduling algorithm for Shortest job first.

PROBLEM DESCRIPTION:
Cpu scheduler will decide which process should be given the CPU for its execution. For this it
use different algorithm to choose among the process. one among that algorithm is sjf algorithm.
In this algorithm the process which has less service time given the cpu after finishing its request
only it will allow cpu to execute next other process.

ALGORITHM:
Step1:Get the number of process.
Step2:Get the id and service time for each process.
Step3:Initially the waiting time of first short process as 0 and total time of first
short is process the service time of that process.
Step4:Calculate the total time and waiting time of remaining process.
Step5:Waiting time of one process is the total time of the previous process.
Step6:Total time of process is calculated by adding the waiting time and service
time of each process.
Step7:Total waiting time calculated by adding the waiting time of each process.
Step8:Total turn around time calculated by adding all total time of each process.
Step9:calculate average waiting time by dividing the total waiting time by total
number of process.
Step10:Calculate average turn around time by dividing the total waiting time by
total number of process.
Step11:Display the result.

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("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%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:
Input:
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 4 6
Enter the Process Name, Arrival Time & Burst Time: 2 5 15
Enter the Process Name, Arrival Time & Burst Time: 3 6 11
Output:
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


3) Write a C program to implement CPU scheduling algorithms
Priority.

AIM: A program to simulate the priority CPU scheduling algorithm.

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])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
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];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[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("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

OUTPUT:
Input:
Enter the number of processes: 3
Enter the Process Name, Arrival Time, execution time & priority: 1 2 3 1
Enter the Process Name, Arrival Time, execution time & priority: 2 4 5 2
Enter the Process Name, Arrival Time, execution time & priority: 3 5 6 3
Output:
Pname arrivaltime executiontime priority waitingtime tatime
1 2 3 1 0 3
2 4 5 2 1 6
3 5 6 3 5 11
Average Waiting Time: 2.0000
Average Turn Around Time: 6.6667


4) Write a C program to implement CPU scheduling algorithms
Round Robin.

AIM :
To write a C program to simulate the CPU scheduling algorithm for round robin

PROBLEM DESCRIPTION:
CPU scheduler will decide which process should be given the CPU for its execution .For this it
use different algorithm to choose among the process .one among that algorithm is Round robin
algorithm.
In this algorithm we are assigning some time slice .The process is allocated according to the time
slice ,if the process service time is less than the time slice then process itself will release the CPU
voluntarily .The scheduler will then proceed to the next process in the ready queue .If the CPU
burst of the currently running process is longer than time quantum ,the timer will go off and will
cause an interrupt to the operating system .A context switch will be executed and the process will
be put at the tail of the ready queue.

ALGORITHM:
Step 1: Initialize all the structure elements
Step 2: Receive inputs from the user to fill process id,burst time and arrival time.
Step 3: Calculate the waiting time for all the process id.
i) The waiting time for first instance of a process is calculated as:
a[i].waittime=count + a[i].arrivt
ii) The waiting time for the rest of the instances of the process is
calculated as:
a) If the time quantum is greater than the remaining burst time then waiting time is calculated as:

a[i].waittime=count + tq
b) Else if the time quantum is greater than the remaining burst
time then waiting time is calculated as:
a[i].waittime=count - remaining burst time
Step 4: Calculate the average waiting time and average turnaround time
Step 5: Print the results of the step 4.

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: %s\n",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);
getch();
}

OUTPUT:
Input:
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





Output:
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
Total Estimated Time: 27


5) Write a C program to implement MVT.

AIM: A program to simulate the MVT.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int m=0,m1=0,m2=0,p,count=0,i;
clrscr();
printf("Enter the memory capacity:");
scanf("%d",&m);
printf("Enter the no of processes:");
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\nEnter memory req for process%d: ",i+1);
scanf("%d",&m1);
count=count+m1;
if(m1<=m)
{
if(count==m)
{
printf("There is no further memory remaining:");
}
else
{
printf("The memory allocated for process%d is: %d ",i+1,m);
m2=m-m1;
printf("\nRemaining memory is: %d",m2);
m=m2;
}
}
else
{
printf("Memory is not allocated for process%d",i+1);
}
printf("\nExternal fragmentation for this process is:%d",m2);
}
getch();
}



OUTPUT:
Input:
Enter the memory capacity: 80
Enter no of processes: 2
Enter memory req for process1: 23
Output:
The memory allocated for process1 is: 80
Remaining memory is: 57
External fragmentation for this process is: 57
Enter memory req for process2: 52
The memory allocated for process2 is: 57
Remaining memory is: 5
External fragmentation for this process is: 5


6) Write a C program to implement MFT.

AIM: A Program to simulate the MFT

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,p,s,p1;
int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1;
clrscr();
printf("Enter the memory size:");
scanf("%d",&m);
printf("Enter the no of partitions:");
scanf("%d",&p);
s=m/p;
printf("Each partn size is:%d",s);
printf("\nEnter the no of processes:");
scanf("%d",&p1);
for(i=0;i<p1;i++)
{
printf("\nEnter the memory req for process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess is allocated in partition%d",i+1);
fra1=s-m1[i];
printf("\nInternal fragmentation for process is:%d",fra1);
f1=f1+fra1;
}
else
{
printf("\nProcess not allocated in partition%d",i+1);
s1=m1[i]-s;
fra2=s-s1;
f2=f2+fra2;
printf("\nExternal fragmentation for partition is:%d",fra2);
}
}
printf("\nProcess\tmemory\tallocatedmemory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d\t%5d",i+1,s,m1[i]);
f=f1+f2;
printf("\nThe tot no of fragmentation is:%d",f);

getch();
}

OUTPUT:
Input:
Enter the memory size: 80
Enter the no of partitions: 4
Each partition size: 20
Enter the number of processes: 2
Enter the memory req for process1: 18
Output:
Process1 is allocated in partn1
Internal fragmentation for process1 is: 2
Enter the memory req for process2: 22
Process2 is not allocated in partn2
External fragmentation for process2 is: 18
Process memory allocated
1 20 18
2 20 22
The tot no of fragmentation is: 20


7) MEMORY MANAGEMENT SCHEME- PAGING

AIM: To write a C program to implement memory management using paging technique.

ALGORITHM:
Step1 : Start the program.
Step2 : Read the base address, page size, number of pages and memory unit.
Step3 : If the memory limit is less than the base address display the memory
limit is less than limit.
Step4 : Create the page table with the number of pages and page address.
Step5 : Read the page number and displacement value.
Step6 : If the page number and displacement value is valid, add the displacement
value with the address corresponding to the page number and display the result.
Step7 : Display the page is not found or displacement should be less than page
size.
Step8 : Stop the program.

PROGRAM:
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],n,i,pa,p,a,d;
printf(\nProgram for paging);
scanf(%d,&n);
printf(\nEnter the base address:);
for(i=0;i<n;i++)
{
scanf(%d,&b[i]);
}
printf(\nEnter the logical address:);
scanf(%d,&p);
for(i=0;i<n;i++)
{
if(i==p)
{
pa=b[i]+d;
a=b[i];
printf(\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t %d \t ,p,a,pa);





}
printf(\nInvalid page);
}


OUTPUT:
Sample Input 1:
Program for paging
Enter the number of pages:2
Enter the base address:
100
150
Enter the Logical address:50
Enter the page number:1
Sample Output 1:
PageNo. BaseAdd. PhysicalAdd.
1 150 200
Sample Input 2:
Program for paging
Enter the number of pages:1
Enter the base address:
100
Enter the Logical address:2
Enter the page number:2
Sample Output 2:
Invalid page.

























8) MEMORY MANAGEMENT SCHEME-SEGMENTATION.

AIM:
To write a C program to implement memory management using segmentation

ALGORITHM:
Step1 : Start the program.
Step2 : Read the base address, number of segments, size of each segment, memory limit.
Step3 : If memory address is less than the base address display invalid memory limit.
Step4 : Create the segment table with the segment number and segment address and display it.
Step5 : Read the segment number and displacement.
Step6 : If the segment number and displacement is valid compute the real address and display the
same.
Step7 : Stop the program.

PROGRAM:
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],l[20],n,i,pa,s,a,d;
printf(\nProgram for segmentation);
printf(\nEnter the number of segments:);
scanf(%d,&n);
printf(\nEnter the base address and limit register:);
for(i=0;i<n;i++)
{
scanf(%d,&b[i]);
scanf(%d,&l[i])
}
printf(\nEnter the logical address:);
scanf(%d,&d);
for(i=0;i<n;i++)
{
if(i==s)
{
if(d<l[i])
{
pa=b[i]+d;
a=b[i];





printf((\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t %d \t ,s,a,pa);
exit(0);
}
else
{
printf(\nPage size exceeds);
exit(0);
}
}
}
printf(\nInvalid segment);
}

OUTPUT:
Sample Input 1:
Program for segmentation
Enter the number of segments:3
Enter the base address and limit register:
100 50
150 20
130 34
Enter the Logical address:25
Enter the segment number:1
Sample Output 1:
PageNo. BaseAdd. PhysicalAdd.
2 130 155
Sample Input 2:
Program for segmentation
Enter the number of segments:2
Enter the Logical address and limit register:
100 50
150 20
Enter the logical address:25
Enter the segment number:1
Sample Output 2:
page size exceeds

9 ) Simulate Paging technique of Memory Management.

AIM: A program to simulate Paging technique of memory management.

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int np,ps,i;
int *sa;
clrscr();
printf("Enter how many pages\n");
scanf("%d",&np);
printf("Enter the page size \n");
scanf("%d",&ps);
for(i=0;i<np;i++)
{
sa[i]=(int)malloc(ps);
printf("Page%d\t Address %u\n",i+1,sa[i]);
}
getch();
}

OUTPUT:
Input:
Enter how many pages: 5
Enter the page size: 4
Output:
Page1 Address: 1894
Page2 Address: 1902
Page3 Address: 1910
Page4 Address: 1918
Page5 Address: 1926

10) Simulate all Page Replacement Algorithms FIFO.

AIM: 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






11) Simulate all Page Replacement Algorithms LRU.

AIM: 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














12) Simulate Bankers Algorithm for Deadlock Avoidance.

AIM: A program to simulate the Bankers Algorithm for Deadlock Avoidance.

PROGRAM:
//Bankers algorithm for deadlock avoidance.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the processes which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{

j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;break;
}
}
if(u==0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}
}
}
if(j==0)

{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}


OUTPUT:
Input:
Enter the no of resources: 4
Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running:
Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Output:
Deadlock will occur


13) Simulate Bankers Algorithm for Deadlock Prevention.

AIM: 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

14) Write a C program to create a file.
AIM: To write a C program to create a file.

ALGORITHM:
Step1:Start the program.
Step2:Create the file using create function and assign a variable to it.
Step3:If the value of the variable is less then print file cannot be created ,otherwise print file is
created.
Step4:Stop the program.

PROGRAM:
void main()
{
int id;
if(id=creat(z.txt,0)==-1)
{
printf(cannot create the file);
exit(1);
}
else
{
printf(file is created);
exit(1);
}
}


OUTPUT: $ cc fc.c
$ a.out
file is created $









15) Write a C program to write the data into a file.
AIM: To write a C program to write the data into a file.

ALGORITHM:
Step1.Get the data from the user.
Step2.Open a file.
Step3.Write the data from the file.
Step4.Get the data and update the file.

PROGRAM:
#include<stdio.h>
int main()
{
char str[100];
FILE *fp;
printf("Enter the string");
gets(str);
fp=fopen("file1.dat","w+");
while(!feof(fp))
{
fscanf(fp,"%s",str);
}
fprintf(fp,"%s",str);
}


OUTPUT:
$ gcc write.c
$ ./a.out
Enter the string: os lab
$vi file1.dat
os lab


16) To create the file,read data from the file,update the file.
AIM: To create the file,read data from the file,update the file.

ALGORITHM:
1.Get the data from the user.
2.Open a file.
3.Read from the file.
4.Close the file.

PROGRAM:
#include<stdio.h>
int main()
{
char str[100];
FILE *fp;
fp=fopen("file1.dat","r");
while(!feof(fp))
{
fscanf(fp,"%s",str);
printf(" %s ",str);
}
fclose(fp);
}


OUTPUT:
$ vi read1.c
$gcc read1.c
$ ./a.out
hai this is a program to read the content of the file.

Das könnte Ihnen auch gefallen