Say I have a C program which creates a child throught fork
. The parent processus ends before the child ends, and the child is blocked on a read
system call because of an empty pipe.
If the processus ID of the child is 1500
, and by using the shell command ls -l /proc/1500/fd
I see that a pipe is open, is it possible to write to this pipe from the terminal using a shell command so that the read
system call unblocks and the child process finish its execution?
Been there done that.
Sh answer:
cat whatever >> /proc/pid/fd/0
C answer:
pid_t pid = whatever;
char buf[30];
sprintf(buf, "/proc/%d/fd/0", pid);
FILE *f = fopen(buf, "w");
But you appear to have the problem of have left the writing half of the pipe open in the child process. You should close it (see man 2 close
) it immediately after your fork()
call that way the reader doesn't get stuck but can observe the end of pipe. The launcher should normally look something like this.
int pipefd[2];
pid_t pid;
pipe(pipefd);
if ((pid = fork()) == 0) {
dup2(pipefd[0], 0);
close(pipefd[0]);
close(pipefd[1]);
/* ... */
/* usually this goes to exec but it doesn't have to */
_exit(3);
}
close(pipefd[0]);
if (pid < 0) {
close(pipefd[1]);
return ;
}
int pipefeed = pipefd[0];
/* ... */
/* I'm guessing in your case you don't wait() */
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.