Download OperatingSystems_FA15_4_Threads

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

CP/M wikipedia , lookup

VS/9 wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

RSTS/E wikipedia , lookup

Library (computing) wikipedia , lookup

Unix security wikipedia , lookup

Distributed operating system wikipedia , lookup

Burroughs MCP wikipedia , lookup

Spring (operating system) wikipedia , lookup

Security-focused operating system wikipedia , lookup

DNIX wikipedia , lookup

Process management (computing) wikipedia , lookup

Thread (computing) wikipedia , lookup

Transcript
Operating Systems
Dr. Jerry Shiao, Silicon Valley University
Fall 2015
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
1
Threads
 Overview

Threads









Process Characteristics: Thread of Execution
User-Level Threads and Kernel-Level Threads
Multithreading Models
 Many-to-One Model
 One-to-One Model
 Many-to-Many Model
Benefit of Multithreading
 Multicore Programming
 Symmetric Multiprocessor (SMP) Design Issues
Thread Libraries
 Linux POSIX Threads
 Win32 Threads
Threading Issues
Thread-Specific Data
Process Vs Threads
Operating System Structures

Monolithic Operating System
Microkernel Operating System

Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
2
Threads

Basic unit of CPU utilitzation:

Process
Code
Data
Registers
Files
Stack
The resource ownership
characteristic of a process.
Thread
The thread or scheduling
characteristic of a process.

Process Characteristics:


Process image in Virtual Address Space (Program, Data, Stack
and Attributes in Process Control Block).
Resource Ownership Characteristic: Controls Resources, such
as memory, I/O Channels, I/O Devices, and Files.


Referred to as the “process” or “task”.
Scheduling/Execution Characteristic: Follows an execution path
(trace) through one or more program.


Execution State (Running, Ready, etc.) and dispatch priority.
Referred to as the “thread” or “lightweight process”.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
3
Threads

Execution Thread

Single thread of execution per process.
Execution of a process follows an
execution path through one or
more programs.
Int main ( )
…
Proc1( )
…
return
Int Proc1( )
…
Proc2( )
…
return
Int Proc2( )
…
…
return
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
4
Threads

Execution Thread

Multithreading is the ability of an OS to support multiple,
concurrent paths of execution within a single process.
Execution of a thread follows an
execution path, but over separate
processors.
Concurrent execution of different
threads of a process on separate
processor.
Int main ( )
…
…
return
Int Proc1( )
…
…
return
Int Proc2( )
…
…
return
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
5
Threads



Basic unit of CPU utilitzation
Thread is flow of control within
process.
Multithreaded Process


Operating System supports
multiple, concurrent paths of
execution within a single process.
Thread has:





Thread ID.
Program Counter.
Register Set.
Stack.
Shares with other threads:



Code
Data
Registers
Files
Stack
Thread
Code
Data
Files
Registers
Registers
Registers
Stack
Stack
Stack
Code Section.
Data Section.
Thread
Operating System
Resources ( files and signals ).
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
6
Threads



Thread Functionality
Thread, similar to process, has execution states for
synchronization and dispatching
Operations associated with thread state changes:





Spawn: New process spawned, a thread in the new process is
created. Has Program Counter, Register Context, Stack Space.
Block: Waiting for an event, thread will block and save its User
Registers, Program Counter, and Stack Pointers.
Unblock: Event occurs, thread will be moved to the Ready
Queue for dispatching.
Finish: Thread exits, Register Context,Stack Space deallocated.
Benefits of multithreaded programming can be broken
down into four major categories:




Responsiveness.
Resource Sharing.
Economy.
Scalability.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
7
Threads


Multithread Process
Benefits

Responsiveness


Allow a program to continue, even if it is blocked or executing lengthy
operation.
Web Server can create separate thread listening for connections. When new
connection is made, Web Server will create a new thread to service the new
connection.
2) Create new thread to
service the request.
1) Request
Client
Server
thread
3) Resume listening for
additional client request.



Less time to create new thread in an existing process than to create a
brand-new process. Thread creation is 10X faster than process creation.
Less time to terminate a thread than a process.
Less time to switch between two threads within the same process than to
switch between processes.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
8
Threads


