Problem checking stdin for input...ipc

Posted on 2005-04-02
Medium Priority
Last Modified: 2010-04-15

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..
Question by:Tim_Heldberg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Accepted Solution

sunnycoder earned 1500 total points
ID: 13690781
what platform/OS ?

- Consider using separate threads for handling user input and message queue handling
- Check if your platform supports non blocking I/O (fcntl etc)

Author Comment

ID: 13690803
Im using a unix shell
LVL 23

Expert Comment

ID: 13690975
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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 12

Expert Comment

ID: 13691428
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.



Expert Comment

ID: 13692794
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.
LVL 12

Expert Comment

ID: 13692819
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.

Expert Comment

ID: 13708851
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_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)
          if (retval)
               printf("Data is available now.\n");
               /* FD_ISSET(0, &rfds) will be true. */
                /*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 ..


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question