简体   繁体   中英

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. 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.

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. 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() . 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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