Generate SIGIO

C

caiqian

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
 
R

Robert Gamble

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top