程序代做CS代考 data structure Java flex concurrency Threads

Threads
CSci4061: Introduction to Operating Systems

September 23, 2021
Computer Science & Engineering, University of Minnesota
1

Project 1 and grouping
• Project 1 has been released
• Mainly about processes
• Start it early
• Still looking for group members? Use Canvas
2

Questions for lectures, labs, and projects
• Canvas is the best place to post your questions
• All can engage in the discussion
• Make use of office hours
• Lab sections on Mondays
• Office hours from Tuesdays to Fridays
3

Improved performance? How about reliability and security?
int main(int argc, char** argv) {
// Get the file size for buffer.
struct stat fstat; stat(“input”, &fstat);
int fsize = fstat.st_size;
// Open input file as read-only.
int in = open(argv[1], O_RDONLY);
// Read file into large buffer.
char *buffer = malloc(fsize);
read(in, buffer, fsize);
<-- System Call <-- I/O request <-- I/O request 4 Improved code without introducing race conditions int main(int argc, char** argv) { struct stat f_stat; // Open input file as read-only. int in = open(argv[1], O_RDONLY); // Get the file size for buffer. fstat(in, &f_stat); int fsize = f_stat.st_size; // Read file into large buffer. char *buffer = malloc(fsize); read(in, buffer, fsize); <-- I/O request <-- System Call <-- I/O request 5 Today’s lecture • Introducing threads • Comparing thread with process • Thread Implementation: Kernel level vs. User level • POSIX Threads (Pthreads) package 6 Review of processes Process is a program in execution, a unit of resource management. • By default, a process has a single “thread” of activity • Single program counter, stack • Code, data, heap, files, signals • What if we want to do multiple related activities at the same time? • Multiple instances of same task • Example: Web servers serve multiple user requests at the same time • Multiple parallel components of a task • Web browser: Read data from network while displaying graphics 7 How to do multiple related activities simultaneously? Possible approach: Use multiple processes • Give one task to each process Any potential problems? 8 How to do multiple related activities simultaneously? Possible approach: Use multiple processes • Give one task to each process Any potential problems? • Problem 1: How to share data and communicate? • IPC (lecture in Week 10): Pipes, files, shared memory, sockets, signals • Requires kernel support ==> Inflexible and inefficient due to user/kernel
crossings
• Problem 2: Overhead
• Creating a new process is costly as every process has its own memory map and resources
• Paging and process-level context-switch cost is typically high
8

How to address the inefficiency and inflexibility?
• What we want to parallelize is really just a small component
• Parallelizing multiple processes has to run irrelevant components • How to address them?
9

Threads

Thread definition
• A thread is
• An active sequence of instructions in a process • An abstraction of a “activity”
• A process may have multiple activities
• A process activity is defined by its context:
• Program counter: Tells which instruction is being executed
• Stack: Determines the execution trace (e.g., which function, what parameters,
sequence)
• So a thread has its own program counter and stack
10

Threads
• Threads exist within a process
• “Lightweight process”
• Multiple threads can run concurrently within the same process
• Threads share
• Process code
• Data region, heap region
• Resources of the process: Files, signals, etc.
• Threads do NOT share
• Program counter, registers
• Stack region
• Signal mask (for blocking or unblocking signals)
11

Process vs. Threads in address space
A single-threaded process A multi-threaded process
Every process by default has a main thread
12

Thread design space
13

Multiprogramming vs. means
• OS has several processes in memory at a time and can execute any of them • Processes are address-space disjoint
• Problem: low memory usage
Multithreading means
• Process can have multiple threads • Threads share address-space
14

Multithreading example
What if foo and bar are called normally without create_thread?
15

Context switch: Process vs. thread
Saves context of the currently running thread/process, and restores the context of the next thread/process
• Process context switch
• Memory addresses, mappings, page tables, and kernel resources
• Expensive!
• Thread context switch
• Processor state—mainly program counter and register contents • Much more efficient!
16

Thread benefits
17

Thread benefits
• Concurrency
• When one thread is blocked, another can run
• Great for multi-tasking applications (Web servers, file servers)
• Resource sharing
• Threads share resources of the process (e.g., code, data, files)
• Less OS resources used up (e.g., memory, buffers, kernel data structures)
17

More benefits
• Efficiency
• Thread operations cheaper than processes
• Creating/destroying, context switches, scheduling
• Communication (normal memory read/write in the same address space)
• Parallelism
• Multithreading gives real parallelism on multiprocessor machines • Can run multiple threads on multiple processors
18

Thread problems
19

Thread problems
• Programming complexity
• Need to be synchronized
• Non-deterministic behavior
• Difficult to debug
• This is serious in large programs!—a major source of bugs
linux-kernel$ git log |grep -i ” race ” |wc -l
11916
• Inefficient in extreme scenarios (e.g., limited memory)
• Stacks could still use up lot of memory
• Context switch has overhead
• Portability problems due to different implementations
19

Thread Implementation: Kernel level vs. User level

Kernel-level threads
Kernel threads are implemented directly inside the kernel
• Like processes with shared address space
• The OS schedules all of the threads in the system • Example: Linux kernel threads
• Use clone() with CLONE_THREAD
20

User-level threads
User threads are implemented by a user-level runtime system, e.g., Java, Pthreads
• Language support or thread-package library
• Creating a new thread, switching between threads, and synchronizing threads
are done via procedure call • No kernel involvement!
21

User threads vs. kernel threads
22

User threads vs. kernel threads
• Kernel-level threads
• Globally manages threads with thread control block (TCB)
• Integrated with OS (informed scheduling)—could take advantage of OS
scheduler
• Slow to create, manipulate, synchronize
• User-level threads
• Fast to create, manipulate, synchronize
• Not integrated with OS (uninformed scheduling)
• Importantly, context switching does not involve kernel!
• Understanding the differences between kernel and user-level threads is important
• Application programming typically uses user threads
23

Hybrid threading models
Many systems provide support for both user and kernel threads to have benefits of both
• Use a combination of user and kernel threads • Map user threads to kernel threads
• Dependent on
• OS thread support
• User thread library implementation
24

Many-to-many model
Multiple user threads mapped to multiple kernel threads
25

Many-to-one model
Many user threads mapped to a single kernel thread
26

One-to-one model
Each user thread mapped to a single kernel thread
One-to-one is a preferred model
• Simplicity and better utilization of multi-core processors • Used by Linux, called “light-weight process” (LWP)
27

POSIX Threads (Pthreads) package

Thread operations using pthreads package
Pthreads: a model for parallel execution. Implementations of Pthreads API are available on many Unix-like POSIX-conformant OSes
• Create a thread
• Pass it a function and arguments, attributes
• Join a thread
• Makes the calling thread wait for a child
• Detach a thread
• Lets the thread release its resources when done
• Terminate a thread
• Finish a thread without exiting the process
28

Thread Creation: pthread_create
#include int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
// Sample code
pthread_t tid;
pthread_attr_t attr;
int i, ret;
void *foo(void *arg){…}
int main(){

ret = pthread_create(&tid, &attr, foo, i);

}
More details: $ man pthread_create 29

Joining Threads: pthread_join
int pthread_join(pthread_t thread, void **retval);
• Makes the calling thread wait on another thread
• Calling thread suspended until target thread finishes • When target thread terminates:
• Its return status is passed • Its resources are released
30

Detaching Threads: pthread_detach
int pthread_detach(pthread_t thread);
• Makes a thread “independent”
• The thread’s resources are reclaimed when it exits • Cannot be joined (waited on)
Important: always call ‘join‘ or ‘detach‘; otherwise, it triggers ‘std::terminate‘ exception
31

Thread Termination
• A thread can exit by
• Returning from its top-level function (the called thread function) • Calling pthread_exit
• Calling exit: Terminates the whole process
• A thread can terminate another thread
• pthread_cancel
• The result of this call depends on the target thread’s type and cancellability
32

Pthread Example
pthread_t tid;
int i = 1, ret;
void *foo(void *arg) {
int myval= (int) arg;
printf(“myval=%d\n”, myval);
}
int main() {

ret = pthread_create(&tid, NULL, foo, i); …
if (!ret)
}
pthread_join(tid, NULL);
33

Review: Program vs. Thread vs. Process
• Program: static code and data in an executable binary file • Process: an execution instance of a program
• Address space, code and data in memory
• Processes are isolated from each other
• Thread: A subset (smaller activities) of a process
• They can communicate with each other, sharing resources (not registers and the stack)
34

More comparison of process and thread
• Process
• heavyweight
• own address space
• slow inter-process communication • context switching is more expensive
• Thread
• lighter weight
• inside a process, share resource among threads (of the same process) • fast inter-thread communication
• faster context switching
35

Quiz 1
Creating a tab in the browser might be implemented as
1. Thread
2. Process
3. Either thread or process
36

Quiz 1
Creating a tab in the browser might be implemented as
1. Thread
2. Process
3. Either thread or process
Answer: 3
36

Quiz 2
Which one of the following is not shared by threads?
1. Program counter
2. Stack
3. Both program counter and stack 4. None of the mentioned
37

Quiz 2
Which one of the following is not shared by threads?
1. Program counter
2. Stack
3. Both program counter and stack 4. None of the mentioned
Answer: 3
37

Quiz 3
Termination of the process terminates
1. First thread of the process
2. First two threads of the process 3. No thread within the process
4. All threads within the process
38

Quiz 3
Termination of the process terminates
1. First thread of the process
2. First two threads of the process 3. No thread within the process
4. All threads within the process
Answer: 4
38

Quiz 4
The time required to create a new thread in an existing process is
1. Greater than the time required to create a new process 2. Less than the time required to create a new process
3. Equal to the time required to create a new process
4. None of the mentioned
39

Quiz 4
The time required to create a new thread in an existing process is
1. Greater than the time required to create a new process 2. Less than the time required to create a new process
3. Equal to the time required to create a new process
4. None of the mentioned
Answer: 2
39

Summary
• Thread definition
• Processes vs. Threads
• Thread benefits and problems
• Kernel vs. User threads
• Pthread package: Thread operations
40

Questions?
41

Next Lecture
• Scheduling
• Robbins 12.6.3
42

References
[1] http://www-users.cselabs.umn.edu/classes/Spring- 2018/csci4061/notes/threads.pdf
[2] http://www-users.cselabs.umn.edu/classes/Fall- 2018/csci4061/notes/w9_l1_post.pdf
[3] https://www.cs.ucr.edu/~csong/cs153/l/thread.pdf
43