0

If you have a process that is waiting for user-input from the stdin scope, then how can you supply that user-input from a second terminal ?

Specifically, if I run the c-program

while(1){
    fgets(string, len, stdin);
    string[strlen(string)-1] = 0;
    if(strcmp("Stop", string) == 0){
        printf("Gotcha");
        return 1;
    }
}

then how can I supply the string "Stop" to stdin of that process from another process, such that the first process will stop (and print "Gotcha") ?

I've tried to run the c-program in terminal 'pts/0' and open a new terminal ('pts/1') with commands:

$ echo "Stop" > /proc/<pid>/fd/0
$ echo "Stop" > /dev/pts/0

where pid is the process id. The "Stop"-command is "repeated" in the first shell, but the process does not receive it.

  • 1
    Take a look at this: serverfault.com/questions/178457/… – Panki Dec 21 '18 at 15:24
  • 1
    Is this substantially different from this other question of yours? It looks like they are addressing the same issue. – fra-san Dec 21 '18 at 15:50
  • Related questions are unix.stackexchange.com/questions/333028 and unix.stackexchange.com/questions/487560 . – JdeBP Dec 21 '18 at 16:36
  • @fra-san They do address the same issue, but are stated differently. If I were to edit the question, then the answer given by ilkachu would not be so relevant anymore, so I'd better just ask differently in a new post – Ronny Landsverk Dec 21 '18 at 17:07
  • In principle you have done the right thing. Still, I think that you could have just added details to your other question (the existing answers' value would not have been affected significantly). But I see that your other question has an accepted answer now, so this is not relevant anymore. – fra-san Dec 21 '18 at 17:52
0

Create a named pipe and let your C program read from it:

$ mkfifo input
$ ./prog <input

In another shell session:

$ echo Stop >/path/to/input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.