summaryrefslogtreecommitdiffstats
path: root/fpipe.c
diff options
context:
space:
mode:
Diffstat (limited to 'fpipe.c')
-rw-r--r--fpipe.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/fpipe.c b/fpipe.c
new file mode 100644
index 0000000..4ec9866
--- /dev/null
+++ b/fpipe.c
@@ -0,0 +1,59 @@
+/* fpipe.c
+ *
+ * Parent writing to child
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <wait.h>
+#define MAX_LINE 80
+
+int main(int argc, char **argv)
+{
+ pid_t pid;
+ int stat_loc;
+
+ int pfds[2];
+ int ret;
+
+ char buf[MAX_LINE+1];
+ const char *testbuf = { "I'm a parent and I'm writing to you" };
+
+ if (pipe(pfds) == 0)
+ {
+ pid = fork();
+ if (pid == -1)
+ {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+
+ if (!pid)
+ {
+ close(pfds[1]); /* child will not be writing anything */
+ ret = read(pfds[0], buf, MAX_LINE);
+ buf[ret] = 0;
+ printf("Child read: %s\n", buf);
+ }
+ else
+ {
+ close(pfds[0]); /* parent will not be reading anything */
+ ret = write(pfds[1], testbuf, strlen(testbuf));
+ ret = waitpid(pid, &stat_loc, WCONTINUED);
+ }
+ }
+ else
+ {
+ perror("pipe");
+ exit(EXIT_FAILURE);
+ }
+
+ close(pfds[0]);
+ close(pfds[1]);
+
+ return 0;
+}
+