You are on page 1of 60

1.

Write C Programs to simulate the following CPU Scheduling algorithms:

Round robin

#include<stdio.h>

main() {

int pt[10][10],a[10][10],at[10],pname[10][10],i,j,n,k=0,q,sum=0;

float avg; printf("\n\n Enter the number of processes : "); scanf("%d",&n); for(i=0;i<10;i++) {

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

{ pt[i][j]=0; a[i][j]=0; }

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

{ j=0; printf("\n\n Enter the process time for process %d : ",i+1); scanf("%d",&pt[i][j]);

printf("\n\n Enter the time slice : ");

scanf("%d",&q); printf("\n\n"); for(j=0;j<10;j++) {

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

{ a[2*j][i]=k; if((pt[i][j]<=q)&&(pt[i][j]!=0))

pt[i][j+1]=0; printf(" %d P%d %d\n",k,i+1,k+pt[i][j]); k+=pt[i][j]; a[2*j+1][i]=k;

else if(pt[i][j]!=0)

{ pt[i][j+1]=pt[i][j]-q;

printf(" %d P%d %d\n",k,i+1,(k+q)); k+=q; a[2*j+1][i]=k;

else
{ a[2*j][i]=0; a[2*j+1][i]=0;

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

sum+=a[0][i]; for(i=0;i<n;i++) {

for(j=1;j<10;j++)

if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))

sum+=((a[j+1][i]-a[j][i]));

avg=(float)sum/n;

printf("\n\n Average waiting time = %f msec",avg); sum=avg=0; for(j=0;j<n;j++)

{ i=1; while(a[i][j]!=0) i+=1; sum+=a[i-1][j];

} avg=(float)sum/n;

printf("\n\n Average turnaround time = %f msec\n\n",avg);

OUTPUT:

[root@localhost ~]# ./a.out

Enter the number of processes : 4

Enter the process time for process 1 : 8 Enter the process time for process 2 : 3 Enter the process
time for process 3 : 6 Enter the process time for process 4 : 1

Enter the time slice : 2

0 P1 2 2 P2 4 4 P3 6 6 P4 7 7 P1 9 9 P2 10 10 P3 12 12 P1 14 14 P3 16 16 P1 18
Average waiting time = 8.250000 msec

Average turnaround time = 12.750000 msec

RESULT:

Thus the program for implementing RR scheduling algorithm was written and successfully
executed.
SJF
2. Write C programs to simulate the following CPU Scheduling algorithms:

FCFS
Priority

#include<stdio.h>

main() {

float avgwt,avgtt;

char pname[10][10],c[10][10]; int wt[10],tt[10],bt[10],pt[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=10;


printf("\n\n Enter the number of processes : "); scanf("%d",&n); printf("\n\n Enter the NAME and
BURSTTIME "); for(i=0;i<n;i++) {

printf("\n\n NAME : "); scanf("%s",&pname[i]); printf("\n\n BURSTTIME : "); scanf("%d",&bt[i]);

printf("\n\n Enter the priorities of the processes ");

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

{ printf("\n\n Priority of process%d : ",i+1); scanf("%d",&pt[i]);

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

for(j=i+1;j<n;j++)

if(pt[i]>pt[j])

{ t=pt[i]; pt[i]=pt[j]; pt[j]=t; q=bt[i];

bt[i]=bt[j]; bt[j]=q; strcpy(c[i],pname[i]); strcpy(pname[i],pname[j]); strcpy(pname[j],c[i]);

wt[0]=0;

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

{ wt[i+1]=wt[i]+bt[i]; sum=sum+wt[i]; sbt=sbt+wt[i+1]; tt[i]=wt[i]+bt[i]; ss=ss+bt[i];

printf("\n\n GANTT CHART");


printf("\n-----------------------------------------------------------------\n");

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

printf("|\t%s\t",pname[i]);

printf("\n-----------------------------------------------------------------\n");

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

printf("%d\t\t",wt[i]);

printf("%d\n",ss);

printf("\n-----------------------------------------------------------------\n");

printf("\n\n Total WAITING TIME of the process = %d",sum);

printf("\n\n Total TURNAROUND TIME of the process = %d",sbt);

avgwt=(float)sum/n;

avgtt=(float)sbt/n;

printf("\n\n Average WAITING TIME of the process = %f",avgwt);

printf("\n\n Average TURNAROUND TIME of the process = %f",avgtt);

OUTPUT:

[root@localhost ~]# ./a.out Enter the number of processes : 4 Enter the NAME and BURSTTIME

NAME : p1

BURSTTIME : 8

NAME : p2

BURSTTIME : 3

NAME : p3

BURSTTIME : 6

NAME : p4

BURSTTIME : 1
Enter the priorities of the processes Priority of process1 : 1 Priority of process2 : 5 Priority of process3 : 2
Priority of process4 : 4

GANTT CHART

------------------------------------------------------

| p1 | p3 | p4 | p2

------------------------------------------------------

0 8 14 15 28

Total WAITING TIME of the process = 37 Total TURNAROUND TIME of the process = 55 Average WAITING
TIME of the process = 9.250000 Average TURNAROUND TIME of the process = 13.750000
3. Write C programs to simulate the following File organizing techniques:
a. Single Level Directory

ALGORITHM

Step1: Start

Step2: read gd DETECT ,gm,count,I,j,mid,cir_x,fname[10][20]

Step3: initgraph(fgd,fgm,”c:\turboc)

Step4: setbkcolor(GREEN)

Step5: for i 0 to i< count then do

Cleardevice()

Setbkcolor(GREEN)

Setfillstyle(1,MAGENTA)

Step6: mid 640/count,cir_x mid/3

Step7: settextxy(2,0,4),settextjustify(1,1)

Step8: outtextxy(320,125,”Root Directory”)

Step9: setcolor(BLUE)

Step10: for j=0 to j<-I and cir_x=cir_x+mid

Do

Step11: line(320,150,cir_x,250)

Step12: Fillellipse(Cir_x,250,30,30)

Outtextxy(Cir_x,250,fname[j])

Step13: end
PROGRAM

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm,count,i,j,mid,cir_x;

char fname[10][20];

clrscr();

initgraph(&gd,&gm,"c:\tc");

cleardevice();

setbkcolor(GREEN);

puts("Nter no.of files u have");

scanf("%d",&count);

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

cleardevice();

setbkcolor(GREEN);

printf("Nter files %d name",i+1);

scanf("%s",fname[i]);

setfillstyle(1,MAGENTA);

mid=640/count;

cir_x=mid/3;

bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);

settextjustify(1,1);

outtextxy(320,125,"ROOT DIRECTORY");

setcolor(BLUE);

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

cir_x+=mid;

line(320,150,cir_x,250);

fillellipse(cir_x,250,30,30);

outtextxy(cir_x,250,fname[j]);

getch();

OUTPUT:

Enter No.of files u have 4

Enter file1 name A

Enter file1 name B

Enter file1 name C

Enter file1 name D


Two Level Directory

ALGORITHM

Step1: Start

Step2: read gd DETECT ,gm,count,I,j,mid,cir_x,fname[10][20]

Step3: initgraph(fgd,fgm,”c:\turboc)

Step4: setbkcolor(GREEN)

Step5: for i 0 to i< count then do

Cleardevice()

Setbkcolor(GREEN)

Setfillstyle(1,MAGENTA)

Step6: mid 640/count,cir_x mid/3

Step7: settextxy(2,0,4),settextjustify(1,1)

Step8: outtextxy(320,125,”Root Directory”)

Step9: setcolor(BLUE)

Step10: for j=0 to j<-I and cir_x=cir_x+mid

Do

Step11: line(320,150,cir_x,250)

Step12: Fillellipse(Cir_x,250,30,30)

Outtextxy(Cir_x,250,fname[j])

Step13: end
PROGRAM

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

struct tree_element

char name[20];

int x,y,ftype,lx,rx,nc,level;

struct tree_element*link[5];

};

typedef struct tree_element node;

void main()

int gd=DETECT,gm;

node *root;

root=NULL;

clrscr();

create(&root,0,"NULL",0,639,320);

clrscr();

initgraph(&gd,&gm,"c:\tc");

diplay(root);

getch();

closegraph();

create(node **root,int len,char *dname,int lx,int rx,int x)


{

int i,gap;

if(*root==NULL)

(*root)=(node*)malloc(sizeof(node));

printf("Enter name of the dir/file(under%s):",dname);

fflush(stdin);

gets((*root)->name);

if(len==0||len==1)

(*root)->ftype=1;

else

(*root)->ftype=2;

(*root)->level=len;

(*root)->y=50+len*50;

(*root)->x=x;

(*root)->lx=lx;

(*root)->rx=rx;

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

(*root)->link[i]=NULL;

if((*root)->ftype==1)

if(len==0||len==1)

if((*root)->level==0)

printf("How many Users");


else

printf("(for %s):",(*root)->name);

scanf("%d",&(*root)->nc);

else

(*root)->nc=0;

if((*root)->nc==0)

gap=rx-lx;

else

gap=(rx-lx)/(*root)->nc;

for(i=0;i<(*root)->nc;i++)

create(&(*root)->link[i],len+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap);

else

(*root)->nc=0;

display(node **root)

int i;

settextstyle(2,0,4);

settextjustify(1,1);

setfillstyle(1,BLUE);

setcolor(14);

if(root!=NULL)

{
for(i=0;i<root->nc;i++)

line(root->x;root->y,root->link[i]->x,root->link[i]->y);

if(root->ftype==1)

bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);

else

fillstyle(root->x,root->y,20,20);

outtextxy(root->x,root->y,root->name);

for(i=0;i<root->nc;i++)

display(root->link[i]);

}}

getch();

OUTPUT:

Enter name of dir/file(Under NULL):ROOT

How many Users (for ROOT): 3

Enter name of dir/file(Under ROOT): user1

How many files:(for user1): 2

Enter name of dir/file(Under user1): A

Enter name of dir/file(Under user1): B

Enter name of dir/file(Under ROOT): user2

How many files:(for user2): 1


Enter name of dir/file(Under user2): D

Enter name of dir/file(Under ROOT): user3

How many files:(for user3): 2

Enter name of dir/file(Under user3): E

Enter name of dir/file(Under user3): F


Hierarchical
4. Write C programs to simulate the following File allocation methods:

Contiguous
b) Linked
c) Indexed
5. Write a C Program to copy contents of one file to another using system calls.

#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
int main(int args,char *ar[])
{
char *source=ar[1];
char *dest="def.txt";
char *buf=(char *)malloc(sizeof(char)*120);
int fd1,fd2;
fd1=open(source,O_CREAT,0744);
fd2=open(dest,O_CREAT,0744);
while(read(fd1,buf,120)!=-1)
{
printf("%s",buf);
//printf("Processing\n");
write(fd2,buf,120);
}
printf("Process Done");
close(fd1);
close(fd2);
}
6. Write a C Program to simulate Bankers Algorithm for DeadLock Avoidance

#include<stdio.h>

Void main()

int n,i,j,c,p[20],av[20],max[20][20],all[20][20],all1[20][20],

int need[20][20];

printf(“\n Enter the number of process:”);

scanf(“%d”,&n);

printf(“\n Enter the process nos:”);

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

scanf(“%d”,&p[i]);

printf(“\n Enter the instances “);

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

scanf(“%d”,&av[i]);

printf(“\n Enter max of each :”);

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

for(j=0;j<3;j++)

scanf(“%d”,&max[i][j]);

}
printf(“\n”);

Printf(“\n Enter the allocation of each:”);

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

for(j=0;j<3;j++)

scanf(“%d”,&alli][j]);

printf(“\n”);

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

for(j=0;j<3;j++)

all1[i][j]=all[i][j];

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

for(j=0;j<3;j++)

need[i][j]=max[i][j]-all[i][j];

}
for(i=0;<3;i++);

c=0;

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

c=c+all[j][i];

av[i]=av[i]-c;

printf(“\n NEED MATRIX”);

printf(“\n A \t B \t C \n”);

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

for(j=0;j<3;j++)

printf(“%d \t”need[i][j]);

printf(“\n”);

Printf(“\n AVAILABLE”);

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

printf(“%d\t”av[j]);

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

for(j=0;j<3;j++)

if(need[i][j]<=av[j])

all[i][j]=all1[i][j]+need[i][j];

av[j]=all1[i][j]+av[j];

else

printf(“\n Process not allowed”);

exit(0);

printf(“\n***SUCCESS***);

printf(“\n***ALLOCATED MATRIX***);

printf(“\n A \t B \t C \n);

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

for(j=0;j<3;j++)

Printf(“%d \t”,all[i][j])

printf(“\n”);
}

printf(“\n INSTANCE AT LAST”);

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

Printf(“\n %d”,av[j]);

Output:

Enter the no of process: 5

Enter the process : p1 p1 p3 p4 p5

Enter the allocation: 010 200 302 211 002

Enter the max : 753 322 902 222 433

Enter available : 332

Safe Sequence :

p2 p4 p5 p1 p3

Need matrix :

743

122

600

011

431
7. Write a C Program to simulate Banker’s Algorithm For Dead Lock Prevention

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

int pno,rno,i,j,k,op,p,re,n,max[10][10],curr[10][10];

int req[10][10],av[10],r[10],a;

clrscr();

printf("ENter no.of processes");

scanf("%d",&pno);

printf("Enter no of resourses");

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

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

av[i]=r[i];

printf("Enter the %d to %d moreallocation matrix",pno,rno);

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

for(j=0;j<pno;j++)

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

printf("\n Nter the %d %d current matrix \n",pno,rno);

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

for(j=0;j<rno;j++)
scanf("%d",&curr[i][j]);

printf("The need matrix is \n");

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

for(j=0;j<rno;j++)

req[i][j]=max[i][j]-curr[i][j];

printf("%d",req[i][j]);

printf("\n");

value=safe(pno,rno);

if(value==0)

printf("\n\n it is not safe in beginning");

else

printf("\n\n it is safe in the beginning");

getch();

int safe(int n,int k)

int rest[10],currav[10],temp[10],pos=0;

int i,j,p,count=0,possible=1;

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

rest[i]=1;

for(i=0;i<k;i++)
currav[i]=av[i];

while(possible)

j=0;

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

if(rest[i]!=0)

for(p=0;p<k;p++)

if(req[i][p]<=currav[p])

currav[p]=currav[p]+max[i][p];

else

break;

if(p==k)

rest[i]=0;

j++;

if(rest[i]==0)

count++;

temp[pos++]=i;

}}
}

