●●● ●●●● ●●●●● ●●●● ●●●●● A Process And its threads ●●●● Windows Process 1 Winword Process 5 Process 2 Lab3 Threads Notepad Process 3 Process 4 Winword+ Excel Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun
6 A Process And Its Threads Windows Winword Process 1 Notepad Process 2 Winword Process 3 Excel Process 4 Process 5 Threads Lab3 Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun
●●● ●●●● ●●●●● ●●●● ●●●●● Why Threads? ●●●● In normal execution, a program always needs to wait Wait for user input, wait for screen display, wait for file access etc It is unwise to require programs to execute one after the finish of another Program B can make use of the waiting time of program Ato start its execution as long as program B is not waiting for the result from programA It was first proposed to achieve this by having multiple processes However, it was soon found that the overhead(e.g. the time required) for switching between processes is very high Besides, processes are not tightly coupled to one another, it is difficult to share resources, such as open files They motivate the idea of further dividing a process into smaller units. i. e, threads
7 Why Threads? ⚫ In normal execution, a program always needs to wait ⚫ Wait for user input, wait for screen display, wait for file access, etc. ⚫ It is unwise to require programs to execute one after the finish of another ⚫ Program B can make use of the waiting time of program A to start its execution as long as program B is not waiting for the result from program A ⚫ It was first proposed to achieve this by having multiple processes ⚫ However, it was soon found that the overhead (e.g. the time required) for switching between processes is very high ⚫ Besides, processes are not tightly coupled to one another, it is difficult to share resources, such as open files ⚫ They motivate the idea of further dividing a process into smaller units, i.e. threads
●●● ●●●● ●●●●● How Threads are executed? ●●●● ●●●●● ●●●● o a computer has only one CPU, which can execute one program at a time Hence, in reality, threads are not executing at the same time, but alternatively one after the other For a multithreading system, a thread has at least the following thi g ree states Start Finish execution e RI unning execution Sleeping Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun
8 ⚫ A computer has only one CPU, which can execute one program at a time ⚫ Hence, in reality, threads are not executing at the same time, but alternatively one after the other ⚫ For a multithreading system, a thread has at least the following three states: Ready Running Sleeping Start execution Finish execution Department of ELECTRONIC AND INFORMATION ENGINEERING 3. Controlling Robot Car by Wireless Sensor by Dr Daniel Lun How Threads Are Executed?