What is a good portable way to implement a global signalable event in a POSIX environment -


the usage case 1 application generates event , sends out signal application cares listen get. e.g. application updates contents of file , signals this. on linux done waiters calling inotify on file. 1 portable way listeners register well-known server, prefer simpler if possible. portable possible ideally means using posix features available.

option using lock files

you can locking file.

signal emitter initial setup:

  • create file well-known name , lock writing (fcntl(f_setlk) f_wrlck or flock(lock_ex)`).

signal receiver procedure:

  • open file using well-known filename , try obtain read lock on (fcntl(f_setlk) f_rdlck or flock(lock_sh)).
  • receiver blocks because emitter holding conflicting write lock.

signal emission:

  • signal emitter creates new temporary file
  • signal emitter obtains write lock on new temporary file
  • signal emitter renames new temporary file well-known filename. clobbers old lock file waiting receivers retain references it.
  • signal emitter closes old lock file. releases lock.
  • signal receivers wake because can obtain read locks.
  • signal receivers should close file they've obtained lock on. won't used again. if want wait condition happen again should reopen file.

in signal emitter, temporary lock file has been renamed on top of original lock file becomes new current lock file.

option using network multicast

have receivers join multicast group , wait packets. have signal emitter send udp packets multicast group.

you can bind both sending , receiving udp sockets loopback interface if want use host-local communication.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -