C PROGRAM FOR INTER-PROCESS COMMUNICATION USING SINGLE PIPE (WITH ALGORITHM)
AIM : TO IMPLEMENT INTERPROCESS COMMUNICATION USING SINGLE PIPE
ALGORITHM :
STEP 1 : START
STEP 2 : DECLARE P[2],PID,BUFF
STEP 3 : CALL THE SYSTEM CALL PIPE(P)
STEP 4 : CREATE A PROCESS
STEP 5 : IF THE PROCESS IS CHILD THEN GO TO STEP 6
STEP 6 : READ THE MESSAGE FOR PARENT
STEP 7 : WRITE THE MESSAGE TO PARENT
STEP 8 : CALL SLEEP()
STEP 9 : READ THE MESSAGE FROM PARENT
STEP 10 : PRINT THE MESSAGE FROM PARENT
STEP 11 : IF THE PROCESS IS PARENT, GO TO STEP 12
STEP 12 : READ THE MESSAGE FROM CHILD
STEP 13 : DISPLAY THE MESSAGE FROM CHILD
STEP 14 : READ THE MESSAGE FROM CHILD
STEP 15 : WRITE THE MESSAGE TO CHILD
STEP 16 : STOP
PROGRAM :
#include<stdio.h>
#include<string.h>
void main()
{
int pid,p[2];
char buff[20];
pipe(p);
pid=fork();
if(pid==0)
{
printf("\n child process \n");
printf("\n enter the message to parent : \n");
gets(buff);
write(p[1],buff,sizeof(buff));
sleep(8);
printf("\n message from parent : \n");
read(p[0],buff,sizeof(buff));
puts(buff);
close(p[0]);
}
else
{
printf("\n parent process ");
read(p[0],buff,sizeof(buff));
printf("\n message from child : ");
puts(buff);
sleep(4);
printf("\n enter the message to child : ");
gets(buff);
write(p[1],buff,sizeof(buff));
sleep(2);
close(p[0]);
}
}
OUTPUT :
parent process
child process
enter the message to parent :
hai
message from child :
hai
enter the message to child :
hai
message from parent : hai
AIM : TO IMPLEMENT INTERPROCESS COMMUNICATION USING SINGLE PIPE
ALGORITHM :
STEP 1 : START
STEP 2 : DECLARE P[2],PID,BUFF
STEP 3 : CALL THE SYSTEM CALL PIPE(P)
STEP 4 : CREATE A PROCESS
STEP 5 : IF THE PROCESS IS CHILD THEN GO TO STEP 6
STEP 6 : READ THE MESSAGE FOR PARENT
STEP 7 : WRITE THE MESSAGE TO PARENT
STEP 8 : CALL SLEEP()
STEP 9 : READ THE MESSAGE FROM PARENT
STEP 10 : PRINT THE MESSAGE FROM PARENT
STEP 11 : IF THE PROCESS IS PARENT, GO TO STEP 12
STEP 12 : READ THE MESSAGE FROM CHILD
STEP 13 : DISPLAY THE MESSAGE FROM CHILD
STEP 14 : READ THE MESSAGE FROM CHILD
STEP 15 : WRITE THE MESSAGE TO CHILD
STEP 16 : STOP
PROGRAM :
#include<stdio.h>
#include<string.h>
void main()
{
int pid,p[2];
char buff[20];
pipe(p);
pid=fork();
if(pid==0)
{
printf("\n child process \n");
printf("\n enter the message to parent : \n");
gets(buff);
write(p[1],buff,sizeof(buff));
sleep(8);
printf("\n message from parent : \n");
read(p[0],buff,sizeof(buff));
puts(buff);
close(p[0]);
}
else
{
printf("\n parent process ");
read(p[0],buff,sizeof(buff));
printf("\n message from child : ");
puts(buff);
sleep(4);
printf("\n enter the message to child : ");
gets(buff);
write(p[1],buff,sizeof(buff));
sleep(2);
close(p[0]);
}
}
OUTPUT :
parent process
child process
enter the message to parent :
hai
message from child :
hai
enter the message to child :
hai
message from parent : hai
No comments:
Post a Comment