加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

EOF在文件结束前到达

发布时间:2020-12-13 18:53:17 所属栏目:Linux 来源:网络整理
导读:我正在为学校制作一个程序,我有一个多进程程序,每个进程读取一个文件的一部分,它们一起工作来计算文件中的单词数.我有一个问题,如果有超过2个进程,那么所有进程在读取文件的部分之前从文件中读取EOF.这是相关的代码: #include 使用3个进程运行文件时的输出

我正在为学校制作一个程序,我有一个多进程程序,每个进程读取一个文件的一部分,它们一起工作来计算文件中的单词数.我有一个问题,如果有超过2个进程,那么所有进程在读取文件的部分之前从文件中读取EOF.这是相关的代码:

#include 

使用3个进程运行文件时的输出:

All files opened successfully
Process 2 found unexpected EOF at 1323008.
Process 1 found unexpected EOF at 823849.
Process 0 found unexpected EOF at 331776.

导致错误的测试文件:https://dl.dropboxusercontent.com/u/16835571/test34.txt

编译:

gcc main.c -o wordc-mp

并运行:

wordc-mp test34.txt 3

值得注意的是,只有那个特定的文件给我带来了问题,但错误的偏移量不断变化,因此它不是文件的内容.

最佳答案
您在分叉之前已经创建了文件描述符.子进程继承文件描述符,该文件描述符指向父对象的相同文件描述,因此,与其中一个子进程一起使光标前进给所有子进程.

从“man fork”,你可以得到确认:

  • The child process is created with a single thread—the one
    that
    called fork(). The entire virtual address space of the parent is
    replicated in the child,including the states of mutexes,condition
    variables,and other pthreads objects; the use of pthread_atfork(3)
    may be helpful for dealing with problems that this can cause.

  • The child inherits copies of the parent’s set of open file descrip

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读