Re: Sharing the timeslice



On May 24, 2:56 am, Andrew Smallshaw <andr...@xxxxxxxxxxxxxxxx> wrote:
On 2008-05-23, karthikbalaguru <karthikbalagur...@xxxxxxxxx> wrote:

On May 22, 2:54\xa0am, Andrew Smallshaw <andr...@xxxxxxxxxxxxxxxx> wrote:

Partly true. \xa0The kernel only handled processes and not threads,
but up until that time threads were implemented as separate processes
created by the clone() system call. \xa0This was basically threading
on the cheap, and did have the potential to cause problems (e.g.
different 'threads' had different PIDs). \xa0It's only recently that
Linux acquired a proper threading implementation.

Thx for that info.
Interesting to know that linux acquired a proper threading
implementation only recently :):)

But, How did they manage with only processes till now ?
Wasn't it a bad design ?

clone() is a special system call that creates a new child process,
however unlike fork() which which at least creates the appearance
of the child process having its own address space a process created
by clone() shares its parents space.  This means that both processes
have access to the same data structures and changes made by one
process show up in the corresponding structure in the other.  This
gives the same semantics (at least for that area) as conventional
threads.

Why do it this way?  Well I've never checked the source (don't
really use Linux myself) but I assume that it is much simpler in
terms of the amount of code that needs adding to at least give the
appearance of supporting threads.  All that is needed is a relatively
small amount of code to create a process but share the address
space.  Creating a fully fledged threading implementation OTOH is
much more work.

However, it does go against some of the reasons for using threads
in the first place.  Sometimes threads are used because they are
supposed to be 'lighter weight' (faster) than a whole new process.
Obviously if they are in fact separate processes then this advantage
is not apparent.

--


I got the below info from internet. Interesting :):)

The scope of a thread can only be specified before the
thread is created.

PTHREAD_SCOPE_SYSTEM
A thread that has a scope of PTHREAD_SCOPE_SYSTEM
will content with other processes and other
PTHREAD_SCOPE_SYSTEM threads for the CPU. That is
if there is one process P1 with 10 threads with scope
PTHREAD_SCOPE_SYSTEM and a single threaded process
P2, P2 will get one timeslice out of 11 and every thread in P1
will get one timeslice out of 11. I.e. P1 will get 10 time more
timeslices than P2.

PTHREAD_SCOPE_PROCESS
All threads of a process that have a scope of
PTHREAD_SCOPE_PROCESS will be grouped together
and this group of threads contents for the CPU. If there is
a process with 4 PTHREAD_SCOPE_PROCESS threads
and 4 PTHREAD_SCOPE_SYSTEM threds, then each of
the PTHREAD_SCOPE_SYSTEM threads will get a fifth
of the CPU and the other 4 PTHREAD_SCOPE_PROCESS
threads will share the remaing fifth of the CPU. How the
PTHREAD_SCOPE_PROCESS threads share their fifth
of the CPU among themselves is determined by the
scheduling policy and the thread's priority.

If there are other processes running, then every
PTHREAD_SCOPE_SYSTEM and every group of
PTHREAD_SCOPE_PROCESS threads (i.e. every
process with PTHREAD_SCOPE_PROCESS threads)
will be handled like a seperate process by the system
scheduler.

Priorities and Scheduling Policy
A PTHREAD_SCOPE_PROCESS thread has a
priority. Whenever a thread is runnable and no
other thread (of this process) has a higher priority
the thread will get the CPU. Note that this might
lead to starvation of other threads. When two or
more runnable threads have the same priority and
no other runnable thread has a higher priority,
then the scheduling policy will determine which
of these highest priority threads to run.

The priority is assigned staticly with
pthread_setschedparam(). The scheduler will not
change the priority of a thread.

The scheduling policy can either be SCHED_FIFO
or SCHED_RR. FIFO is a first come first serve policy.
RR is a round robin policy that might preempt threads.
But again, the policy only effects threads that have
the same priority.

A more extensive description of priorites and policies
can be found in [1] and [2]. Note that these documents
discuss process scheduling, but the principle is the same.

Note: The priority and scheduling policy settings are
meaningless when a thread has scope PTHREAD_SCOPE_SYSTEM.

Realtime Process Scheduling
It is also possible to do realtime process scheduling.
[2] explains how realtime process scheduling works.
sched_setscheduler() is used to set the process
scheduling parameters.

Nice values
The nice value of a process also influences the
scheduling behaviour. A process (and the threads
therein) with a lower nice value (i.e., higher priority)
will get a higher share of the CPU time. Starting a
program with nice works as expected. Using the
nice() system-call from a threaded program has
not been tested (the question is: does a nice()
call effect the whole process or the current thread.
This may well depend on the pthread imlementation
and scope).

.



Relevant Pages

  • Re: RT task scheduling
    ... wide strict realtime priority scheduling". ... doesn't do SWSRPS, but it could with more expensive checking. ... RT apps from getting at CPU time more directly. ...
    (Linux-Kernel)
  • Re: What really happened on Mars? Priority Inversion and Mars Pathfinder
    ... scheduling by the time share. ... The task switching code took up 20% of the CPU cycles, ... the idle state or to do a low priority work. ... time slicing, with each task getting its fixed time slice always, it ...
    (comp.arch.embedded)
  • Re: RT task scheduling
    ... is "system wide strict realtime priority scheduling". ... patch doesn't do SWSRPS, but it could with more expensive checking. ... CPU binding can be used by the application developer to fine tune a complex ...
    (Linux-Kernel)
  • Re: How to increase the priority of process
    ... This process is cpu and memory intensive. ... going to get almost all cpu it wants under default scheduling. ... > This process runs with normal priority. ... In later Solaris versions you could make use of the FX ...
    (comp.unix.solaris)
  • Gnat Scheduling Question
    ... The operating system schedules tasks, but gnat allow us to set ... or are the tasks in gnat are application threads and has nothing to do with the OS scheduling. ... If someone wants to write a new scheduling policy, ... For example, if I want a high priority task to block, even though it does NOT use a shared data object, but if another lower priority task on the "same" cpu is running and is using a shared data object that another task on another CPU needs, then how can I block that high priority task and tell it to yield the cpu to that lower task unitl that lower task finish using that shared data object? ...
    (comp.lang.ada)