?
Solved

Timeout in UNIX

Posted on 1998-09-29
1
Medium Priority
?
682 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
[X]
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
1 Comment
 

Accepted Solution

by:
kamath earned 100 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

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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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