c++ - exit() or _exit() after forking? -


i writing program requires communicating external program two-way simultaneously, i.e., reading , writing external program @ same time.

i create 2 pipes, 1 sending data external process, 1 receiving data external process. after forking child process, becomes external program, parent forks again. new child writes data outgoing pipe external program, , parent reads data incoming pipe external program further processing.

i've heard using exit(3) may cause buffers flushed twice, afraid using _exit(2) may leave buffers left unflushed. in program, there outputs both before , after forking. which, exit(3) or _exit(2), should use in case?

the below main function. #includes , auxiliary function left out simplicity.

int main() {     srand(time(null));     ssize_t n;     cin >> n;     (double p = 0.0; p <= 1.0; p += 0.1) {         string s = generate(n, p);         int out_fd[2];         int in_fd[2];         pipe(out_fd);         pipe(in_fd);         pid_t child = fork();         if (child) {             // parent             close(out_fd[0]);             close(in_fd[1]);             if (fork()) {                 close(out_fd[1]);                 ssize_t size = 0;                 const ssize_t block_size = 1048576;                 char buf[block_size];                 ssize_t n_read;                 while ((n_read = read(in_fd[0], buf, block_size)) != 0) {                     size += n_read;                 }                 size += n_read;                 close(in_fd[0]);                 cout << "p = " << p << "; compress ratio = " << double(size) / double(n) << '\n'; // data written before forking (the loop continues fork)             } else {                 write(out_fd[1], s.data(), s.size()); // data written after forking                 exit(exit_success); // exit(3) or _exit(2) ?             }         } else {             // child             close(in_fd[0]);             close(out_fd[1]);             dup2(out_fd[0], stdin_fileno);             dup2(in_fd[1], stdout_fileno);             close(stderr_fileno);             execlp("xz", "xz", "-9", "--format=raw", reinterpret_cast<char *>(null));         }     } } 

you need careful these sort of things. exit() different things _exit() , yet again different _exit(), , answer suggested duplicate explains, _exit (not same _exit, note upper case e) not call atexit() handlers, or flush output buffers, delete temporary files, etc [which may in fact atexit() handling, done direct call, depending on how c library code has been written].

most of output done via write, should unbuffered applications perspective. calling cout << ... well. need make sure flushed before exiting. right now, using '\n' end of line marker, may or may not flush output. if change endl instead, flush file. can safely use _exit() output perspective - if code set own atexit() handler example, open temporary files or bunch of other such things, problematic. if want more complex things in forked process, should done exec.

in program stands, there isn't pending output flush, "works" anyway, if add cout << ... << '\n'; (or without newline) type statement @ beginning of code, see go wrong. if add cout.flush();, "fix" problem (based on current code).

you should check return value execlp() call , call _exit() in case (and handle in main process don't continue loop in case of failure?)


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -