c - how is `while(1)` optimized not to take all resources? -
suppose have code this:
while(1) { scanf("%c", &key); // if e or e, exit if (key == 'e' || key == 'e') break; }
this not take resources of single core. just... "sits" there until presses e. question is: how runtime find out should not take resources of core waiting scanf? scanf special case in i/o , such os schedules out until key pressed? can somehow force while
ing time say, adding a++
inside loop?
is scanf special case in i/o , such os schedules out until key pressed
that's happening. in case scanf
blocking operation. is, if can't executed (nothing in input buffer) os puts process sleep, remembering wake when turns up.
can somehow force whileing time say, adding a++ inside loop
an a++
won't change anything. won't able peg cpu long have long-waiting blocking calls.
until key pressed
in linux input "cooked". is, pressing single key isn't typically enough (you need hit return before process gets chance @ data).
as basile correctly mentions in comments scanf
may not block. if there's enough input in stdio buffer scanf
return it. if not call read(2)
can block.
Comments
Post a Comment