summaryrefslogtreecommitdiffstats
path: root/pipe.c
diff options
context:
space:
mode:
Diffstat (limited to 'pipe.c')
-rw-r--r--pipe.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/pipe.c b/pipe.c
new file mode 100644
index 0000000..a5c86cc
--- /dev/null
+++ b/pipe.c
@@ -0,0 +1,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;
+}
+