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

C Programming Scheduling Algorithms

The document contains 9 code snippets implementing various computer science algorithms and concepts: 1. A program to toggle the CAPS lock key using memory addresses. 2. A program to find the system memory size by accessing a memory address. 3. Implementation of the First Come First Serve (FCFS) CPU scheduling algorithm. 4. Implementation of priority-based CPU scheduling. 5. Implementation of Round Robin CPU scheduling. 6. Simulation of the readers-writers problem synchronization. 7. Simulation of the producer-consumer problem with synchronization. 8. Simulation of the sleeping barber problem. 9. Implementation of FIFO page replacement algorithm in memory management.

Uploaded by

nitin_280890
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views27 pages

C Programming Scheduling Algorithms

The document contains 9 code snippets implementing various computer science algorithms and concepts: 1. A program to toggle the CAPS lock key using memory addresses. 2. A program to find the system memory size by accessing a memory address. 3. Implementation of the First Come First Serve (FCFS) CPU scheduling algorithm. 4. Implementation of priority-based CPU scheduling. 5. Implementation of Round Robin CPU scheduling. 6. Simulation of the readers-writers problem synchronization. 7. Simulation of the producer-consumer problem with synchronization. 8. Simulation of the sleeping barber problem. 9. Implementation of FIFO page replacement algorithm in memory management.

Uploaded by

nitin_280890
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) WAP to turn on CAPS key

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int i;
char far *far1;
clrscr();
far1 = (char far *)0x417;

*far1^=1;
scanf("%c",&ch);
getch();
}
2) WAP to find the memory size

#include<stdio.h>
#include<conio.h>

void main()
{

int far *far1;


clrscr();
far1=(int far *)0x413;

printf("Memory size is: %d",*far1);


getch();

}
3) WAP to implement First Come First Serve (FCFS) Scheduling

#include <stdio.h>
#include <stdlib.h>

int main()
{
int at[5],bt[5],p[5],i,n,j,temp,time[5];
float wt=0.0f,ta=0.0f;
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the CPU burst time and Arrival time of processes\n");
for(i=0;i<n;i++)
{
p[i]=i+1;
scanf("%d%d",&bt[i],&at[i]);
}
printf("P_No. C_time A_time\n");
for(i=0;i<n;i++)
{
printf("%d\t %d\t %d\n",p[i],bt[i],at[i]);
}

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(at[j]>at[j+1])
{
temp=at[j];
at[j]=at[j+1];
at[j+1]=temp;
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;

}
}
}
for(i=0;i<=n;i++)
{
if(i==0)
{
time[i]=0;
continue;
}
time[i]=time[i-1]+bt[i-1];
}
printf("\n ");
for(i=0;i<n;i++)
{
printf("%d ",p[i]);
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("%d ",time[i]);
}
printf("\nAverage waiting time = ");
for(i=0;i<n;i++)
{
wt=wt+(time[i]-at[i]);
}
wt/=n;
printf("%f\n",wt);
printf("\nAverage turnaround time = ");
for(i=0;i<n;i++)
{
ta=ta+(time[i+1]-at[i]);
}
ta/=n;
printf("%f\n",ta);

return 0;
}
4) WAP to implement Priority Scheduling

#include<stdio.h>
#include<conio.h>

int main()
{
int i=0,n=0,t=0,flag=0,a[10],p[10],cpub[10],nos[10],wt[10],ta[10],min=0,temp=0,pos=0,j=0,total=0;
float avg_wt=0.0f,avg_ta=0.0f;
printf("enter the number of processes \n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("\n%d %d %d",&cpub[i],&a[i],&p[i]);

for(i=0;i<n;i++)
{
total+=cpub[i];
nos[i]=i+1;
}
printf("P_No. C.B.T A.T Priority\n");
for(i=0;i<n;i++)
printf("\n%d\t %d\t%d\t%d\n",nos[i],cpub[i],a[i],p[i]);
for(i=0;i<n-1;i++)
{
min=a[i];
pos=0;
temp=0;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];pos=j;
flag=1;
}

}
min=0;
if(flag==1)

