The document describes implementing interprocess communication using pipes in C. It creates 5 processes that communicate using pipes. Process 1 takes a string as input. Process 2 splits the string in half and passes the halves to Processes 4 and 5 using pipes. Process 3 checks if the original string is a palindrome by reading it from a pipe. The document also includes code for implementing scheduling algorithms like FCFS and Round Robin.
1. 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("nt PROCESS 2:it splits the given stringn");
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 %dt",getpid());
printf("p2 %dn",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));
}
2. else
{
int c=fork();
if(c==0)
{
printf("p5 %dt",getpid());
printf("p2 %dn",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 %dt",getpid());
printf("p1 %dn",getppid());
} }}
else
{
wait();
int d=fork();
if(d==0)
{
printf("p3 %dt",getpid());
printf("p1 %dn",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 palindromen");
else
printf("t PROCESS 3: the given string is not palindromen");
}
else
{
printf("p1 %dt",getpid());
printf("kernal %dtn",getppid());
3. }
}
}
To write aprogram 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];
4. att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("ntprocesstwaiting timetturn arround timen");
for(i=0;i<n;i++)
{
printf("tp%dtt%dtt%dn",i,w[i],t[i]);
}
printf("the average waiting time is %fn",awt);
printf("the average turn around time is %fn",att);
}
c program for round robinnon preemptive cpuscheduling 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++)
{
5. 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("nProcesst Burst Time tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
6. 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 stringt page framesn");
for(i=1;i<=n;i++)
{
printf("%dtt",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("%dt",frame[k]);
}
printf("n");
}
printf("Page Fault Is %d",count);
return 0;
}
7. To write ac program to implement LRUpage 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("nt%dn",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])