简体   繁体   中英

calling an elaborate function from SIGINT handle - when is it ok?

I have a large function, and I want to allow the user to stop it and send it to a recovery procedure (implemented by elaborate_function ). I thought of using a SIGINT handle:

void handle(){
    FILE * devNull = fopen("/dev/null", "w");
    elaborate_function(devNull);
}

void foo(){
    signal(SIGINT, handle);
    ...
}

The elaborate_function() does the following:

  1. file manipulation
  2. printf / fprintf
  3. calls other functions
  4. system calls ( tar, cp, mv... )
  5. reset

I know that using printf from a signal handler is considered unsafe ( as said here) but from what I understand, the unsafety only means possible bad output, and this doesn't bother me in this specific case (I'll maybe pass /dev/null as the FILE* fout parameter to block output, as suggested here ).

Aside from the <stdio.h> functions, is there any other problem with what I intend? This is the simplest way of a non-blocking-input-mechanism I could think of.

Running only on Linux, so I don't worry about portability

There are many unsafe function because internally, these function can lock something (like a mutex), so the "worst" scenario for you is that to have a race condition causing a deadlock. You can also have race condition causing sigsev, and many other hellish bug that you want to avoid (since race condition is hard to debug).

As you have said, only the function listed here are safe.

For a non-blocking mecanism input : You can create a thread that will pool the input file. You can use signal to raise a flag (like "must_read_input") and in your code, call "elaborate_function" as soon as you see the flag raised. You can modify your "elaborate_function" to use only signal safe function.

There is plenty way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM