intmain(void){ pid_t pid; printf("Hello from Parent Process, PID is %d.\n", getpid());
pid = fork(); //创建子进程
if (pid == 0) { sleep(1); for (int k = 0; k < 3; ++k) { printf("Hello from Child Process %d. %d times\n", getpid(), k + 1); } } elseif ( pid != -1) { tPrint("Parent process forked one child process--%d.\n", pid); tPrint("Parent process is waiting for child process to exit, \n"); waitpid(pid, NULL, 0); tPrint("Child Process has exited.\n"); tPrint("Parent process had exited.\n"); } else { tPrint("Everything was done without error.\n"); }
intmain(void){ pid_t pid = 0; printf("Hello from Parent Process, PID is %d.\n", getpid());
for (int i = 0; i < 2; ++i) { pid = fork(); if (pid != 0 && pid != -1) { tPrint("Parent process forked one child process--%d.\n", pid); tPrint("Parent process is waiting for child process to exit, \n"); } }
if (pid == 0) { sleep(1); printf("Hello from Child Process %d.\n", getpid()); } elseif ( pid != -1) { waitpid(pid, NULL, 0); tPrint("Child Process has exited.\n"); tPrint("Parent Process has exited.\n"); } else { tPrint("Everything was done without error.\n"); }
intmain(void){ pid_t pid = 0; printf("Hello from Parent Process, PID is %d.\n", getpid());
for (int i = 0; i < 2; ++i) { pid = fork(); if (pid != 0 && pid != -1) { tPrint("Parent process forked one child process--%d.\n", pid); tPrint("Parent process is waiting for child process to exit, \n"); } if (pid == 0 || pid == -1) break; }
if (pid == 0) { sleep(1); printf("Hello from Child Process %d.\n", getpid()); } elseif ( pid != -1) { waitpid(pid, NULL, 0); tPrint("Child Process has exited.\n"); tPrint("Parent Process has exited.\n"); } else { tPrint("Everything was done without error.\n"); }