0% found this document useful (0 votes)
30 views27 pages

Oslab Program

The document contains C code to implement the First Come First Serve (FCFS) CPU scheduling algorithm. It takes input for the number of processes, their process IDs, burst times, and arrival times. It sorts the processes by arrival time. It then calculates the completion times, waiting times, and turnaround times of each process. Finally, it prints the results and calculates the average waiting and turnaround times.

Uploaded by

Elias K B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views27 pages

Oslab Program

The document contains C code to implement the First Come First Serve (FCFS) CPU scheduling algorithm. It takes input for the number of processes, their process IDs, burst times, and arrival times. It sorts the processes by arrival time. It then calculates the completion times, waiting times, and turnaround times of each process. Finally, it prints the results and calculates the average waiting and turnaround times.

Uploaded by

Elias K B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

#include<stdio.

h>
#define false 0
#define true 1
#define PROC 5
#define RES 3
int avail[RES],used[PROC][RES],max_res[PROC][RES];
int safe()
{
int i,j;
int need_it_work;
int work[4];
int finish[PROC];
for(i=0;i<PROC;i++) work[i]=avail[i];
for(i=0;i<PROC;i++) finish[i]=false;
while(1)
{
for(i=0;i<PROC;i++)
{
need_it_work=true;
for(j=0;j<RES;j++)
{
if((max_res[i][j]-used[i][j]) > work[j])
need_it_work=false;
}
if(need_it_work ==true && finish[i]==false)
break;
}
if(i==PROC)
{
for(j=0;j<PROC;j++)
{
if(finish[j]==false)
return false;
}
return true;
}
else
{
for(j=0;j<RES;j++)
{
work[j]+=used[i][j];
finish[i]=true;
}
}
}
}
void print_state()
{
int i,j;
printf("\n------------CURRENT STATE \n");
printf("avail[%d]\n",RES);
for(i=0;i<RES;i++)
printf("%d ",avail[i]);
printf("\nallocated[%d][%d]:\n",PROC,RES);
for(i=0;i<PROC;i++)
{
for(j=0;j<RES;j++)
printf(" %d",used[i][j]);
printf("\n");
}
printf("need[%d][%d]:\n",PROC,RES);
for(i=0;i<PROC;i++)
{
for(j=0;j<RES;j++)
printf("%d ",max_res[i][j]-used[i][j]);
printf("\n");
}
}
int main()
{
int p;
int req[RES],i,j;
int error;
printf("Enter Available REsorces << avail[%d] >>",RES);
for(i=0;i<RES;i++)
scanf("%d",&avail[i]);
printf("Enter Resource Usage allocated<< used[%d][%d] >>",PROC,RES);
for(i=0;i<PROC;++i)
{
for(j=0;j<RES;j++)
scanf("%d",&used[i][j]);
}
printf("Enter Maximum Allocated Resources << max_res[%d][%d] >>",PROC,RES);
for(i=0;i<PROC;i++)
{
for(j=0;j<RES;j++)
scanf("%d",&max_res[i][j]);
}
p=-1;
while(p<PROC)
{
error=false;
printf("\nENTER NEW REQUEST\n");
printf("Enter #process:");
scanf("%d",&p);
if(p>=PROC)
break;
printf("Enter #resources:");
for(i=0;i<RES;i++)
scanf("%d",&req[i]);
for(i=0;i<RES;i++)
{
if( req[i] >(max_res[p][i]-used[p][i]))
{
printf("ERROR more resources than maximum allocated");
error=true;
break;
}
}
if(!error)
{
for(i=0;i<RES;++i)
{
if(req[i] >avail[i])
{
printf("ERROR---more resources than available");
error=true;
break;
}
}
}
if(!error)
{
for(i=0;i<RES;i++)
{
avail[i]-=req[i];
used[p][i]+=req[i];
}
if( safe() )
{
printf("syetem is in SAFE STATE requested resource allocated");
}
else
{
printf("SORRY result state not safe");
for(i=0;i<RES;++i)
{
avail[i]+=req[i];
used[p][i]-=req[i];
}
}
}
print_state();
}
}
/*Enter Available REsorces << avail[3] >>3 3 2
Enter Resource Usage allocated<< used[5][3] >>0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter Maximum Allocated Resources << max_res[5][3] >>7 5 3
3 2 2
9 0 2
2 2 2
4 3 3

ENTER NEW REQUEST


Enter #process:1
Enter #resources:1 0 2
syetem is in SAFE STATE requested resource allocated
------------CURRENT STATE
avail[3]
2 3 0
allocated[5][3]:
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
need[5][3]:
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1
ENTER NEW REQUEST
Enter #process:4
Enter #resources:3 3 0
ERROR---more resources than available
------------CURRENT STATE
avail[3]
2 3 0
allocated[5][3]:
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
need[5][3]:
7 4 3
0 2 0
6 0 0
0 1 1
4 3 1

ENTER NEW REQUEST


Enter #process:6
#include<stdio.h>
void main()
{
int frag[20],b[20],p[20],i,j,nb,np,temp,low=9999;
static int barray[20],parray[20];
printf("BEST FIT\n");
printf("ENTER THE NUMBER OF PROCESES\n");
scanf("%d",&np);
printf("ENTER THE NUMBER OF BLOCKS\n");
scanf("%d",&nb);
printf("ENTER THE SIZE OF THE BLOCKS\n");
for(i=1;i<=nb;i++)
{
printf("BLOCK NUMBER %d ",i);
scanf("%d",&b[i]);
}
printf("ENTER THE SIZE OF THE proceses\n");
for(i=1;i<=np;i++)
{
printf("proceses NUMBER %d ",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(low>temp)
{
parray[i]=j;
low=temp;
}
}
}
frag[i]=low;
barray[parray[i]]=1;
low=10000;
}
printf("\nPROCESSES NUMBER \t PROCESES SIZE \t block number \t BLOCK SIZE \t
FRAGMENT");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],frag[i]);
}
/*BEST FIT
ENTER THE NUMBER OF PROCESES
3
ENTER THE NUMBER OF BLOCKS
3
ENTER THE SIZE OF THE BLOCKS
BLOCK NUMBER 1 100
BLOCK NUMBER 2 150
BLOCK NUMBER 3 200
ENTER THE SIZE OF THE proceses
proceses NUMBER 1 80
proceses NUMBER 2 120
proceses NUMBER 3 180

PROCESSES NUMBER PROCESES SIZE block number BLOCK SIZE FRAGMENT


1 80 1 100 20
2 120 2 150 30
3 180 3 200 20
# include <stdio.h>
void main()
{
int bsize[10],psize[10],bno,pno,flags[10],alloc[10],i,j;
for(i=0;i<10;i++)
{
flags[i]=0;
alloc[i]=-1;
}
printf("Enter no. of blocks:");
scanf("%d",&bno);
printf("\nEnter size of each block:");
for(i=0;i<bno;i++)
scanf("%d",&bsize[i]);
printf("\n Enter no. of process:");
scanf("%d",&pno);
printf("\nEnter size of each process:");
for(i=0;i<pno;i++)
scanf("%d",&psize[i]);
for(i=0;i<pno;i++)
for(j=0;j<bno;j++)
if(flags[i]==0&& bsize[j]>=psize[i])
{
alloc[j]=i;
flags[j]=1;
break;
}
printf("\n Block no. \tsize\tprocess no. \t\tsize");
for(i=0;i<bno;i++)
{
printf("\n%d\t\t%d\t\t",i+1,bsize[i]);
if(flags[i]==1)
printf("%d\t\t\t%d",alloc[i]+1,psize[alloc[i]]);
else
printf("Not allocated");
}
}
/*Enter no. of blocks:10

Enter size of each block:1 2 3 4 5 6 7 8 9 10

Enter no. of process:10

Enter size of each process:32 14


7 5 21 8 43 87 10 23

Block no. size process no. size


1 1 Not allocated
2 2 Not allocated
3 3 Not allocated
4 4 Not allocated
5 5 4 5
6 6 Not allocated
7 7 3 7
8 8 6 8
9 9 Not allocated
#include<stdio.h>
void main()
{
int frag[20],b[20],p[20],i,j,nb,np,temp,low=0;
static int barray[20],parray[20];
printf("WORST FIT\n");
printf("ENTER THE NUMBER OF PROCESES\n");
scanf("%d",&np);
printf("ENTER THE NUMBER OF BLOCKS\n");
scanf("%d",&nb);
printf("ENTER THE SIZE OF THE BLOCKS\n");
for(i=1;i<=nb;i++)
{
printf("BLOCK NUMBER %d ",i);
scanf("%d",&b[i]);
}
printf("ENTER THE SIZE OF THE proceses\n");
for(i=1;i<=np;i++)
{
printf("proceses NUMBER %d ",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(low<=temp)
{
parray[i]=j;
low=temp;
}
}
}
frag[i]=low;
barray[parray[i]]=1;
low=0;
}
printf("\nPROCESSES NUMBER \t PROCESES SIZE \t block number \t BLOCK SIZE \t
FRAGMENT");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],frag[i]);
}
/*WORST FIT
ENTER THE NUMBER OF PROCESES

3
ENTER THE NUMBER OF BLOCKS
3
ENTER THE SIZE OF THE BLOCKS
BLOCK NUMBER 1 100
BLOCK NUMBER 2 150
BLOCK NUMBER 3 200
ENTER THE SIZE OF THE proceses
proceses NUMBER 1 80
proceses NUMBER 2 120
proceses NUMBER 3 180

PROCESSES NUMBER PROCESES SIZE block number BLOCK SIZE FRAGMENT


1 80 3 200 120
2 120 2 150 30
//PROGRAM 1
//Executing linux commands through C

# include <stdio.h>
# include <sys/types.h>
# include <dirent.h>
int main()
{
printf("\n Directory listing\n");
system("ls -l");
printf("Directory listing completed");
return 0;
}

/*
Output
total 20
-rwxr-xr-x 1 14052 14052 16048 Jun 28 13:08 a.out
-rwxrwxrwx 1 root root 187 Jun 28 13:08 main.c

Directory listing completed


*/
//PROGRAM 2
//Forking a child process

