我無法理解以下程序的輸出。我觀察到在子進程返回後,父進程在wait()之前3秒沒有休眠。如果SIGCHLD設置爲默認處理程序,則它會休眠3秒鐘,等待並按預期返回。這裏究竟發生了什麼?當子進程終止時瞭解SIGCHLD
# include <unistd.h>
# include <sys/types.h>
# include <stdio.h>
# include <sys/wait.h>
# include <signal.h>
void handler(int sig) {
printf("Iam in handler ...\n");
}
main() {
int status;
pid_t pid;
struct sigaction act;
//act.sa_flags=SA_NOCLDSTOP;
act.sa_handler=handler;
sigaction(SIGCHLD,&act,NULL);
if(!fork()) {
printf("child process id is %d\n",getpid());
return 1;
}
printf("xxx ...\n");
sleep(3);
pid = wait(&status);
printf("process terminated is %d\n",pid);
}
output::
xxx ...
child process id is 2445
Iam in handler ...
process terminated is 2445
感謝您的回答。還有一個疑問 - 我從帖子中讀到,重新啓動系統調用取決於其實現定義,並且一些系統調用根本不會重新啓動。是這樣嗎?重新啓動系統調用是否與調用同一個調用兩次相同? – Goutham
@Goutham不一樣。把它想象成「試圖不返回'EINTR'的信號。是的,信號重啓主要取決於實現。 – cnicutar