linux - Interprocess communication via Pipes -


it known during interprocess communication in linux, processes communicate each other through special file named "pipe".

it known the operations performed on file write 1 process , read 1 process in order communicate each other.

now, question :

do these write , read operations performed in parallel during communication (operations executed parallely) ? , if not than,

what happens when 1 of process enters sleep state during communication? performs write operation first second process read or goes directly sleep without performing of write , read operation?

the sending process can write until pipe buffer full (64k on linux since 2.6.11). after that, write(2) block.

the receiving process block until data available read(2).

for more detailed pipe buffering, @ https://unix.stackexchange.com/a/11954.

for example, program

#include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>  int main(int argc, char *argv[]) {     int pipefd[2];     pid_t cpid;     char wbuf[32768];     char buf[16384];      /* initialize writer buffer 012...89 sequence */     (int = 0; < sizeof(wbuf); i++)       wbuf[i] = '0' + % 10;      if (pipe(pipefd) == -1) {         perror("pipe");         exit(exit_failure);     }      cpid = fork();     if (cpid == -1) {         perror("fork");         exit(exit_failure);     }      if (cpid == 0) {    /* child reads pipe */         close(pipefd[1]);          /* close unused write end */         while (read(pipefd[0], &buf, sizeof(buf)) > 0);         close(pipefd[0]);         _exit(exit_success);      } else {            /* parent writes sequence pipe */         close(pipefd[0]);          /* close unused read end */         (int = 0; < 5; i++)           write(pipefd[1], wbuf, sizeof(wbuf));         close(pipefd[1]);          /* reader see eof */         wait(null);                /* wait child */         exit(exit_success);     } } 

will produce following sequence when run gcc pipes.c && strace -e trace=open,close,read,write,pipe,clone -f ./a.out:

open("/etc/ld.so.cache", o_rdonly|o_cloexec) = 3 close(3)                                = 0 open("/lib/x86_64-linux-gnu/libc.so.6", o_rdonly|o_cloexec) = 3 read(3, "\177elf\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\3\2\0\0\0\0\0"..., 832) = 832 close(3)                                = 0 pipe([3, 4])                            = 0 clone(child_stack=null, flags=clone_child_cleartid|clone_child_settid|sigchld, child_tidptr=0x7f32117489d0) = 21114 close(3)                                = 0 write(4, "01234567890123456789012345678901"..., 32768) = 32768 write(4, "01234567890123456789012345678901"..., 32768) = 32768 write(4, "01234567890123456789012345678901"..., 32768strace: process 21114 attached  <unfinished ...> [pid 21114] close(4)                    = 0 [pid 21114] read(3, "01234567890123456789012345678901"..., 16384) = 16384 [pid 21114] read(3,  <unfinished ...> [pid 21113] <... write resumed> )       = 32768 [pid 21114] <... read resumed> "45678901234567890123456789012345"..., 16384) = 16384 [pid 21113] write(4, "01234567890123456789012345678901"..., 32768 <unfinished ...> [pid 21114] read(3, "01234567890123456789012345678901"..., 16384) = 16384 [pid 21114] read(3,  <unfinished ...> [pid 21113] <... write resumed> )       = 32768 [pid 21114] <... read resumed> "45678901234567890123456789012345"..., 16384) = 16384 [pid 21113] write(4, "01234567890123456789012345678901"..., 32768 <unfinished ...> [pid 21114] read(3, "01234567890123456789012345678901"..., 16384) = 16384 [pid 21114] read(3,  <unfinished ...> [pid 21113] <... write resumed> )       = 32768 [pid 21114] <... read resumed> "45678901234567890123456789012345"..., 16384) = 16384 [pid 21113] close(4)                    = 0 [pid 21114] read(3, "01234567890123456789012345678901"..., 16384) = 16384 [pid 21114] read(3, "45678901234567890123456789012345"..., 16384) = 16384 [pid 21114] read(3, "01234567890123456789012345678901"..., 16384) = 16384 [pid 21114] read(3, "45678901234567890123456789012345"..., 16384) = 16384 [pid 21114] read(3, "", 16384)          = 0 [pid 21114] close(3)                    = 0 [pid 21114] +++ exited 0 +++ --- sigchld {si_signo=sigchld, si_code=cld_exited, si_pid=21114, si_uid=1000, si_status=0, si_utime=0, si_stime=0} --- +++ exited 0 +++ 

you'll notice reads , writes interleaved , writing , reading processes block few times either pipe full or not enough data available reading.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -