c - Why isn't my signal handler being called? -
im working on assignment uses signals transfer binary message between 2 processes, goal of learning signals (it strange use indeed).
in program, 2 processes communicate code, , 1 transfers message other. sigusr1 represents 0, sigusr2 represents 1. idea process sending message use kill function whichever sigusr message across, , receiving process have signal handler interpret codes.
so here problem. have sender start up. sleeps while waits code sent. reciever sends 2 sigint's signify 'password', using pidof(8)
find pid of sender.
once sender's signal handler has read these signals, recognizes proper password, proceeds send message.
the reciever has gone through few functions, , sleeping every second waiting each bit passed via interrupt. problem is, never happens.
i have set such sender sending bit(0 in case) so:
kill(sigusr1,washingtonpid);
where washingtonpid pid of receiver, , have verified correct pid.
the receiver's handler hooked so:
//in main signal(sigint,bitreceiver); signal(sigusr1,bitreceiver); signal(sigusr2,bitreceiver); //outside main void bitreceiver(int signum) { if(signum == sigusr1) { fprintf(stderr,"sigusr1 - 0"); bit = 0; } else if (signum == sigusr2) { fprintf(stderr,"sigusr2 - 1"); bit = 1; } else //sigint raise(sigint); return; }
where bit global variable. set -1.
here function reads bits:
int receivebit() { while(bit == -1) { sleep(1); } fprintf(stderr,"%d",bit); int bit2 = bit; bit = -1; return bit2; }
so basic run through this: after code has been sent receiver sender, sender starts send kill signals of usr1 , usr2 receiver, should form binary message.
the receiver waiting @ point, sleeping every second. when gets interrupted, handler set bit 0 or 1, kicking out of sleep, printing bit, , returning it.
if let 2 programs run normally, reciever sits in sleep, , handler never called (even though can see calls being made other process.
if stop sender, , manually send kill signals, can send one, maybe 2 signals, both handled properly. after that, , message printed terminal 'user signal 2'. not have in program, , program stops.
any insights why handler isn't being envoked, , why can't manually send more 1 or 2 signals appreciated.
thanks time.
edit: seems people stumped on this. there debugging tips try?
as many have commented, shouldn't doing signals @ all. when goes wrong (and will, did) trying find out wrong when undefined behaviour behind hard if not impossible.
using non async-safe system calls fprintf inside signal handlers can corrupt data since fprintf operating on same stream. same shared variables.
since using linux, signals of same type not blocked, meaning rapid delivery of same signal can result in recursive call handler. once signal caught, disposition of signal reset sig_dfl , needs reestablished in handler again (which can fail if signal delived before has change reestablished).
that why can send maximum of 1 signal of same type before signal gets reset default , terminated program "user signal xx".
i recommend stop tormenting code , grab textbook or tutorial , try follow that.
signal call should avoided if goes. man pages:
the behavior of signal() varies across unix versions, , has varied historically across different versions of linux. avoid use: use sigaction(2) instead.
Comments
Post a Comment