测试用例℃,WIFSIGNALED,WIFSTOPPED,WIFCONTINUED [英] Test cases in C for WIFSIGNALED, WIFSTOPPED, WIFCONTINUED

查看:373
本文介绍了测试用例℃,WIFSIGNALED,WIFSTOPPED,WIFCONTINUED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和waitpid函数()和信号(打)和我在寻找可靠的测试案例返回WIFSIGNALED(状态)= WIFSTOPPED(状态)= WIFCONTINUED(状态)= true,但找不到任何..

介意告诉我怎样才能确保那些返回true,所以我可以调试我的code?

此外,什么信号应我的信号()来测试这些宏赶上将是有益的一些提示...


解决方案

 的#include<&errno.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / wait.h>
#包括LT&;&unistd.h中GT;#定义NELEMS(X)(sizeof的(X)/的sizeof(X)[0])静态无效testsignaled(无效){
   杀号(getpid(),SIGINT);
}静态无效teststopped(无效){
   杀号(getpid(),SIGSTOP);
}静态无效testcontinued(无效){
   杀号(getpid(),SIGSTOP);
   / *忙工作,以使我们从父之前等待退出。
    *这是一场比赛。
    * /
   报警(1);
   而(1){}
}诠释主要(无效){
   无效(*测试[])(无效)= {testsignaled,teststopped,testcontinued};
   将为pid_t PID [NELEMS(试验)];
   INT I,地位;
   对于(I = 0; I&下; sizeof的测试/ sizeof的测试[0] ++ⅰ){
      PID [I] =叉();
      如果(0 == PID由[i]){
         测试[I]();
         返回0;
      }
   }
   / *暂停让子进程做他们的事。
    *这是一场比赛。
    * /
   睡眠(1);
   / *遵守第三个过程的停止,继续吧。 * /
   wait4(PID [2],&放大器;状态,WUNTRACED,0);
   杀死(PID [2],SIGCONT);
   / *等待子进程。 * /
   对于(I = 0; I&下; NELEMS(测试)++ⅰ){
      wait4(PID由[i],&放大器;状态,WCONTINUED | WUNTRACED,0);
      ?的printf(%D%s%S%S \\ n,我,WIFCONTINUED(状态)续:,WIFSIGNALED(状态)?暗示:,WIFSTOPPED(状态)已停止? );
   }
   返回0;
}

I'm playing with waitpid() and signal() and I'm looking for reliable test cases for returning WIFSIGNALED(status) = WIFSTOPPED(status) = WIFCONTINUED (status) = true but can't find any...

Care to tell me how can I make sure those return true so I can debug my code?

Also, a few hints about what signals should I catch with signal() to test those macros would be helpful...

解决方案

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

#define NELEMS(x) (sizeof (x) / sizeof (x)[0])

static void testsignaled(void) {
   kill(getpid(), SIGINT);
}

static void teststopped(void) {
   kill(getpid(), SIGSTOP);
}

static void testcontinued(void) {
   kill(getpid(), SIGSTOP);
   /* Busy-work to keep us from exiting before the parent waits.
    * This is a race.
    */
   alarm(1);
   while(1) {}
}

int main(void) {
   void (*test[])(void) = {testsignaled, teststopped, testcontinued};
   pid_t pid[NELEMS(test)];
   int i, status;
   for(i = 0; i < sizeof test / sizeof test[0]; ++i) {
      pid[i] = fork();
      if(0 == pid[i]) {
         test[i]();
         return 0;
      }
   }
   /* Pause to let the child processes to do their thing.
    * This is a race.
    */
   sleep(1);
   /* Observe the stoppage of the third process and continue it. */
   wait4(pid[2], &status, WUNTRACED, 0);
   kill(pid[2], SIGCONT);
   /* Wait for the child processes. */
   for(i = 0; i < NELEMS(test); ++i) {
      wait4(pid[i], &status, WCONTINUED | WUNTRACED, 0);
      printf("%d%s%s%s\n", i, WIFCONTINUED(status) ? " CONTINUED" : "", WIFSIGNALED(status) ? " SIGNALED" : "", WIFSTOPPED(status) ? " STOPPED" : "");
   }
   return 0;
}

这篇关于测试用例℃,WIFSIGNALED,WIFSTOPPED,WIFCONTINUED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