+ All Categories
Home > Education > Operating Systems 1 (7/12) - Threads

Operating Systems 1 (7/12) - Threads

Date post: 12-Jul-2015
Category:
Upload: peter-troeger
View: 200 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Threads Beuth Hochschule Summer Term 2014 Pictures (C) W. Stallings, if not stated otherwise
Transcript
Page 1: Operating Systems 1 (7/12) - Threads

Threads

Beuth HochschuleSummer Term 2014!Pictures (C) W. Stallings, if not stated otherwise

Page 2: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Process Concept

• Classically, processes are executed programs that have ...

• Resource Ownership

• Process includes a virtual address space to hold the process image

• Operating system prevents unwanted interference between processes

• Scheduling/Execution

• Process follows an execution path that may be interleaved with other processes

• Process has an execution state (Running, Ready, etc.) and a dispatching priority and is scheduled and dispatched by the operating system

• Today, the unit of dispatching is referred to as a thread or lightweight process

• The unit of resource ownership remains the process or task

2

Page 3: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Single and Multithreaded Processes

3

code% data% files%

registers% stack%

Thread'

code% data% files%

registers%

stack%

Thread'

stack%

registers%

stack%

registers%

Thread' Thread'

Page 4: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Control Blocks

• Information associated with each process: Process Control Block

• Memory management information

• Accounting information

• Information associated with each thread: Thread Control Block

• Program counter

• CPU registers

• CPU scheduling information

• Pending I/O information

4

Page 5: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Control Blocks

520

Program'Counter'

Parent'PID'

…'

Handle'Table'

Process'ID'(PID)'

Registers'

Next'Process'Block'

Image'File'Name'

PCB'

List'of'Thread'Control'Blocks'

List'of'open'files'

…'

Next'TCB'

…'

Thread'Control'Block'(TCB)'

Page 6: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Multithreading

• Each thread has

• An execution state (Running, Ready, etc.)

• Saved thread context when not running

• An execution stack

• Some per-thread static storage for local variables

• Access to the memory and resources of its process (all threads of a process share this)

• Suspending a process involves suspending all threads of the process

• Termination of a process terminates all threads within the process

6

Page 7: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Multithreading

7

• Advantages

• Better responsiveness - dedicated threads for handling user events

• Simpler resource sharing - all threads in a process share same address space

• Utilization of multiple coresfor parallel execution

• Faster creation and termination of activities

• Disadvantages

• Coordinated termination

• Signal and error handling

• Reentrant vs. non-reentrant system calls

Page 8: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Thread States

• The typical states for a thread are running, ready, blocked

• Typical thread operations associated with a change in thread state are:

• Spawn: a thread within a process may spawn another thread

• Provides instruction pointer and arguments for the new thread

• New thread gets its own register context and stack space

• Block: a thread needs to wait for an event

• Saving its user registers, program counter, and stack pointers

• Unblock: When the event for which a thread is blocked occurs

• Finish: When a thread completes, its register context and stacks are deallocated.

8

Page 9: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Thread Dispatching

9

Thread'T1'

execu,ng'

execu,ng'

ready'or'wai,ng'

Save'state'into'TCB2'

Reload'state'from'TCB1'

Save'state'into'TCB1'

Reload'state'from'TCB2'

Interrupt'or'system'call' Thread'T2'

execu,ng'Interrupt'or'system'call'

ready'or'wai,ng'

ready'or'wai,ng'

Page 10: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Example: Windows

• The Windows kernel dispatches threads for multi-tasking

• Implementation of 1:1 mapping - „processes do not run, threads run“

• In principle, all threads are equal - no consideration of process in scheduling

• Thread context switch always involves the kernel

• Every process starts with one main thread, may create more

• Per-process data:

• Virtual address space, working set („owned“ physical memory), access token, handle table for kernel objects, environment strings, command line

• Per-thread data:

• User-mode stack (call frames, arguments), kernel-mode stack, thread-local storage, scheduling state, thread priority, hardware context, optional access token

10

Page 11: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Thread and Process Creation

11

BOOL CreateProcess( LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)

HANDLE CreateThread ( LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack, LPTHREAD_START_ROUTINE lpStartAddr, LPVOID lpvThreadParm, DWORD fdwCreate, LPDWORD lpIDThread) receives thread ID

DWORD WINAPI ThreadFunc(LPVOID)

Page 12: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Identifiers

• Every process and every thread has an identifier

• In general: “client ID” (debugger shows as “CID”)

• A.K.A. process ID and thread ID, respectively

• Process IDs and thread IDs are in the same “number space”

• IDs are unique among all existing processes and threads

• Might be reused as soon as a process or thread is deleted

• Identify the requesting process or thread to its subsystem “server” process

• Relevant for API calls that need the server’s help

• Visible in PerfMon, Task Manager (for processes), Process Explorer

12

Page 13: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Thread Creation and Termination

• Threads start in the kernel, but have a trap frame to return to user mode

• Threads run until:

• The thread returns to the OS

• ExitThread() is called by the thread

• TerminateThread() is called on the thread

• ExitProcess() is called on the process

• When the last thread in a process terminates, the process itself terminates

• Thread continues to exist until last handle is closed (CloseHandle())

• Each thread has suspend count

• Can only execute if suspend count == 0, can be created in suspended state

13

Page 14: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Thread Creation

• Flow of CreateThread():

• The thread count in the process object is incremented.

• An executive thread block (ETHREAD) is created and initialized.

• A thread ID is generated for the new thread.

• The TEB is set up in the user-mode address space of the process.

• The user-mode thread start address is stored in the ETHREAD.

• KeInitThread is called to set up the KTHREAD block.

• Initial and current base priorities are set to the process’s base priority

• Affinity and quantum are set to that of the process.

• Allocates kernel stack and initializes machine-dependent hardware context

14

Page 15: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Thread Termination

• Thread rundown sequence:

• DLL notification, unless TerminateThread() was used

• All handles to Windows User and GDI objects are closed

• Outstanding I/Os are cancelled

• Thread stack is deallocated

• Thread’s exit code changes from STILL_ACTIVE to the specified exit code

• Thread kernel object becomes signaled

• When handle and reference counts == 0, thread object deleted

• If last thread in process, process exits

15

Page 16: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Windows - Thread States

• Init: The thread is being created.

• Ready: The thread is waiting to be assigned to a CPU.

• Running: The thread’s instructions are being executed.

• Waiting: The thread is waiting for some event to occur.

• Terminated: The thread has finished execution.

16

init$

ready$

wai+ng$

running$

terminated$

scheduler$dispatch$

wai+ng$for$I/O$or$event$

I/O$or$event$comple+on$

interrupt$$quantum$expired$

admi<ed$ exit$

Page 17: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Linux Threads

• No explicit distinguishing between threads and processes

• User-level threads are mapped to kernel tasks

• User threads in the same user process share the same thread group ID

• Enables resource sharing, avoids context switch on dispatching

• clone() as extended version of fork()

• On cloning, decision about sharing of memory, file handles, a.s.o. is made

• Thread libraries use this capability

17

CLONE_CLEARID Clear the task ID.

CLONE_DETACHED The parent does not want a SIGCHLD signal sent on exit.

CLONE_FILES Shares the table that identifies the open files.

CLONE_FS Shares the table that identifies the root directory and the current

working directory, as well as the value of the bit mask used to mask the initial file permissions of a new file.

CLONE_IDLETASK Set PID to zero, which refers to an idle task. The idle task is employed

when all available tasks are blocked waiting for resources.

CLONE_NEWNS Create a new namespace for the child.

CLONE_PARENT Caller and new task share the same parent process.

CLONE_PTRACE If the parent process is being traced, the child process will also be

traced.

CLONE_SETTID Write the TID back to user space.

CLONE_SETTLS Create a new TLS for the child.

CLONE_SIGHAND Shares the table that identifies the signal handlers.

CLONE_SYSVSEM Shares System V SEM_UNDO semantics.

CLONE_THREAD Inserts this process into the same thread group of the parent. If this flag is true, it implicitly enforces CLONE_PARENT.

CLONE_VFORK If set, the parent does not get scheduled for execution until the child

invokes the execve() system call.

CLONE_VM Shares the address space (memory descriptor and all page tables).

Page 18: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

Linux Threads

• Thread group ID (TGID) and thread ID (TID) are stored in task structure of the kernel

• Processes with threads have identical PID and TGID

• Linux has distinct kernel threads

• Only executed in kernel mode, typically needs access to kernel data structures

• Can wait for events to occur, e.g. USB hotplugging

• Example: ps -eLf (watch for square brackets, LWP equals thread in the output)

18

Page 19: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

POSIX Threads

• POSIX standardized (IEEE 1003.1c) API for thread creation and synchronization

• API specifies behavior of the thread library, not an implementation

• Thread creation and termination, stack management

• Synchronization between threads

• Scheduling hints

• Thread-local storage

• Implemented on many UNIX operating systems to allow portable concurrent C code

• Windows: Services for Unix (SFU) implement pthreads on Windows

• Linux 2.6: Native POSIX Thread Library (NPTL) implementation of pthreads

19

Page 20: Operating Systems 1 (7/12) - Threads

20

#include <pthread.h>!#include <stdio.h>!#include <string.h>!#include <unistd.h>!!void * hello_thread( void *arg ) {!! printf( "hello " ); return( 0 ); }!!void * world_thread( void *arg ) {!int n;!

! pthread_t!tid!= (pthread_t) arg;!! if ( n = pthread_join( tid, NULL ) ) {!! ! fprintf( stderr, "pthread_join: %s\n", strerror( n ) );!! ! return( NULL ); }!! printf( "world\n" );!! pthread_exit( 0 ); }!!int main( int argc, char *argv[] ) {!! int!n;!! pthread_t!htid, wtid;!! if ( n = pthread_create( &htid, NULL, hello_thread, NULL ) ) {!! ! fprintf( stderr, "pthread_create: %s\n", strerror( n ) );!! ! return( 1 ); }!!! if ( n = pthread_create( &wtid, NULL, world_thread, (void *) htid ) ) {!! ! fprintf( stderr, "pthread_create: %s\n", strerror( n ) );!! ! return( 1 ); }!!! if ( n = pthread_join( wtid, NULL ) ) {!! ! fprintf( stderr, "pthread_join: %s\n", strerror( n ) );!! ! return( 1 ); }!! return( 0 ); }

Page 21: Operating Systems 1 (7/12) - Threads

Operating Systems I PT / FF 2014

MacOS X - Grand Central Dispatch (GCD)

• Provides a pool of available threads

• Portions of applications (blocks) can be dispatched independently

• Blocks are lightweigt to create and queue

• Operating system dispatches available blocks on given processors

• GCD provides system queues used by the application,private queues are possible

• Queues are either concurrent or serial, latter act as lock replacement

• Blocks can be associated with an event source (timer, socket, file descriptor)

• Similar solutions available for other systems

21


Recommended