summaryrefslogtreecommitdiffstats
path: root/pipe.c
blob: a5c86ccd408d6ee710044c2e174c8609806d2b0f (plain)
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE    80
#define PIPE_STDIN  0
#define PIPE_STDOUT 1

int main(int argc, char **argv)
{
    const char *string = {"A sample message."};
    int ret, myPipe[2];
    char buffer[MAX_LINE+1];

    /* create the pipe */
    ret = pipe(myPipe);

    if (ret == 0)
    {
        /* write message into the pipe */
        write(myPipe[PIPE_STDOUT], string, strlen(string));

        /* read the message from the pipe */
        ret = read(myPipe[PIPE_STDIN], buffer, MAX_LINE);

        /* null terminate the string */
        buffer[ret] = 0;

        printf("%s\n", buffer);
    }
    else
    {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    return 0;
}