# include <stdio.h>
# include <sys/types.h>
# include <dirent.h>
main()
{
int pid;
int status;
pid=fork();

if(pid==0)
{
printf("\n I am child,My id is%d\n &My parent id is %d\n\n",getpid(),getppid()); }

else
{
wait(&status);
printf("\n I am parent,My id is %d\n\n",getpid());
printf("\n My child is %d\n\n My parent is %d",pid,getppid());

}
return 0;
}

/*
Output
I am child,My id is2586 & My parent id is 2585

I am parent,My id is 2585

My child is 2586

My parent is 2578
*/
//First come first serve

# include <stdio.h>
void main()
{
int i=0,j=0,b[i],g[20],p[20],w[20],t[20],a[20],n=0,m;
float avgw=0,avgt=0;
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process ID:");
scanf("%d",&p[i]);
printf("Burst Time:");
scanf("%d",&b[i]);
printf("Arrival Time:");
scanf("%d",&a[i]);
}
int temp=0;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
g[0]=0;
for(i=0;i<=n;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];
}
avgw=avgw/n;
avgt=avgt/n;
printf("pid\tarrivalT\tBurstT\tCompletionT\tWaitingtime\tTurnaroundTi\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%d\t\t\t%d\t\t%d\t\t\t%d\t\t\t%d\t\t\t\n",p[i],a[i],b[i],g[i+1],w[i],t[i]);
}
printf("\n Average waiting time %f",avgw);
printf("\n Average turnaround time %f",avgt);
}

