我正在开发可以全天运行的C ++程序。它输出到stdout,我想压缩此输出。未压缩的输出可以是许多GB。启动的Bourne Shell脚本编译C ++代码并按如下方式启动程序:
./prog | gzip > output.gz
当我使用CTRL-C中断脚本时,.gz文件总是损坏。当我从终端启动程序并使用CTRL-C中断它时,.gz文件也总是损坏。当我在终端上启动程序并使用Linux killall终止该程序时,.gz文件就可以了。
另一方面,可以使用CTRL-C中断端子cat <large_file> | gzip > cat.gz
,并且cat.gz总是可以的。所以我怀疑cat有某种信号处理程序,我也必须在我的C ++程序中实现...但是在网上查看cat实现时,我发现没有类似的东西。不管怎样,我实现了这一点:
void SignalHandler(int aSignum)
{
exit(0);
}
void Signals()
{
signal(SIGINT, SignalHandler);
signal(SIGKILL, SignalHandler);
signal(SIGTERM, SignalHandler);
}
...以及bsh脚本中的某些内容,但没有任何帮助。 CTRL-C之后,gz文件已损坏。
问题:
使用zcat
打开生成的文件会提供一些输出,但是然后:gzip: file.gz: unexpected end of file
。在Ubuntu的存档管理器中打开它只会显示一个弹出窗口,提示An error occurred while extracting files.
尝试冲洗;没有发现问题的变化。
有关此问题的更多信息:末尾(EOCDR)签名
Fix archive (-F) - assume mostly intact archive
zip warning: bad archive - missing end signature
zip warning: (If downloaded, was binary mode used? If not, the
zip warning: archive may be scrambled and not recoverable)
zip warning: Can't use -F to fix (try -FF)
zip error: Zip file structure invalid (file.gz)
maot@HP-Pavilion-dv7:~/temp$ zip -FF file.gz --out file2.gz
Fix archive (-FF) - salvage what can
zip warning: Missing end (EOCDR) signature - either this archive
is not readable or the end is damaged
Is this a single-disk archive? (y/n): y
Assuming single-disk archive
Scanning for entries...
zip warning: zip file empty
maot@HP-Pavilion-dv7:~/temp$ ls -lh file2.gz
-rw------- 1 maot maot 22 feb 15 15:18 file2.gz
maot@HP-Pavilion-dv7:~/temp$
当您按下Ctrl + C时,外壳程序将SIGINT
发送到管道中的last进程,此处为gzip
。 gzip
终止,下次prog
写入stdout
时,它接收SIGPIPE
。
[您需要将SIGINT
发送到prog
才能刷新其stdout
并退出(前提是您已像以前那样安装了信号处理程序),以便gzip
接收其所有输出然后终止。] >