Link to home
Start Free TrialLog in
Avatar of Tim_Heldberg
Tim_Heldberg

asked on

Problem checking stdin for input...ipc

Hello,

I am working on a program ins which I have a server that is communicating with clients using the message queue system.  The problem is that after the message queue is set up, the client and server go into a loop where the client loop first check whether there are any messages for it in the queue(no problem)  then it checks whether there is a command entered to it from the console.
The problem is, I can not figure out any way to check stdin for any input, and if there is none to go on.

Help would be very appreciated..
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim_Heldberg
Tim_Heldberg

ASKER

Im using a unix shell
The common shells do not directly support multithreading or asynchronous I/O.
What shell programming languages are great at is managing multiple processes.
Your shell script should spawn a separate process to manage the message queue,
while the original script waits for input on stdin.   That separate process could
simply be another shell script running in a subhell.

Hi Tim_Heldberg,
> Im using a unix shell
You need non-blocking I/O for this; check the fcntl call with the O_NONBLOCK option. Unfortunately, this doesn't work in the shell, you need a C program (or something similar) for this.

Cheers!

Stefan
Nonblocking IO will work for most things here, although I, personally, have managed to get stdin to behave properly without blocking.  What I have found works very well as a replacement is multithreading.

Make a thread thats sole purpose is input from stdin.  whenever it gets a line, it stores it somewhere accessable by a "main" thread, then wakes the main thread up.  The main thread processes the new data, then goes back to sleep.
That's probably a lot more complicated than nonblocking I/O, since you need to implement the thread synchronization. And multi-threaded code is a poin to debug.
Hi Tim,

It is unclear whether you are using shell scripts for the client & server ...or you are using C (under the UNIX platform)
I assume that you are using C ... (because you have posted the question in the C programming section.

Getting back to your question...
You can use the select() function for achieving this.

the following is a sample code from the unix man pages:

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
     fd_set rfds;
     struct timeval tv;
     int retval;

     /* Watch stdin (fd 0) to see when it has input. */
     FD_ZERO(&rfds);
     FD_SET(0, &rfds);     // here '0' is for stdin
 
      /* Wait up to five seconds. */
      tv.tv_sec = 5;
      tv.tv_usec = 0;
 
      retval = select(1, &rfds, NULL, NULL, &tv);

      /* Don't rely on the value of tv now! */

      if (retval == -1)
           perror("select()");
      else
          if (retval)
               printf("Data is available now.\n");
               /* FD_ISSET(0, &rfds) will be true. */
          else
                /*here you can write code to proceed with the checking of message queue...*/
               printf("No data within five seconds.\n");    
               

     return 0;
}

you can use the above select code fragment to check whether there is data available at the stdin ...you can set the tv.tv_sec to a lower value to avoid waiting for longer ..

Regards,
Punit