c - Risk of losing data when sending variables through pipe? -


i have following code:

#include <unistd.h> #include <stdio.h> #include <sys/wait.h> #include <sys/types.h> // may not needed #include <sys/stat.h> // may not needed #include <stdlib.h> #include <string.h>  typedef struct {     int pid;     char arg[100];     int nr; } str;  int main() {     int c2p[2];     pipe(c2p);     int f = fork();      if (f == 0) {         str s;         s.pid = 1234;         strcpy(s.arg, "abcdef");         s.nr = 1;          close(c2p[0]);         write(c2p[1], &s, sizeof(str));         close(c2p[1]);         exit(0);     }      wait(0);     close(c2p[1]);     str s;     read(c2p[0], &s, sizeof(str));     printf("pid: %d nr: %d arg: %s", s.pid, s.nr, s.arg);     close(c2p[0]);     return 0; } 

i have worked fine until (pid, nr , arg never altered), but:

when child process done, memory segment (used child) destroyed (marked free)? if so, there risk between time of writing , time of reading lose acces segment or data altered?

(the original question this: sending structure through pipe without losing data )

although child process' memory given operating system when process exits, suspect not you're asking about.

you more concerned happens data written pipe after child process exits. pipe(2) man page states:

data written write end of pipe buffered kernel until read read end of pipe.

so data arrive, if process wrote has exited.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -