[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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..
0
Tim_Heldberg
Asked:
Tim_Heldberg
1 Solution
 
sunnycoderCommented:
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)
0
 
Tim_HeldbergAuthor Commented:
Im using a unix shell
0
 
brettmjohnsonCommented:
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.

0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
stefan73Commented:
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
0
 
CmdrRickHunterCommented:
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.
0
 
stefan73Commented:
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.
0
 
punit_rCommented:
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
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now