{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
temp=cpub[i];
cpub[i]=cpub[pos];
cpub[pos]=temp;
temp=nos[i];
nos[i]=nos[pos];
nos[pos]=temp;
}
flag=0;
}

t=a[0];
for(i=0;i<n;i++)
{
pos=i;
min=100;
for(j=0;j<n;j++)
{
if(a[j]<=t)
{
if(p[j]<=min)
{
min=p[j];
pos=j;
}
}
}
wt[pos]=t-a[pos];
t+=cpub[pos];
ta[pos]=t-a[pos];
avg_wt+=wt[pos];
avg_ta+=ta[pos];
a[pos]=total+1;
}
avg_wt/=n;
avg_ta/=n;

printf("Average Waiting time = %f\n",avg_wt);


printf("Average Turnaround time = %f\n",avg_ta);
return 0;
}
5) WAP to implement Round Robin Scheduling

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j,k,n,ts,process[100],time[100],total=0,flag=0;
float avg_wt=0.0f,avg_ta=0.0f;
printf("Enter the number of processes\n");
scanf("%d",&n);
int bt[n],p[n],wt[n],ta[n];
printf("Enter the CPU burst time of processes\n");
for(i=0;i<n;i++)
{
p[i]=i+1;
scanf("%d",&bt[i]);
total+=bt[i];
}
printf("Enter time slice\n");
scanf("%d",&ts);
printf("P_No. C_time\n");
for(i=0;i<n;i++)
{
printf("%d\t %d\n",p[i],bt[i]);
}

time[0]=0;
for(i=0;i<n;i++)
wt[i]=0;
i=0,j=0;
while(total)
{

if(bt[i]==0)
{

++i;
i=i%n;
flag=0;
}
else if(bt[i]>ts)
{
flag=1;
process[j]=i+1;
j++;
time[j]=time[j-1]+ts;
bt[i]=bt[i]-ts;
total-=ts;

++i;
i=i%n;

}
else
{
flag=1;
process[j]=i+1;
j++;
time[j]=time[j-1]+bt[i];
total-=bt[i];
bt[i]=0;
ta[i]=time[j];
++i;
i=i%n;

}
for(k=0;k<n;k++)
{
if(bt[k]!=0 && process[j-1]!=k+1 && flag==1)
{
wt[k]+=(time[j]-time[j-1]);

}
}

}
for(i=0;i<n;i++)
{
avg_ta+=ta[i];
avg_wt+=wt[i];
}
avg_wt/=n;
avg_ta/=n;
printf("\n ");
for(i=0;i<j;i++)
{
printf("%d ",process[i]);
}
printf("\n");
for(i=0;i<=j;i++)
{
printf("%d ",time[i]);
}
printf("\nAverage waiting time = %f",avg_wt);
printf("\nAverage turnaround time = %f",avg_ta);
return 0;
}
6) Simulate Readers and Writers problem

#include<stdio.h>
#include<stdlib.h>

int wait(int *m)


{
if(*m<=0)
return 0;
else
{
*m-=1;
return 1;
}
return 0;
}

void signal(int *m)


{
*m+=1;

void writer(int *mutex)


{

if(wait(mutex));
printf("\nWriter is running\n");
signal(mutex);
}

void reader(int *mutex,int *wrt,int *readcount)


{
if(wait(mutex));
{
*readcount++;
if(*readcount==1)
if(wait(wrt));
}
signal(mutex);
printf("\nReader is running\n");
if(wait(mutex));
{
*readcount--;
if(*readcount==0)
signal(wrt);
}
signal(mutex);

}
int main()
{
int wrt=1;
int mutex=0;
int readcount=0;
int ch;
ch=0;
while(ch==0 ||ch==1)
{

if(ch==0)
{
if(readcount==0)
{
writer(&mutex);
}
else
{
printf("\nWriter cann't write\n");
}
}
else
if(ch==1)
{
if(wrt==0&&readcount==0)
{
printf("\nreader cann't read\n");
}
else
{
reader(&mutex,&wrt,&readcount);
}
}

else
{
printf("Do nothing");
}
printf("\n");
printf("Enter your choice :");
scanf("%d",&ch);

}
return 0;
}
7) Simulate Producer and Consumer Problem

