Solved

Timeout in UNIX

Posted on 1998-09-29
1
653 Views
Last Modified: 2010-05-18
How do you implement a timeout in Unix???
 
Let me explain what I am trying to do:
 
I've opened a device file (serial port - connected to a modem) for I/O. I am communicating with a terminal (phone) which also has a modem. I need to wait for a response from the terminal after having sent a command. So I do a 'write' to the device file to send the command to the terminal, and do a 'read' from the device to get the response. I want to wait, let us say, only 15 seconds for the response, HOW DO I DO IT?
 
I am using the standard 'read' and 'write' routines.                                
0
Comment
Question by:sanjaychopra
1 Comment
 

Accepted Solution

by:
kamath earned 50 total points
ID: 2007158
there is a system call called *select* in UNIX. you can define a file descriptor set and a filter and specify a timeout:

Look out man pages for select
also here is a code that does it:
 // Reference   : C Library function select(3C)
   fd_set readfds;
   FD_ZERO(&readfds);
   FD_SET(mSocket,   &readfds);
   struct timeval timeout;
   timeout.tv_sec = mReceiveTimeout;
   timeout.tv_usec   = 0;
   int numFds =   select(mSocket+1, &readfds, 0, 0, &timeout);
   int numReceived   = 0;

   // If an error occurred, deal with it
   if (numFds == -1)
      throw(Exception(errno));
 
   // Read  the   input, if available
   else  if  (numFds  ==   1)
   {
      int ret  = recv(mSocket,    aBuf, aBufSize,   0);
      if (ret  == -1)
         throw(Exception(errno));
      else /*  if (ret  != -1) */
         numReceived = ret;
   }
 
   // Otherwise, there was a timeout
   else  if  (numFds  ==   0)
      throw(Exception(errno));

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

832 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