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
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <sys/wait.h> #include <sys/stat.h>
int main(int argc, char *argv[]) { printf("hello world (pid:%d)\n", (int)getpid()); int rc = fork(); if (rc < 0) { fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { close(STDOUT_FILENO); printf("hello, I am child(pid:%d)\n", (int)getpid()); open("p4.output", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
char *myargs[3]; myargs[0] = strdup("wc"); myargs[1] = strdup("p3.c"); myargs[2] = NULL; execvp(myargs[0], myargs); printf("this shouldn't print out"); } else { int wc = wait(NULL); printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid()); } return 0; }
|