简体   繁体   English

为什么代码显示对管道的写不能确保原子性?

[英]why the code show write to pipe doesn't ensure atomic?

This is a very simple program.create pipe,then fork,use pipe between parent and child process. 这是一个非常简单的程序。创建管道,然后派生,在父子进程之间使用管道。 and the result show that write to pipe doesn't ensure atomic. 结果表明写入管道并不能确保原子。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(void)
{
    int pipe_fd[2];
    pid_t pid;
    char r_buf[4096];
    char w_buf[4096*2];
    int writenum;
    int rnum;
    memset(r_buf,0,sizeof(r_buf));  
    if(pipe(pipe_fd)<0)   //create pipe
    {
        printf("pipe create error\n");
        return -1;
    }

    if((pid=fork())==0)         //fork
    {
        close(pipe_fd[1]);
        while(1)
        {
            sleep(1);   
            rnum=read(pipe_fd[0],r_buf,1000);
            printf("child: readnum is %d\n",rnum);
        }
        close(pipe_fd[0]);

        exit(EXIT_SUCCESS);
    }
    else if(pid>0)
    {
        close(pipe_fd[0]);//write
        memset(r_buf,0,sizeof(r_buf));  
        if((writenum=write(pipe_fd[1],w_buf,1024))==-1)
            printf("write to pipe error\n");
        else    
            printf("the bytes write to pipe is %d \n", writenum);
        writenum=write(pipe_fd[1],w_buf,4096);
        close(pipe_fd[1]);
    }
    return EXIT_SUCCESS;
}

result:
the bytes write to pipe 1000
the bytes write to pipe 1000  //show write pipe not atomic
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 1000
the bytes write to pipe 120  //show write pipe not atomic
the bytes write to pipe 0
the bytes write to pipe 0
......

then i want to ask what does write atmoic mean?and why this program show it ? 那我想问一下写大气的意思是什么?为什么这个程序显示它呢?

Writes to pipes not greater than PIPE_BUF bytes must be atomic. 写入不超过PIPE_BUF字节的管道必须是原子的。 This means that if you have several processes writing concurrently to the same pipe, if every write() is of no more than PIPE_BUF bytes, their contents don't get intermixed, allowing you to setup a protocol than can have several writers using only one pipe, as long as you don't need longer writes. 这意味着,如果您有多个进程同时写入同一个管道,并且每个write()不超过PIPE_BUF个字节,那么它们的内容就不会混在一起,这使您可以设置一个协议,而只有几个编写器只使用一个管道,只要您不需要更长的写入时间即可。

Learn more about Atomicity here 在此处了解有关原子性的更多信息

Atomicity should be ensured to all the shared data-structure in the multi-threaded/-processing systems. 应确保多线程/处理系统中所有共享数据结构的原子性。

There are some good examples here 有一些很好的例子在这里

Your program and its results do not show that the writes are not atomic. 您的程序及其结果未显示写入不是原子的。 What it means for the writes to be atomic is that the bytes written in a single write() won't be available until after the write() finishes, and when they are, all of them will be available. 对于写入来说是原子的,这意味着直到write()完成之后,在单个write()的字节才可用,并且当它们可用时, 所有字节都将可用。 Basically, if you can read any of the bytes at all, you can read all of them. 基本上,如果您可以读取所有字节,则可以读取所有字节。 You don't have to read them all at once; 不必一次读他们所有; you can choose to read() only some of them, and get the rest in a later read() . 您可以选择只read()其中的一些内容,然后在以后的read()获取其余内容。 If there are bytes available from more than one write() , you can even read all the bytes (that are left) from one write() and some from the next. 如果有字节可以从不止一个write()你甚至可以读取所有从一个字节(即是左) write()和一些来自未来。 All atomicity guarantees here is that you won't be able to read() only some of the bytes from a certain write() but unable to read() the rest if you choose. 这里所有的原子性保证的是,你将无法read()只有一些从某个字节的write()但无法read()其余的,如果你选择。

Of course, atomic writes are only guaranteed if the pipe's buffer has room for the bytes written -- but your program shouldn't be reaching the limit. 当然,仅当管道的缓冲区中有足够的空间写入字节时,才能保证原子写入-但您的程序不应达到限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM