gpt4 book ai didi

c - system() 函数从守护进程返回值始终为 -1

转载 作者:行者123 更新时间:2023-11-30 20:06:26 25 4
gpt4 key购买 nike

我正在尝试通过系统(从c守护进程程序启动bash脚本获取返回值 )函数。

如果 sculpture_daemon 被注释,则输出正确为 5120 (20 << 8),否则始终为 -1

int main (void)
{
skeleton_daemon();
printf("%d", system("/tmp/prova.sh") ); // prova.sh return 20
}

void skeleton_daemon(void)
{
pid_t pid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
umask(0);
// chdir("/"); // commented as per nos suggestion, nothing changed
}

有人知道如何从守护进程获取脚本值吗?

编辑:按照 nos 的要求使用 strace -f 运行

说实话,我什么都不懂

execve("./pr", ["./pr"], [/* 17 vars */]) = 0
brk(0) = 0xd79000
uname({sys="Linux", node="alarm", ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f80000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=30110, ...}) = 0
mmap2(NULL, 30110, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb6f53000
close(3) = 0
open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\370\203\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1541182, ...}) = 0
mmap2(NULL, 1262984, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb6e1e000
mprotect(0xb6f45000, 32768, PROT_NONE) = 0
mmap2(0xb6f4d000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x127000) = 0xb6f4d000
mmap2(0xb6f50000, 9608, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb6f50000
close(3) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb6f7f000
set_tls(0xb6f7f4c0, 0xb6f7fba8, 0xb6f82000, 0xb6f7f4c0, 0xb6f82000) = 0
mprotect(0xb6f4d000, 8192, PROT_READ) = 0
mprotect(0xb6f81000, 4096, PROT_READ) = 0
munmap(0xb6f53000, 30110) = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb6f7f068) = 7344
exit_group(0) = ?
+++ exited with 0 +++

提前致谢

最佳答案

发生这种情况是因为您的 sculpture_daemon() 函数忽略了 SIGCHLD,但是 system() 调用假定它不会被忽略,因此它可以正确等待,直到执行的程序已完成。

关于c - system() 函数从守护进程返回值始终为 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078908/

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