kwonryul
kwonryul

Reputation: 553

Is there a difference between "I/O block status" and "sleep"?

Can SIGCONT wake up the sleeping process?

I learned the day before yesterday that signal handlers fail the "sleep" of the process.

In the same way, I tried to fail "read" with a signal handler.

The code is as follows.

#include <signal.h>
#include <unistd.h>

void signal_handler(int signo)
{
        write(1, "\nI've got signal\n", 17);
        return;
}

int main()
{
        char buf[10];

        signal(SIGINT, signal_handler);

        read(0, buf, 1);

        write(1, buf, 1);

        return 0;
}

However, after the signal handler was carried out, the process went back into I/O block state.

The following code was also executed for re-verification.

#include <signal.h>
#include <unistd.h>

void signal_handler(int signo)
{
        write(1, "\nI've got signal\n", 17);
        return;
}

int main()
{
        char buf[10];

        signal(SIGINT, signal_handler);

        sleep(100);

        write(1, "awake", 5);

        return 0;
}

In this case, after receiving the signal handler, the process was no longer asleep.

Is there a way to get out of the blocked state after receiving the signal and continue the process? (with the input failed)

Upvotes: 1

Views: 76

Answers (1)

mar
mar

Reputation: 154

According to documentation of signal() function:

The behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below.

...

Portability

   The only portable use of signal() is to set a signal's disposition to
   SIG_DFL or SIG_IGN.  The semantics when using signal() to establish a
   signal handler vary across systems (and POSIX.1 explicitly permits
   this variation); do not use it for this purpose.

Thus, you should use sigaction() instead of signal().

If you replace signal() with sigaction() in your program it should work as expected.

Upvotes: 1

Related Questions