假设我通过fork从父进程创建了一个子进程,然后使用管道将X值传递给了子进程。首先,该子进程处于暂停状态,然后使用SIGINT信号启动它。是将值X传递给管道中使用的信号处理程序。此外,i的值将在父进程运行期间发生变化,并且我将必须多次传递它,因此我不认为将其全局设置会起作用。
==10412== HEAP SUMMARY:
==10412== in use at exit: 1 bytes in 1 blocks
==10412== total heap usage: 3 allocs, 2 frees, 1,033 bytes allocated
==10412==
==10412== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==10412== at 0x483577F: malloc (vg_replace_malloc.c:299)
==10412== by 0x10927C: duplicate_args (in /root/Documents/OS/proj2/project/proj2)
==10412== by 0x109194: main (in /root/Documents/OS/proj2/project/proj2)
==10412==
==10412== LEAK SUMMARY:
==10412== definitely lost: 1 bytes in 1 blocks
==10412== indirectly lost: 0 bytes in 0 blocks
==10412== possibly lost: 0 bytes in 0 blocks
==10412== still reachable: 0 bytes in 0 blocks
==10412== suppressed: 0 bytes in 0 blocks
==10412==
==10412== For counts of detected and suppressed errors, rerun with: -v
==10412== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
这怎么办?
答案 0 :(得分:1)
在子代的开头添加一个值为0的全局变量。在孩子中放置一会儿循环。当信号到来时,将其设置为1。在while循环中测试全局变量的值是否变为1,如果是,则读取并打印并将该变量返回0。
print(" " * (10 - y), word[:y], word[:y:-1], sep="")
当然,这是您想要的存根,还有更多。
您可以在全局变量上添加并发保护。
如果您的系统更复杂,则消息队列更合适。
希望它会有所帮助:)