Assignment2Soln

Assignment2Soln - sigprocmask(SIG_BLOCK,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Solution for Assignment2 Question 1 Problem (2) and (3) (race condition and preserving the original disposition) are solved by reliable implementation of sleep. No, another SIGALARM will not be generated after 2 seconds The modified code given below takes care of the previously set alarm if any. static void sig_alrm(int signo) { /* nothing to do, just returning wakes up sigsuspend() */ } unsigned int sleep(unsigned int nsecs) { struct sigaction newact, oldact; sigset_t newmask, oldmask, suspmask; unsigned int unslept,prev_alarm_rem,new_sleep_time,rem_time; if(nsecs == 0) return 0; /* set our handler, save previous information */ newact.sa_handler = sig_alrm; sigemptyset(&newact.sa_mask); newact.sa_flags = 0; sigaction(SIGALRM, &newact, &oldact); prev_alarm_rem=alarm(0); if(prev_alarm_rem!=0) { if(prev_alarm_rem > nsecs) { new_sleep_time=nsecs; rem_time = prev_alarm_time -nsecs; } else { new_sleep_time=prev_alarm_time; } } /* block SIGALRM and save current signal mask */ sigemptyset(&newmask); sigaddset(&newmask, SIGALRM);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sigprocmask(SIG_BLOCK, &newmask, &oldmask); alarm(new_sleep_time); suspmask = oldmask; sigdelset(&suspmask, SIGALRM); /* make sure SIGALRM isn't blocked */ sigsuspend(&suspmask); /* wait for any signal to be caught */ /* some signal has been caught, SIGALRM is now blocked */ unslept = alarm(0); sigaction(SIGALRM, &oldact, NULL); /* reset previous action */ /* reset signal mask, which unblocks SIGALRM */ sigprocmask(SIG_SETMASK, &oldmask, NULL); if(nsecs > prev_alarm_rem){ return (nsecs - prev_alarm_rem + unslept); } else if(rem_time > 0) { alarm(rem_time+unslept); return unslept; } } Question 2) In a single-threaded processes since there is only one thread, If the thread is waiting for input it will not be able to fork a new process. So the problem mentioned would not occur in single threaded processes Question 3) Answer from Tanenbaum's book (Advantages : efficiency, portability etc, Disadvantage: blocking calls)...
View Full Document

Page1 / 2

Assignment2Soln - sigprocmask(SIG_BLOCK,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online