Multithread Process
Benefits

Resource Sharing




Threads share the memory and resources of the parent process.
Application can have several different threads of activity within the
same address space.
Word processor can have thread displaying graphics, thread
responding to user keystrokes, thread for performing spelling and
grammer checking in the background.
Threads enchance efficiency in communication between different
executing programs.


Threads within same process share memory and files, threads can
communicate with each other without involving the kernel (i.e.
protection and Message Queues.
Economy



Threads share the resources of the parent process.
More economical to create and context-switch threads.
Process creation and process context-switch slower than thread
creation.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
9
Threads
 Multithread Processing
 Benefits

Scalability


Threads may run in parallel in multiprocessor architecture.
Multithreading on a multi-CPU machine increases parallelism: Uses
of threads simplifies a program that logically has different functions.





Copyright @ 2009 John
Wiley & Sons Inc.
Shared memory: File Server process receives file request and opens a
thread to service the request. Data sharing between threads handling
requests faster than using kernel message queues.
Foreground/Background: Spreadsheet process could display menus
and read user input. Another thread execute commands and updates
the spreadsheet.
Asynchronous: Word processor writes its buffer to disk once every
minute. A thread can schedule itself with kernel and periodically
awaken to backup its buffer.
Speed of execution: Multi-threaded process can read and block for
data, while another thread can process the data.
Modularity: Processes that has separate activities or variety of sources
and destinations can implement these activities as threads.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
10
Threads
 Multithread Processing
 Uniprocessor
Single-threaded process, the results from
two Remote Procedure Calls (RPC) are
obtained in sequence.
Process has to wait for a response from
each server.
Multi-threaded process has separate
thread for each RPC results in a
performance improvement. Once one
thread blocks waiting for RPC response,
the other thread will execute and block for
its RPC response. Both the RPC request
and RPC response will execute
concurrently.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
11
Threads
 Multithread Models
 User-level Threading



Many user-level threads supported by one kernel thread.
User-level threads managed by Thread Manager in user space.
Advangages:




User-level threads managed by user-space thread library.
Does not require Kernel mode privileges and does not need to
switch to Kernel mode for Thread Management.
User Thread Manager scheduler does not affect System Kernel
level processes.
User Thread environment in the Threads Library, not in Kernel: User
Thread Library runnable under different Operating Systems.
User-level Threads
Threads Library
K
Copyright @ 2009 John
Wiley & Sons Inc.
Kernel-level Threads
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
User Space
Kernel Space
12
Threads
 Multithread Models
 User-level Threading

Disadvantages:



Since user-level threads managed in user space, user-threads
cannot be scheduled by kernel scheduler for multiprocessor
systems.
When user-level thread makes a blocking system call and kernellevel thread blocks, all user-level threads are blocked.
Systems:

Solaris
Null Fork: Time in Usec to create, schedule, execute, and complete
forking a process or thread.
Signal Wait: Time in Usec to signal a wait process or thread and
then wait on a condition (synchronizing two process or thread).
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
13
Threads
 Multithread Models
 User-level Threading
Process B is executing in its Thread 2. Kernel is
UNAWARE of the User-level Threads managed in User
Space.
Thread 2 makes an I/O Request, Process B is in the
Blocked state.
NOTE: Thread 2 state is still “Running” state.
Process B has
completed its
quantum, and is
placed in the Ready
State.
NOTE: Thread 2 state
is still “Running” state.
Thread 2 waiting on
Thread 1 semaphore.
Thread 2 is Blocked
and Thread 1 is in
Running State.
NOTE: Process B
state is “Running”
state.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
14
Threads


Multithread Models
Kernel-level Threading



One user-level thread supported by one kernel thread.
Kernel-level threads scheduled by Kernel scheduler.
Advangages:



Disadvantages:


Each user-level thread has kernel-level thread and user call of
blocking system calls does not affect other user-level threads.
Threads scheduled by kernel scheduler to different CPUs in
multiprocessor system.
Creating user-level thread always creates kernel-level thread.
Overhead creating kernel-level thread high.
Systems:

Linux, Solaris
User-level Threads
User Space
Kernel Space
K
Copyright @ 2009 John
Wiley & Sons Inc.
K
K
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
K
Kernel-level Threads
15
Threads
 Multithread Models
 Hybrid-level Threading


Multiplex many user-level threads to smaller or equal number of
kernel-level threads.
Advantages:



Disadvantages:


With multiple kernel-level threads, user call of blocking system calls
does not affect other user-level threads.
Threads scheduled by kernel scheduler to different CPUs in
multiprocessor system.
Complex.
Systems:


User-level Threads
Linux 2.4,
Solaris (older)
K
Copyright @ 2009 John
Wiley & Sons Inc.
K
K
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
K
Kernel-level Threads
16
Threads


Multithread Processing
Multicore Programming


On system with multicore, concurrency means that the threads
can run in parallel, as the system assigns a separate thread to
each core.
Areas present challenges in programming multicore systems:





Dividing Activities: Application must find areas that can divided into
separate concurrent tasks.
Balance: Concurrent tasks must perform equal work of equal value.
Data Splitting: Data accessed and manipulated by concurrent tasks
must be divided to run on separate cores.
Data Dependency: Concurrent tasks must be synchronized to
accommodate data dependency.
Testing and Debugging: Testing and debugging concurrent tasks
are inherently more difficult than testing and debugging singlethreaded tasks.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
17
Threads


Multithread Processing
Multicore Programming
Single Core
T1
T2
T3
T4
T1
T2
T3
T4
T1
. . .
Single Core
Concurrency: Execution of the threads interleaved over time.
Core 1
T1
T3
T1
T3
T1
T3
T1
T3
T1
. . .
Core 2
T2
T4
T2
T4
T2
T4
T2
T4
T2
. . .
Multiple Core
Concurrency: Threads run in parallel, Operating System assigns
separate thread for each core.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
18
Threads
 Multithread Processing
 Category of Parallel Processor Systems
 Dedicated Memory Multiprocessor




Processors has dedicated memory, each processor is in a selfcontained server.
Interprocess communication through Networking facility.
Clusters: Group of interconnected computers working as unified
computing resource (Distributed Database, or RAID).
Shared Memory Multiprocessor



Processors share a Common Memory.
Interprocess communication through Shared Memory.
General Classification:

Master/Slave Architecture: OS Kernel runs on particular processor.



Master (OS Kernel) schedules processes or threads.
Slave sends request to Master for service (I/O Request).
Symmetric Multiprocessor ( SMP): OS Kernel runs on any processor.


Copyright @ 2009 John
Wiley & Sons Inc.
Each processor does self-scheduling.
OS Kernel executes in parallel.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
19
Threads
 Multithread Processing
 Symmetric Multiprocessor (SMP) Design Issues:

Manages processor and other computer resources similar to
multiprogramming uni-processor system.


Simultaneous scheduling concurrent processes or threads:






User applications multiprogramming same between uni-processor or
multiple processors.
Kernel must be reentrant.
Kernel tables and structures must be protected and avoid deadlocks.
Multiple threads from same process simultaneously executes on
multiple processors.
Sychronization: Mutual exclusion and event ordering.
Memory Management: Paging mechanism on different processors
must be coordinated when page or segment are shared.
Reliability and Fault Tolerance: Scheduler must detect failed
processor and restructure Kernel tables.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
20
Threads
 Multithread Processing
 Symmetric Multiprocessor (SMP) Organization

Multiple Processors:


Control Unit, ALU, Registers, Memory Caches.
Shared Main Memory and Shared I/O Devices through Shared Bus.
Processor
Processor
L1 Cache
L2 Cache
Processor
L1 Cache
L2 Cache
Kernel can execute on
any processor.
Each processor is selfscheduling from
available processes or
threads.
Kernel written as
multiple processes or
threads.
L1 Cache
L2 Cache
System Bus
I/O Subsystem
Main Memory
I/O Adapter
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
21
Threads
 Thread Libraries
 Linux POSIX Threads ( Pthreads )

Monolithic Operating System: Layered approach, but all
layers executed in kernel space.
 User-level Library


Library Code/Data in user space ( no system calls ).
Kernel-level Library





Library Code/Data in kernel space ( use system calls ).
Current Linux supports mapping User-level Threads to
Kernel-level Threads.
Linux does not recognize distinction between Process and
Threads.
Processes and Threads share resources such as files and
memory, but cannot share user stack.
Minimizes context switch when kernel switches from one
process/thread to another process/thread.


Copyright @ 2009 John
Wiley & Sons Inc.
Kernel checks page directory with the next process/thread.
If the same, then the memory space is shared, the context switch is
jump from one location of code to another location of code.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
22
Threads
 Linux POSIX Threads ( Pthreads )
Process halted and can only resume
by action from another process
(process in debut mode and placed in
“Stopped” state.
Linux
Process/Thread
Execution State.
Running process state is either
executing or it is ready to execute.
Process has terminated, but waiting
for parent process to acknowledge.
Process is in Blocked state, but
process is waiting for hardware
condition and will not handle any other
signal or event.
Process is in Blocked state, but can
accept other signal or event.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
23
Threads


Thread Libraries
Win32 Threads




Kernel-level Library
Windows process design supports variety of OS subsystem
environments (OS/2, POSIX, Win32).
Hybrid Microkernel: Kernel space native process structures and
services are simple and general purpose, each OS subsystem
emulates a particular process structure in User Space.
Windows object-oriented structure.


Window Process represented as an object type, that owns resources, such
as memory and files.
Window Process must contain one thread object type to execute: thread is a
dispatchable unit of work that executes sequentially and is interruptible.
Process Object Type controls resources
through Object Table.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
24
Threads


Thread Libraries
Win32 Threads

Windows Process represented by an object type.
Windows Process
represented by an Object
Type.
Each process defined by a
number of attributes and
encapsulates a number of
actions, or services, that it
may perform.
Copyright @ 2009 Pearson
Education, Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
25
Threads
 Thread Libraries
 Win32 Threads
Windows is a hybrid microkernel rather
than monolithic kernel:
a) Subsystems run in user-mode server
processes.
b) Most system components run in
same address space as the kernel.
Copyright @ 2009 Pearson
Eduction, Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
26
Threads
 Thread Libraries
 Win32 Threads
