signals - C SIGALARM clarification -


"rewrite 'fgets' include alarm if after 5 seconds user doesn't enter data, 'fgets' return pointer null (times out)"

this 1 problem of large homework assignment , starting @ signals, catching, handling, etc. said book , lecture slides lacking on this...

i idea of creating handler catch type of exception, , understand how alarm works, don't understand how interface work.

just thinking "fgets" right now... if put alarm in prior other work being done (just before return pointer):

char *fgets(//...//) {     alarm(5);     char * to_return = null;     //...// 

if alarm goes off before user types in data, can handler "return null"? won't execution resume within "fgets" after handler done? guess i'm confused handler can inside of function (ie can force function return value, can modify variables declared within function, can force jump different function call, etc.)

first off, signal handler separate thread of execution. when signal raised, "normal" execution of code interrupted, signal handler executed, , once handler returns, execution of code resumed @ point interrupted.

so, 1) signal handler cannot effect return fgets() (at least not directly), , 2) cannot return value. (you might note signal() function, installs signal handler, demands pointer a function taking integer parameter , having void return type.

what signal handler can set value. standard (c99, chapter "signal handling", 7.14.1.1 (5)) says on this:

if signal occurs other result of calling abort or raise function, behavior undefined if signal handler refers object static storage duration other assigning value object declared volatile sig_atomic_t, or signal handler calls function in standard library other abort function, _exit function, or signal function first argument equal signal number corresponding signal caused invocation of handler.

what means is, can declare variable of type volatile sig_atomic_t both fgets() , signal handler can "see" it, initialize default value, , have signal handler change value fgets() can see signal fired.

of course, if fgets() waits until user enters something, wait after signal fired... how solve matter of how doing input. either use non-blocking check input getch() (which isn't iso/ansi c), or rely on behaviour of read() r. mentioned (which relying on posix layer underneath fgets()), or else. cannot think on strictly iso/ansi c way this, depends on learned / did in course.

oh, , if feel brave, tell instructor really bad idea (to avoid calling him "stupid") redefine standard functions different behaviour. if want fgets() isn't fgets(), give different name, e.g. fgets_timed().


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -