-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipes.c
More file actions
44 lines (38 loc) · 654 Bytes
/
pipes.c
File metadata and controls
44 lines (38 loc) · 654 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
int main(void)
{
pid_t pid;
int fd[2];
char buffer[80];
char my_string[] = "Hello there!\n";
pipe(fd);
pid = fork();
if(pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid == 0)
{
/* Child Process */
close(fd[0]);
write(fd[1], my_string, (strlen(my_string) + 1));
exit(EXIT_SUCCESS);
}
else
{
/* Parent Process */
close(fd[1]);
while(read(fd[0], buffer, sizeof(buffer)) > 0)
{
printf("Received String: %s", buffer);
}
}
return EXIT_SUCCESS;
}