编程代写 CS 563 Concurrent Programming

CS 563 Concurrent Programming
Lecture 2: Processes & Threads

Processes vs. Threads

Copyright By PowCoder代写 加微信 powcoder

Process 1 Process 2

What is a process?
A program in execution
Associated with a number of resources (memory, files) which are protected from other processes
Processes exist concurrently

History of Concurrency – 60’s

History of Concurrency – 70’s

History of Concurrency – 80’s

Threads: Motivation
Process are created and managed by the OS kernel
Process creation is expensive, e.g., fork
Context switching is expensive
Inter process communication requires kernel intervention Cooperating processes: no need for memory protection

User space
System space

History of Concurrency – 80’s

Thread Implementation
Threads are provided as a package, including operations to create, destroy, and synchronize them
A package can be implemented as:
User-level threads Kernel threads Hybrid

User-Level Threads
Threads management done by user-level threads library Thread library entirely executed in user space
Cheap management and context switching
However, a blocking system call blocks all peer threads Examples
POSIX Pthreads -threads Java threads
User Space
Kernel Space
Threads Library

Kernel-Level Threads
Kernel is aware of and schedules threads
A blocking system call will not block all peer threads
Management and context switching is relatively expensive
Windows UNIX Linux
Threads Library
User Space
Kernel Space
User Space
Kernel Space

Thread creation is done in the user space
Bulk of scheduling and synchronization of threads is done by application
User level threads are mapped to a number of
kernel level threads
User Space
Kernel Space
User Space
Kernel Space
User Space
Kernel Space
Threads Library

History of Concurrency – 90’s

History of Concurrency – 00’s

Example: Java Threads
Threads are created through thread objects which belong to the standard class Thread
Each thread object is assigned a “virtual processor” that can be accessed and controlled through the operations of the thread object
e.g. the execution of a thread belonging to a thread object t is started by calling the start operation of the object: t.start()

Java Threads
Thread creation
Option 1 Option 2

Java Threads
Termination
To await the termination of a thread t:
This operation may return with an Interrupted exception To force a thread to terminate:
This is deprecated (not recommended to use)

Java Threads
Thread cancellation
Set the interrupt mark of the thread t:
t.interrupt()
Test the interrupt mark of the current thread:
Thread.interrupted()

Java Threads
Other operations
Thread.sleep(n) – waits for (at least) n milliseconds
Thread.currentThread() – returns the Thread object of the calling thread
t.suspend(), t.resume() – temporary cease the activity of a thread (deprecated)

Java Threads: example

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com