-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab52-1.c
67 lines (56 loc) · 1.3 KB
/
lab52-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#define FILE1 "myfile1.txt"
#define FILE2 "myfile2.txt"
int main()
{
int num1,num2,fd1,fd2;
pid_t p=fork();
if(p==0)
{
mkfifo(FILE1,0666);
printf("created\n");
fd1=open(FILE1,O_RDONLY);
printf("created pipe and ready\n");
char s1[300];
while(1)
{
if((num1=read(fd1,s1,300))==-1)
perror("read");
else
{
s1[num1]='\0';
printf("Read:");
puts(s1);
if(s1[0]=='b' && s1[1]=='y' && s1[2]=='e' && s1[3]=='\0')
break;
}
}
}
else
{
mkfifo(FILE2,0666);
printf("created\n");
fd2=open(FILE2,O_WRONLY);
printf("created pipe and ready\n");
char s2[300];
while(1)
{
gets(s2);
if((num2=write(fd2,s2,strlen(s2)))==-1)
perror("write");
else
printf("Sent\n");
if(s2[0]=='b' && s2[1]=='y' && s2[2]=='e' && s2[3]=='\0')
{ break;
}
}
}
return 0;
}