Ex 8
Ex 8
AVOIDANCE
Ex. No:7
OBJECTIVE:
ALGORITHM:
Step 6: If the new request comes then check that the system is in safety or not if we allow the
request.
#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];
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 process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
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]<=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");
}
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
getch();
}
OUTPUT:
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
Deadlock will occur
RESULT :
OBJECTIVE:
ALGORITHM:
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t"); for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t"); if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{ c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i); if(finish[i]==1)
{
i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i; j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n"); for(i=0;i<n;i++)
{
}
}
else
{
printf("P%d\t",dead[i]);
}
}
output
Enter the no. Of processes 3
Enter the no of resources instances 3
Enter the max matrix
368
433
344
Enter the allocation matrix
333
203
124
Enter the available resources
120
Process allocation max available
P1 333 368 120
P2 203 433
P3 124 344
System is in deadlock and deadlock process are
P0 p1 p2
RESULT :
Ex. No. 10
OBJECTIVE:
ALGORITHM:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main()
{
int id,semid,count=0,i=1,j;
int *ptr;
id=shmget(8078,sizeof(int),IPC_CREAT|0666);
ptr=(int *)shmat(id,NULL,0);
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}u;
struct sembuf sem;
semid=semget(1011,1,IPC_CREAT|0666);
ushort a[1]={1};
u.array=a;
semctl(semid,0,SETALL,u);
while(1)
{
sem.sem_num=0;
sem.sem_op=-1;
sem.sem_flg=0;
semop(semid,&sem,1);
*ptr=*ptr+1;
printf("process id:%d countis :%d \n",getpid(),*ptr);
for(j=1;j< =1000000;j++)
{
sem.sem_num=0;
sem.sem_op=+1;
sem.sem_flg=0;
semop(semid,&sem,1);
}
}
shmdt(ptr);
}
SAMPLE INPUT AND OUTPUT:
enter Process id 16338992
pprocess
10
process 1
RESULT :
Ex. No. 11
OBJECTIVE:
ALGORITHM:
Step 4: Calculate the physical address using the following Physical address=(frame number *
#include<stdio.h>
#include<conio.h>
main()
int np,ps,i;
int *sa;
scanf("%d",&np);
scanf("%d",&ps);
sa=(int*)malloc(2*np);
for(i=0;i<np;i++)
sa[i]=(int)malloc(ps);
getch();
Input:
Output:
Page1 Address: 1894
Page2 Address: 1902
Page3 Address: 1910
Page4 Address: 1918
Page5 Address: 1926
RESULT:
EX NO: 12
OBJECTIVE:
ALGORITHM:
#include <string.h>
#include<pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
return NULL;
int main(void)
if (err != 0)
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
SAMPLE OUTPUT:
Job 1 started
Job 1finished
RESULT: