Lecture 7 Processes and Threads 1 Process vs















































- Slides: 47

Lecture 7 Processes and Threads 1

Process vs. Program • Processes are a basic unit of execution. • An “instance of a running program”. • The OS creates processes for programs. • The OS manages the processes and their resources. 2

Process resources • To keep running, a process at least requires: • A program • blocks of memory: stack , heap, code segment • resource tables: File handles, socket handles, IO handles, window handles. • control attributes: Execution state, Process relationship • processor state information: contents of registers, Program Counter (PC), Stack Pointer 3

Process’s context • All the information the OS needs to keep track of the state of a running process: • A program counter. • Registers’ values. • Memory associated with the process. 4

The Scheduler • The OS runs multiple processes “simultaneously” on a single CPU. • The scheduler: The OS component responsible for sharing the CPU. • OS interleaves the execution of processes: • Runs a process a little while • Interrupts it and saves its context. • Runs another process for a little while 5

The Scheduler • We have a list of active processes - and the scheduler switches from one executing process to another. • Two interaction paradigms for the scheduler with current active processes: • Non-Preemptive Scheduling • Preemptive Scheduling 6

Non-preemptive scheduling Non-Preemptive - process signals to OS when process is ready to relinquish the CPU: • Special system call • Process waits for an external event (I/O) • Scheduler select next process to execute according to a scheduling policy • pick the highest priority process • Dos, Windows 3. 1 , older versions of Mac OS. 7

Preemptive scheduling Preemptive: The scheduler interrupts the executing process. • Interrupt: • Sends interrupt at regular intervals. • Suspends the currently executing process. • Starts scheduler. • Scheduler selects the process to execute, according to the scheduling policy. • All modern OS support preemptive scheduling. 8

9

Context Switch • Context Switch - OS switches between one executing process and next one. • Consumes several milliseconds of processing time (10^4 simple CPU operations). • Transparent to processes (process cannot tell it was preempted). 10

Context Switch - steps • Timer interrupt – suspend currently executing process, start scheduler • Save process’s context, for later resume. • Select next process using scheduling policy. • Retrieve next process’s context. • Restore the state of the new process (registers , program counter) • Flush CPU cache (process has new memory map, can not use cache of old process). • Resume new process (start executing code from instruction that was interrupted). 11

The video player example • The player should take the following steps in playing the video: • Read video from disk/internet • Decompress video • Decode video • Display on screen • Video is larger than actual RAM memory • Video is watched before download completes 12

Interleaving (sequential) solution • Read some video data (disk /network) • Decompress • Decode • Display on screen. • Repeat until the video ends 13

14

Concurrency • In order to run tasks concurrently: • Define the task. • Ask "someone“ (thread or process) to run it. 15

Concurrency vs. Parallelism • Concurrency: • Two tasks can start and compete for CPU resources • Two threads running on same processor • They share the processor’s time • Parallelism • Two tasks that run at the same time in parallel • They do not compete on processor’s time • Threads are executing simultaneously

Concurrency vs. Parallelism CPU Core Process 1 Process 2 CPU Core 1 CPU Core 2

Multi-process solution • Playing the movie is decomposed into several independent tasks = processes: • read movie, decompress , decode , display +no need to control interleaving of tasks - processes communication: how will all of these processes communicate with each other? 18

Threads - definition • Single process, multiple tasks • No communication, no context switch… • Multiple threads executing concurrently. • Share all the resources allocated to process • Share memory space / each thread own stack. • Share opened files and access rights. • Can communicate with each other. 19

Multi-threaded solution (concurrency) • Single process, several threads design: • Threads video stream and place chunks in chunk queue. • Threads chunks from queue, decompresses and places in decompressed queue. • Thread decodes into frames and queues in frame queue… • Thread takes frame and displays on screen. 20

Context switch between threads • Not all steps of a regular context switch need to be taken: • No need to restore context (share resources). • No need to flush the CPU cache (share memory) • Need to switch stacks. 21

Concurrency: Advantages • Increases program reactiveness to events: • Reactiveness to GUI applications and their input • Reduces blocking due to executing methods that require long time to finish • Increases availability: • Suitable for service provider programs • Thread as gateway for incoming requests, and other threads to handle user requests • Example: FTP server: gateway thread to handle new clients connecting, thread (per client) to deal with long file transfers. • Allows controllability: • thread can be suspended, resumed or stopped by another thread – inside code! • Simpler design: • Software objects usually model real objects - real life objects are independent and work in parallel • Designing autonomous behavior is easier - simplified implementation than sequential processes • Allows parallelization: • On multiprocessor machines, threads execute in parallel - one thread per CPU, otherwise concurrently • Services provided by RTE operate in a concurrent manner

