這個程序的目標是分叉並讓子進程休眠,同時父進程無限等待中斷。當我點擊^ C時,它會調用void parent
函數。然而,這部分工作,從kill (pid, SIGALRM)
消息不起作用。我檢查了並且pid
是孩子正確的進程ID。C++進程fork和sigalarm
我搜索了一段時間,但我還沒有找到我做錯了什麼。我之前使用的kill (pid, SIGALRM)
從子進程到父,但我想不通這是爲什麼不工作..
#include <signal.h>
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int pid;
void parent (int sig)
{
kill (pid, SIGALRM);
cout << "I'm a parent " << getpid() << " My child is " << pid << endl;
}
void child (int sig)
{
cout << "I am " << getpid() << "my parent is " << getppid()<< endl;
cout << "Use ctrl+backslash to actually end the program" << endl;
}
int main()
{
pid = fork();
if(pid == 0)
{ //Child process
cout << "Child pid = " << getpid() << " Waiting for interrupt." << endl;
(void) signal (SIGALRM, child);
pause();
}
else if(pid > 0)
{ //Parent
sleep(2);
cout << "child pid = " << pid << endl;
struct sigaction act;
act.sa_handler = parent;
sigemptyset (&act.sa_mask);
sigaction (SIGINT, &act, 0);
while(1)
{
sleep (1);
}
}
return 0;
}
什麼是輸出,你在做什麼操作系統?此外,在這裏迂腐,那些信號處理程序應該用C鏈接來定義。 (即把它們放在extern「C」塊內) – Arafangion 2012-02-15 07:09:57
另外,你如何知道iostreams在信號處理程序中是否安全?我想知道如果這是http://stackoverflow.com/questions/7623401/override-ctrl-c重複(但即使是這樣,我認爲這個問題值得保持原樣) – Arafangion 2012-02-15 07:12:14
我使用Fedora 15 。 我得到的輸出是: '兒童的pid = 23779等待interrupt.' '孩子的pid = 23779' '^ CI'm父23778我的孩子23779' '^ CI'm父23778我的孩子是23779'''' \退出(核心傾銷)' 並感謝您的外部C塊,我從來沒有使用過之前 – OrangeGrover 2012-02-15 07:19:10