1 / 33

Processes and threads

Processes and threads. Lecture 4 ~ Fall, 2007 ~. Contents. Processes Threads Scheduling. Process definition. OS abstraction to represent what is needed to run a single program A program in execution , including the current values of PC, registers, and variables

hcoleman
Download Presentation

Processes and threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Processes and threads Lecture 4 ~ Fall, 2007 ~

  2. Contents • Processes • Threads • Scheduling TUCN. Operating Systems. Lecture 4

  3. Process definition • OS abstraction to represent what is needed to run a single program • A program in execution, including the current values of PC, registers, and variables • Formally, a process is a sequential stream of execution in its own address space TUCN. Operating Systems. Lecture 4

  4. The process model Process and program • The program is a static (inactive) entity, whereas the process is an active one • A process is an activity of some kind • A process has a program, input, output, and a state • There are two parts of a process • sequential execution: no concurrency inside a process; everything happens sequentially • process state: everything that process interacts with (registers, memory, files, etc) TUCN. Operating Systems. Lecture 4

  5. The process modelThe context of this presentation • Single-processor systems • Pseudo-parallelism • Multiprogramming • Switching among processes • A single processor may be shared among several processes • Scheduling algorithm TUCN. Operating Systems. Lecture 4

  6. Process creation • Automatically by the system • At system initialization • Foreground and background processes • By another process • A system call for process creation • Situations • A process needs help doing some computation • A user action – interaction with the shell TUCN. Operating Systems. Lecture 4

  7. Process hierarchies • A process creates a new process • The two processes are distinct • Process hierarchy • Parent–Child Relationship • The child can itself create new processes • Tree of processes • No hierarchy • All processes are equal TUCN. Operating Systems. Lecture 4

  8. Process termination • Voluntary – using a special system call • Normal exit • Error detection exit • Ex.: inexistent files, insufficient or incorrect input • Involuntary – receiving an interruption • Initiated by the system due to a fatal error • Ex.: illegal instructions, division by zero etc. • Initiated (killed) by another process TUCN. Operating Systems. Lecture 4

  9. Process states • Running • using the CPU at that moment (executed by the CPU) • Ready • ready to be executed, but no CPU available • Blocked • wait for same event to occur • Just created (optional) • waiting for same resources to be allocated • Terminated (optional) • keeping same information about the exit state TUCN. Operating Systems. Lecture 4

  10. Process states transitions • Just created to Ready or Running • Running to Ready • suspended by the scheduler • time slice expired • Ready to Running • its turn comes again • selected by the scheduler • Running to Blocked • wait for some event to occur • Blocked to Ready • the awaited event occurs • Running to Terminated Just created (optional) Terminated (optional) Running Blocked Ready TUCN. Operating Systems. Lecture 4

  11. Implementation of processes • OS maintains a process table • each process is allocate an entry – PCB (Process Control Block) • used as a repository when the process is suspended or blocked • Information in a PCB • process state (new, ready, running, waiting, or halted) • the value of the program counter (PC) • the value of the CPU registers • CPU scheduling information (process priority etc.) • memory management information (pointers etc.) • accounting information (the amount of CPU and real time used, time limits etc.) • I/O status information (outstanding I/O req., I/O devices allocated, list of open files etc.) TUCN. Operating Systems. Lecture 4

  12. Relation between processes • Independent processes • the set of object accessed by them are disjoint • a process cannot affect or be affected by other processes executing in the system • Competing (concurrent) processes • processes share resources but there is no information exchanged between them • a process can affect or be affected by the other processes executing in the system • Cooperating processes • exchange information either by using shared data objects or through message passing TUCN. Operating Systems. Lecture 4

  13. The thread model (1) • Process model • resource grouping and execution • Processes • group related resources together • Threads of one process • describe an sequential execution within a process • share the same address space and resources of the process • each thread has its own PC, registers and stack of execution • there is no protection between threads in one process • lightweight processes, multithreading TUCN. Operating Systems. Lecture 4

  14. The thread model (2) Three processes each with one thread Multiprogramming One process with three threads Multithreading TUCN. Operating Systems. Lecture 4

  15. The thread model (3) TUCN. Operating Systems. Lecture 4

  16. Thread usage (1)A multithreaded document editor TUCN. Operating Systems. Lecture 4

  17. Thread usage (2)A multithreading Web server TUCN. Operating Systems. Lecture 4

  18. Implementing threads in user space (1) TUCN. Operating Systems. Lecture 4

  19. Implementing threads in user space (2) • The kernel knows nothing about threads • The approach is suitable for OS that does not support threads • The threads run on top of a runtime system • Each process has its own thread table • Advantages • Thread switching and scheduling is faster than to trapping the kernel • Each process can have its own customized scheduling algorithm • Scale better • Disadvantages • The implementation of blocking system calls • The need that a thread voluntarily gives up the CPU TUCN. Operating Systems. Lecture 4

  20. Implementing threads in the kernel (1) TUCN. Operating Systems. Lecture 4

  21. Implementing threads in the kernel (2) • The kernel knows about the threads • The kernel schedules all the threads • The kernel has a thread table • Advantages • The kernel can switch between threads belonging to different processes • No problem with blocking system calls • Disadvantages • Greater cost (time and resources) – Solution: recycling TUCN. Operating Systems. Lecture 4

  22. Hybrid implementation TUCN. Operating Systems. Lecture 4

  23. Introduction to scheduling (1) • Scheduler • OS component that decides what process will be run and for how long • uses a scheduling algorithm • Processes • Compute-bounded (CPU-bounded) • I/O-bounded TUCN. Operating Systems. Lecture 4

  24. Introduction to scheduling (2) TUCN. Operating Systems. Lecture 4

  25. Introduction to scheduling (3) The moment of scheduling • Process creation • Process termination • Process blocking • Interrupt occurrence • Clock interrupt occurrence • non-preemptive scheduling algorithms • preemptive scheduling algorithms TUCN. Operating Systems. Lecture 4

  26. Introduction to scheduling (4) Categories of scheduling algorithms • Batch • Non-preemptive algorithms • Preemptive algorithms with long time quanta • Reduces processes switches and increase performance • Interactive • Preemptive algorithms are needed • Real-Time • Preemption normally used, but sometimes not needed TUCN. Operating Systems. Lecture 4

  27. Scheduling algorithm goals (1) • All systems • Fairness • giving each process a fair share of CPU • equivalent processes get equivalent CPU times • Balance – keeping all parts of the system busy • Batch systems • Throughput – maximize jobs per hour • Turnaround time – minimize time between submission and termination TUCN. Operating Systems. Lecture 4

  28. Scheduling algorithm goals (2) • Interactive systems • Response time – respond (react) to request quickly • Proportionality – meet, if possible, user’s expectations • Real-time systems • Meeting deadlines – avoid losing data TUCN. Operating Systems. Lecture 4

  29. Scheduling in batch systems (1)First-Come First-Served • Is non-preemptive • Simple to understand and implement • The average waiting time (a.w.t.) depends on the process order and execution time • Example 1 • P1:24, P2:3, P3:3 (process : execution time) • waiting time for P1 is 0 ms, for P2 is 24, and for P3 is 27 • a.w.t. = 17 ms • Example 2 • P2:3, P3:3, P1:24 (process:execution time) • waiting time for P1 is 6 ms, for P2 is 0, and for P3 is 3 • a.w.t. = 3 ms TUCN. Operating Systems. Lecture 4

  30. Scheduling in batch systems (2)Shortest Job First • Runtime is known in advance • Is the optimal algorithm (only) when all the jobs are available simultaneously • Non-preemptive • Example • 0:P1:8,1:P2:4, 2:P3:9, 3:P4:5(submit time:process:run time ) • a.w.t. = (0 + (8-1) + (17-3) + (12-2))/4 = 7.75 • Preemptive  Shortest remaining time next • Example – the same as previously • a.w.t. = ((10-1)+(1-1)+(17-2)+(5-3))/4 = 6,5 TUCN. Operating Systems. Lecture 4

  31. Scheduling in interactive sys. (1)Round-Robin Scheduling • Each process is assigned a time interval • time quantum or slice • Is a preemptive algorithm • Maintain a FIFO list for ready processes • The length of the quantum • Too short  lower the CPU efficiency • Too large  poor response to interactive requests • 20-50 msec is a reasonable compromise TUCN. Operating Systems. Lecture 4

  32. Scheduling in interactive sys. (2)Priority scheduling • Each process has a priority assigned • The greatest priority runnable process is always run • Give a chance to other processes  change the priority of the running process or assign it a quantum • Priorities assignment • statically • dynamically • Priority classes • Use priority scheduling between classes • Use another scheduling algorithm with each class TUCN. Operating Systems. Lecture 4

  33. Bibliography [Tann01] Andrew Tannenbaum, “Modern Operating Systems”, second edition, Prentice Hall, 2001, pg. 71 – 100, pg. 132 – 151. TUCN. Operating Systems. Lecture 4

More Related