0

我正在尝试通过 fifo 将 file1 的内容复制到其他 file2 中。我想在fifo中写回前四个字符(在读取期间,而不是在将内容从file1写入fifo时更早),然后也将其复制到file2中。但是前四个字符不会附加在后面,而是随机插入到中间。我的代码是

int main( int argc, char* argv[] ) {

    int fdes,fdes1;
    pid_t pid;
    ssize_t numRead;

    char readBuff[1];
    char writeBuff[1];
    int readCounter;
    int c=0;

    umask(0);

    if (mkfifo("ajjp.e",0666) == -1  /*make the fifo*/
        && errno != EEXIST)
    {}

    if( argc < 3 ) {
        printf( "Atleast need 2 params " );
        exit(1);
    }

    int to_copy = open( argv[1], 0 );/* file from which contents are to be copied */
    int oo = creat(argv[2], 0666);/* file created where we've to write contents*/

    if ( to_copy == -1  ) {
        printf( "Opening file failed " );
        exit(1);
    }
    if ( (pid = fork()) < 0) /* child process is made*/
        perror("fork error");

    /* in parent process,I'm cwriting contents of file1 to fifo character by character */
    else if(pid>0)
    {
        fdes = open("ajjp.e", O_WRONLY);
        while( (readCounter = read( to_copy, readBuff, sizeof( readBuff ) ) > 0 ) )  {

            write( fdes, readBuff, sizeof( readBuff ) );
        }
        close(to_copy);

    }
/* now, in child process, I opened its read end then I'm reading contents from fifo and writing it to file2(i.e copy_to here) but for first four characters( c< 5 here), I'm writing them to fifo also by opening its write end. */
    else
    {
        fdes1 = open("ajjp.e", O_RDONLY);

        fdes = open("ajjp.e", O_WRONLY);

        if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
            printf("signal");

        int copy_to = open( argv[2], 0666);/* opened the file where we've to write*/
        if ( copy_to == -1  ) {
            printf( "Opening file failed " );
            exit(1);
        }


        for(;;) {

            c++;
            numRead = read(fdes1, readBuff, sizeof(readBuff));/* reading from read end of fifo*/

            if (numRead == 0)
                break;

            /* write to the file2*/
            if (write(copy_to, readBuff, numRead) != numRead)
            {}
            /* for first 4 characters, I am rewriting to the back of fifo*/
            if(c<5)
            {
                write(fdes,readBuff,sizeof(readBuff));
            }
            /*after writing those 4 characters, write end I've closed*/
            if(c==5)
                close(fdes);
        }
        close(fdes);
        close(fdes1);

    }//end else

    return 0;    
}

现在,如果在终端上,我运行

$ ./a.out a.txt b.txt

我想从 a.txt 复制到 b.txt,b.txt 包含 a.txt 加上在字符之间随机插入的前 4 个字符。

4

1 回答 1

0

你有一些逻辑问题和同步问题。你的目标不是很明确,但似乎你想复制一个文件,说“Hello World”,在副本中,它应该有“Hello World”,但也有“Hell”。所以,也许“HelHleo世界”?

计算机速度很快,可以缓冲并一次完成很多工作。您的子进程甚至可能在父进程完全完成后才会执行,因为启动一个新进程需要一些时间。因此,您可能会得到“Hello WorldHell”。也就是说,父级在子级甚至开始读取之前将文件完全复制到 FIFO。您需要研究一些同步方法。例如,使用您拥有的工具,您可以制作另一个 fifo。让父母等到它可以从中读取。当它被加载时让孩子写信告诉父母它准备好了。使您的文件非常大也可能让孩子有时间开始或在每个字符之后添加睡眠语句。

如果您提供了输入文件以及输出(错误的输出),则推测起来会更容易。但是,基本上,您有一个多进程/多线程问题,您希望它们一起运行,但它们实际上最终一次运行一个。放慢父母的速度或让它等待孩子。

于 2016-03-24T11:54:10.333 回答