-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab61-1.c
97 lines (80 loc) · 2.01 KB
/
lab61-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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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 "file11.txt"
#define FILE2 "file21.txt"
#define FILE3 "file31.txt"
#define FILE4 "file41.txt"
int main()
{
int num1,num2,num3,num4,fd1,fd2,fd3,fd4;
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);
}
}
}
else
{
mkfifo(FILE2,0666);
printf("created2\n");
mkfifo(FILE3,0666);
printf("created3\n");
mkfifo(FILE4,0666);
printf("created4\n");
fd2=open(FILE2,O_WRONLY);
printf("created pipe and ready2\n");
fd3=open(FILE3,O_WRONLY);
printf("created pipe and ready3\n");
fd4=open(FILE4,O_WRONLY);
printf("created pipe and ready4\n");
char s2[300];
while(1)
{
gets(s2);
char s5[300];
s5[0]='u';
s5[1]=':';
s5[2]='1';
int i;
for(i=0;i<strlen(s2);++i)
s5[i+3]=s2[i];
if(((num2=write(fd2,s5,strlen(s5)))==-1))
perror("write");
else
printf("Sent2\n");
if((num3=write(fd3,s5,strlen(s5)))==-1)
perror("write");
else
printf("Sent3\n");
if((num4=write(fd4,s5,strlen(s5)))==-1)
perror("write");
else
printf("Sent4\n");
if(s2[0]=='b' && s2[1]=='y' && s2[2]=='e' && s2[3]=='\0')
{
break;
}
}
}
return 0;
}