3

I have multithread applications. In one thread I want to waiting for data, and if this datas will not appear through some time, I just close whole application. Function select() is runs in another thread and thread is detach

A piece sorce some class

#define STDIN 0
A::A(){

   runFunction1();
   runFunction2();
   thread listen(&A::isData , this);
   listen.detach();

}

// and function with select

void A::isData{

 struct timeval tv;
    fd_set readfds;
    tv.tv_sec = 50; // wait to 50 seconds
    tv.tv_usec = 500000;
    FD_ZERO(&readfds);
    FD_SET(STDIN, &readfds);
    for(;;) {

        select(STDIN+1, &readfds, NULL, NULL, &tv);
        if (FD_ISSET(STDIN, &readfds)){
            cout << "I something catch :)" << endl;
            // reset time
        }else{
            cout << "I nothing catch ! By By :(" << endl;
            exit(1);
            break;
        }
    }
}

If my program run then I get pid, so I have tryed write to file descriptor some datas in below way:

$ cd /proc/somePID/fd
$ echo 1 >> 0

Then I should be get info I something catch :) but in console IDE I only get 1 , however if time is out I get I nothing catch ! By By :( to console in IDE.

EDIT: SOLVED

@Vladimir Kunschikov gave me correct tip. Basic on this I show how I do it in C/C++

It will be posible send something to process using command echo we must create pipe. Pipe using function

A::A(){
       fd[2]; //two file descriptor for pipes write/read
       pipe(fd); // c function to create pipes
       runFunction1(); 
       runFunction2();
       thread listen(&A::isData , this);
       listen.detach();

    }

Then in our proces will create other two new file descriptor as below:

lrwx------ 1 user user 64 gru 15 14:02 0 -> /dev/pts/0
lrwx------ 1 user user 64 gru 15 14:02 1 -> /dev/pts/0
lrwx------ 1 user user 64 gru 15 14:02 2 -> /dev/pts/1
lr-x------ 1 user user 64 gru 15 14:02 3 -> pipe:[1335197]
lrwx------ 1 user user 64 gru 15 14:02 4 -> socket:[1340788]
l-wx------ 1 user user 64 gru 15 14:02 5 -> pipe:[1335197]

pipe:[1335197] is descriptor to read in this proces. Now command echo will be works if we will write some to descriptor 3. Using is simply:

$ cd /proc/PID/fd
$ echo 1 > 3

Then we also can using this in select() function but descriptor number is 3 so define should be looks like .

#define STDIN 3

And works

Mbded
  • 1,754
  • 4
  • 23
  • 43

1 Answers1

0

You have wrong assumption that writing to the /proc/pidof application/fd/0 will put data to the stdin stream of the application. Just read answers to this question: Sending command to java -jar using stdin via /proc/{pid}/fd/0

Community
  • 1
  • 1
Vladimir Kunschikov
  • 1,735
  • 1
  • 16
  • 16