gpt4 book ai didi

c - 单元测试kill命令

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:56:26 25 4
gpt4 key购买 nike

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

我想在我的检查单元测试中调用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com