-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileserver_fifo.c
48 lines (47 loc) · 950 Bytes
/
fileserver_fifo.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
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include <string.h>
#define mode (S_IRUSR|S_IWUSR)
void client(int, int);
void server(int, int);
main(){
int readfd, writefd, id;
int fifo1,fifo2;
mkfifo("fifo1",mode);
mkfifo("fifo2",mode);
id=fork();
if(id==0){
readfd=open("fifo1",O_RDONLY,0);
writefd=open("fifo2",O_WRONLY,0);
server(readfd, writefd);
}
else{
writefd=open("fifo1",O_WRONLY,0);
readfd=open("fifo2",O_RDONLY,0);
client(readfd,writefd);
}
unlink ("fifo1");
unlink ("fifo2");
}
void client(int readfd, int writefd){
char buff[1024];
int n,len;
fgets(buff,1024,stdin);
len=strlen(buff);
len--;
write(writefd,buff,len);
while((n=read(readfd,buff,1024))>0)
write(1,buff,n);
}
void server(int readfd, int writefd){
int n;
char buff[1024];
int fd;
n=read(readfd,buff,1024);
buff[n]='\0';
fd=open(buff,O_RDONLY);
while((n=read(fd,buff,1024))>0)
write(writefd,buff,n);
}