Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering Signal Handler For I/O

Tags:

c

linux

Using C on Linux, how would I go about triggering a signal handler every time I write data to a buffer using the write() function. The handler will be reading all data written to the buffer at the time of execution.

like image 617
Ohanes Dadian Avatar asked Feb 27 '23 07:02

Ohanes Dadian


1 Answers

Sockets support this by enabling async mode on the socket file descriptor. On Linux this is done using fcntl calls:

/* set socket owner (the process that will receive signals) */
fcntl(fd, F_SETOWN, getpid());

/* optional if you want to receive a real-time signal instead of SIGIO */
fnctl(fd, F_SETSIG, signum);

/* turn on async mode -- this is the important part which enables signal delivery */
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_ASYNC);
like image 61
mark4o Avatar answered Mar 05 '23 15:03

mark4o