避免杀死文件损坏问题

时间:2015-02-20 13:16:00

标签: c linux

我有一个不断运行的进程(wlescan_dump),其输出通过管道传递给程序(process_dump)。 process_dump是一个用C编写的可执行文件,它不断写入文件:

sudo wlescan_dump | process_dump 

现在,如果我想以编程方式杀死这两个进程(wlescan_dump和process_dump),我该​​怎么做才能干净利落?杀死-15两个进程就足够了吗?我主要担心避免文件损坏,并确保在kill_dump之前完成挂起的文件写入。

2 个答案:

答案 0 :(得分:0)

杀死wlescan_dump并代码process_dump关闭所有打开的文件,并在stdin关闭时退出。

如果您真的偏执,请实施建议的信号处理程序larsks,并根据您使用打开文件的方式,只能原子地更新它们:写入outputfile的副本,outputfile.temp,然后重命名outputfile .temp到outputfile。

答案 1 :(得分:0)

您应该使用SIGTERM信号

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_01.html

When killing a process or series of processes, it is common sense to start  
trying with the least dangerous signal, SIGTERM. That way, programs that care
about an orderly shutdown get the chance to follow the procedures that they 
have been designed to execute when getting the SIGTERM signal, such as 
cleaning up and closing open files.
相关问题