Os scheduling Algorithms

10
To implement the concept of interprocess communication using pipes using c program. #include<stdio.h> #include<unistd.h> #include<string.h> main() { int p1[2],p2[2],p3[2],p4[2]; int i,j=0,k=0,l=0; char r[10],s[10],t[10],u[10]; printf("\t PROCESS 1.ENTER THE STRING"); scanf("%s",r); pipe(p1); pipe(p2); write(p1[1],r,sizeof(r)); write(p2[1],r,sizeof(r)); int a=fork(); if(a==0) { printf("\n\t PROCESS 2:it splits the given string\n"); read(p1[0],r,sizeof(r)); int n=strlen(r); for(i=0;i<n/2;i++) { s[i]=r[i]; } for(i=n/2;i<=n;i++) { t[j++]=r[i]; } pipe(p3); pipe(p4); write(p3[1],s,sizeof(s)); write(p4[1],t,sizeof(t)); int b=fork(); if(b==0) {

Transcript of Os scheduling Algorithms

Page 1: Os scheduling Algorithms

To implement the concept of interprocess communication using pipes using c program.

#include<stdio.h>#include<unistd.h>#include<string.h>main(){ int p1[2],p2[2],p3[2],p4[2]; int i,j=0,k=0,l=0; char r[10],s[10],t[10],u[10]; printf("\t PROCESS 1.ENTER THE STRING"); scanf("%s",r); pipe(p1); pipe(p2); write(p1[1],r,sizeof(r)); write(p2[1],r,sizeof(r)); int a=fork(); if(a==0) { printf("\n\t PROCESS 2:it splits the given string\n"); read(p1[0],r,sizeof(r)); int n=strlen(r); for(i=0;i<n/2;i++) { s[i]=r[i]; } for(i=n/2;i<=n;i++) { t[j++]=r[i]; } pipe(p3); pipe(p4); write(p3[1],s,sizeof(s)); write(p4[1],t,sizeof(t)); int b=fork(); if(b==0) { printf("p4 %d\t",getpid()); printf("p2 %d\n",getppid()); read(p3[0],s,sizeof(s)); printf("\t PROCESS 4:sub string \t %s \t",s); printf("no of char=%d \n",strlen(s)); }

Page 2: Os scheduling Algorithms

else { int c=fork(); if(c==0) { printf("p5 %d\t",getpid()); printf("p2 %d\n",getppid()); read(p4[0],t,sizeof(t)); printf("\t PROCESS 5:sub string \t %s \t",t); printf("no of char=%d \n",strlen(t)); } else { wait(); printf("p2 %d\t",getpid()); printf("p1 %d\n",getppid()); } }}else{ wait(); int d=fork(); if(d==0) { printf("p3 %d\t",getpid()); printf("p1 %d\n",getppid()); read(p2[0],r,sizeof(r)); for(i=strlen(r)-1;i>=0;i--) { u[l++]=r[i]; } for(i=0;i<strlen(r);i++) { if(u[i]==r[i]) k++; else continue; } if(k==strlen(r)) printf("\t PROCESS 3: the given string is palindrome\n"); else printf("\t PROCESS 3: the given string is not palindrome\n"); } else { printf("p1 %d\t",getpid()); printf("kernal %d\t\n",getppid());

Page 3: Os scheduling Algorithms

} }}

To write a program to implement the FCFS scheduling algorithm

#include<stdio.h>main(){

int n,a[10],b[10],t[10],w[10],g[10],i,m;

float att=0,awt=0;

for(i=0;i<10;i++)

{

a[i]=0; b[i]=0; w[i]=0; g[i]=0;

}

printf("enter the number of process");

scanf("%d",&n);

printf("enter the burst times");

for(i=0;i<n;i++)

scanf("%d",&b[i]);

printf("\nenter the arrival times");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

g[0]=0;

for(i=0;i<10;i++)

g[i+1]=g[i]+b[i];

