Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

71%
+3 −0
Q&A How kill a child process without read() hanging in the parent process?

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

posted 3y ago by sktpin‭  ·  edited 3y ago by sktpin‭

Answer
#5: Post edited by user avatar sktpin‭ · 2021-10-07T09:41:42Z (about 3 years ago)
  • 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 by user avatar sktpin‭ · 2021-10-07T09:39:28Z (about 3 years ago)
  • 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 by user avatar sktpin‭ · 2021-10-06T16:31:20Z (about 3 years ago)
  • 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 by user avatar sktpin‭ · 2021-10-06T16:31:05Z (about 3 years ago)
  • 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 by user avatar sktpin‭ · 2021-10-06T16:29:19Z (about 3 years ago)
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;
}
```