thread accounting in libpthread

Kazuaki Oda kaakun at highway.ne.jp
Sun Feb 20 03:19:52 PST 2005


Daniel Eischen wrote:

>If there was no running thread before kse_wait(), then after
>kse_wait() returns and if there are completed threads, then
>either one thread completed or N threads completed.  Either
>way, they are all added to the end of the run queue.  But
>the run queue must have been empty to begin with, otherwise
>kse_wait() would not have been called.  So it doesn't matter
>whether they are added to the head or the end of the queue.
>If one thread completes, then it will be run right away
>since it will be the only thread in the queue.  If N threads
>complete (assuming they are all at the same priority) then
>the first thread pulled from the completed list will be
>run first since it will be the first thread added to the
>run queue.
>
>  
>

Did you try to run the program attached to my past mail?  If you run it
under X11 terminal on a UP machine, you probably get a unfair result.

I traced the program, and so I have noticed that the program repeats
the following  operations:

thread 00 is switched in
thread 00 outputs many lines
thread 00 is blocked in the kernel
thread 01 is switched in
thread 01 outputs 1 line
thread 01 is blocked in the kernel
thread 02 is switched in
thread 02 outputs 1 line
thread 02 is blocked in the kernel
thread 03 is switched in
thread 03 outputs 1 line
thread 03 is blocked in the kernel
thread 04 is switched in
thread 04 outputs 1 line
thread 04 is blocked in the kernel
kse_wait() is called because there is no thread ready to run
we return from kse_wait(), and kse_check_completed() is called
we add thread 04 to the tail of run queue
we add thread 03 to the tail of run queue
we add thread 02 to the tail of run queue
we add thread 01 to the tail of run queue
we add thread 00 to the tail of run queue
we return from kse_check_completed()
thread 04 is switched in
thread 04 outputs many lines
thread 04 is blocked in the kernel
thread 03 is switched in
thread 03 outputs 1 line
thread 03 is blocked in the kernel
thread 02 is switched in
thread 02 outputs 1 line
thread 02 is blocked in the kernel
thread 01 is switched in
thread 01 outputs 1 line
thread 01 is blocked in the kernel
thread 00 is switched in
thread 00 outputs 1 line
thread 00 is blocked in the kernel
kse_wait() is called because there is no thread ready to run
we return from kse_wait(), and kse_check_completed() is called
we add thread 00 to the tail of run queue
we add thread 01 to the tail of run queue
we add thread 02 to the tail of run queue
we add thread 03 to the tail of run queue
we add thread 04 to the tail of run queue
we return from kse_check_completed()


Because the program repeats the above operations, I get a result:

--------------------
thread 00: 55666
thread 01: 1161
thread 02: 1112
thread 03: 1112
thread 04: 55494
--------------------

Is this a expected result?  At least this is not for me.  It seems unfair.
As we just add completed threads to the tail of run queue in the order
which kernel returns, thread 00 and thread 04 get much time slice...


--------------------
Kazuaki Oda



More information about the freebsd-threads mailing list