File: pipe.c

package info (click to toggle)
lsof 4.99.4%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,924 kB
  • sloc: ansic: 50,680; sh: 8,351; makefile: 1,194; perl: 940; awk: 214
file content (41 lines) | stat: -rw-r--r-- 726 bytes parent folder | download
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
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    int no_close = 0;

    if (argc > 1 && strcmp(argv[1], "no-close") == 0)
        no_close = 1;

    int pd[2];

    if (pipe(pd) < 0) {
        perror("pipe");
        return 1;
    }

    pid_t self, child;

    self = getpid();
    child = fork();

    if (child == 0) {
        if (!no_close)
            close(pd[0]);
        pause();
        return 0;
    } else if (child < 0) {
        perror("fork");
        return 1;
    }

    if (!no_close)
        close(pd[1]);
    printf("%d %d %d %d\n", self, child, pd[0], pd[1]);
    fflush(stdout);
    wait(NULL);
    return 0;
}