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.

drC1Ron
  • 161
  • 1
    Take a look at this: https://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi – 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 https://unix.stackexchange.com/questions/333028/ and https://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 – drC1Ron 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
  • Yes that is correct, but if you know another way of achieving the same thing, then please do tell – drC1Ron Dec 21 '18 at 17:59

2 Answers2

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
Kusalananda
  • 333,661
0

Identify stdin before starting the c program, then write to that device

$ tty
/dev/pts/2
$ ./prog

then in another terminal (etc)

$ echo Stop > /dev/pts/2
stevea
  • 431