下記のサイトに、以下のようなコードがあります。
https://hiroyukichishiro.com/process-in-c-language/
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main(void) { pid_t pid; int status; int ret; if ((pid = fork()) < 0) { perror("fork"); exit(1); } else if (pid == 0) { sleep(1); printf("Child Process: %d\n", pid); } else { printf("Parent Process: Child PID = %d\n", pid); if ((ret = wait(&status)) < 0) { perror("wait"); exit(2); } printf("Parent Process end\n"); } return 0; }
結果は
Parent Process: Child PID = 139
Child Process: 0
Parent Process end
となりました。
自分はサブプロセスについてまだ勉強不足なのでご教授頂きたいのですが、
このコードでなぜif(pid = fork()) < 0というif文について、
elseやelse ifのすべてが単に一回の実行で実行されるのでしょうか?(ループもしていないのに?)
よろしくお願いいたします。

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/05 04:20