单元测试kill命令

Unit testing kill command

所以我想做一些坏事和肮脏的事情;)

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

我得到"test: (after this point) Received signal 9 (Killed)"

有什么解决办法吗? 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 会杀死 parent + child。

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