Threads
- 2 things make a process a process: its resources (address space,
etc), and the fact it gets scheduled. Every process differs from
every other one in that they have different address spaces, and
they get scheduled at different times.
- These are orthogonal properties, we could remove one with out
the other.
- If we create an entity that gets run at separate times, but
shares resources with other of these entities, then we have what
are know as threads.
aside: Dragons hate threads. Here's a picture of a dragon burning
threads:

always be careful letting a dragon near your operating system.
- The management of these new "lightweight processes" (they lack
the baggage of their own address space) is usually done by the OS.
- Basic Multithreading
- for a multi-threaded process, we still have a single address space
and we have a PCB.
- But if we have multiple threads, we will need a Thread
Control Block for each thread, and we will need a separate
call stack too.
- This way threads will be at different places in the same
code, modifying the same data.
- Benefits: Faster creation of thread than process, faster
exiting, faster switching, easy communication between threads.
- Examples: GUI threads while program running; network
servers, running threads on different processors, etc.
- All this is added complexity for the OS. It dispatches at
the thread level, but if a process is blocked or suspended,
then all threads are stopped.
- Thread functionality:
- Spawn. When a process is spawned, a thread for that
process is spawned. That thread can spawn other threads.
- Block. A single thread can block for, say, I/O.
- Unblock, obviously.
- Finish.
- Synchronization? Next week.
- Types of threads
- 2 Kinds of threads, user level and kernel level.
- In user level threads, the application itself manages the
threads. Essentially when the application compiles, the thread
library linked in contains a little dispatcher. When the
process is running, the dispatcher can schedule the threads.
- Since this dispatcher isn't in the kernel, it can't be
invoked with an interrupt, so the only way to switch threads is
for a thread to call the user-level dispatcher directly.
- Advantages:
- thread switches are fast since we don't have to jump to
the kernel.
- Can create application specific scheduling.
- Can run on any OS.
- Disadvantages:
- When a thread blocks, the process blocks.
- Cannot use multiple threadsfrom same process on multiple cores.
- In kernel level threads, management is done in the kernel,
just like processes. Basicaly, schedule threads, not processes.
- Multicore
- Amdahl's law: The speedup of parallelizing a process that
takes time T on N processes, where f is the fraction of the time
that is parallelizable is:
\[\begin{align}
S & = \frac{ST}{PT} \\
& = \frac{T}{\frac{fT}{N}+(1-f)T}\\
& = \frac{1}{(1-f)+\frac{f}{N}}
\end{align} \]
- This looks good, but note that even with minimal amounts of
serial code (%10), the speedup nearly halves.
- In "embarasingly parallel" problems, we often do get perfect
speedup.
- Here's a diagram of the threads in Valve's Engine
software
- Semantics of fork() and exec() system calls
Does fork() duplicate only the calling thread or all threads? System dependent. Many version of UNIX have multiple fork calls to handle. Linux is weird.
- Signal handling
- All threads
- The thread that wants it
- a handler thread.
- Thread pools
- Java
- Implemented by JVM.
- Typically NOT user threads, though originally was
- JVM calls kernel level threads
- No global variables, so thread must be passed shared object.
- Windows
- Implemented pretty much as described.
- uses C++, so a process is an object.
- In XP, each thread contains:
- A thread id
- Register set
- Separate user and kernel stacks
- Private data storage area
- In the kernel there is an executive thread bloc
- Windows 7 also allows user level threads they call Fibers.
- "In general, fibers do not provide advantages over a
well-designed multithreaded application. However, using fibers can
make it easier to port applications that were designed to schedule
their own threads."
- Linux
- Linux does not do threads as described above. It uses the more
generic term "tasks".
- The traditional fork( ) system call completely duplicates a
process ( task ).
- clone( ) creates new task that shares resources
- one of the arguments is a flag:
- CLONE_FS File-system information is shared
- CLONE_VM The same memory space is shared
- CLONE_SIGHAND Signal handlers are shared
- CLONE_FILES The set of open files is shared
- All of them together make for a traditional thread model.