Reputation: 3
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
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:
read
but hasn't called it yet.close
on the serial_port
.read
another thread accepts
an incoming socket connection for a critical internal system purpose.serial_port
.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