Simulation of Multiprogramming with Variable and Fixed number of Tasks:-
MVT:-
#include<stdio.h>
#include<conio.h>
main()
{
static int jobs[20][20],flag[10];
int ch;
static int i,k,nj,nb,tms;
clrscr();
printf("Enter time"); /* reading time */
scanf("%d",&tms);
printf("Enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("Enter job information 1.jobid 2.jobsize");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
for(i=0;i<nj;i++)
{
if(tms>=jobs[i][1])
{
tms=tms-jobs[i][1];
nb=nb+1;
flag[i]=1;
}
}
printf("Total memory space available which is not allocated is:%d\n",tms);
printf("Jobs which are not allocated:");
for(i=0;i<nj;i++)
if(flag[i]==0)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
if(nb!=nj)
{
while(1)
{
printf("enter jobid to deallocate:");
scanf("%d",&k);
for(i=0;i<nj;i++)
{
if(jobs[i][0]==k)
{
if(flag[i]==1)
{
tms=tms+jobs[i][1];
flag[i]=2;
printf("Deallocated job %d\t%d\n", jobs[i][0],jobs[i][1]);
}
}
}
for(i=0;i<nj;i++)
{
if (tms>=jobs[i][1])
{
if(flag[i] == 0)
{
tms=tms-jobs[i][1];
flag[i]=1;
}
}
}
printf("Remaining memory is: %d",tms);
printf("Jobs which are not allocated are:");
for( i=0;i<nj;i++) /* dellocating mamory*/
if(flag[i] ==0)
printf("%d\t%d\n", jobs[i][0],jobs[i][1]);
printf("Do you want to deallocate 1.Yes 2.No");
scanf("%d",&ch);
if(ch ==2)
break;
}
}
printf("Allocated jobs are:");
for(i=0;i<nj;i++)
if(flag[i]==1)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
getch();
}
#include<conio.h>
main()
{
static int jobs[20][20],flag[10];
int ch;
static int i,k,nj,nb,tms;
clrscr();
printf("Enter time"); /* reading time */
scanf("%d",&tms);
printf("Enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("Enter job information 1.jobid 2.jobsize");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
for(i=0;i<nj;i++)
{
if(tms>=jobs[i][1])
{
tms=tms-jobs[i][1];
nb=nb+1;
flag[i]=1;
}
}
printf("Total memory space available which is not allocated is:%d\n",tms);
printf("Jobs which are not allocated:");
for(i=0;i<nj;i++)
if(flag[i]==0)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
if(nb!=nj)
{
while(1)
{
printf("enter jobid to deallocate:");
scanf("%d",&k);
for(i=0;i<nj;i++)
{
if(jobs[i][0]==k)
{
if(flag[i]==1)
{
tms=tms+jobs[i][1];
flag[i]=2;
printf("Deallocated job %d\t%d\n", jobs[i][0],jobs[i][1]);
}
}
}
for(i=0;i<nj;i++)
{
if (tms>=jobs[i][1])
{
if(flag[i] == 0)
{
tms=tms-jobs[i][1];
flag[i]=1;
}
}
}
printf("Remaining memory is: %d",tms);
printf("Jobs which are not allocated are:");
for( i=0;i<nj;i++) /* dellocating mamory*/
if(flag[i] ==0)
printf("%d\t%d\n", jobs[i][0],jobs[i][1]);
printf("Do you want to deallocate 1.Yes 2.No");
scanf("%d",&ch);
if(ch ==2)
break;
}
}
printf("Allocated jobs are:");
for(i=0;i<nj;i++)
if(flag[i]==1)
printf("%d\t%d\n",jobs[i][0],jobs[i][1]);
getch();
}
output:-
_________________________________________________________________________________________
MFT:-
#include<stdio.h>
#include<conio.h>
void main()
{
int tms,element,nb,i,j,t,index,frag,ch,count=0;
static int jobs[20][20],sz[20][20],nj,s;
clrscr();
printf("enter total memory space"); /* reading memory */
scanf("%d",&tms); /* reading choices */
printf("enter choice\n1.equal partition 2.unequal partition\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter size of each block");
scanf("%d",&s);
nb=tms/s;
for(i=0;i<nb;i++)
scanf("%d",&sz[i][0]);
}
else
{
printf("enter no. of blocks");
scanf("%d",&nb);
printf("enter size of %d blocks");
for(i=0;i<nb;i++)
scanf("%d",sz[i][0]);
}
printf("enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("enter job information 1.jobid 2.job size\n");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
frag=0;
for(j=0;j<nj;j++)
{
if(sz[j][0]>=element && sz[i][0]<=t)
{
if(sz[j][1]!=1)
{
t=sz[j][0];
index=j;
}
}
}
if(sz[index][1]!=1)
{
sz[index][1]=1;
jobs[i][2]=2;
frag=frag+(t-element);
count++;
}
printf("total internal fragmentation : %d", frag);
printf("no. of free blocks: %d" , nb-count);
printf("the jobs which are not allocated");
if(count==nj)
printf(" 0");
for(i=0;i<nj;i++)
{
if(jobs[i][2]!=2)
printf("jobid ------%d\tjob size-----%d\n",jobs[i][0],jobs[i][1]);
}
getch();
}
#include<conio.h>
void main()
{
int tms,element,nb,i,j,t,index,frag,ch,count=0;
static int jobs[20][20],sz[20][20],nj,s;
clrscr();
printf("enter total memory space"); /* reading memory */
scanf("%d",&tms); /* reading choices */
printf("enter choice\n1.equal partition 2.unequal partition\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter size of each block");
scanf("%d",&s);
nb=tms/s;
for(i=0;i<nb;i++)
scanf("%d",&sz[i][0]);
}
else
{
printf("enter no. of blocks");
scanf("%d",&nb);
printf("enter size of %d blocks");
for(i=0;i<nb;i++)
scanf("%d",sz[i][0]);
}
printf("enter no. of jobs"); /* reading no of jobs */
scanf("%d",&nj);
printf("enter job information 1.jobid 2.job size\n");
for(i=0;i<nj;i++)
scanf("%d%d",&jobs[i][0],&jobs[i][1]);
frag=0;
for(j=0;j<nj;j++)
{
if(sz[j][0]>=element && sz[i][0]<=t)
{
if(sz[j][1]!=1)
{
t=sz[j][0];
index=j;
}
}
}
if(sz[index][1]!=1)
{
sz[index][1]=1;
jobs[i][2]=2;
frag=frag+(t-element);
count++;
}
printf("total internal fragmentation : %d", frag);
printf("no. of free blocks: %d" , nb-count);
printf("the jobs which are not allocated");
if(count==nj)
printf(" 0");
for(i=0;i<nj;i++)
{
if(jobs[i][2]!=2)
printf("jobid ------%d\tjob size-----%d\n",jobs[i][0],jobs[i][1]);
}
getch();
}
output:-
____________________________________________________________________________________________
0 comments:
Post a Comment