Concurrency: Limitations • Safety issues: • Multiple threads share resources – we require synchronization mechanisms! • Liveness problems: • We require our threads to be executed, and finished in a reasonable time • Using multi-threading adds new issues such as deadlocks, starvation and livelock • Non deterministic execution: • Each execution of same program is different – order is not guaranteed! • They are also harder to predict, understand, and debug • Added overhead: • Context switching – the more threads the more context switching happens • Synchronization - consumes execution time and complexity

Threads in Java • Java has built in support for threads! • Done by implementing the interface java. lang. Runnable. • java. lang. Runnable • Allows any class that implements it to operate as a thread • Requires one method to be implemented: • public void run() • That is the entry point of the thread. • Implementation: class <class. Name> implements Runnable { public void run(){ //the “main” function of the thread } }

Executing Java Threads • By implementing Runnable and wrapping it with a Thread instance • It takes a single Runnable object and executes it in a separate thread • It is activated by calling start()- which is a Thread function • A (thread) context switch is performed. • It calls the implemented run() method • By implementing Runnable and passing it to an Executor • By extending the Thread class • deprecated syntax

Implementing a Runnable: Example 1 class Simple. Runnable implements Runnable { 2 private String name; 3 Simple. Runnable(String name) { 4 this. name = name; 5 } 6 //prints name three time waits 0. 1 second between each print 7 public void run() { 8 for (int i = 0; i < 3; i++) { 9 System. out. println("RUNNABLE: " + this. name); 10 try { 11 Thread. sleep(100); 12 //time in miliseconds 13 } catch (Exception e) { 14 e. print. Stack. Trace(); 15 } 16 } 17 } 18 }

Togg Executing a Runnable: Example 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Threads 01 { //creates to sime. Runnable threads and starts them public static void main(String[] a) { Simple. Runnable r 1 = new Simple. Runnable("r 1"); Thread t 1 = new Thread(r 1); Simple. Runnable r 2 = new Simple. Runnable("r 2"); Thread t 2 = new Thread(r 2); t 1. start(); t 2. start(); } }

Thread API • java. lang. Thread contains useful functions that can be used in concurrent programming • Complete API: https: //docs. oracle. com/javase/8/docs/api/java/lang/Thread. html • Useful functions: • Thread. current. Thread() – returns the current active thread object • Thread. Object. join() – current. Thread awaits Thread. Object to finish executing before proceeding with code • object. interrupt() – • Thread. current. Thread(). is. Interrupted()

29

30

Sequential. Printer in JVM • Loads Sequential. Printer class • Calls main() function • Creates two objects of Message. Printer. • Message. Printer’s run() is invoked. • Result: two words printed, one after the other. "Hello“ "Goodbye" 31

Java Support for Threads 32

Concurrent. Printer in JVM • Loads Concurrent. Printer class. • Calls the main() function. • Creates two objects of Message. Printer. • Instantiate two Thread objects, with arguments for Runnable objects r. • When thread’s start() is invoked, it invokes r's run(). • Two separate threads running run()of different Message. Printer objects - we cannot know ahead of time printed order. 33

Thread Joining • Thread Joining - When ever a thread wants to wait to another thread to complete it can use the join method of that thread • Here: main thread waits for barney to finish in line 10. 34

Thread Interruption • Bad example: • How can we stopper? Can he tell the time? • One (bad) option – Kill it. Will end up being in an unknown state. 35

Thread Interruption • dfbvgsd • In this example: stopper is running without a purpose. 36

Thread Interruption • dfbvgsd • In this example: stopper cannot know the time (up to 10 seconds). 37

Thread interrupt flag • For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption. 38

Thread interrupt flag • In java, many of the methods that block a thread execution (like sleep) throw an interrupted exception. 39

When to use threads? • Example: Get an n x m matrix and create a vector of length n, which has in each cell i the sum of the elements in the ith row of the matrix 40

41

42

• threads complicates our code • threads will actually make our program slower 43

• threads creation takes both time and memory many threads means many context switches 100 x 100 Matrix 1, 000 Matrix 10, 000 Matrix Sequential Version ~0 millis ~4 millis ~145 millis Parallel Version ~48 millis ~100 millis ~330 millis 44

Threadpool • A pool of worker threads, it contains a queue that keeps tasks waiting to get executed. • No need to re-create threads for every task! • The threads are reused over and over again by receiving new Runnables. • Java library contains a ready made thread-pool implementation called Execution. Service

46

100 x 100 Matrix 1, 000 Matrix 10, 000 Matrix 1, 000 x 200, 000 Matrix Sequential Version ~0 millis ~4 millis ~145 millis ~280 millis Parallel Version ~57 millis ~63 millis ~103 millis ~150 millis 47