diff options
author | Kamil Kaminski <kyle@kkaminsk.com> | 2012-02-06 04:52:13 -0600 |
---|---|---|
committer | Kamil Kaminski <kyle@kkaminsk.com> | 2012-02-06 04:52:13 -0600 |
commit | d18964c5fe9d745eeb9b88b003f01c43dadcf018 (patch) | |
tree | e39e9bbbe3b76af21f72cbfbe5ee3d193ba17edb | |
parent | 1c411a3946dec2976aa3456b2a8b39b9f6d42a8f (diff) | |
download | sandbox-d18964c5fe9d745eeb9b88b003f01c43dadcf018.tar.gz sandbox-d18964c5fe9d745eeb9b88b003f01c43dadcf018.tar.bz2 sandbox-d18964c5fe9d745eeb9b88b003f01c43dadcf018.zip |
forgot a duped fd, add another pipe sample
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | dup.c | 8 | ||||
-rw-r--r-- | parent_to_child.c | 71 |
3 files changed, 80 insertions, 4 deletions
@@ -1,6 +1,7 @@ BINS = ascii class depipe_strings dup fpipe pipe realloc strpbrk strsep \ tokenizer getopt prime_mask linked_list pi_bbp string_tokenizer \ - tcpserver tcpclient regexp c99 asm playground_05-25-2011 + tcpserver tcpclient regexp c99 asm playground_05-25-2011 \ + parent_to_child CURL = curl_tomem curl_tofile @@ -25,4 +26,4 @@ $(CURL): %: %.c .PHONY: clean clean: - rm -f ./$(BINS) ./$(CURL) + @rm -f ./$(BINS) ./$(CURL) @@ -31,8 +31,10 @@ int main(int argc, char **argv) if (!pid) { - close(1); /* break the link to stdout */ + close(1); /* break the link to stdout, dup2 already kills this but meh */ dup2(pfds[1], 1); /* redirect stdout to pfds[1] */ + close(pfds[1]); /* new stdout has been setup above, pfds[1] can be closed since it's a dup */ + close(pfds[0]); /* child will not be reading anything from out pipe */ execlp("ls", "ls", "-1", NULL); @@ -40,7 +42,9 @@ int main(int argc, char **argv) else { close(0); /* break the link to stdin */ - dup2(pfds[0], 0); /* redirect stdout to pfds[0] */ + dup2(pfds[0], 0); /* redirect stdin to pfds[0] */ + close(pfds[0]); /* new stdin has been setup above, pfds[1] can be closed since it's a dup */ + close(pfds[1]); /* parent will not be writing anything into our pipe */ ret = waitpid(pid, &stat_loc, WCONTINUED); diff --git a/parent_to_child.c b/parent_to_child.c new file mode 100644 index 0000000..c0c0695 --- /dev/null +++ b/parent_to_child.c @@ -0,0 +1,71 @@ +/* dup.c + * + * Parent piping to a child where child spawns using new stdin + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <wait.h> + +extern char **environ; + +int main(int argc, char **argv) +{ + pid_t pid; + int stat_loc; + + char *str = "all base are belong to us"; + int pfds[2]; + int ret; + + if (pipe(pfds) == 0) + { + pid = fork(); + if (pid == -1) + { + perror("fork"); + exit(EXIT_FAILURE); + } + + if (pid) /* parent */ + { + close(1); /* break the link to stdout, dup2 already kills this but meh */ + dup2(pfds[1], 1); /* redirect stdout to pfds[1] */ + close(pfds[1]); /* new stdout has been setup above, pfds[1] can be closed */ + close(pfds[0]); /* parent will not be reading anything from out pipe */ + + printf("parent sends out: \"%s\"\n", str); + fflush(stdout); /* printf is buffered! */ + close(1); /* close stdin or else the child would not know that he's done reading */ + + ret = waitpid(pid, &stat_loc, WCONTINUED); + } + else /* child */ + { + close(0); /* break the link to stdin */ + dup2(pfds[0], 0); /* redirect stdin to pfds[0] */ + close(pfds[0]); /* new stdin has been setup above, pfds[1] can be closed */ + close(pfds[1]); /* parent will not be writing anything into our pipe */ + + /* here the child will use a redefined stdin */ + execlp("wc", "wc", NULL); + /* if there wasn't a spawn then we would need to close stdin */ + } + } + else + { + perror("pipe"); + exit(EXIT_FAILURE); + } + + ret = ret; /* hush the dumb compiler */ + + close(pfds[0]); + close(pfds[1]); + + return 0; +} + |