/*
Output
Enter the number of process:3
Process ID:741
Burst Time:5
Arrival Time:2
Process ID:693
Burst Time:5
Arrival Time:3
Process ID:781
Burst Time:4
Arrival Time:6
Pid ArrivalT BurstT CompletionT Waitingtime TurnaroundTi
741 2 5 5 -2 3
693 3 5 10 2 7
781 6 4 14 4 8

Average waiting time 1.333333


Average turnaround time 6.000000
*/
// Shortest Job First

# include <stdio.h>
void main()
{
int i=0,j=0,p[i],b[i],g[20],w[20],t[20],a[20],n=0,m;
int k=1,min=0,btime=0;
float avgw=0,avgt=0;
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process ID:");
scanf("%d",&p[i]);
printf("Burst Time:");
scanf("%d",&b[i]);
printf("Arrival Time:");
scanf("%d",&a[i]);
}
int temp=0;
for(i=0;i<n-1;i++)
{ for(j=0;j<n-1;j++)
{ if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp; } } }
for(i=0;i<n;i++)
{ btime=btime+b[i];
min=b[k];
for(j=k;j<n;j++)
{
if(btime>=a[j]&&b[j]<min)
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
temp=p[j];
p[j]=p[j-1];
p[j-1]=temp;
} } k++; }
g[0]=a[0];
for(i=0;i<n;i++)
{
g[i+1]=g[i]+b[i];
if(g[i]<a[i])
{ g[i]=a[i]; } }
for(i=0;i<n;i++)
{
t[i]=g[i+1]-a[i];
w[i]=t[i]-b[i];
avgw+=w[i];
avgt+=t[i];
}
avgw=avgw/n;
avgt=avgt/n;
printf("pid\tBursttime\tGantchart\tWaiting Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t\t%d\t%d\t\t%d\t%d\t\t\t\n",p[i],b[i],g[i],g[i+1],w[i],t[i]);
}
printf("\n Average waiting time %f",avgw);
printf("\n Average turnaround time %f",avgt); }

