Operating Systems
Operating Systems
3. Course Outcomes (COs): At the end of the course, the student will be able to:
CO1: Experiment with Unix commands and shell programming (Understand- L2)
CO2: Implement CPU scheduling algorithms and memory management Techniques (Apply- L3)
CO3: Simulate process synchronization and file system management using system calls
(Apply - L3).
CO4: Improve individual / teamwork skills, communication & report writing skills with ethical values.
4. Course Articulation Matrix:
Course COs Programme Outcomes PSOs
Code 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
CO1 2 1 - - - - - - - - - - - - -
CO2 - 2 1 - - - - - - - - - - - -
20CS59
CO3 - 2 1 - - - - - - - - - - - -
CO4 - - - - - - - 2 2 2 - - - - -
1 = Slight (Low) 2 = Moderate (Medium) 3-Substantial(High)
Operating Systems Lab Manual
List of programs
Cycle-1:
Execute various UNIX system calls
1. Process Management
2. File Management
3. Input/Output System Calls
Cycle-2:
Simulate the following CPU scheduling algorithms.
a) FCFS b) SJF c) Round Robin d) Priority.
Cycle-3:
Simulate the file allocation strategies:
a) Sequential b) indexed c) Linked
Cycle-4:
Simulate MVT and MFT
simulate contiguous memory allocation techniques
a) Worst-fit b) Best fit c) First fit
Cycle-5:
Simulate all File Organization techniques
A) Single level directory b) Two level c)Hierarchical d)DAG
Cycle-6:
Simulate Bankers Algorithm for Deadlock Avoidance
Simulate Bankers algorithm for Deadlock Prevention
Cycle-7:
Simulate disk scheduling algorithms.
a) FCFS b) SCAN c) C-SCAN
Cycle-8:
Programs on process creation and synchronization, inter process communication
Including shared memory , pipes and and messages. (Dinning - Philosopher
problem).
Operating Systems Lab Manual
#include<stdio.h>
#include<sys/types.h>
main()
{ int pid;
pid=fork();
if(pid==0)
{
printf("\n I am the child");
printf("\n I am the parent :%d",getppid());
printf("\n I am the child :%d",getpid());
}
else
{
printf("\n I am the parent ");
printf("\n I am the parents parent :%d",getppid());
printf("\n I am the parent :%d\n",getpid());
}
}
Output:
I am the child
I am the parent: 3944
I am the child: 3945
I am the parent
I am the parents parent: 3211
I am the parent: 3944
Operating Systems Lab Manual
#include<stdio.h>
#include<unistd.h>
main()
{
char *temp[3];
temp[0]="ls";
temp[1]="-l";
temp[2]=(char *)0;
execv("/bin/ls",temp);
printf("this will not print\n");
}
Output:
total 76
-rwxr-xr-x 1 be322 group 4716 Mar 7 10:13 a.out
-rw-r--r-- 1 be322 group 688 Feb 20 13:52 comm.c
-rw-r--r-- 1 be322 group 925 Feb 20 13:54 echomsg.c
-rw-r--r-- 1 be322 group 722 Feb 20 13:55 echopipe.c
-rw-r--r-- 1 be322 group 178 Feb 20 13:57 exel.c
-rw-r--r-- 1 be322 group 167 Mar 7 10:13 exev.c
-rw-r--r-- 1 be322 group 1109 Feb 20 13:57 fflag.c
-rw-r--r-- 1 be322 group 341 Dec 26 14:47 frk.c
-rw-r--r-- 1 be322 group 140 Feb 20 13:57 linearg.c
-rw-r--r-- 1 be322 group 528 Feb 20 13:57 lock.c
-rw-r--r-- 1 be322 group 254 Feb 20 13:57 msg.c
-rw-r--r-- 1 be322 group 1036 Feb 20 13:57 msgpass.c
-rw-r--r-- 1 be322 group 203 Feb 20 13:58 sem.c
-rw-r--r-- 1 be322 group 1167 Feb 20 13:58 sharememory.c
-rw-r--r-- 1 be322 group 312 Feb 20 13:58 slp.c
-rw-r--r-- 1 be322 group 1182 Feb 20 13:58 threadf.c
-rw-r--r-- 1 be322 group 287 Feb 20 13:59 wt.c
Operating Systems Lab Manual
OUTPUT:
end$
fork program started a.out
comm.c
echomsg.c
echopipe.c
exel.c exev.c
fflag.c frk.c
linearg.c
lock.c msg.c
msgpass.c
sem.c
sharememory.c
slp.c threadf.c
wt.c
Operating Systems Lab Manual
Output:
parent process starts
child process started
0
1
2
3
4
5
6
7
8 9 child process
ends
parent process ends
Operating Systems Lab Manual
Output:
ready for fork
I am the parent
parent process
ends
child process started
0
1
2
3
4
5
6
7
89
child process ends
Operating Systems Lab Manual
#define msgsize 16
main()
{
char *msg="hello world";
char inbuff[msgsize];
int p[2],pid,j;
pipe(p);
pid=fork();
if(pid>0)
{
close(p[0]);
write(p[1],msg,msgsize);
}
if(pid==0)
{
close(p[1]);
read(p[0],inbuff,msgsize);
printf("%s \n",inbuff);
}
}
Output:
hello world
Operating Systems Lab Manual
#include<unistd.h>
#include<sys/ipc.h>
main()
int semid,key,nsem,flag;
key=(key_t)0X200f;
flag=IPC_CREAT|0666;
nsem=1;
semid=semget(key,nsem,flag);
printf("Created a semaphore
}
Output:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
main()
int shmid,flag;
key_t key=0X1000;
shmid=shmget(key,10,IPC_CREAT|0666);
if(shmid<0)
perror("shmid failed");
exit(1);
Output:
Output:
Enter the text now….abcdef
Cat file1 is
hai
Operating Systems Lab Manual
a) write a C program for simulating the FCFS (First Come First Serve) CPU
scheduling algorithm
Aim: To write a C program for simulating FCFS (first come first serve)
CPU Scheduling Algorithm
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int arrival[10],burst[10],start[10],finish[10],wait[10],turn[10];
int i,j,n,sum=0;
float totalwait=0.0,totalturn=0.0;
float avgwait=0.0,avgturn=0.0;
start[0]=0;
printf("Enter number of Process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter process %d Arrival and Burst time \n",(i+1));
scanf("%d %d",&arrival[i],&burst[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
{
sum=sum+burst[j];
}
start[i]=sum;
}
for(i=0;i<n;i++)
{
finish[i]=burst[i]+start[i];
wait[i]=start[i]-arrival[i];
turn[i]=burst[i]+wait[i];
}
for(i=0;i<n;i++)
{
totalwait=totalwait+wait[i];
totalturn=totalturn+turn[i];
}
avgwait=totalwait/n;
Operating Systems Lab Manual
avgturn=totalturn/n;
printf("\n Arrival Burst Start Finish Wait Turn \n");
for(i=0;i<n;i++)
{
printf("%7d %5d %5d %6d %4d %4d \n",arrival[i],burst[i],start[i],finish[i],wait[i],turn[i]);
}
printf("Average waiting time %f\n",avgwait);
printf("Average turnaround time %f\n",avgturn);
getch();
}
Output:
Enter number of Process: 3
Enter process 1 Arrival and Burst time
0 24
Enter process 2 Arrival and Burst time
03
Enter process 3 Arrival and Burst time
03
Arrival Burst Start Finish Wait Turn
0 24 0 24 0 24
0 3 24 27 24 27
0 3 27 30 27 30
Average waiting time 17.000000
Average turnaround time 27.000000
Operating Systems Lab Manual
b) Write a C program for simulating the SFJ ( Shortest Job First) CPU scheduling
algorithm
Aim: To write a C program for simulating SJF (Shortest Job First) CPU
Scheduling Algorithm
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,burst[10],start[10],finish[10],wait[10];
int n,temp;
float totalwait=0.0,totalturn=0.0;
float avgwait,avgturn;
printf("Enter number of Process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter process %d Burst time:",i);
scanf("%d",&burst[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(burst[i]>burst[j])
{
temp=burst[i];
burst[i]=burst[j];
burst[j]=temp;
}
}
}
for(i=1;i<=n;i++)
{
if(i==1)
{
start[i]=0;
finish[i]=burst[i];
wait[i]=0;
}
else
{
start[i]=finish[i-1];
finish[i]=start[i]+burst[i];
wait[i]=start[i];
}
}
Operating Systems Lab Manual
c) Write a C program for simulating the Round robin CPU scheduling algorithm
{
if(need[i]>0)
{
execution[i]=execution[i]+need[i];
totaltime=totaltime+need[i];
wait[i]=totaltime-execution[i];
finish[i]=wait[i]+burst[i];
turn[i]=wait[i]+burst[i];
totalburst=totalburst-need[i];
need[i]=0;
}
}
}
}
printf("\n process burst start wait finish turnaround ");
for(i=0;i<n;i++)
{
printf("%7d %5d %5d %5d %4d %6d \n",(i+1),burst[i],start[i],wait[i],finish[i],turn[i]);
}
for(i=0;i<n;i++)
{
totalwait=totalwait+wait[i];
totalturn=totalturn+turn[i];
totalresp=totalresp+start[i];
}
avgwait=totalwait/n;
avgturn=totalturn/n;
avgresp=totalresp/n;
printf("\n Average waiting time %f\n",avgwait);
printf("\n Average turnaround time %f\n",avgturn);
printf("\n Average response time %f\n",avgresp);
getch();
}
Output:
Enter number of processes 3
Enter process 1 burst time 24
Enter process 2 burst time 3
Enter process 3 burst time 3
Enter time slice 2
Process burst start wait finish turnaround
1 24 0 6 30 30
232699
3 3 4 7 10 10
Average waiting time 6.333333
Average turnaround time 16.333334
Average response time 2.000000
Operating Systems Lab Manual
for(i=1;i<=n;i++)
{
printf("%5d %8d %5d %4d %6d ",burst[i],pri[i],start[i],wait[i],finish[i]);
}
for(i=1;i<=n;i++)
{
totalwait=totalwait+wait[i];
totalturn=totalturn+finish[i];
}
avgwait=totalwait/n;
avgturn=totalturn/n;
printf("\n Average waiting time=%f \n",avgwait;
printf("\n Average turnaround time=%f \n",avgturn);
getch();
}
Output:
Enter n value 3
Enter Burst time and priority of process 1
24 3
Enter Burst time and priority of process 2
32
Enter Burst time and priority of process 3
31
Burst Priority Start Wait Finnish
24 3 0 0 24
3 2 24 24 27
3 1 27 27 30
Average waiting time=17.000000
Average turnaround time=27.000000
Operating Systems Lab Manual
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int memory[25];
int i,len,startaddr,flag,endaddr,name;
for(i=0;i<25;i++)
{
memory[i]=0;
printf("%d",memory[i]);
}
printf("\n Enter file name(0 to quit):");
scanf("%d",&name);
while(name!=0)
{
printf("\n Enter length of file:");
scanf("%d",&len);
printf("\n enter starting location of the file :");
scanf("%d",&startaddr);
endaddr=startaddr+len;
flag=0;
for(i=startaddr;(i<endaddr && endaddr<25);i++)
{
if(memory[i]!=0)
{
flag=1;
printf("\n No sufficient memory to fill ....");
break;
}
}
if(flag==0)
{
for(i=startaddr;i<endaddr;i++)
{
memory[i]=name;
}
}
printf("\n enter file name(0 to quit):");
scanf("%d",&name);
}
for(i=0;i<25;i++)
{
printf("%d",memory[i]);
}
Operating Systems Lab Manual
getch()
}
Output:
0000000000000000000000000
Enter file name(0 to quit):1
break;
}
return k;
}
Output:
Input
entyer no.of files:3
output
file 1
block 83 contains: 86 77 15 93 35
file 2
block 86 contains: 92 49 21 62 27 90
file 3
block 59 contains: 63 26 40 26 72 36 11 68 67
Operating Systems Lab Manual
Aim:To write a C program for simulating the Linked File Allocation algorithm
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int bno,flag,bn[20];
}
block;
block b[100],b1;
void main()
{
int rnum();
int p[30],kk[20],i,n,t,s1,s,r,j,c=1;
//clrscr();
printf("\n enter no of inputs files:");
scanf("%d",&n);
printf("\n input the requirements:");
for(i=1;i<=n;i++)
{
printf("\n enter no of blocks needed for file%d:",i);
scanf("%d",&p[i]);
}
t=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=p[i];j++)
{
s=rnum();
b[s].flag=1;
b[c].bno=s;
r=p[i]-1;
kk[i]=s;
t=1;
c++;
}
}
while(r!=0)
{
s1=rnum();
b[s].bn[t]=s1;
b[s].flag=1;
b[i].bno=s1;
r=r-1;
t=t+1;
}
c++;
Operating Systems Lab Manual
printf("\n allocation\n");
c=1;
for(i=1;i<=n;i++)
{
printf("\nallocated for file %d:",i);
for(j=1;j<=p[i];j++)
{
if(j==1)
{
printf("%3d",b[c].bno);
c++;
}
else
{
printf("--->%3d",b[c].bno);
c++;
}
}
printf("\n");
}
}
int rnum()
{
int k=0,i;
for(i=1;i<=100;i++)
{
k=rand()%100;
k+=10;
if(b[k].flag!=1)
break;
}
return k;
}
Output:
enter no of inputs files:3
allocation
Cycle-4:
----------------------------------------------------------------------
Output:
Operating Systems Lab Manual
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
}
Operating Systems Lab Manual
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
ff[i]=j;
highest=temp;
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
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]);
getch();
Output:
Operating Systems Lab Manual
Operating Systems Lab Manual
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
Operating Systems Lab Manual
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
ff[i]=j;
lowest=temp;
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
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]);
getch();
Output:
Operating Systems Lab Manual
Operating Systems Lab Manual
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("File %d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
Operating Systems Lab Manual
for(j=1;j<=nb;j++)
if(bf[j]!=1)
temp=b[j]-f[i];
if(temp>=0)
ff[i]=j;
break;
frag[i]=temp;
bf[ff[i]]=1;
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
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]);
getch();
Output:
Operating Systems Lab Manual
Operating Systems Lab Manual
Aim: To write a C program for simulating Single level Directory file Organization
technique
Program:
#include<stdio.h>
//#include<conio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
//clrscr();
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another file(yes - 1 or no - 0):");
scanf("%d",&ch);
}
while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
//getch();
}
Output:
#include<stdio.h>
//#include<conio.h>
#include<string.h>
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
//clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
Operating Systems Lab Manual
}
//getch();
}
OUTPUT:
enter number of directories:1
enter directory 1 names:aaa
enter size of directories:2
enter subdirectory name and size:abc 2
enter file name:bb
enter file name:cc
enter subdirectory name and size:def 2
enter file name:dd
enter file name:ee
Aim: To write a C program for simulating Single level Directory file Organization
technique
Program:
#include<stdio.h>
#include<stdlib.h>
struct node{
char N[25];
int df;
struct node *pc;
struct node *ps;
};
struct node *A[20];
int in = 0,c = 0;
void create(struct node *P,int N)
{
int i;
struct node *Tmp,*T;
Tmp = P;
for(i = 0 ;i<N;i++)
{
T = malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0): ");
scanf("%d",&T->df);
if(T-> df == 1)
{
A[c] = T;
c++;
}
T->pc = NULL;
T->ps = NULL;
if(i == 0)
{
Tmp -> pc = T;
Tmp = T;
}
else{
Tmp -> ps = T;
Tmp = T;
}
}
}
void display(struct node *P)
{
int i;
P = P->pc;
do{
Operating Systems Lab Manual
printf("\n%s(%d)",P->N,P->df);
if(P->df == 1 && P->pc != NULL)
display(P);
P = P->ps;
}while(P!=NULL);
}
void main()
{
int nu,nc,i,j,k;
struct node *Hdr;
Hdr = malloc(sizeof(struct node));
Hdr->df = 1;
Hdr->pc = NULL;
Hdr->ps = NULL;
printf("Enter number of users: ");
scanf("%d",&nu);
create(Hdr,nu);
for(in = 0;in<c;in++)
{
printf("\nEnter number of child nodes for %s: ",A[in]->N);
scanf("%d",&nc);
create(A[in],nc);
}
printf("\nHierarchical\n");
display(Hdr);
}
Output:
Hierarchical
aaa(1)
file1(0)
Operating Systems Lab Manual
file2(1)
Operating Systems Lab Manual
while(P!= NULL)
{
printf("->%s(%d)",P->N,P->df);
P = P->ptr;
}
}
}
void DAG()
{
struct node *T,*P,*Tmp;
int i,j,Flag,nv;
for(in=0;in<c;in++)
{
P = A[in];
printf("\n enter no.of adjacent vertices for %s:",A[in]->N);
scanf("%d",&nv);
for(i=0;i<nv;i++)
{
T = malloc(sizeof(struct node));
printf("enter name");
scanf("%s",T->N);
printf("enter dir(1) or file(0):");
scanf("%d",&T->df);
T->ptr = T;
P=T;
if(T->df==1)
Operating Systems Lab Manual
{
Flag = 1;
for(j=0;j<c;j++)
{
if(strcmp(A[j]->N,T->N)==0)
{
Flag = 0;
break;
}
}
if(Flag==1)
{
Tmp = malloc(sizeof(struct node));
strcpy(Tmp->N,T->N);
Tmp->df = T->df;
Tmp->ptr = NULL;
A[c] = Tmp;
c++;
}
}
}
}
}
void create(int N)
{
int i;
struct node *T;
for(i=0;i<N;i++)
{
T = malloc(sizeof(struct node));
printf("enter name:");
scanf("%s",T->N);
printf("enter dir(1) or file(0):");
scanf("%d",&T->df);
T->ptr=NULL;
A[c]=T;
c++;
}
}
void main()
{
int nu;
//clrscr();
printf("enter no.of users:");
scanf("%d",&nu);
create(nu);
DAG();
printf("\n DAG - adjancey list representation\n");
display();
//getch();
}
Operating Systems Lab Manual
Output:
enter no.of users:2
enter name:abc
enter dir(1) or file(0):1
enter name:def
enter dir(1) or file(0):0
abc(1)
def(0)
Operating Systems Lab Manual
Cycle-6: simulate bankers algorithm for Deadlock Avoidance and Deadlock Prevention
------------------------------------------------------------------------
Aim: To write a C program for simulating Bankers Algorithm for Deadlock Avoidance
Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
int available[3],work[5],max[5][3],allocation[5][3],need[5][3],safe[5],totalres[5];
char finish[5];
int i,j,k,totalloc=0,state,value=0;
//clrscr();
printf("Enter Instances of each Resource");
for(i=0;i<3;i++)
{
scanf("%d",&totalres[i]);
}
printf("Enter Maximum resources for each processes");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter process %d Resource %d",i,(j+1));
scanf("%d",&max[i][j]);
}
}
//clrscr();
printf("Enter number of resources allocated to each Process");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the resource of R%d allocated to process %d",(j+1),i);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
for(i=0;i<5;i++)
{
finish[i]='f';
}
Operating Systems Lab Manual
for(i=0;i<3;i++)
{
totalloc=0;
for(j=0;j<5;j++)
{
totalloc=totalloc+allocation[j][i];
}
available[i]=totalres[i]-totalloc;
work[i]=available[i];
}
//clrscr();
printf("\n Allocated Resources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",allocation[i][j]);
}
printf("\n");
}
printf("\n Maximum Resources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",max[i][j]);
}
printf("\n");
}
printf("\n Needed Reources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",need[i][j]);
}
printf("\n");
}
printf("\n Available Reources");
for(i=0;i<3;i++)
{
printf("%d",available[i]);
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if((finish[i]=='f')&&(need[i][j]<=work[j]))
{
state=1;
Operating Systems Lab Manual
continue;
}
else
{
state=0;
break;
}
}
if(state==1)
{
for(j=0;j<3;j++)
{
work[j]=work[j]+allocation[i][j];
}
finish[i]='t';
safe[value]=i;
++value;
}
if(i==4)
{
if(value==5)
{
break;
}
else
{
i=-1;
}
}
}
printf("\n Safe States are");
for(i=0;i<5;i++)
{
printf("P%d",safe[i]);
}
}
Output:
Enter Instances of each Resource 10
5
7
Enter Maximum resources for each processes
Enter process 0 Resource 1: 7
Enter process 0 Resource 2: 5
Enter process 0 Resource 3: 3
Enter process 1 Resource 1: 3
Enter process 1 Resource 2: 2
Enter process 1 Resource 3: 2
Enter process 2 Resource 1: 9
Enter process 2 Resource 2: 0
Enter process 2 Resource 3: 2
Operating Systems Lab Manual
Allocated Resources
010
200
302
211
002
Maximum Resources
753
322
902
222
433
Needed Resources
743
122
600
011
431
Available Reources332
Safe States areP1P3P4P0P2
Operating Systems Lab Manual
Aim: To write a C program for simulating Bankers Algorithm for Deadlock Prevention
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int nort,nopro,avail[20],req[20][20],i,j,k,flag=0;
clrscr();
printf("\n enter the no of resource types:");
scanf("%d",&nort);
for(i=0;i<nopro;i++)
{
flag=0;
for(j=0;j<nort;j++)
{
if(req[i][j]>avail[j])
{
flag=1;
}
}
if(flag==1)
{
printf("\n resources for process p%d cannot be allocated to prevent deadlock",i);
}
else
{
for(k=0;k<nort;k++)
{
avail[k]=avail[k]-req[i][k];
printf("\n%d instances of resource type R%d are allocated to process
P%d",req[i][k],k,i);
}
}
}
printf("\n remaining resources after allocation are");
for(i=0;i<nort;i++)
Operating Systems Lab Manual
printf("\n %d",avail[i]);
getch();
}
Output:
enter the no of resource types:2
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
Output:
Enter the number of Requests
8
Enter the Requests sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Total head moment is 644
Operating Systems Lab Manual
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0;
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
Output:
Enter the number of Requests
8
Enter the Requests sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0
1
Total head movement is 337
Operating Systems Lab Manual
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
Output:
Enter the number of Requests
8
Enter the Requests sequence
95 180 34 119 11 123 62 64
Enter initial head position
50
Operating Systems Lab Manual
Cycle-8:
Programs on process creation and synchronization, inter process communication
including shared memory, pipes, and messages. (Dinning - Philosopher problem).
Output
Parent Passing value to child 100
Child printing received value 100
Operating Systems Lab Manual
//Program 1: Sender program : This program creates a shared memory segment, attaches
itself to it and then writes some content into the shared memory segment.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT); //creates shared memory segment
with key 2345, having size 1024 bytes. IPC_CREAT is used to create the shared segment if
it does not exist. 0666 are the permisions on the shared segment
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory); //this prints the address where the
segment is attached with this process
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}
Output
Operating Systems Lab Manual
//Program 2: Receiver Process : This program attaches itself to the shared memory
segment created in Program 1. Finally, it reads the content of the shared memory
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
Output