Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Below is a new version of popen2() that works. Some pipe ends were not closed in the linked to original version. (see the branch for "pid != 0", where extra handles are closed - whether there is ...
Answer
#5: Post edited
- Below is a new version of popen2() that works.
- Some pipe ends were not closed in the linked to original version.
(see the branch for "pid != 0", where extra handles are closed - whethere there is an error or not, but it's the parent process)- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
- Below is a new version of popen2() that works.
- Some pipe ends were not closed in the linked to original version.
- (see the branch for "pid != 0", where extra handles are closed - whether there is an error or not, but it's the parent process)
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
#4: Post edited
- Below is a new version of popen2() that works.
I found a crucial hint elsewhere with someone having a similar problem.- Some pipe ends were not closed in the linked to original version.
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
- Below is a new version of popen2() that works.
- Some pipe ends were not closed in the linked to original version.
- (see the branch for "pid != 0", where extra handles are closed - whethere there is an error or not, but it's the parent process)
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
#3: Post edited
- Below is a new version of popen2() that works.
- I found a crucial hint elsewhere with someone having a similar problem.
- Some pipe ends were not closed in the linked to original version.
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
- Below is a new version of popen2() that works.
- I found a crucial hint elsewhere with someone having a similar problem.
- Some pipe ends were not closed in the linked to original version.
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
#2: Post edited
- Below is a new version of popen2() that works.
- I found a crucial hint elsewhere with someone having a similar problem.
- Some pipe ends were not closed in the linked to original version.
- (I also renamed the pipe variables to something more clear to me)
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
- Below is a new version of popen2() that works.
- I found a crucial hint elsewhere with someone having a similar problem.
- Some pipe ends were not closed in the linked to original version.
- (I also renamed the pipe variables to something more clear to me)
- The exec_proc2() code from the 1st post remains unchanged.
- ```
- pid_t popen2(const char *command, int *infp, int *outfp)
- {
- constexpr int READ = 0, WRITE = 1;
- int tochild[2], fromchild[2];
- if (pipe(tochild) != 0 || pipe(fromchild) != 0)
- return -1;
- const pid_t pid = fork();
- if (pid != 0) // we're in the parent process
- { close(tochild[READ]); // Always close these ends, too
- close(fromchild[WRITE]);
- if (pid < 0) // ERROR
- return pid;
- }
- else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process
- {
- dup2(tochild[READ], READ);
- dup2(fromchild[WRITE], WRITE);
- close(tochild[READ]); close(tochild[WRITE]);
- close(fromchild[READ]); close(fromchild[WRITE]);
- execl("/bin/sh", "sh", "-c", command, NULL);
- perror("execl");
- _exit(1);
- }
- if (infp == NULL)
- close(tochild[WRITE]);
- else
- *infp = tochild[WRITE];
- if (outfp == NULL)
- close(fromchild[READ]);
- else
- *outfp = fromchild[READ];
- return pid;
- }
- ```
#1: Initial revision
Below is a new version of popen2() that works. I found a crucial hint elsewhere with someone having a similar problem. Some pipe ends were not closed in the linked to original version. (I also renamed the pipe variables to something more clear to me) ``` pid_t popen2(const char *command, int *infp, int *outfp) { constexpr int READ = 0, WRITE = 1; int tochild[2], fromchild[2]; if (pipe(tochild) != 0 || pipe(fromchild) != 0) return -1; const pid_t pid = fork(); if (pid != 0) // we're in the parent process { close(tochild[READ]); // Always close these ends, too close(fromchild[WRITE]); if (pid < 0) // ERROR return pid; } else // if (pid == 0) // fork() returns the child process ID to the parent and returns 0 to the child process { dup2(tochild[READ], READ); dup2(fromchild[WRITE], WRITE); close(tochild[READ]); close(tochild[WRITE]); close(fromchild[READ]); close(fromchild[WRITE]); execl("/bin/sh", "sh", "-c", command, NULL); perror("execl"); _exit(1); } if (infp == NULL) close(tochild[WRITE]); else *infp = tochild[WRITE]; if (outfp == NULL) close(fromchild[READ]); else *outfp = fromchild[READ]; return pid; } ```