Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

ROUND ROBIN

#include<stdio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt,atat;
printf("enter the number of processes");
scanf("%d",&n);
printf("enter the burst time of each process /n");
for(i=0;i<n;i++)
{

printf(("p%d",i+1);
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("enter the time quantum");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("process no\t burst time\t waiting time\t turnaround time\n");
for(i=0;i<n;i++)
printf("%d\t\t %d\t\t %d\t\t %d\n",i+1,bt[i],wt[i],tat[i]);
printf("avg wt time=%f,avg turn around time=%f",awt,atat);
}

PRIORITY

#include <stdio.h>
int main()
{
int n,at[10],bt[10],wt[10],tat[10],ct[10],pr[10],sum,i,j,k,pos,temp;
float totaltat=0,totalwt=0;
printf("Enter the total number of processes:");
scanf("%d",&n);
printf("\nEnter The Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("Enter Burst time of process[%d]:",i+1);
scanf("%d",&bt[i]);
}
printf("\nEnter The Priority of each process \n");
for(i=0;i<n;i++)
{
printf("Enter priority of process[%d]:",i+1);
scanf("%d",&pr[i]);
}
/* Apply Selection sort to sort according to burst time and process priority */
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
{
pos=j;
}
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
}
/*Calculate completion time of processes*/
sum=0;
for(j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=sum;
printf("%d\t",ct[j]);
}
/*Calculate Turn Around time */
for(k=0;k<n;k++)
{
tat[k]=ct[k];
totaltat=totaltat+tat[k];
}
/* Calculate Waiting time */
for(k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalwt=totalwt+wt[k];
}
printf("\nProcess\tBT\tTAT\tWT\n\n\n");
for(i=0;i<n;i++)
{
printf("\nP%d\t %d\t %d\t %d\n\n",i+1,bt[i],tat[i],wt[i]);
}
printf("\nAverage TurnaroundTime:%f\n",totaltat/n);
printf("\nAverage Waiting Time:%f",totalwt/n);
return 0;
}
SJF

#include<stdio.h>
int main()
{
int n,at[10],bt[10],wt[10],tat[10],ct[10],p[10],sum,i,j,k,temp;
float totaltat=0,totalwt=0;
printf("Enter the total number of processes:");
scanf("%d",&n);
printf("\nEnter the process number:\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\nEnter The Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("Enter Burst time of process[%d]:",i+1);
scanf("%d",&bt[i]);
}
/* Apply Bubble sort to sort according to burst time and process number */
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(bt[j]>bt[j+1])
{
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;

temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
/*Calculate completion time of processes*/
sum=0;
for(j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=sum;
printf("%d\t",ct[j]);
}
/*Calculate Turn Around time */
for(k=0;k<n;k++)
{
tat[k]=ct[k];
totaltat=totaltat+tat[k];
}
/* Calculate Waiting time */
for(k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalwt=totalwt+wt[k];
}

printf("\nProcess\tBT\tTAT\tWT\n\n\n");
for(i=0;i<n;i++)
{
printf("\nP%d\t %d\t %d\t %d\t %d\t %d\t\n",i+1,bt[i],tat[i],wt[i]);
}
printf("\nAverage TurnaroundTime:%f\n",totaltat/n);
printf("\nAverage Waiting Time:%f",totalwt/n);

return 0;
}

SEMAPHORE

#include<stdio.h>
#include<stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int main ()
{
int n;
void producer ();
void consumer ();
int wait (int);
int signal (int);
printf ("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
printf ("\nEnter your choice:");
scanf ("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
producer ();
else
printf ("Buffer is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer ();
else
printf ("Buffer is empty!!");
break;
case 3:
exit (0);
break;
}
}
return 0;
}
int wait (int s)
{
return (--s);
}
int signal (int s)
{
return (++s);
}
void producer ()
{
mutex = wait (mutex);
full = signal (full);
empty = wait (empty);
x++;
printf ("\nProducer produces the item %d", x);
mutex = signal (mutex);
}
void consumer ()
{
mutex = wait (mutex);
full = wait (full);
empty = signal (empty);
printf ("\nConsumer consumes item %d", x);
x--;
mutex = signal (mutex);
}
BANKER

#include<Stdio.h>
#include<conio.h>
int main()
{
int available[3],work[5],max[5][3],allocation[5][3],need[5][3],safe[5],totalres[5];
char finish[5];
int i,j,k,totalloc=0,state,value=0;
printf("Enter Instances of each Resource");
for(i=0;i<3;i++)
{
scanf("%d",&totalres[i]);
}
printf("Enter Maximum resources for each processes");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter process %d Resource %d",i,(j+1));
scanf("%d",&max[i][j]);
}
}
printf("Enter number of resources allocated to each Process");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the resource of R%d allocated to process %d",(j+1),i);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
for(i=0;i<5;i++)
{
finish[i]='f';
}
for(i=0;i<3;i++)
{
totalloc=0;
for(j=0;j<5;j++)
{
totalloc=totalloc+allocation[j][i];
}
available[i]=totalres[i]-totalloc;
work[i]=available[i];
}
printf("\n Allocated Resources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",allocation[i][j]);
}
printf("\n");
}
printf("\n Maximum Resources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",max[i][j]);
}
printf("\n");
}
printf("\n Needed Reources \n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",need[i][j]);
}
printf("\n");
}
printf("\n Available Reources");
for(i=0;i<3;i++)
{
printf("%d",available[i]);
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if((finish[i]=='f')&&(need[i][j]<=work[j]))
{
state=1;
}
else
{
state=0;
break;
}
} // end of innermost loop
if(state==1)
{
for(j=0;j<3;j++)
{
work[j]=work[j]+allocation[i][j];
}
finish[i]='t';
safe[value]=i;
++value;
}
if(i==4)
{
if(value==5)
{
break;
}
else
{
i=-1;
}
}
} // end of outermost loop
printf("\n Safe States are");
for(i=0;i<5;i++)
{
printf("P%d",safe[i]);
}
return 0;
}

FCFS

#include<Stdio.h>

int main()
{
int n,at[10],bt[10],wt[10],tat[10],ct[10],sum,i,j,k;
float totaltat=0,totalwt=0;
printf("enter the total number of processes:");
scanf("%d",&n);
printf("\nEnter The Process Arrival Time & Burst Time\n");
for(i=0;i lessthan n;i++)
{ printf("Enter Arrival time of process[%d]:",i+1);
scanf("%d",&at[i]);
printf("Enter Burst time of process[%d]:",i+1);
scanf("%d",&bt[i]);
}
/*Calculate completion time of processes*/
sum=at[0];
for(j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=sum;
}
/*Calculate Turn Around time */
for(k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totaltat=totaltat+tat[k];
}
/* Calculate Waiting time */
for(k=0;k<n;k++)
{
wt[k]=tat[k]-bt[k];
totalwt=totalwt+wt[k];
}

printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n\n\n");
for(i=0;i<n;i++)
{
printf("\nP%d\t %d\t %d\t %d\t %d\t %d\t\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("\nAverage TurnaroundTime:%f\n",totaltat/n);
printf("\nAverage Waiting Time:%f",totalwt/n);

return 0;}
DIRECTORIES

#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
main()
{
char d[10]; int c,op; DIR *e;
struct dirent *sd;
printf("**menu**\n1.create dir\n2.remove dir\n 3.read dir\n enter ur choice");
scanf("%d",&op);
switch(op)
{
case 1: printf("enter dir name\n"); scanf("%s",&d);
c=mkdir(d,777);
if(c==1)
printf("dir is not created");
else
printf("dir is created"); break;
case 2: printf("enter dir name\n"); scanf("%s",&d);
c=rmdir(d);
if(c==1)
printf("dir is not removed");
else
printf("dir is removed"); break;
case 3: printf("enter dir name to open");
scanf("%s",&d);
e=opendir(d);
if(e==NULL)
printf("dir does not exist"); else
{
printf("dir exist\n"); while((sd=readdir(e))!=NULL) printf("%s\t",sd->d_name);
}
closedir(e);
break;
}
}

OPEN READ WRITE

#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];
f1=open("data",O_RDWR|O_CREAT|O_TRUNC);
while((c=getchar())!='\n')
{
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0);
printf("\n%s\n",strin);
close(f2);
return 0;
}

You might also like