#include<stdio.h>
#include<stdlib.h>

int ar[10];
int size;
int item;
void push()
{
ar[size]=item+1;
item++;
size=size+1;

}
int pop()
{
int i;
int temp=ar[0];
for(i=0;i<size;i++)
{
ar[i]=ar[i+1];
}
size--;
return temp;
}

int wait(int *m)


{
if(*m<=0)
return 0;
else
{
*m-=1;
return 1;
}
return 0;
}

void signal(int *m)


{
*m+=1;

void producer(int *mutex,int *full,int *empty)


{
if(wait(empty));
if(wait(mutex));
printf("\nProducer is running\n");
push();
signal(mutex);
signal(full);
printf("Mutes : %d\tFull : %d\tEmpty : %d\n",*mutex,*full,*empty);

void consumer(int *mutex,int *full,int *empty)


{
if(wait(full));
if(wait(mutex));
printf("\nConsumer is running\n");
printf("\nConsumer consumed the item no:\t %d",pop());
signal(mutex);
signal(empty);
printf("Mutex : %d\tFull : %d\tEmpty : %d\n",*mutex,*full,*empty);
}

int main()
{
int mutex=1;
int full=0;
int empty=5;
int ch;
size=0;
item=0;
ch=0;
while(ch==0 ||ch==1)
{

if(ch==0)
{

if(empty==0)
{
printf("\nThe producer cant produce");
}
else
{
producer(&mutex,&full,&empty);
}

}
else
if(ch==1)
{
if(full==0)
{
printf("\nThe consumer cant consume preeently");
}
else
{
consumer(&mutex,&full,&empty);
}

}
else
{
printf("Do nothing");
}
printf("\nEnter your choice :");
scanf("%d",&ch);

}
return 0;
}
8) Simulate Sleeping Barber Problem

#include <stdio.h>
#include <stdlib.h>
#define chairs 5
int waiting=0;
int barbers=0;
int wait(int *m)
{
if(*m<=0)
return 0;
else
{
*m-=1;
return 1;
}
return 0;
}
void signal(int *m)
{
*m+=1;
}
void barber(int *mutex ,int *customers)
{
if(wait(customers))
{
if(wait(mutex))
{
waiting-=1;
printf("Barber is cutting hair\n");
barbers=1;
signal(mutex);
}
}
else
{
printf("No customers...Barber is sleeping\n");
barbers=0;
}
}
void customer(int *mutex, int *customers)
{
if(wait(mutex))
{
if(waiting<chairs)
{
waiting+=1;
signal(customers);
signal(mutex);
if(barbers==1)
printf("Customer seated and waiting\n");
else
barber(mutex,customers);
}
else
{
signal(mutex);
printf("Shop is full\n");
}
}
}
int main()
{
int customers=0;
int mutex=1;
int choice;
printf("Enter 1 for customer and 2 for barber\n");
while(1)
{
scanf("%d",&choice);
if(choice==1)
customer(&mutex,&customers);
else if(choice==2)
barber(&mutex,&customers);
printf("Waiting = %d\n",waiting);
}
return 0;
}
9) Implement FIFO Page Replacement

#include<iostream.h>
#include<conio.h>

int array[5];
int pg;

void replace(int x)
{
static int count=0;
static int old=0;
int i,found;
if(count<5)
{
found=0;
for(i=0;i<count;i++)
{
if(array[i]==x)
{
found=1;
break;
}
}
if(found==0)
{
pg++;
array[count]=x;
count++;
}
}
else
{
found=0;
for(i=0;i<5;i++)
{
if(array[i]==x)
{
found=1;
break;
}
}
if(found==0)
{
pg++;
array[old]=x;
old=(old+1)%5;
}
}
cout<<"\nCurrently the pages in the frames are\n";
for(i=0;i<5;i++)
{
cout<<array[i]<<"\t";
}
cout<<"\n";
}

void main()
{
clrscr();
int i;
pg=0;
for(i=0;i<5;i++)
{
array[i]=-1;
}
int n;
cout<<"Enter the number of pages";
cin>>n;
cout<<"Enter the page vlaues";
int a[15];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
replace(a[i]);
}
cout<<"\nNumber of page faults "<<pg<<"\n";
getch();
}
10) Implement LRU Page Replacement

