私はlinuxとcのプロセスが初めてです。 (child_pid_or_zero> 0)親cのwait()の目的
何が起こるならば、私は待機を省略する
で()メソッドであれば
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, const char * argv[]) { pid_t child_pid_or_zero = fork(); //fork returns twice if(child_pid_or_zero < 0) { //if fork returns a number smaller than zero, something wrong happened perror("Something wrong happened\n"); exit(-1); } if(child_pid_or_zero > 0) { //if fork returns a number greater than zero, this is the parent process printf("I'm the parent, my pid is: %d\t My child pid is %d\n", getpid(), child_pid_or_zero); wait(NULL); } else { //this means that fork now returned 0, the child process is running printf("I am the child with pid: %d\t My parent pid is: %d\n",child_pid_or_zero, getppid()); } return 0; }
:私はこの簡単な例を使用しています ?私はこれを自分で試しました。明らかに、すぐに違いはありませんでした。私たちは常にwait()を使用する必要がありますか?これは、子供が重い計算などを行うことになっている場合にのみ適用されますか?
ありがとうございます。
https://en.wikipedia.org/wiki/Orphan_process –
http://stackoverflow.com/questions/23709888/how-to-use-wait-in-c – SMW
コンパイル時には、常にすべての警告を有効にし、それらの警告を修正してください。読みやすさと理解を容易にするために – user3629249