Vous êtes sur la page 1sur 38

Ex.No.

2a SHELL PROGRAMMING
EVEN OR ODD
PROGRAM

echo "enter the number"


read num
echo "enter the number"
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "number is even"
else
echo "number is odd"
fi

OUTPUT:

[students@localhost ~]$ sh evenodd.sh


enter the number: 5
the number is odd.
Ex.No.2b BIGGEST OF THREE NUMBERS

PROGRAM:

echo "enter three numbers"


read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A is big"
else if [ $b -gt $c ]
then
echo "B is big"
else
echo "C is big"
fi

OUTPUT:

[students@localhost ~]$ sh big3.sh


ENTER THREE NUMBERS:
23 54 78
C IS BIG.
Ex.No.2c FACTORIAL OF NUMBER

PROGRAM:

echo "enter the number"


read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $i \* $fact`
i=`expr $i + 1`
done
echo "the fcatorial number of $ni is $fact

OUTPUT:

[students@localhost ~]$ sh fact.sh


Enter the number :4
The factorial of 4 is 24.
Ex.No.2d FIBONACCI SERIES

PROGRAM:

echo " ENTER THE LIMIT FOR FIBONNACI SERIES"


read lim
n1=0
n2=1
var=0
echo "FIBONACCI SERIES IS "
echo "$n1"
echo "$n2"
while [ $var -lt `expr $lim - 2` ]
do
n3=`expr $n1 + $n2 `
n1=`expr $n2 `
n2=`expr $n3 `
var=`expr $var + 1 `
echo "$n2"
done

OUTPUT :

[students@localhost ~]$ sh fib.sh


enter the limit for Fibonacci: 5
The Fibonacci series is: 0,1,1,2,3
Ex.No:3a FCFS (FIRST COME FIRST SERVE)
CPU SCHEDULING
PROGRAM:

#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}
OUTPUT: (FCFS Scheduling Algorithm)
Ex.No:3b SJF (SHORTEST JOB FIRST)
CPU SCHEDULING ALGORITHM
PROGRAM:

#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

OUTPUT:
Ex.No:3c PRIORITY CPU SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}
OUTPUT:
Ex.No:3d ROUND ROBIN CPU SCHEDULING ALGORITHM

PROGRAM:

#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}

OUTPUT:
Ex. No: 4a IMPLEMENT ALL FILE ALLOCATION STRATEGIES
(SEQUENTIAL FILE ALLOCATION-CONTIGUOUS)

PROGRAM :

#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k,count=0;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n enter starting block & length of files");
scanf("%d%d",&st,&len);
printf("\n file not allocated(yes-1/no-0)");
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[i]==0)
{
f[j]=1;
printf("\n%d\t%d",,j,f[j]);
if(j==(st+len-1))
printf("\n the file is allocated to disk");
}
}
else
printf("file is not allocated");
count=0;
printf("\n if u want to enter more files(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}
OUTPUT:
Ex. No: 4b IMPLEMENT ALL FILE ALLOCATION STRATEGIES
(INDEXED FILE ALLOCATION)

Program:

#include<stdio.h>
main()
{
int n,m[20],i,j,ib[20],b[20][20];
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter index block :",i+1);
scanf("%d",&ib[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",i+1,ib[i],m[i]);
printf("blocks occupiedare:");
for(i=0;i<n;i++)
{ printf("fileno%d",i+1);
for(j=0;j<m[i];j++)
printf("\t%d--->%d\n",ib[i],b[i][j]);
printf("\n");
}
getch();
}
OUTPUT:

Enter no. of files:2


Enter index block 3
Enter blocks occupied by file1: 4
enter blocks of file1:9
467
Enter index block 5
Enter blocks occupied by file2:2
enter blocks of file2: 10 8
File index length
134
252
blocksoccupiedare:
file1
3--->9
3--->4
3--->6
3--->7
file2
5--->10
5--->8
Ex. No: 4c IMPLEMENT ALL FILE ALLOCATION STRATEGIES
(LINKED FILE ALLOCATION)

Program:

#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index sarting block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
}
else
printf("\nif u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( ); }
OUTPUT:
Ex.No:5 IMPLEMENT SEMAPHORES PRODUCER CONSUMER
PROBLEM USING SEMAPHORES

PROGRAM:

#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#define NBUFF 10
int nitems;
struct {
int buff[NBUFF];
sem_t mutex, nempty, nstored;
} shared;
void *produce (void *);
void *consume (void *);
int main(int argc, char **argv)
{
pthread_t tid_produce, tid_consume;
if(argc !=2)
{
printf("Useage: filename <nitems> ");
return 0;
}
printf ("\n\n Producer - Consumer Problem using Semaphore\n");
printf (" -------------------------------------------\n\n");
nitems=atoi(argv[1]);
sem_init(&shared.mutex,0,1);
sem_init(&shared.nempty,0,NBUFF);
sem_init(&shared.nstored,0,0);
pthread_setconcurrency(2);
pthread_create(&tid_produce,NULL,produce,NULL);
pthread_create(&tid_consume,NULL,consume,NULL);
pthread_join(tid_produce,NULL);
pthread_join(tid_consume,NULL);
sem_destroy(&shared.mutex);
sem_destroy(&shared.nempty);
sem_destroy(&shared.nstored);
}
void *produce (void *arg)
{
int i;
for(i=0;i<nitems;i++)
{
sem_wait(&shared.nempty);
sem_wait(&shared.mutex);
shared.buff[i % NBUFF]=i;
printf ("\tProducer.....");
printf ("buff[%d] = %d\n\n",i,shared.buff[i % NBUFF]);
sem_post(&shared.mutex);
sem_post(&shared.nstored);
sleep(3);
}
return NULL;
}
void *consume (void *arg)
{
int i;
for(i=0;i<nitems;i++)
{
sem_wait(&shared.nstored);
sem_wait(&shared.mutex);
printf("\tConsumer.....");
printf("buff[%d] = %d\n\n\n",i,shared.buff[i%NBUFF]);
sem_post(&shared.mutex);
sem_post(&shared.nempty);
sleep(3);
}
return NULL;
}
OUTPUT:
Ex. No: 6a SINGLE LEVEL DIRECTORY USING FILE ORGANIZATION
TECHNIQUES

PROGRAM:

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}

OUTPUT:
Enter total memory size: 50
Enter memory for OS: 10
Enter no. of partitions to be divided: 4
Enter size of page: 10
Enter size of page: 9
Enter size of page: 8

Internal Fragmentation is = 4
Ex. No: 6b TWO LEVEL DIRECTORY USING FILE ORGANIZATION
TECHNIQUES

PROGRAM:

#include<stdio.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");
}
getch(0);
}

OUTPUT:

Enter total memory size : 50


Enter no.of pages : 4
Enter memory for OS :10
Enter size of page : 10
Enter size of page : 9
Enter size of page : 9
Enter size of page : 10

External Fragmentation is = 2
Ex. No: 6c HIERARCHICAL DIRECTORY USING FILE ORGANIZATION
TECHNIQUES

PROGRAM:

#include<stdio.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element
node; void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
48
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)>nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rx-lx)/(*root)->nc; for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root
!=NULL)
{
for(i=0;i<root->nc;i++)
49
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
display(root->link[i]); } } }

OUTPUT:

Enter total memory size : 100


Enter no.of pages : 20
Enter memory for OS :50
Enter size of page : 25
Enter size of page : 25
Enter size of page : 32
Enter size of page : 20

External Fragmentation is = 21
Ex.No:6.d DAG USING FILE ORGANIZATION TECHNIQUES

PROGRAM:

#include<stdlib.h>
#include<stdio.h>
struct bin_tree
{
char data;
int label;
struct bin_tree *right, *left;
};
typedef struct bin_tree node;

int R[10];
int top;
char *op;
void insertnode(node **tree,char val)
{
node *temp = NULL;

if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
temp->label=-1;
*tree = temp;
}
}

void insert(node **tree,char val)


{
char l,r;
int numofchildren;

insertnode(tree, val);

printf("\nEnter number of children of %c:",val);


scanf("%d",&numofchildren);

if(numofchildren==2)
{
printf("\nEnter Left Child of %c:",val);
scanf("%s",&l);
insertnode(&(*tree)->left,l);
printf("\nEnter Right Child of %c:",val);
scanf("%s",&r);
insertnode(&(*tree)->right,r);

insert(&(*tree)->left,l);
insert(&(*tree)->right,r);
}
}
void findleafnodelabel(node *tree,int val)
{
if(tree->left != NULL && tree->right !=NULL)
{
findleafnodelabel(tree->left,1);
findleafnodelabel(tree->right,0);
}
else
{
tree->label=val;
}
}
void findinteriornodelabel(node *tree)
{
if(tree->left->label==-1)
{
findinteriornodelabel(tree->left);
}
else if(tree->right->label==-1)
{
findinteriornodelabel(tree->right);
}
else
{
if(tree->left != NULL && tree->right !=NULL)
{
if(tree->left->label == tree->right->label)
{
tree->label=(tree->left->label)+1;
}
else
{
if(tree->left->label > tree->right->label)
{
tree->label=tree->left->label;
}
else
{
tree->label=tree->right->label;
}
}
}
}
}

Output :

Enter total memory size: 100


Enter memory for OS: 25
Enter no. of partitions to be divided: 10
Enter size of page: 25
Enter size of page: 20
Enter size of page: 20
Enter size of page: 30

Internal Fragmentation is = 10
Ex. No: 7 IMPLEMENT BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

PROGRAM:

#include<stdio.h>
static int visited[20];
int i,j,np,nr;
int safe(int n[10][10],int ar[10],int a[10][10],int m[10][10]);
int canbeprocessed(int x[10],int y[10],int z[10],int ar[10]);
void additionalrequest(int a1[10][10],int m1[10][10],int ar1[10]);
int main()
{
int a[10][10],m[10][10],n[10][10],ar[10],t[10];
int a1[10][10],m1[10][10],ar1[10];
printf(“\nEnter the no of process: “);
scanf(“%d”,&np);
printf(“\nEnter the no of resources: “);
scanf(“%d”,&nr);
for(i=0;i<nr;i++)
{
printf(“\nTotal Amount of the Resource R%d: “,i+1);
scanf(“%d”,&t[i]);
}
printf(“\nEnter the allocation matrix\n\n\t”);
for(i=0;i<nr;i++)
printf(“R%d\t”,i+1);
for(i=0;i<np;i++)
{
printf(“\nP%d\t”,i+1);
for(j=0;j<nr;j++)
{
scanf(“%d”,&a[i][j]);
a1[i][j]=a[i][j];
}
}
printf(“\n\n”);
printf(“\nEnter the maximum matrix\n\n\t”);
for(i=0;i<nr;i++)
printf(“R%d\t”,i+1);
for(i=0;i<np;i++)
{
printf(“\nP%d\t”,i+1);
for(j=0;j<nr;j++)
{
scanf(“%d”,&m[i][j]);
m1[i][j]=m[i][j];
}
}
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
n[i][j]=m[i][j]-a[i][j];

/*Available Resource calculation*/


for(j=0;j<nr;j++)
{
ar[j]=t[j];
for(i=0;i<np;i++)
{
ar[j]-=a[i][j];
ar1[j]=ar[j];
}
}
/*Available Resource Calculation ends*/
if(safe(n,ar,a,m))
printf(“\n\nConclusion: System is in safe state “);
else
printf(“\n\nConclusion: The system cannot acheive safe state” );
additionalrequest(a1,m1,ar1);
return 0;
}
/*Function To Check The Safe State*/
int safe(int n[][10],int ar[10],int a[][10],int m[][10])
{
int count;
count=0;
printf(“\nSafe State Sequence: “);
while(count<np)
{
for(i=0;i<np;i++)
{
if((visited[i]==0) && canbeprocessed(n[i],a[i],m[i],ar))
{
count++;
visited[i]=1;
printf(“P%d\t”,i+1);
break;
}
}
if(i==np)
return 0;
}
return 1;
}
/*function for checking the processing of the process*/
int canbeprocessed(int x[],int y[],int z[],int ar[])
{
for(j=0;j<nr;j++)
if(x[j]>ar[j])
return 0;
for(j=0;j<nr;j++)
{
ar[j]+=y[j];
y[j]=z[j]=0;
}
return 1;
}
/*Additional Request*/
void additionalrequest(int a1[10][10],int m1[10][10],int ar1[10])
{
int pno,vector[10];
int n1[10][10];
printf(“\nEnter the process no which is making additional request “);
scanf(“%d”,&pno);
printf(“\nEnter the request vector: “);
for(j=0;j<nr;j++)
scanf(“%d”,&vector[j]);
for(j=0;j<nr;j++)
{
a1[pno][j]+=vector[j];
ar1[j]-=vector[j];
}
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
n1[i][j]=m1[i][j]-a1[i][j];
if(safe(n1,ar1,a1,m1))
printf(“\nAllocation made \n”);
else
{
printf(“\nAllocation cannot be made \n”);
a1[pno][j]-=vector[j];
ar1[j]+=vector[j];
}
}
OUTPUT:

ENTER THE NO. OF PROCESSES:4


ENTER THE NO. OF RESOURCES:3
PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:0
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 100 222 936 112
P2 613 511 102
P3 314 211 103
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


P2 010 623
P1 401 723
P3 620 934
P4 514 936

THE ABOVE SEQUENCE IS A SAFE SEQUENCE


Ex. No. 08 IMPLEMENTS AN ALGORITHM FOR DEAD LOCK DETECTION
PROGRAM:

#include<stdio.h>
int max[10][10],alloc[10][10],need[10][10],avail[10],i,j,p,r,finish[10]={0},flag=0;
main( )
{
clrscr( );
printf("\n\nSIMULATION OF DEADLOCK PREVENTION");
printf("Enter no. of processes, resources");
scanf("%d%d",&p,&r);printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("enter max matrix");
for(i=0;i<p;i++) /*reading the maximum matrix and availale matrix*/
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter available matrix");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
fun(); /*calling function*/
if(flag==0)
{i
f(finish[i]!=1)
{
printf("\n\n Failing :Mutual exclusion");
for(j=0;j<r;j++)
{ /*checking for mutual exclusion*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}fun();
printf("\n By allocating required resources to process %d dead lock is
prevented ",i);
printf("\n\n lack of preemption");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;
}
fun( );
printf("\n\n daed lock is prevented by allocating needed resources");
printf(" \n \n failing:Hold and Wait condition ");
for(j=0;j<r;j++)
{ /*checking hold and wait condition*/
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun( );
printf("\n AVOIDING ANY ONE OF THE CONDITION, U CAN PREVENT DEADLOCK");
}
}
getch( );
}
fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
continue;
elsebreak;
}
if(j==r)
{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
finish[i]=1;
}
}
}
if(flag==0)
break;
}
}

OUTPUT:
SIMULATION OF DEADLOCK PREVENTION
Enter no. of processes, resources 3, 2
enter allocation matrix 2 4 5
345
Enter max matrix4 3 4
561

Enter available matrix2 5 1

Failing : Mutual Exclusion by allocating required resources to process dead is prevented Lack of
no preemption deadlock is prevented by allocating needed resources
Failing : Hold and Wait condition

Ex. No: 9a IMPLEMENTS PAGE REPLACEMENT ALGORITHMS


(First In First Out)
PROGRAM: