468,513 Members | 941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Generate SIGIO

Hi,

I am tring to use the following code to generate SIGIO, but there is no
signal. Is there anything wrong?

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>

void
handle (int sig)
{
printf ("GET SIGIO\n");
}

int
main (void)
{
fcntl (0, F_SETFL, FASYNC);
fcntl (0, F_SETOWN, getpid ());
signal (SIGIO, handle);
write (0, "ls\n", 3);
return 0;
}

Qian

Sep 3 '06 #1
1 5182
ca*****@gmail.com wrote:
Hi,

I am tring to use the following code to generate SIGIO, but there is no
signal. Is there anything wrong?
There is no SIGIO in Standard C.
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
Not a Standard header.
void
handle (int sig)
{
printf ("GET SIGIO\n");
Calling printf from a signal handler is undefined behavior in Standard
C.
}

int
main (void)
{
fcntl (0, F_SETFL, FASYNC);
fcntl (0, F_SETOWN, getpid ());
fcntl is not a Standard C function and is off-topic here.
signal (SIGIO, handle);
write (0, "ls\n", 3);
write is not a Standard C function and is off-topic here. In Standard
C you can use the raise() function to send a signal to the executing
process instead of performing an operation in the hopes that it will
send such a signal to your process.
return 0;
}
The signal functionality provided by Standard C is limited almost to
the point of uselessness. Below is a simple Standard C example using
signal and raise:

#include <stdio.h>
#include <signal.h>

volatile sig_atomic_t signal_pending;

void handle (int sig) {
signal_pending = 1;
}

int main (void) {
if (signal(SIGTERM, handle) == SIG_ERR)
puts("failed to install signal handler");
else
puts("successfully installed signal handler");

raise(SIGTERM);
if (signal_pending) {
puts("received signal");
signal_pending = 0;
}
return 0;
}

The signal functionality provided by Standard C is limited, unreliable,
and chock-full of caveats. The POSIX Standard greatly extends the
usefulness of signal handling and adds functionality. Discussion of
the POSIX functions should be done in comp.unix.programmer.

Robert Gamble

Sep 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Garry Dawkins | last post: by
9 posts views Thread by Henk Verhoeven | last post: by
1 post views Thread by c_attitude | last post: by
1 post views Thread by A Traveler | last post: by
reply views Thread by ward | last post: by
2 posts views Thread by bthubbard | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.