150 likes | 165 Vues
Concurrency: Introduction. Ilsoo Byun(isbyun@dcslab.snu.ac.kr) School of Computer Science and Engineering Seoul National University. Contents. Introduction to Thread Linux Implementation Concurrency Problem Necessity of Atomicity S ummary. Process vs Thread. Similarities
E N D
Concurrency: Introduction Ilsoo Byun(isbyun@dcslab.snu.ac.kr) School of Computer Science and Engineering Seoul National University
Contents • Introduction to Thread • Linux Implementation • Concurrency Problem • Necessity of Atomicity • Summary
Process vs Thread • Similarities • Unit of a context switch • PC • Registers • Differences • Process Context Block/Thread Context Block • Process can not access the address space of other processes but threads of a process can access every addressin the process. • After context switching between threads, the address space remains the same. • With threads, there can be multiple stacks in the address space.
A Single- and Multi-Threaded Address space Code Code Data Data BSS BSS Heap Heap Free Stack(2) Stack Stack(1) (a) Sigle-threaded (b) Multi-threaded
Linux Implementation (1) TCB Thread ID 4 State: Ready PID 111222 PCB Thread ID 5 State: Ready User ID PC SP Group ID Registers Registers Address space Open files PC SP Sockets etc Separate the concept of a process from a thread of control Process is just a container for threads Each thread has its own CPU execution state
Linux Implementation (2) Thread ID 4 State: Ready Thread ID 5 State: Ready Ready Queue PC SP PC SP Registers Registers • TCB is the unit of a context switch • Ready queue, wait queues, etc. now contain pointers to TCB • Context switch cause CPU state to be copied to /from the TCB • TCB's are smaller and cheaper than PCB • Linux TCB(thread_struct) has 24 fields • Linux PCB(task_struct) has 106 fields
Why it is a problem? counter = 20000000 or counter = 19345221 or counter = 18221041 • Shared Data • Running order depends on the scheduler decision
Uncontrolled Scheduling Simply adding 1 to counter requires three instructions
Necessity of Atomicity • Add more powerful instructions? • Cannot support all cases that require atomic operations. • Provide synchronization primitives for the general purpose • Access critical section in a synchronized and controlled manner • Reliably produce result despite the nature of concurrent execution
Summary • Threads • Multiple points of execution in a process • Accessing shared data, non-deterministic behavior can occur. • To support atomic operation, synchronization primitives are required.