Standby: Thread selected to run next
on a particular processor. Thread waits
until the processor is made available.
Ready: May be scheduled for
execution.
Running: Standby thread switched into
Running State and begins execution
until preempted by higher priority
thread, exhausts time slice, blocks, or
terminates.
Transition: After Waiting State and
ready to execute, but resources not
available (thread’s stack paged out of
memory.
Terminated: Thread terminate itself, by
another thread, or parent terminates.
Copyright @ 2009 Pearson
Education , Inc.
Waiting: Thread blocked on event (I/O),
waiting for sync (semaphore),
suspended. When condition satisfied,
SILICON VALLEY UNIVERSITY moves to Ready State when resources
27
CONFIDENTIAL
are available.
Threads
 Thread Libraries
 Java Threads


Java Thread APIs managed directly in Java programs.
Java Virtual Machine and Interpreter on top of local Operating
System.



Windows Operating System (i.e. XP):
 Win32 Kernel-level Library.
UNIX/Linux Operating System, MAC OS X, Solaris 9:
 Pthreads User-level Library or Kernel-level Library.
Java Thread API implemented using the Thread Library
available on the Host System.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
28
Threads
 Thread Libraries
 Windows / Linux Comparision
Windows
Linux
Processes are containers for the user-mode address
space, a general handle mechanism for referencing
kernel objects, and threads; Threads run in a
process and are schedulable entities.
Process are both containers and the schedulable
entities; processes can share address space and
system resources, making processes effectively
usable as threads.
Processes created by discrete steps and container
for a new process created.
Processes created as virtual copies with fork() and
over-written with exec() for a new process.
Process uses table to uniformly reference kernel
objects (threads, memory, sync, I/O devices).
Kernel objects referenced by Kernel Library APIs
and mechanisms (File Descriptors, PIDs for open
files and sockets).
16 million kernel objects per process.
64 open files/sockets supported per process.
Kernel fully multi-threaded with kernel preemption.
Kernel processes used with kernel preemption.
Many system services implemented using a
client/server computing, including the OS personality
subsystems (Win32, POSIX, OS/2) and
communicate with Remote-Procedure Calls.
Most services are implemented in the kernel, with
exception of networking functions.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
29
Threads
Linux Pthreads
Windows Win32 API
#include pthread.h
…
Int main (int argc, char *argv[ ])
{
…
/* get the default attributes */
pthread_attr_init (&attr);
/* create the thread */
pthread_create(&tid, &attr, runner, argv[ 1 ]);
/* wait for the thread to exit */
pthread_join(tid, NULL);
…
}
…
/* The thread will begin control in this function */
void (*runner(void *param)
...
pthread_exit( 0 );
…
#include windows.h
…
DWORD WINAPI Summation(LPVOID Param)
…
return( 0 );
…
Int main (int argc, char *argv[ ])
{
…
Threadhandle = CreateThread (…Summation
…
If (Threadhandle != NULL) {
/* now wait for the thread to finish */
WaitForSingleObject(ThreadHandle, INFINITE);
/* close the thread handle */
CloseHandle(ThreadHandle);
…
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
30
Threads
 Thread Libraries
 Solaris ( UNIX ) Operating System
 Multithread Architecture concepts:





Process: UNIX process with User’s address space, Stack, PCB.
User-level Threads ( ULT ): Using Threads Library in User space,
invisible to Operating System. A ULT is a user-created unit of
execution within a process.
Lightweight Processes ( LWP ): Mapping between ULTs and
Kernel Threads. Always one Kernel Thread for each LWP. A
LWP is scheduled by kernel independently and executes in
parallel for concurrency on multiprocessors.
Kernel Threads: Fundamental entities that are scheduled and
dispatched to run on one of the system processes. Kernel
threads associated with LWP, but kernel can also create Kernel
Threads to execute system functions within kernel.
Interrupts handled as high priority Kernel Threads.


Synchronizes access to data structures using mutual exclusion.
Interrupt threads has higher priority than other Kernel Threads.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
31
Threads
 Thread Libraries
 Solaris ( UNIX ) Operating System
 Solaris Process Structure:
User-level thread (ULT)
supported by Lightweight
Process (LWP). Each LWP is
mapped to a Kernel Thread.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
32
Threads
 Thread Libraries
 Solaris ( UNIX ) Operating System
 Multithread Architecture concepts:
Solaris maintains similar
process structure as UNIX,
but the Processor State
structure has a linked list of
LWP structures.
There is at least one LWP for
each Solaris process.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
33
Threads
 Thread Libraries
 Solaris ( UNIX ) Operating System
 Solaris Thread States:
Pinned: Interrupt pins the
interrupted thread to the
processor and suspended
until interrupt is processed.
Run: Thread is runnable
(thread is ready to execute).
Onproc: Thread is executing
on a processor.
Sleep: Thread is blocked.
Stop: Thread is stopped.
Zombie: Thread was
terminated and waiting for
parent process to retrieve
status.
Copyright @ 2009 Pearson
Education Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
Free: Thread resources are
released and waiting for
removal from OS thread data
structure.
34
Threads
 Threading Issues

fork( ) and exec( )



fork( ) duplicates all threads of parent process?
exec( ) replaces the calling process, including all threads?
Thread Cancellation

Terminating a thread before it has completed and release all
resources.


Asynchronous Cancellation: The “target” thread is immediately
terminated.


Web page being loaded and stop button is pressed.
Immediate cancellation might leave unreleased resources.
Deferred Cancellation:

Cancellation points allow process to check for cancellation and
release all resources (Pthreads).
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
35
Threads
 Threading Issues
 Signal Handling

Operating System notifies a process a signal has occurred.


Synchronous Signals: Process execution creates signal ( i.e. illegal
memory reference ).
Asynchronous Signals: Signal created by external event ( i.e.
<CTRL-C> ).



Default Signal Handler: Kernel execute.
User-Defined Signal Handler: Called within process ( Overrides Default
Signal Handler ).
Delivering signal in multithreaded process.





Deliver signal to the thread to which signal applies.
 Synchronous Signals.
Deliver signal to every thread in the process.
 <Ctrl-C> Entry.
Deliver signal to certain threads in the process.
 Delivered to thread that is not blocking the signal.
kill ( pid_t pid, int signal ): Delivering “signal” to “pid”.
pthread_kill ( pthread_t tid, int signal )
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
36
Threads
 Threading Issues
 Problems with Creating Kernel-level Threading



Creating one-to-one User Thread / Kernel Thread overhead.
No bounds on number of concurrent Kernel Threads.
Thread Pools


Process startup, create a pool of Kernel Threads.
When User Thread created, link with Kernel Thread from pool or
wait until Kernel Thread becomes available.


Servicing User Thread with existing Kernel thread eliminates Kernel
thread creation overhead.
Pool of Kernel Threads automatically creates bound on concurrent
Kernel Threads.

Copyright @ 2009 John
Wiley & Sons Inc.
Number of Kernel Threads based on number of CPUs, amount of
physical memory, statistical average of expected number of concurrent
threads.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
37
Threads
 Threading Issues
 Thread-Specific Data



Sharing memory could cause privacy problem.
Each thread declare thread-specfic global data.
Compiler Support


__thread char *variable;
Function Support






Keys or index to identify thread unique data area.
Max 128 keys or unique data areas.
pthread_key_create ( ): Prepare a key for use.
pthread_setspecific ( ): Use key to set a value to a thread-specific data.
pthread_getspecific ( ): Use key to retrieve the current value of threadspecific data.
pthread_key_delete ( ): Delete a thread-specific data key previously returned
by pthread_key_create ( ).
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
38
typedef struct { int threadSpecific1; int threadSpecific2; } threadSpecific_data_t;
Threads
 Threading Issues
 Thread-Specific Data
typedef struct {
int threadspecific1;
int threadspecific2;
} threadspecific_data_t;
…
pthread_key_t threadSpecificKey;
…
void *theThread(void *param)
threadSpecific_data_t *gData;
…
gData = (threadSpecific_data_t *)parm;
rc = pthread_setspecific(threadSpecificKey, gData);
…
Int main (int argc, char **argv)
threadSpecific_data_t *gData;
…
rc = pthread_create(&thread[ I ], NULL, theThread, gData)
…
rc = pthread_join( thread[ I ], NULL);
…
pthread_key_delete( threadSpecificKey);
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
Structure used in thread-specific
data.
Key used to reference the threadspecific data item.
Thread with thread-specific data.
Save the thread-specific data in a
thread.
Thread created in the main process.
Delete the key after thread has
exited.
39
Threads
 Windows Operating System Threads
 Win32 API: Uses One-to-One Model


Windows 95, 98, NT, 2000, XP
Many-to-Many Model


Fiber Library
Context of the thread


User Space
Kernal Space
Thread start address,
KTHREAD – Kernel Thread Block.


Thread-local
storage
ETHREAD – Executive Thread Block

Thread identifier
User stack
Windows XP

TEB
Scheduling and Information, Kernel Stack.
. . .
KTHREAD
Scheduling and
synchronization
information
Kernel stack
TEB – Thread Environment Block.

Thread Identifier, User Stack, Thread-Local Storage
. . .
ETHREAD
Thread start
address
Pointer to
parent process
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
. . .
40
Threads
 Linux Operating System Threads

Linux Threading: clone()
 fork(): process




pthread_create(): thread









Fall 2015
CLONE_CHILD_CLEARTID: erase child thread ID when child
exits.
CLONE_CHILD_SETTID: store child thread ID.
SIGCHLD: Termination signal sent to parent when child dies.
CLONE_FILES: share the same file descriptor table.
CLONE_VM: share the same memory space.
CLONE_FS: share file system information
CLONE_SIGHAND: share same table of signal handlers.
CLONE_THREAD: share the same thread group as the calling
process.
CLONE_SYSVSEM: share a single list of System V semaphores.
CLOSE_SETTLS: Set Thread Local Storage descriptor.
CLONE_PARENT_SETTID: store child thread ID.
CLONE_CHILD_CLEARTID: erase child thread ID when child
exits.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
41
Threads
 Linux Process Vs Threads
int main()
{
…
pid_t pID = fork();
If (pID == 0) {
sIdent = “Child Process: “;
…
} else if (pID < 0) {
/* Failed to fork. */
exit(1);
} else {
sident = “Parent Process:”;
}
…
}
Fall 2015
fork:
Spawn a new child process which is an identical
process to the parent, except that it has a new
system process ID. The process is copied in memory
from the parent.
- Copy-on-write memory pages created.
- Duplicate parent’s page tables.
- Create unique task_struct for the child.
- Return value = 0 indicates child’s process.
child PID in the parent’s process.
-1 error, no child process created.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
42
Threads
 Linux Process Vs Threads
int func1()
{
/* Thread function. */
…
}
int main()
{
…
iret1 = pthread_create(
&tid1, NULL, func1,
(void *)message1);
…
pthread_join( tid1, NULL);
…
return 0;
}
Fall 2015
pthread_create:
Creates a new thread with attributes in the call or
NULL for default attributes.
- tid1 recieves the thread ID.
- NULL uses the default thread attributes.
- func1 is the pointer to the function to be threaded.
- message1 is the pointer to the argument.
pthread_join:
Waits for termination of another thread.
- tid1 is the thread the main process is waiting to
terminate.
NULL specifies return value not needed.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
43
Threads
 Summary
 Thread is a flow of control within a process.
 Multithreading Benefits:





Responsiveness to the user.
Resource sharing within the process.
Economy.
Scalability, more efficient use of multiple cores.
User Level Threads


Thread Manager and Thread Library in User Space.
Many-to-One, One-to-One, and Many-to-Many


Thread Library




User Level and Kernel Level Threads Mapping.
Linux POSIX Pthreads Library
Windows Win32 Threads Library
Fork() and exec() System Calls
Thread Cancellation, Signal Handling, Thread-Specific
Data.
SILICON VALLEY UNIVERSITY
Fall 2015
CONFIDENTIAL
44
Threads
 Operating System Structure
 Monolithic Operating System




Modular Programming Techniques.
Layered Operating System: Functions organized hierarchically and
interaction only takes place between adjacent layers.
Most or all of the layers execute in Kernel Mode.
Performance:



Better than Microkernel: Messaging between “Kernel” services takes
longer than single service call on Monolithic OS.
Performance more important is OS than Microkernel’s modularity.
Problems:



Major changes in one layer affects above and below layers.
Tailored version of OS difficult to implement.
Security difficult to build because of interactive.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
45
Threads
 Operating System Structure
 Microkernels: Essential OS functions are in the Kernel
(Kernel Mode), while less essentail services and
applications execute in User Mode.


Decouples Kernel and Server development: Servers customized to
specific applications or environments.
Simplifies Kernel implementation.




Traditional OS Functions moved to User Mode.




Device Drivers.
Protocol Stacks.
File Systems.
Windowing System.
Security Services.
Virtual Memory Manager (Page Replacement Algo).
Flexibility.



Address Space Management ( Map Virtual Page to Physical Frame).
Process and Thread Management ( Scheduling ).
Inter-process Communication ( IPC ).
OS has common core, changes to Device Drivers, Protocol Stacks, File
Systems, and other low-level systems developed in User Space.
Since these functions or services in User Space allowed server development
without the complexity managing of Kernel Space services.
Performance problems forced User Space services back to Kernel.
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
46
Threads
 Operating System Structure
 Microkernels:
Copyright @ 2009 John
Wiley & Sons Inc.
SILICON VALLEY UNIVERSITY
CONFIDENTIAL
47