C getpid(),WIFSHSTOPPED(状态),用于检查子进程睡眠

C getpid(),WIFSHSTOPPED(状态),用于检查子进程睡眠,c,linux,fork,waitpid,C,Linux,Fork,Waitpid,您好,我正在使用3个进程P0、P1、P2制作程序 我想做一个像下面这样的循环 init: P0 P1,P2 paused, signal to P0 P0 : wake up by signal -> finish job -> signal to P3 -> pause P1 : wake up by signal -> finish job -> signal to P2 -> pause P2 : wake up by signal -> fini

您好,我正在使用3个进程P0、P1、P2制作程序 我想做一个像下面这样的循环

init: P0 P1,P2 paused, signal to P0
P0 : wake up by signal -> finish job -> signal to P3 -> pause
P1 : wake up by signal -> finish job -> signal to P2 -> pause
P2 : wake up by signal -> finish job -> signal to P3 -> pause
repeat 0~2
不幸的是,有时信号出现在目标进程睡眠之前

所以我想检查目标进程是否暂停。 我像这样插入[检查代码]

//end job here here
//
int status = 0;
do {
    waitpid(target_pid, &status, WNOHANG); // WNOHANG : check child status without waiting
} while ( ! WIFSTOPPED(status));           // linux man said that WIFSTOPPED
                                           // return true if process stopped
//signal to target_pid and pause() here
但它不起作用

如果我使用

 while( ! WIFSTOPPED(status))
程序100%无法脱离循环

如果我使用

 while(   WIFSTOPPED(status))
它的工作原理与之前一样,我将[检查代码]

我知道waitpid函数用于检查进程结束


但是不能waitpid()只用于检查暂停与否吗?

如果
子项
正常完成
则可以使用
WIFEXITED(status)
,如果子项正常退出则为真

如果子进程被其他进程或同一进程的任何
信号
杀死(使用来自其他终端的kill命令)或挂起,则可以使用
WIFSIGNALED(status)

根据您的要求,我将您的代码修改为

int main()
{

        if(fork()==0) // p1
        {
                int t;
                srand(getpid());
                t = rand()%10 +1;//generating delay b/w 1 to 10 seconds
                printf("child1 delay : [%d] pid : [%d] ppid : [%d] \n",t,getpid(),getppid());
                sleep(t);
                printf("child1 pid : [%d] and its ppid : [%d] exiting \n",getpid(),getppid());
                exit(0);
        }
        else
        {
                if(fork()==0) // p2
                {
                        int t;
                        srand(getpid());
                        t = rand()%10 +1;//generating delay b/w 1 to 10 seconds
                        printf("child2 delay : [%d] pid : [%d] ppid : [%d] \n",t,getpid(),getppid());
                        sleep(t);
                        printf("child2 pid : [%d] and its ppid : [%d] exiting \n",getpid(),getppid());
                        exit(1);
                }
                else // p3
                {
                        int status;
                        printf("In parent...[%d]\n",getpid());
                        int ret;
                        while((ret = waitpid(-1,&status,0))!=-1)//when there is no child left it returns -1
                        {
                                if(WIFEXITED(&status))//if child is completed normally after sleep is over
                                {
                                        printf("child [%d] terminates normally : %d\n",ret,WEXITSTATUS(status));
                                }
                                if(WIFSIGNALED(&status))//true if child was killed by any signal from other process or same process
                                {
                                        printf("child [%d] terminates normally : %d\n",ret,WTERMSIG(status));//it will print the signal no by which it was killed
                                }
                        }
                }
        }
        return 0;
}
不需要
pause()
,因为当一个进程被阻塞时,您应该使用
pause()
,直到它没有收到来自其他进程的信号为止


请浏览
waitpid()

第三个作业实例的手册页:(这不是它的工作方式。我建议您检查它返回的内容。您是指WIFSTOPPED()的“检查停止”)与我所说的不同?你认为进程停止意味着什么?你不完整的程序片段没有给我们很多线索,但你应该知道
pause()
sleep()
都与此无关。“停止”意味着“暂停”。
waitpid()
(不与
ptrace
d进程一起使用时)需要
WUNTRACED
接收
WIFSTOPPED
通知。感谢您的帮助。现在我意识到我使用了错误的函数。waitpid用于检测更关键的问题,而不是暂停()我应该使用其他方法,如proc/pid/status