#include <unistd.h> #include <stdio.h> #include <stdlib.h> // provide a declaration of exit() #include <err.h>
int main() { pid_t pid = fork(); if (pid == -1) err(EXIT_FAILURE, "fork() failed"); if (pid == 0) { // child process came here because fork() returns 0 for child process printf("I'm child! my pid is %d.\n", getpid()); exit(EXIT_SUCCESS); } else { // parent process came here because fork() returns the pid of newly created child process (> 1) printf("I'm parent! my pid is %d and the pid of my child is %d.\n", getpid(), pid); exit(EXIT_SUCCESS); } }
运行结果:
1 2 3
lhx@ubuntu:~/book-demo/how_linux_works/chapter_3$ ./fork I'm parent! my pid is 4015 and the pid of my child is 4016. I'm child! my pid is 4016.
#include <unistd.h> #include <stdio.h> #include <stdlib.h> // provide a declaration of exit() #include <err.h>
int main() { pid_t pid = fork(); if (pid == -1) err(EXIT_FAILURE, "fork() failed"); if (pid == 0) { // child process came here because fork() returns 0 for child process char *args[] = { "/bin/echo", "hello world!" , NULL}; printf("I'm child! my pid is %d.\n", getpid()); fflush(stdout); execve("/bin/echo", args, NULL); err(EXIT_FAILURE, "exec() failed"); } else { // parent process came here because fork() returns the pid of newly created child process (> 1) printf("I'm parent! my pid is %d and the pid of my child is %d.\n", getpid(), pid); exit(EXIT_SUCCESS); } }
运行结果:
1 2 3 4
lhx@ubuntu:~/book-demo/how_linux_works/chapter_3$ ./fork-and-exec I'm child! my pid is 4027. I'm parent! my pid is 4026 and the pid of my child is 4027. hello world!