for(i=0;i<n;i++)

{

w[i]=g[i]-a[i];

t[i]=g[i+1]-a[i];

awt=awt+w[i];

Page 4: Os scheduling Algorithms

att=att+t[i];

}

awt =awt/n;

att=att/n;

printf("\n\tprocess\twaiting time\tturn arround time\n");

for(i=0;i<n;i++)

{

printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);

}

printf("the average waiting time is %f\n",awt);

printf("the average turn around time is %f\n",att);

}

c program for round robin non preemptive cpu scheduling algorithm

#include<stdio.h> void main(){    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;    float avg_wt,avg_tat;    printf("Enter number of process:");    scanf("%d",&n);     printf("\nEnter Burst Time:\n");    for(i=0;i<n;i++)    {        printf("p%d:",i+1);        scanf("%d",&bt[i]);        p[i]=i+1;           //contains process number    }     //sorting burst time in ascending order using selection sort    for(i=0;i<n;i++)    {

Page 5: Os scheduling Algorithms

        pos=i;        for(j=i+1;j<n;j++)        {            if(bt[j]<bt[pos])                pos=j;        }         temp=bt[i];        bt[i]=bt[pos];        bt[pos]=temp;         temp=p[i];        p[i]=p[pos];        p[pos]=temp;    }     wt[0]=0;            //waiting time for first process will be zero     //calculate waiting time    for(i=1;i<n;i++)    {        wt[i]=0;        for(j=0;j<i;j++)            wt[i]+=bt[j];         total+=wt[i];    }     avg_wt=(float)total/n;      //average waiting time    total=0;     printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");    for(i=0;i<n;i++)    {        tat[i]=bt[i]+wt[i];     //calculate turnaround time        total+=tat[i];        printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);    }     avg_tat=(float)total/n;     //average turnaround time    printf("\n\nAverage Waiting Time=%f",avg_wt);    printf("\nAverage Turnaround Time=%f\n",avg_tat);}

Page 6: Os scheduling Algorithms

write a c program to implement FIFO page replacement algorithm

#include<stdio.h>int main(){

int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\n ENTER THE NUMBER OF PAGES:\n");

scanf("%d",&n); printf("\n ENTER THE PAGE NUMBER :\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\n ENTER THE NUMBER OF FRAMES :"); scanf("%d",&no);

for(i=0;i<no;i++) frame[i]= -1; j=0; printf("\tref string\t page frames\n");for(i=1;i<=n;i++) { printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++)if(frame[k]==a[i]) avail=1; if (avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]);

} printf("\n");

} printf("Page Fault Is %d",count); return 0;}

Page 7: Os scheduling Algorithms

To write a c program to implement LRU page replacement algorithm

#include<stdio.h>main(){

int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];printf("Enter no of pages:");scanf("%d",&n);printf("Enter the reference string:");for(i=0;i<n;i++)

scanf("%d",&p[i]);printf("Enter no of frames:");scanf("%d",&f);q[k]=p[k];printf("\n\t%d\n",q[k]);c++;k++;for(i=1;i<n;i++)

{ c1=0; for(j=0;j<f;j++) { if(p[i]!=q[j]) c1++; } if(c1==f) { c++; if(k<f) { q[k]=p[i]; k++; for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n"); } else { for(r=0;r<f;r++) { c2[r]=0; for(j=i-1;j<n;j--) { if(q[r]!=p[j])

Page 8: Os scheduling Algorithms

c2[r]++; else break; } } for(r=0;r<f;r++) b[r]=c2[r]; for(r=0;r<f;r++) { for(j=r;j<f;j++) { if(b[r]<b[j]) { t=b[r]; b[r]=b[j]; b[j]=t; } } } for(r=0;r<f;r++) { if(c2[r]==b[0]) q[r]=p[i]; printf("\t%d",q[r]); } printf("\n"); } }}printf("\nThe no of page faults is %d",c);}