c - 单元测试kill命令

标签 c linux unit-testing posix kill

所以我正在尝试做一些坏事和肮脏的事情;)

我想在我的检查单元测试中调用 kill(0, SIGKILL) 来终止我在这个测试中启动的子进程。

ck_assert_int_eq(magic(13), 13); //<- success, but I cannot stop magic now

如果我这样做

ck_assert_int_eq(kill(0, SIGKILL), 0); 

我得到“测试:(在此之后)接收到信号 9(已杀死)”

有什么解决办法吗? kill(0, SIGKILL) 也在实际代码中完成,所以我认为如果我尝试从我的测试中调用破坏函数,我将以同样的错误结束。

最佳答案

int kill(pid_t pid, int sig)

If pid equals 0, then sig is sent to every process in the process group of the calling process.

来源:man 2 kill

你需要调用kill(child_pid, SIGKILL),因为child_pid == 0会杀死父+子。

pid_t fork(void);

Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

来源:man 3 fork

关于c - 单元测试kill命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834335/

相关文章:

ios - 单元测试 UILocalNotification

linux - 为什么新创建的线程不是通过pthread_create()返回参数获取它的tid,而是pthread_self()

c - 如何仅使用指针(无结构)来实现链表?

c - 内存空闲抛出 segFault

python - 将C套接字程序转换为python

c - linux 文件创建和 umask()

c - 执行权限被拒绝

unit-testing - 如何在docker-compose.yml中开始测试之前等待数据库迁移完成

php - PHPunit 配置文件 phpunit.xml.dist 或 phpunit.xml 的命名有什么不同吗

c - 自动从静态库中删除文件