You are on page 1of 7

4A_19F0217_LAB8

Interprocess Communication

19F0217
M SHAHID IMRAN
BSCS
Question 1:
Code:
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

using namespace std;

int main()
{
int fd[2],fd1[2];//creating two files directories one for input read and
second to store answer
int status;
pipe(fd);//creating pipes
pipe(fd1);
int num1=0,num2=0;
cout<<"\n\n Enter the first Number 1: ";cin>>num1;
cout<<"\n\n Enter the first Number 2: ";cin>>num2;
write(fd[1],&num1,sizeof(num1));//writing to file
write(fd[1],&num2,sizeof(num2));
if(fork()==0)//creating first child of parent
{
cout<<"\n\n I'm first Child doing addition and subtraction!";
int temp1,temp2;
read(fd[0],&temp1,sizeof(temp1));
read(fd[0],&temp2,sizeof(temp2));
int temp3=temp1+temp2;
write(fd1[1],&temp3,sizeof(temp3));
temp3=temp1-temp2;
write(fd1[1],&temp3,sizeof(temp3));
}
else if(wait(&status))//parent waits for first child to complete
execution
{
cout<<"\n\n Enter the second Number 1: ";cin>>num1;
cout<<"\n\n Enter the second Number 2: ";cin>>num2;
write(fd[1],&num1,sizeof(num1));
write(fd[1],&num2,sizeof(num2));
if(fork()==0)//creating second child
{
cout<<"\n\n I'm second Child doing multiplication and
division!\n";
int temp1,temp2;
read(fd[0],&temp1,sizeof(temp1));
read(fd[0],&temp2,sizeof(temp2));
int temp3=temp1*temp2;
write(fd1[1],&temp3,sizeof(temp3));
temp3=temp1/temp2;
write(fd1[1],&temp3,sizeof(temp3));
if(fork()==0)//creating third child of a child
{
cout<<"\n\n I'm Child of Parent's Child showing
Results!";
read(fd1[0],&num1,sizeof(num1));
cout<<"\n\n The Addition of first two number is:
"<<num1;
read(fd1[0],&num1,sizeof(num1));
cout<<"\n\n The Subtraction of first two number is:
"<<num1;
read(fd1[0],&num1,sizeof(num1));
cout<<"\n\n The Multiplication of second two
number is: "<<num1;
read(fd1[0],&num1,sizeof(num1));
cout<<"\n\n The division of second two number is:
"<<num1<<endl<<endl;
}
else
wait(&status);//parent to wait
}
else
wait(&status);//parent to wait
}
return 0;
}

Output:
Question 2:
Code:
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

using namespace std;

int main()
{
int fd[2];
int status;
pipe(fd);
char message[80]="I'm Parent I have sent message to Child!";
char read_message[80];
if(fork()!=0)
{
write(fd[1],message,80);
cout<<"\n\n "<<message<<endl;
close(fd[1]);
}
else
{
read(fd[0],read_message,80);
cout<<"\n\n I have received this message from parent:\n\n
"<<read_message<<endl<<endl;
close(fd[0]);
}
return 0;
}
Output:
Question 3:
Code: file1
#include <iostream>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

int main()
{
char filepath[80]="/tmp/myfifo";
mkfifo(filepath,0666);
char write_message[80],read_message[80];
int fd;

while(1)
{
cin.getline(write_message,80);
fd = open(filepath,O_WRONLY);
write(fd,write_message,80);
close(fd);

fd = open(filepath,O_RDONLY);
read(fd,read_message,80);
cout<<"\n USER 2: "<<read_message<<endl;
close(fd);
}
return 0;
}
Code: file 2
#include <iostream>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

int main()
{
char filepath[80]="/tmp/myfifo";
mkfifo(filepath,0666);
char write_message1[80],read_message1[80];
int fd1;

while(1)
{
fd1=open(filepath,O_RDONLY);
read(fd1,read_message1,80);
cout<<"\n USER 1: "<<read_message1<<endl;
close(fd1);

cin.getline(write_message1,80);
fd1=open(filepath,O_WRONLY);
write(fd1,write_message1,80);
close(fd1);
}
return 0;
}
Output:

You might also like