#include<conio.h>
#include<iostream.h>
struct arr
{
int pg;
int count;
}a[5];

void replace(int x)
{
static int counter=0;
int i,found=0,max=0,index;
if(counter<5)
{
for(i=0;i<counter;i++)
{
if(a[i].pg==x)
{
a[i].count=0;
counter--;
found=1;
break;
}
else
a[i].count++;

}
if(found==0)
{
a[counter].pg=x;
a[counter].count=0;

}
}
else
{
found=0;
for(i=0;i<5;i++)
{
if(a[i].pg==x)
{
found=1;
break;
}
}
for(i=0;i<5;i++)
{
a[i].count=a[i].count+1;
if(max<a[i].count)
{
max=a[i].count;
index=i;
}
}
a[index].pg=x;
a[index].count=0;
}
cout<<"\nCurrently the pages in the frames are\n";
for(i=0;i<5;i++)
{
cout<<a[i].pg<<"\t";
}
cout<<"\n";
counter++;
}
void main()
{
clrscr();
int i;
for(i=0;i<5;i++)
{
a[i].pg=-1;
a[i].count=0;
}
int n;
cout<<"Enter the number of pages";
cin>>n;
cout<<"Enter the page vlaues";
int array[15];
for(i=0;i<n;i++)
{
cin>>array[i];
}
for(i=0;i<n;i++)
{
replace(array[i]);
}
getch();
}
11) Implement Optimal Page Replacement

#include <iostream>
#define num 3
using namespace std;
int a[20],frames[num],n;
int counter=0;

void replacepage(int k,int val)


{
int i,j,loc=0,index=0,flag=0;
for(i=0;i<counter;i++)
{
flag=0;
for(j=k+1;j<n;j++)
{
if(a[j]==frames[i])
{
flag=1;
if(j>loc)
{
loc=j;
index=i;

}
break;
}
}
if(flag==0)
{
index=i;
break;
}

}
frames[index]=val;

}
void replace(int val,int k)
{
int i;
int flag=0;
for(i=0;i<counter;i++)
{
if(frames[i]==val)
{
flag=1;
break;
}
}
if(flag==1)
return;
else
{
if(counter<num)
{
frames[counter]=val;
counter++;
}
else
{
replacepage(k,val);
}
}
}
int main()
{
int i,j;
cout<<"Enter the number of pages\n";
cin>>n;
cout<<"Enter the page values\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<num;i++)
{
frames[i]=-1;

}
for(i=0;i<n;i++)
{
replace(a[i],i);
cout<<"Pages in memory are\n";
for(j=0;j<num;j++)
cout<<frames[j]<<"\t";
cout<<"\n";
}
return 0;
}
12) Implement FCFS Disk Scheduling

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int n,i,a[10],stime=0;
printf("Enter the initial position of disk head\n");
scanf("%d",&a[0]);
printf("Enter the number of cylinders\n");
scanf("%d",&n);
printf("Enter their positions\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
printf("Disk head moved to %d\t",a[i]);
printf("Seek Time for movement is %d\n",abs(a[i]-a[i-1]));
stime+=abs(a[i]-a[i-1]);
}
printf("Total seek time is %d",stime);

return 0;
}
13) Implement SSTF Disk Scheduling

#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,a[10],stime=0,cur,min=1000,index;
printf("Enter the initial position of disk head\n");
scanf("%d",&a[0]);
printf("Enter the number of cylinders\n");
scanf("%d",&n);
printf("Enter their positions\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
cur=a[0];
for(i=1;i<=n;i++)
{
min=1000;
for(j=1;j<=n;j++)
{
if(a[j]!=-1)
{
if(min>abs(cur-a[j]))
{
min=abs(cur-a[j]);
index=j;
}
}
}
printf("Disk head moved to %d\t",a[index]);
printf("Seek Time for movement is %d\n",min);
stime+=min;
cur=a[index];
a[index]=-1;
}
printf("Total seek time is %d",stime);
return 0;
}

You might also like