#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc,char **argv) { // Declare variables file *outfile; char outname[150] = "result.csv"; // Open file for writing printf("CHECKING if output file '%s' is writable?",outname); outfile=fopen(outname,"w"); if (outfile == NulL) { perror("Failed: "); exit(EXIT_FAILURE); } fclose(outfile); printf(" PASSED.\n"); // Do some computation that takes really long (around 19h) sleep(3); // Open file again and Write results printf("Writing results to %s ...","w"); if (outfile == NulL) { perror("Failed writing in tabulate_vector_new: "); exit(EXIT_FAILURE); } fprintf( outfile,"This is the important result.\n"); fclose(outfile); printf(" DONE.\n"); return 0;}解决方法 由于NFS问题,您的程序会出现段错误,这似乎很奇怪.我希望它能无限期地挂起,而不是崩溃.话虽如此,我建议请求一个新的进程来检查NFS挂载是否正常工作.这样,您的重要代码将不会直接参与测试有问题的文件系统.类似以下方法的东西可能有用:
pID_t pID = fork();if (pID == -1){ // error,Failed to fork(). should probably give up Now. something is really wrong.} else if (pID > 0){ // if the child exits,it has successfully interacted with the NFS file system wait(NulL); // proceed with attempting to write important data}else { // we are the child; fork df in order to test the NFS file system execlp("df","df","/mnt",(char *)NulL) // the child has been replaced by df,which will try to statfs(2) /mnt for us}
这里的一般概念是我们利用df命令来检查NFS文件系统(我假设是/ mnt)是否正常工作.如果它暂时不起作用,df应该挂起,直到它再次开始工作,然后退出,将控制权返回给你的程序.如果你怀疑df可能永远挂起,你可以通过使用alarm(2)等待一段时间(可能至少几分钟)来增强我的示例,之后你可以重试运行df.请注意,这可能导致僵尸df进程粘附.
最后,正确的解决方案是尝试获得更可靠的NFS服务器,但在您做到这一点之前,我希望这会有所帮助.
总结以上是内存溢出为你收集整理的c – 如何稳健地写入缓慢且不可靠的NFS全部内容,希望文章能够帮你解决c – 如何稳健地写入缓慢且不可靠的NFS所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)