-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo3.c
More file actions
57 lines (50 loc) · 1.04 KB
/
fifo3.c
File metadata and controls
57 lines (50 loc) · 1.04 KB
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 *10)
int main(int argc, char *argv[])
{
int pipe_fd;
int res;
int open_mode =O_WRONLY;
int i;
int bytes_sent =0;
char buffer[BUFFER_SIZE+1];
if(access(FIFO_NAME,F_OK)==-1)
{
res=mkfifo(FIFO_NAME,0777);
if(res!=0)
{
fprintf(stderr,"Could not create fifo %s\n",FIFO_NAME);
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO O_WRONLY\n",getpid());
pipe_fd = open(FIFO_NAME,open_mode);
printf("Process %d result %d\n",getpid(),pipe_fd);
if(pipe_fd !=-1){
while(bytes_sent < TEN_MEG)
{
res = write(pipe_fd,buffer,BUFFER_SIZE);
if(res ==-1)
{
fprintf(stderr,"Write error on pipr\n");
exit(EXIT_FAILURE);
}
bytes_sent +=res;
}
(void)close(pipe_fd);
}
else {
exit(EXIT_FAILURE);
}
printf("Process %d finished\n",getpid());
exit(EXIT_SUCCESS);
}