/* Output
Enter the number of process:5
Process ID:456
Burst Time:5
Arrival Time:2
Process ID:789
Burst Time:2
Arrival Time:4
Process ID:964
Burst Time:7
Arrival Time:6
Process ID:987
Burst Time:2
Arrival Time:3
pid Bursttime Gantchart Waiting Time Turnaround Time
5 5 2 7 0 5
2 2 7 9 3 5
Average waiting time 1.500000
Average turnaround time 5.000000
*/
// Priority Scheduling

# include <stdio.h>
int main()
{
int btime[20],p[20],w[20],t[20],pri[20];
int i,j,limit,sum=0,pos,temp;
float avgw,avgt;
printf("Enter the number of process:\t");
scanf("%d",&limit);
printf("\nEnter Burst Time and Priority for %d Processes\n",limit);
for(i=0;i<limit;i++)
{
printf("\n Process[%d]\n",i+1);
printf("Process Burst Time:\t");
scanf("%d",&btime[i]);
printf("Process Priority:\t");
scanf("%d",&pri[i]);
p[i]=i+1;
}
for(i=0;i<limit;i++)
{
pos=i;
for(j=i+1;j<limit;j++)
{
if(pri[j]<pri[pos])
{
pos=j;
}
}
temp=pri[i];
pri[i]=pri[pos];
pri[pos]=temp;
temp=btime[i];
btime[i]=btime[pos];
btime[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
w[0]=0;
for(i=1;i<limit;i++)
{
w[i]=0;
for(j=0;j<i;j++)
{
w[i]=w[i]+btime[j];
}
sum=sum+w[i];
}
avgw=sum/limit;
sum=0;
printf("\n Process ID \tBurst Time \t Waiting time\tTurnaround Time\n");
for(i=0;i<limit;i++)
{
t[i]=btime[i]+w[i];
sum=sum+t[i];
printf("\np [%d]\t\t%d\t\t%d\t\t%d\n",p[i],btime[i],w[i],t[i]);
}
avgt=sum/limit;
printf("\n Average waiting time %f",avgw);
printf("\n Average turnaround time %f",avgt);
return 0;
}

/*
Output
Enter the number of process: 3
Enter Burst Time and Priority for 3 Processes

Process[1]
Process Burst Time: 3
Process Priority: 2
Process[2]
Process Burst Time: 5
Process Priority: 1
Process[3]
Process Burst Time: 7
Process Priority: 4
Process ID Burst Time Waiting time Turnaround Time

p [2] 5 0 5

p [1] 3 5 8

p [3] 7 8 15

Average waiting time 4.000000


Average turnaround time 9.000000

*/
//Round Robin Scheduling

# include <stdio.h>
int main()
{
int i,limit,total=0,x,counter=0,tquant;
int wt=0,tt=0,at[10],bt[10],temp[10];
float avgw,avgt;
printf("\n Enter Total Number of Processes:\n");
scanf("%d",&limit);
x=limit;
for(i=0;i<limit;i++)
{
printf("\n Enter the details of process[%d]\n",i+1);
printf("Arrival Time:\t");
scanf("%d",&at[i]);
printf("Burst Time:\t");
scanf("%d",&bt[i]);
temp[i]=bt[i];
}
printf("\n Enter Time Quantum:\t");
scanf("%d",&tquant);
printf("\n Process ID \tBurst Time \tTurnaround Time\t\tWaiting time\n");
for(total=0,i=0;x!=0;)
{
if(temp[i]<=tquant&&temp[i]>0)
{
total=total+temp[i];
temp[i]=0;
counter=1;
}
else if(temp[i]>0)
{
temp[i]=temp[i]-tquant;
total=total+tquant;
}
if(temp[i]==0 && counter==1)
{
x--;
printf("\n Process[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],total-at[i],total-at[i]-bt[i]);
wt=wt+total-at[i]-bt[i];
tt=tt+total-at[i];
counter=0;
}
if(i==limit-1)
{
i=0;
}
else if(at[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
avgw=wt*1.0/limit;
avgt=tt*1.0/limit;
printf("\nAverage waiting time:\t%f",avgw);
printf("\nAverage turnaround time:\t%f",avgt);
return 0;
}

/*
Output
Enter Total Number of Processes:
3
Enter the details of process[1]
Arrival Time: 3
Burst Time: 6
Enter the details of process[2]
Arrival Time: 1
Burst Time: 9
Enter the details of process[3]
Arrival Time: 2
Burst Time: 5
Enter Time Quantum: 3
Process ID Burst Time Turnaround Time Waiting time

Process[1] 6 9 3
Process[3] 5 15 10
Process[2] 9 19 10
Average waiting time: 7.666667
Average turnaround time: 14.333333

*/
//Shared Memory

# include <stdio.h>
# include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
# include <sys/types.h>
# include <unistd.h>
# define size 4096
void itoa(char *str,int n)
{
int i=1000000,j=0;
for(j=0;j<7;++j)
{
str[j]=n/i+48;
n=n%i;
i=i/10;
}
str[j]='\0';
}
void read_matrix(int A[10],int *shared_mem)
{
int i,j=0;
for(i=0;i<5;i++)
{
scanf("%d",&A[i]);
shared_mem[j]=A[i];
j++;
}
}
int main()
{
int sh_mem_id,*shared_mem;
int A[10];
char str[10];
pid_t child;
sh_mem_id=shmget(IPC_PRIVATE,size,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH);
shared_mem=(int*)shmat(sh_mem_id,0,0);
itoa(str,sh_mem_id);
system("clear");
printf("\nEnter the element of the matrix A....\n");
read_matrix(A,shared_mem);
child=fork();
if(child==0)
{ sleep(1);
execlp("./shm2","shm2",str,NULL);
}
shmdt(shared_mem);
printf("closing process 1..........\n"); }
# include <stdio.h>
# include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
# include <sys/types.h>
# include <unistd.h>
# include <stdlib.h>
void read_from_shm(int B[10],int *shared_mem)
{
int i,j=0;
printf("\t the shared array is \n");
for(i=0;i<5;++i)
{
B[i]=shared_mem[j];
printf("%d",B[i]);
j++;
}
}
int main(int argc,char *argv[])
{
int *shared_mem,sh_mem_id;
int B[10];
pid_t child;
sh_mem_id=atoi(argv[1]);
shared_mem=(int *)shmat(sh_mem_id,0,0);
system("clear");
read_from_shm(B,shared_mem);
printf("closing process 2......\n");
shmdt(shared_mem);
shmctl(sh_mem_id,IPC_RMID,0);
}

/* Output
Enter the elements of matrix A….
1
2
3
4
5
Closing process 1……

The shared array is 1 2 3 4 5


Closing Process 2……
*/
// Producer Consumer

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 10
pthread_mutex_t mutex;
pthread_t tidP[20],tidC[20];
sem_t full,empty;
int counter;
int buffer[buffersize];
void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}
void write(int item)
{
buffer[counter++]=item;
}
int read(){
return(buffer[--counter]);
}
void * producer (void *param)
{
int waittime,item,i;
item=rand()%5;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\nProducer has produced item: %d\n",item);
write(item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
void * consumer (void * param)
{
int waittime,item;
sem_wait(&full);
pthread_mutex_lock(&mutex);
item=read();
printf("\nConsumer has consumed item: %d\n",item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
int main()
{
int n1,n2,i;
initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tidP[i],NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i],NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);
//sleep(5);
exit(0);
}

/*
Output

Enter the no of producers: 3


Enter the no of consumers: 3
Producer has produced item: 3
Producer has produced item: 2
Consumer has consumed item: 2
Producer has produced item: 1
Consumer has consumed item: 1
Consumer has consumed item: 3
*/
//Page Replacement
#include<stdio.h>
void main() {
int n,f,fr[20],p[50],rep=0,found,fi=0;
int i,k;
printf("Enter the number of pages: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d", &p[i]);
printf("Enter the frame number: ");
scanf("%d",&f);
for(i=0;i<f;i++)
fr[i]=-1;
printf("\n\nPages\t\tFrames\n\n");
for(i=0;i<n;i++){
printf("%d\t\t",p[i]);
found=0;
for(k=0;k<f;k++){
if (p[i]==fr[k]){
found=1;
break; } }
if(!found) {
fr[fi]=p[i];
rep++;
fi=(fi+1) %f;
for (k=0;k<f; k++)
printf("%d\t",fr[k]);}
printf("\n");}
printf("\n\nNumber of page fault: %d\n", rep); }
output
Enter the number of pages: 10
Enter the reference string: 1 0 2 3 6 4 0 5 0 7
Enter the frame number: 3
Pages Frames
1 1 -1 -1
0 1 0 -1
2 1 0 2
3 3 0 2
6 3 6 2
4 3 6 4
0 0 6 4
5 0 5 4
7 0 5 7
Number of page fault: 9
//FCFS
#include<stdio.h>
void main(){
int ioq[20],i,n,ihead,tot;
float seek=0,avgs;
printf("Enter the number of request: ");
scanf("%d", &n);
printf("Enter the initial head position:");
scanf("%d", &ihead);
ioq[0]=ihead;
ioq[n+1]=0;
printf("Enter the I/0 queue requests: \n");
for (i=1;i<=n;i++) {
scanf("%d",&ioq[i]); }
ioq[n+1]=ioq[n];
printf("\norder of request served\n");
for (i=0;i<=n;i++){
tot=ioq[i+1]-ioq[i];
if(tot<0)
tot=tot*-1;
seek+=tot;
printf("%d-->",ioq[i]); }
avgs=seek/(n);
printf("\nTotal Seek Time\t\t:%2f", seek);
printf("\nAverage Seek Time\t:%2f\n\n", avgs); }

/* output
Enter the number of request: 5
Enter the initial head position:100
Enter the I/0 queue requests: 23 89 132 42 187
order of request served 100-->23-->89-->132-->42-->187-->
Total Seek Time :421.000000
Average Seek Time :84.199997 */

//SCAN
#include<stdio.h>
void main() {
int ioq[20],i,n,j,ihead,temp,scan,tot;
float seek=0, avgs;
printf("Enter the number of requests:");
scanf("%d", &n);
printf("Enter the initial head position: ");
scanf("%d", &ihead);
ioq[0]=ihead;
ioq[1]=0;
n+=2;
printf("Enter the I/O queue requests:\n");
for(i=2;i<n;i++) {
scanf("%d",&ioq[i]); }
for(i=0;i<n-1;i++) {
for(j=0;j<n-1;j++) {
if(ioq[j]>ioq[j+1]) {
temp=ioq[j];
ioq[j]=ioq[j+1];
ioq[j+1]=temp; } } }
ioq[n]=ioq[n-1];
for(i=0;i<n;i++) {
if (ihead==ioq[i]) {
scan=i;
break; } }
printf("norder of request served\n\n");
tot=0;
for(i=scan;i>0;i--) {
tot=ioq[i]-ioq[i-1];
if(i==0)
tot=ioq[i]-ioq[scan+1];
if(tot<0)
tot=tot*-1;
printf("%d\t%d\n", ioq[i], tot); }
for(i=scan+1;i<n;i++) {
tot=ioq[i+1]-ioq[i];
if(tot<0)
tot=tot*-1;
printf("%d\t%d\n", ioq[i],tot); }
seek=ihead+ioq[n-1];
avgs=seek/(n-2);
printf("\n\nTotal Seek Time\t\t: %2f", seek);
printf("\nAverage Seek Time\t: %2f\n\n", avgs); }

/* Output
Enter the number of requests:8
Enter the initial head position: 53
Enter the I/O queue requests: 98 183 37 122 14 124 65 67
No of order of request served
53 16
37 23
14 14
65 2
67 31
98 24
122 2
124 59
183 0
Total Seek Time : 236.000000
Average Seek Time : 29.500000*/

//CSCAN
#include<stdio.h>
void main() {
int ioq[20],i,n,j,ihead,itail,temp,scan, tot=0;
float seek=0,avgs;
printf("Enter the number of requests: ");
scanf ("%d",&n);
ioq [0]=0;
printf("Enter the initial head position: ");
scanf("%d",&ihead);
ioq[0]=ihead;
printf("Enter the maximum track limit: ");
scanf("%d", &itail);
ioq[2]=itail;
n+=3;
printf("Enter the I/O queue requests: \n");
for (i=3;i<n;i++) {
scanf("%d", &ioq[i]); }
for (i=0;i<n-1;i++) {
for (j=0;j<-1;j++) {
if(ioq[j]>ioq[j+1]) {
temp=ioq[j];
ioq[j]=ioq[j+1];
ioq[j+1]=temp; } } }
for(i=0;i<n+1;i++) {
if(ihead==ioq[i]) {
scan=i;
break; } }
i=scan;
temp=n;
printf("\norder of request served\n");
printf("\n");
while (i!=temp) {
if(i<temp-1) {
tot=ioq[i+1]-ioq[i];
if(tot<0)
tot=tot*-1;
seek+=tot; }
printf("%d-->", ioq[i]);
i++;
if(i==n) {
i=0;
temp=scan;
seek+=itail; } }
avgs=seek/(n-3);
printf("\n\nTotal Seek Time\t\t:%2f", seek);
printf("\nAverage Seek Time\t: %2f\n\n", avgs);
}
/*Output
Enter the number of requests: 8
Enter the initial head position: 50
Enter the maximum track limit: 200
Enter the I/O queue requests: 90 120 35 122 38 128 65 68
order of request served

50-->0-->200-->90-->120-->35-->122-->38-->128-->65-->68-->

Total Seek Time :1002.000000


Average Seek Time : 125.250000 */

You might also like