Docsity

Threads - Operating System - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
1
Chapter 4: Threads
•Resources
–Posix Thread Programming (Pthreads)
•http://www.llnl.gov/computing/tutorials/pthreads/
–Wikipedia article on threads
•http://en.wikipedia.org/wiki/Multithreading
•Outline
–Threads vs. Heavy-Weight Process (HWP)
–Advantages and disadvantages of threads
–Separate stacks
–POSIX threads
–O/S design issues
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Threads - Operating System - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

1

Chapter 4: Threads

  • Resources
    • Posix Thread Programming (Pthreads)
      • http://www.llnl.gov/computing/tutorials/pthreads/
    • Wikipedia article on threads
      • http://en.wikipedia.org/wiki/Multithreading
  • Outline
    • Threads vs. Heavy-Weight Process (HWP)
    • Advantages and disadvantages of threads
    • Separate stacks
    • POSIX threads
    • O/S design issues

Multi-threaded

Process

2

Single-threaded

Process

Figure 4.1 Docsity.com

4

Threads: Benefits

  • Resource sharing: economy
    • Memory is shared (i.e., address space shared)
    • Open files, sockets shared
  • Speed
    • E.g., Solaris: thread creation about 30x faster than heavyweight process creation; context switch about 5x faster with thread
  • User responsiveness
    • When one thread blocks, another may handle user I/O
    • But: depends on threading implementation
  • Utilizing hardware parallelism
    • Like heavy weight processes, can also make use of multiprocessor architectures
    • But: depends on threading implementation Docsity.com

5

Threads: Drawbacks

  • Synchronization
    • Access to shared memory or shared variables must be controlled if the memory or variables are changed
    • Can add complexity, bugs to program code
    • E.g., need to be very careful to avoid race conditions, deadlocks and other concurrency problems (see Ch 6)
  • Lack of independence
    • Threads not independent, within a Heavy-Weight Process (HWP)
    • The RAM address space is shared; No memory protection from each other
    • The stacks of each thread are intended to be in separate RAM, but if one thread has a problem (e.g., with pointers or array addressing), it could write over the stack of another thread Docsity.com

7

Each Thread has a Separate Stack,

Program Counter (PC) , & Registers

8

Separate Stack Per Thread

• Why?

• When a function or procedure is called, an

“activation record” or “stack frame” is

pushed on the stack

• This may contain (a) parameters, (b) return

address, and (c) local variables.

• If two threads are using same stack they

may corrupt these activation records, or at

best use each others activation records.

10

Parameter Passing Mechanism

• Arguments evaluated to produce values

• Memory allocated on stack frame for each

parameter

• Argument values are copied to these places

in the stack frame

11

Example: Nested function calls

1: void nested2(int x) 2: { 3: int y = 9; 4: printf("hello2= %d,%d\n", x, y); 5: }

6: void nested1(int x) 7: { 8: int y = 6; 9: printf("hello1= %d, %d\n", x, y); 10: nested2(77); 11:}

12: void nested0(int x) 13: { 14: int y = 0; 15: printf("hello0= %d, %d\n", x, y); 16: nested1(13); 17: } 18: int main(void) 19: { 20: int z = 1; 21: nested0(3); 22: fflush(stdin); 23: getchar(); 24: return 0; 25: }

13

What happens if >1 thread?

• Say that each thread has own PC, stack

pointer, and registers, but all threads share

the same stack

14

Tracing Nested Function Calls:

With Two Threads and One Stack–

One Possible Sequence

After

line 20

z 1

After line 14 After call on line 21

z 1

x 3 y 0 line 21

z 1

z 1

Etc.

key: thread 1 thread 2

Problem : The stack pointer and the stack data are both being shared by 2 threads, in an unsynchronized manner!

Docsity.com

19

POSIX Threads: Pthreads

• Specification for thread behavior

• pthread_create

  • Start thread given a function in program to run

• pthread_exit

  • terminate current thread

• For details and other functions in API, see:

http://www.llnl.gov/computing/tutorials/pthreads

20

http://www.cprince.com/courses/cs5631/lectures/Chapter (Adapted from Figure 4.9; runs on OS X)

How is threading implemented?

Two main possibilities

  • User level support
    • Same PCB (Process Control Block) for all threads in HWP
    • Relatively fast; no O/S system calls required
    • Implemented by a library at the user level
    • No support from kernel; kernel “unaware” of threads
    • One thread blocks: Causes other threads in HWP to block
  • Kernel level support
    • Separate PCB per thread
    • Relatively slow; Requires O/S support
    • Other threads in a task execute when one is blocked
    • Can make use of hardware parallelism (e.g., multiple processors) within HWP

23

Threading ‘Models’

Many-to-one

One-to-one

Many-to-many

Docsity.com