Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!pacbell!rtech!ingres!Ingres.COM!jeff From: jeff@Ingres.COM (Jeff Anton) Newsgroups: comp.lang.c Subject: Re: When do you use "if ( a = b )"? (was Re: Funny mistake) Message-ID: <1991Mar19.203341.8682@ingres.Ingres.COM> Date: 19 Mar 91 20:33:40 GMT References: <8148@rsiatl.Dixie.Com> <15481@smoke.brl.mil> <775@camco.Celestial.COM> <65837@eerie.acsu.Buffalo.EDU> Reply-To: jeff@Ingres.COM (Jeff Anton) Organization: Ingres Division, ASK Computer Systems. Lines: 32 Consider: if (tmp = *wakeup_pid) { kill(tmp, SIGALRM); *wakeup_pid = 0; } wakeup_pid is in shared memory. Several programs may run this code segment at once. If it were written like: if (*wakeup_pid) { kill(*wakeup_pid, SIGALRM); *wakeup_pid = 0; } Then there is a race condition where the loser process might call kill(0, SIGALRM) which will be very nasty as it kills the current process group. Of course you could write this like: tmp = *wakeup_pid; if (tmp) { kill(tmp, SIGALRM); *wakeup_pid = 0; } But the separation of the assignment and the if invites new code between the two which will increase the likelyhood of race conditions. There is no side effect in the assignment in the if in this case unless you consider any shared memory access to have side effects. Jeff Anton (p.s. Semaphores would be a safer way to deal with this) (p.p.s. All such code which has such complex behavior but reads simply should have a comment describing the complexities)