if(count==n)

possible=0;

if(j==0)

possible=0;

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

if(rest[i]!=0)

return(0);

printf("\n Resourses r allocated in this Order:\n \n");

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

printf("P %d \t", temp[i]);

return(1);

OUTPUT:

ENter no.of processes2

Enter no of resourses2

Nter the no of resourses1 2

Enter the 2 to 2 moreallocation matrix1 2

21

enter the 2 2 current matrix


12

21

The need matrix is

00

00

Resourses r allocated in this Order:

P0 P1

It is safe in the beginning


8. Write CProgram to simulate the following page replacement algorithms:

FIFO

ALGORITHM

Step1: Start
Step2: Global Declaration fifo(),t[5],pgf,n,a[20],I,j,frm
Step3: pgf fifo()
Step4: Read pos,flag,i 0
Step5: while i<n do
Step6: for pos=0 to pos<frm & i<n do
Step7: for j=0 to j<pos or j<frm do
Step8: if a[i]=t[j] then
Flag 1, break
Step9: if flag=1 then continue
Step10: t [pos] a[i]
Step11: for j 0 to j<frm do
Print t[j]
Step12: pgf pgf+1,pos pos+1
Step13: return pgf to Step3
Step14: end
PROGRAM

#include<stdio.h>

#include<conio.h>

