alarm() problem

Hello, i’m a qnx beginner, and i’ve a problem while using the alarm()
function. My code is:

#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv)
{
FILE *fd;
int error;
char buffer[255];

fd = fopen ("/dev/ser2", “rwb”);

alarm (5);
error = fread (buffer, 1, sizeof (buffer), fd);
alarm (0);

if (error == -1)
fprintf (stderr, “Erreur\n”);

printf (“FIN\n”);
return 0;
}

I want to time out the read call, but i get a Task Exeption when the SIGALRM
is issued. Is there a solution ?

Thanks for your answers.

Sebastien Cantos <scantos@technodiva.com> wrote:

Hello, i’m a qnx beginner, and i’ve a problem while using the alarm()
function. My code is:

#include <stdio.h
#include <unistd.h

int main (int argc, char **argv)
{
FILE *fd;
int error;
char buffer[255];

fd = fopen ("/dev/ser2", “rwb”);

alarm (5);
error = fread (buffer, 1, sizeof (buffer), fd);
alarm (0);

if (error == -1)
fprintf (stderr, “Erreur\n”);

printf (“FIN\n”);
return 0;
}

I want to time out the read call, but i get a Task Exeption when the SIGALRM
is issued. Is there a solution ?

That is expected behaviour – getting hit by most signals will result
in your process terminating.

You need to have a handler for the signal – look at using either
the signal() or sigaction() calls to setup such a signal handler.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

Sebastien Cantos <> scantos@technodiva.com> > wrote:

alarm (5);
error = fread (buffer, 1, sizeof (buffer), fd);
alarm (0);

if (error == -1)
fprintf (stderr, “Erreur\n”);

I want to time out the read call, but i get a Task Exeption when the SIGALRM
is issued. Is there a solution ?

You need to have a handler for the signal – look at using either
the signal() or sigaction() calls to setup such a signal handler.

One other thing is that this way of timing out a read is not entirely
reliable. If it’s possible for higher priority processes to take the
CPU away from you for five seconds or more, then there’s a chance that
the signal will hit you before you even enter fread(), and fread() will
never time out.

Even if you know enough about all the proceeses running in your system
to rule out that possibility, there still is a chance that fread() may
not notice the signal. That’s because fread() is defined to behave as
if it read one character at a time using getc(). If the signal arrives
between two calls to getc(), fread will keep trying to read, and
potentially block forever. Typically, this kind of problems don’t
happen during testing, only after the finished product is shipped to the
customer…

Using read() instead of fread() cures this problem.

Even better: since it’s a character device that you’re reading from, use
dev_read() and get rid of the signals altogether. Or, you care about
portability more than about efficiency and simplicity, use tcgetattr()
and tcsetattr() to set up the timeout, and then call read().


Wojtek Lerch QNX Software Systems Ltd.