A process take input from /proc/<pid>/fd/0, but won't process it

Joined
Oct 29, 2023
Messages
1
Reaction score
0
VirtualBox ubuntu.
I have a program that read input from trminal and output it.

c code:

#include <stdio.h>
#include <unistd.h>

int main() {
char input[256]; // Assuming a maximum line length of 255 characters
pid_t pid = getpid();
printf("My PID is %d\n", pid);

while(1){
printf("input:");
if (fgets(input, sizeof(input), stdin) != NULL) {
printf("\toutput:%s\n", input);
}
//sleep(1);
}

return 0;
}

x@ub22:~/tmp$ ./a.out
My PID is 189425
input:123
output:123
input:abc
output:abc
now from another terminal, I do

echo "123" > /proc/189425/fd/0
./a.out got the 123, but did not output it.

input:123



please help me to understand why.
thank you in advance.
Xian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top