Re: Processes vs Threads

From: Molnar Ingo <mingo_at_PROBLEM_WITH_INEWS_DOMAIN_FILE>
Date: 17 Nov 1994 11:29:21 GMT
Message-ID: <3afeqh$dhr_at_goliat.eik.bme.hu>


psb_at_sambusys.demon.co.uk (Paul Beardsell) writes:

*> What type of application other than number crunching doesn't do system
*> calls all the time? Please just name a few. Because then I'll save any
*> consideration of threads until I have the occasion to write one of the
*> apps.

Here is an example how 'IO mixed witch CPU work' can be optimized with threads:

  1. a non-threaded example:

There are 10 processes in the system, they do the same thing: they use I/O and run computations.

timer click 0
 PROCESS 1: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: context switch.
 PROCESS 2: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: context switch.
 PROCESS 3: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: context switch.
 PROCESS 4: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: context switch.
 PROCESS 5: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: context switch.
 PROCESS 6: runs, gets interrupted by the next timer interrupt: timer click 1
 KERNEL/SCHEDULER: context switch.
 PROCESS 7: runs
-> point of control now

B) the worst-case non-threaded example:

If there would be only one (or a few) process:

timer click 0
 PROCESS 1: runs, wants to do some I/O and blocks. system call calls sched:  KERNEL/SCHEDULER: all processes are waiting for I/O, call idle:

 idle ...
 idle ...
 idle ...
 idle ...
 idle ...
 idle ...
 idle ...
 idle ...
 idle ...

timer click 1
 KERNEL/SCHEDULER: all processes are waiting for I/O, call idle:  idle ...
 idle ...
-> point of control now

C) a threaded example:

timer click 0
 PROCESS 1/THREAD 1: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 2: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 3: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 4: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 5: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 6: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 7: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.
 PROCESS 1/THREAD 8: runs, wants to do some I/O and blocks.  KERNEL/SCHEDULER: thread switch.

 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
 PROCESS 1/THREAD 9: 'real work' is being done,
                         gets interrupted by the next timer interrupt:
timer click 1
 KERNEL/SCHEDULER: process switch.
 PROCESS 2 runs ...
-> point of control now

Some evaluations:


In A), 6 full context switches occured, in C) only one switch happenend, forced by the timer interrupt.
In C), after 6 I/O operations pure code is executed, codenamed as 'real work'. In A) no 'real work' code is executed because the system switches heavily and can't get over those pending I/O jobs. In C) 'real work' code is executed because  the system does 8 'light switches' and gets quickly to the 'real work' section.

In B) the wort case happened: I/O opearations are blocking all processes, CPU time is almost wasted. (note that some systems do useful things during idle as garbage collection, VM reorganisation, etc., but they can't be compared to the usefulness of a SELECT * FROM TAB; for example :) ) The code in A) B) differs from C) because C) is written 'multithreaded', but they do the same thing.

ps. the above examples are NOT real, there are far more things happening in real systems between two time-clicks, so please don't flame.

--
                                                    --------------------------
                                                    | MIngo  (-: Ingo Molnar |
                                                    | mingo_at_hercules.elte.hu |
                                                    --------------------------
Received on Thu Nov 17 1994 - 12:29:21 CET

Original text of this message