You are on page 1of 7

MULTIPROGRAMMING WITH VARIABLE NO.

OF TASKS

#include<stdio.h>
void main()
{
int mem,n,p_size[100],rem_mem[50],all_mem=0,flag=0,i,j;
printf("enter the main memory size::");
scanf("%d",&mem);
printf("enter the no. of processes::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the size of p[%d]::",i+1);
scanf("%d",&p_size[i]);
all_mem+=p_size[i]; //available mem calculation
if(all_mem>=mem)
{
flag=1;
break;
}
rem_mem[i]=mem-all_mem; //remaining mem calculation
}
j=i;
printf(" PROCESS\tPRO_SIZE\tREM_MEM");
for(i=0;i<j;i++)
printf("\n%d\t\t%d\t\t%d",i+1,p_size[i],rem_mem[i]);

if(flag==1)
{
printf("\n******* MEMORY UNAVAILABLE********\n");
}
}

OUTPUT:-

enter the main memory size::1500


enter the no. of processes::4
enter the size of p[1]::135
enter the size of p[2]::200
enter the size of p[3]::500
enter the size of p[4]::400
PROCESS PRO_SIZE REM_MEM
1 135 1365
2 200 1165
3 500 665
4 400 265

MULTIPROGRAMMING WITH FIXED NO.OF TASKS

#include<stdio.h>
#include<math.h>
void main()
{
int np,nb,mem,bs,l,ps[100],nba[100],ifm[100],sb=0,flag=0;
int i,j;
float x;
printf("enter the memory size");
scanf("%d",&mem);
printf("enter the no.of blocks");
scanf("%d",&nb);
printf("enter the no.of processes");
scanf("%d",&np);
//block size calculation
bs=mem/nb;
printf("****BLOCK SIZE IS:%d****",bs);
for(i=1;(i<=np)&&(sb<nb);i++)
{
printf("\n enter the size of p[%d]:",i);
scanf("%d",&ps[i]);
if(ps[i]<=bs)
nba[i]=1;
else
{
x=ps[i]/(float)bs;
nba[i]=(int)x;
}

ifm[i]=nba[i]*bs-ps[i];
sb=sb+nba[i];
if(sb>nb)
{
i=i-1;
flag=1;
}
}
j=i;
printf("PROCESS\t\tPRO_SIZE\tNBA\t\tINTER_FRAG");
for(i=1;i<j;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d",i,ps[i],nba[i],ifm[i]);

if(flag==1)
printf("\n******* MEMORY UNAVAILABLE********\n");
}

OUTPUT:-

enter the memory size1500


enter the no.of blocks4
enter the no.of processes4
****BLOCK SIZE IS:375****
enter the size of p[1]:340
enter the size of p[2]:200

enter the size of p[3]:400

enter the size of p[4]:375


PROCESS PRO_SIZE NBA INTER_FRAG
1 340 1 35
2 200 1 175
3 400 1 -25
4 375 1 0

BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE:-

#include<stdio.h>
main()
{
int clm[7][5],req[7][5],rsrc[5],avail[5],comp[7],alloc[7][5];
int first,p,r,i,j,prc,count,t;
count=0;
for(i=0;i<=7;i++)
comp[i]=0;
printf("Enter the no of process");
scanf("%d",&p);
printf("Enter the no of resources");
scanf("%d",&r);
printf("Enter the claim for each process");
for(i=1;i<=p;i++)
{
printf("\nFor process %d:-",i);
for(j=1;j<=r;j++)
scanf("%d",&clm[i][j]);
}
printf("Enter the allocation for each process\n");
for(i=1;i<=p;i++)
{
printf("\n For process %d:-",i);
for(j=1;j<=r;j++)
scanf("%d",&alloc[i][j]);
}
printf("Enter total no of each resource");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);

for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{
total+=alloc[i][j];
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];
}
}
printf("\n\n Available Resources is");
for(j=1;j<=r;j++)
{
printf("%d",avail[j]);
}
printf("\n Claim matrix:\t\t Allocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\n The system is in unsafe state!!");
else
printf("\nThe system is in safe state");
}
}
OUTPUT:-

Enter the no of process1


Enter the no of resources1
Enter the claim for each process
For process 1:-1
Enter the allocation for each process

For process 1:-0


Enter total no of each resource1

Available Resources is1


Claim matrix: Allocation matrix:
1 0

The system is in safe state

You might also like