gleb zheglov
gleb zheglov

Reputation: 3

interruption of blocked I/O functions

I have a thread, that reads serial device (/dev/ttyUSB0), and writes data to stdout :

void io_thread_func () {

    int serial_port = open(settings.port, O_RDWR);

    while (1) {

        char buf[100];

        int n = read(serial_port, buf, 100);

        fwrite(buf, 1, n, stdout);

    }

}

How I can interrupt "read(...)" syscall from another thread ? Can I call "close(serial_port)" from another thread to interrupt all I/O blocking functions ? (I need to interrupt I/O blocking functions to close the thread correctly)

Upvotes: 0

Views: 167

Answers (1)

David Schwartz
David Schwartz

Reputation: 182865

How i can interrupt "read(...)" syscall from another thread ? Can i call "close(serial_port)" from another thread to interrupt all I/O blocking functions ? (i need to interrupt I/O blocking functions to close the thread correctly)

You can send a signal to the thread. That will interrupt the read function.

You absolutely cannot close the serial port from another thread. That can cause all kinds of disasters. Consider:

  1. This thread is about to call read but hasn't called it yet.
  2. Another thread calls close on the serial_port.
  3. Before the first thread can call read another thread accepts an incoming socket connection for a critical internal system purpose.
  4. That thread happens to get the same descriptor as serial_port.
  5. This thread calls read then fwrite, dumping the data received on the critical socket to stdout.

Good thing it's stdout. Because a bug once caused that exact same scenario -- except sensitive data was sent to random inbound connections from the Internet rather than stdout.

Upvotes: 2

Related Questions