Solved

Timeout in UNIX

Posted on 1998-09-29
1
642 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now