Re: linux sleep implementation



Ask wrote:

1> sleeping in the kernel is like this (source taken from Robert Love
Book, page 53)

add_wait_queue(q, &wait);
while (!condition) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
...............................

schedule();
}
set_current_state(TASK_RUNNING);
remove_wait_queue(q, &wait);

Isn't it possible that just before the schedule is invoked, the task is
pre-empted. What if it is preempted because of the event that it
intends to wait for occurs. Will it give weird result in that case ?

It can be preempted. The effect is the same as if schedule was called.
When the signal is delivered, the task changes back to TASK_RUNNING.

I vaguely remember the above code to be a workaround to avoid a race
condition when using a 2.4.x kernel. Newer kernels have a call which
allows you to go to sleep and check your condition atomically.


2> What happens after the task in TASK_INTERRUPTIBLE state receives a
signal and finshes with the associated handler. Does it go back to
sleep again ?


No, it stays in TASK_RUNNING.

Kind regards,

Iwo

.



Relevant Pages

  • Re: Sleep and older children
    ... kids will fit it all in and still find a balance, because unlike me, I ... and the need for a manageable schedule. ... To me it's a given that teens need a lot of sleep, ...
    (misc.kids)
  • Re: Sleep and older children
    ... we'll have family time in the evening. ... You want your older kids to be on the same schedule as the toddler. ... move family time later to accomodate *their* sleep needs. ... likely have no luxury of sleeping in later since the young one gets up ...
    (misc.kids)
  • Re: Sleep and older children
    ... sleep in until whenever encourages a behavior that becomes ... the groove when the school year starts. ... natural for a teen's sleep schedule to gradually drift. ... but sleeping ...
    (misc.kids)
  • Re: Sleep and older children
    ... really make up the sleep deficit by sleeping away the ... deficit means avoiding those wild swings in schedule ... knowing I was suffering from a major sleep deficit - and sure enough, ... Especially teens, who are natural ...
    (misc.kids)
  • Re: dcps out there... sleep question
    ... Getting him to understand that here we have meal and snack times and the things that go along with that; sitting at a table, once you hop down to play you are done... ... Aid him in learning to sleep at this place? ... I agree with much of what has already said, and want to add that over the years I have had very few children who could not acclimate to our daily schedule, which includes a mandatory nap/quiet time for all of the cherubs, regardless of age. ... There is no talking, I have 'kids' classical or multicultural music playing quietly, and all told, once everyone is settled, nap lasts for approximately an hour and a half. ...
    (misc.kids)