int t[3],pgf=0,n,a[20],i,j,frm;

void main()

clrscr();
printf("Nter the no.of elements in reference string");

scanf("%d",&n);

printf("Nter the reference String");

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

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

printf("Nter the No of frames");

scanf("%d",&frm);

pgf=fifo();

printf("No of page fault is %d",pgf);

getch();

int fifo()

int pos,flag;

i=0;

while(i<n)

for(pos=0;pos<frm && i<n;i++)

if(a[i]==t[j])

flag=1;

break;

if(flag==1)
continue;

t[pos]=a[i];

printf("\n");

for(j=0;j<frm;j++)

printf("%d \t",t[j]);

printf("\n");

pgf++;

pos++;

return(pgf);

OUTPUT:

Nter the no.of elements in reference string3

Nter the reference String1 2 4

Nter the No of frames3

1 0 0

1 2 0

1 2 4

No of page fault is 3
Optimal Page Replacement

ALGORITHM

Step1: Start

Step2: Read a[20],f[20],pos,max[10],p,pfault 0,l,I,k,m,j,prea 0,n,size

Step3: for i 1 ,pos 0 to pos<size & i<n do

If pos>=1 then

For m 1 to m<=pos do

If a[i]=f[m] then

Pres 1

Break;

Step4: if pres=0 then

F[++pos]=a[i],pfault pfault+1

Step5: for k 1 to k<=pos do

Print f[k]

Step6: for j 1 to j<=size-pos do

Print * and print go to new line

Step7: pres 0

Step8: for i o to i<=n do

Frm 1 to m<= size do

If a[i]=f[m]

Pres ,break

Step9: if pres=0 do

For j=1 to j<= size do

For k<-i+1 to k<=n do

If f[j]=a[k] then
If max[j]=999 then

Max[j] k

Break

Step10: l max[1],pos 1

Step11: for p 2 to p<=size do

If l<max[p] then

Pos p

L max[p]

Step12: f[pos] a[i]

Step13: for k 1 to k<=size do

Max[k] 999

Step14: pfault=pfault+1

Step15: pres 0

Step16: Print number of page faults

Step17: end

PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

int a[20],f[20],pos,max[20],p,pfault=0,l,i,k,m,j,pres=0,n,size;

clrscr();

printf("Nter the no.of Elements");

scanf("d",&n);

for(i=1;i<=n;i++)
{

printf("Nter %d Element",i);

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

max[i]=999;

printf("Nter the frame size");

scanf("%d",&size);

printf("\n****Optimal Policy****\n");

for(i=1,pos=0;pos<size,i<=n;i++)

if(pos>=1)

for(m=1;m<=pos;m++)

if(a[i]==f[m])

pres=1;

break;

if(pres==0)

f[++pos]=a[i];

++pfault;
for(k=1;k<=pos;k++)

printf("%d",f[k]);

for(j=1;j<=size-pos;j++)

printf("\t");

printf("\n");

pres=0;

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

for(m=1;m<=size;m++)

if(a[i]==f[m])

pres=1;

break;

if(pres==0)

for(j=1;j<=size;j++)

for(k=i+1;k<=n;k++)

if(f[j]==a[k])
{

if(max[j]==999)

max[j]=k;

break;

l=max[i];

pos=1;

for(p=2;p<=size;p++)

if(l<max[p])

pos=p;

l=max[p];

f[pos]=a[i];

for(k=1;k<=size;k++)

printf("%d",f[k]);

max[k]=999;

++pfault;

printf("\n");
}

pres=0;

printf("\n No of Page faults are %d",pfault);

getch();

OUTPUT:

Nter the no.of Elements10

Nter 1 Element2

Nter 2 Element3

Nter 3 Element4

Nter 4 Element6

Nter 5 Element6

Nter 6 Element5

Nter 7 Element4

Nter 8 Element3

Nter 9 Element3

Nter 10 Element7

Nter the frame size3

****Optimal Policy****
2
23
234
634
534
734

No of Page faults are 6


LRU

ALGORITHM

Step1: start

Step2: read t[3],pgf 0,n,a[20],I,j,frm[10],nfm,k,min[10]

Step3: for i=0 to i<nfm do

Min[i]=-1

Step4: pgf lru()

Step5: pos -1,pres 0,m,l,p

Step6: for i 0,pos 0 to pos nfm & i<n do

Step7: if pos>=1 then

For m 0 to m<pos do

If a[i]=frm[m] then

Pres 1,break

Step8: if pres=0 then

Frm[pos++] a[i],pg++

Step9: for k 0 to k<nfm do

Print frm[k] pres 0

Step10: for i<n do

For m 0 to m<nfm do

Step11: if a[i]=frm[m] then

Pres 1 break

Step12: if pres=0 then

For j=0 to j<nfm do

For k=0 to k<I do

If frm[j]=a[k] then
Min[j]=k

Pos=0

L=min[0]

Step13: for p 1 to p<nfm do

If(min<p3<1) then

L=min[p],pos=p

Step14: frm[pos] a[i]

Step15: for k 0 to k<nfm do

Min[k] -1

Step16: pgf pgf+1

Step17: pres 0

Step18: return pgf

Step19: end

PROGRAM

#include<stdio.h>

#include<conio.h>

int t[3],pgf=0,n,a[20],i,j,frm[10],nfm,k;

int min[10];

void main()

printf("Nter the no of elements in reference string");

scanf("%d",&n);

printf("enter the reference string");

for

scanf("%d",&a[i]);
printf("Nternthe no of frames");

scanf("%d",&nfm);

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

min[i]=-1;

pgf(lru();

printf("No og page faults is %d ",pgf);

getch();

int lru()

int po=-1,pres=0;m,l,p;

for(i=0,pos=0;pos<nfm ,i<n;i++)

if(pos>=1)

for(m=0;m<pos;m++)

if(a[i]==frm[m])

pres=1;

break;

if(pres==0)

frm[pos++]=a[i];
pgf+

+;

for"(k=0;k<nfm;k++)

printf("%d",frm[k]);

printf("\n");

pres=0;

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

for(m=0;m<nfm;m++)

if(a[i]==frm[m])

pres=1;

break;

if(pres==0)

for(j=0;j<nfm;j++)

for(k=0;k<1;k++)

if(frm[j]==a[k])

min[j]=k;

pos=0;
l=min[0];

for(p=1;p<nfm;p++)

if(mi9n[p]<l)

l=min[p];

pos=p;

frm[pos]=a[i];

for(k=0;k<nfm;k++)

printf("%d",frm[k]);

min[k]=-1;

pgf++;

printf?("\n");

pres=0;

return pgf;

}
OUTPUT:

Enter the no of Elements in reference string 10

Enter the reference string 1 2 3 4 1 2 5 6 4 5

Enter the No. of Frames 4

1 0 0 0

1 2 0 0

1 2 3 0

1 2 3 4

1 2 5 4

1 2 5 6

4 2 5 6

No of page faults is : 7
9. Write C Program to implement the following techniques of memory management:

Paging
10. Write a C Program to implement the ls | sort command.( Use unnamed pipe)

#include <unistd.h>

#include <stdio.h>

#include <errno.h>

int main() {

int ret_val;

int pfd[2];

char buff[32];

char string1[]="String for pipe I/O";

ret_val = pipe(pfd); /* Create pipe */

if (ret_val != 0) { /* Test for success */

printf("Unable to create a pipe; errno=%d\n",errno);

exit(1); /* Print error message and exit */

if (fork() == 0) {

/* child program */

close(pfd[0]); /* close the read end */

ret_val = write(pfd[1],string1,strlen(string1)); /*Write to pipe*/

if (ret_val != strlen(string1)) {

printf("Write did not return expected value\n");

exit(2); /* Print error message and exit */


}

else {

/* parent program */

close(pfd[1]); /* close the write end of pipe */

ret_val = read(pfd[0],buff,strlen(string1)); /* Read from pipe */

if (ret_val != strlen(string1)) {

printf("Read did not return expected value\n");

exit(3); /* Print error message and exit */

printf("parent read %s from the child program\n",buff);

exit(0);

}
11. Write a C Program to solve the Dining-philosopher problem using semaphores
12. Write a C Program to implement ipc between two unrelated processes using named pipe.

#include<stdio.h>

#include<stdlib.h>

#include<errno.h>

#include<unistd.h>

int main()

int pfds[2];

char buf[30];

if(pipe(pfds==-1)

Perror(“pipe”);

exit(1);

Printf(“writing to file descriptor #%d\n”, pfds[1]);

Write(pfds[1],”test”,5);

Printf(“reading from file descriptor #%d\n”, pfds[0]);

Read(pfds[0],buf,5);

Printf(“read\”%s\”’\n”,buf);

Output:
[student @gcet~]$ vi pipes1.c
[student @gcet~]$ cc pipes1.c
[student @gcet~]$ ./a.out

writing to file descriptor #4

reading from file descriptor #3

read”test”

You might also like