signals - Can alarm(int) override sleep(int) in C? -


i've created signal handler alarm(). parent forks out process not have pending alarm signal. so, alarm invoked in parent process. alarm(2) invoked, overrides sleep(10) in parent process. parent waits 2 seconds before printing :

"parent retval=8" 

whereas child waits 10 seconds. why alarm() signal override sleep() , sleep return (obviously in case, why? sleep() shouldn't return anything, right?)?

the code given below.

<headers>  pid_t cpid;  int main() {     int retval;     signal(sigalrm, handler);     alarm(2);     if ((cpid = fork()) == 0) {         printf(“i’m child\n”);         retval = sleep(10);         printf(“child retval=%d\n”, retval);     }     else {         printf(“i’m parent\n”);         retval = sleep(10);         printf(“parent retval=%d\n”, retval);     } }  /* handle sigalrm */ void handler(int sig) {     if (cpid == 0)         printf(“running child handler\n”);     else         printf(“running parent handler\n”); } 

sleep returns 0 or number of seconds remaining slept if returns early. also, man page indicates mixing sleep , alarm bad idea.

name    sleep - sleep specified number of seconds  synopsis    #include <unistd.h>     unsigned int sleep(unsigned int seconds);  description    sleep() makes calling process sleep until seconds seconds have elapsed or signal arrives not ignored.  return value    0 if requested time has elapsed, or number of seconds left sleep, if call interrupted signal handler.  conforming    posix.1-2001.  bugs    sleep() may implemented using sigalrm; mixing calls alarm(2) , sleep() bad idea.     using longjmp(3) signal handler or modifying handling of sigalrm while sleeping cause undefined results.  see    alarm(2), signal(2), signal(7) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -