Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processes and threads.

Similar presentations


Presentation on theme: "Processes and threads."— Presentation transcript:

1 Processes and threads

2 Overview Process and Process states Operations on processes
Process Scheduling Threads

3 Definition A process is a unit of work in a computer system.
Processes, can be operating-system processes (those that execute system code) OR user / application processes (those that execute user code).

4 Processes The OS is responsible for the following activities related to process management: Scheduling processes and threads on the CPUs Creating and deleting both user and system processes Suspending and resuming processes Providing mechanisms for process synchronization Providing mechanisms for process communication

5 Process Vs Program A program by itself is not a process; a program is a passive entity, such as a file containing a list of instructions stored on disk (often called an executable file), whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. A program becomes a process when an executable file is loaded into memory.

6 Process in memory

7 Process Memory Process memory is divided into four sections as shown on previous slide: The text section comprises the compiled program code, read in from non-volatile storage when the program is launched. The data section stores global and static variables, allocated and initialized prior to executing main. The heap is used for dynamic memory allocation during run time. The stack contains temporary data e.g. local variables. Space on the stack is reserved for local variables when they are declared and the space is freed up when the variables go out of scope. **** Note that the stack and the heap start at opposite ends of the process's free space and grow towards each other.

8 Process Memory Contd… When processes are swapped out of memory and later restored, additional information must also be stored and restored. Key among them are the program counter and the value of all program registers. A program counter is a register in a computer processor that contains the address (location) of the instruction being executed at the current time

9 Processes Although two processes may be associated with the same program, they are considered two separate execution sequences. For instance, several users may be running different copies of the mail program, or the same user may invoke many copies of the Web browser program. Each of these is a separate process; and although the text sections are equivalent, the data, heap, and stack sections vary. It is also common to have a process that spawns many processes as it runs.

10 Process States

11 Process States Contd… Processes may be in one of 5 states, as shown previously: New - The process is in the stage of being created. Ready - The process has all the resources available that it needs to run, but the CPU is not currently working on this process's instructions. Running - The CPU is working on this process's instructions. Waiting - The process cannot run at the moment, because it is waiting for some resource to become available or for some event to occur. For example the process may be waiting for keyboard input, disk access request, inter-process messages, a timer to go off, or a child process to finish. Terminated - The process has completed.

12 Process Control Block (PCB)
For each process there is a PCB which stores the following process-specific information Process State - Running, waiting, etc., as discussed above. Process ID, and parent process ID. CPU registers and Program Counter - These need to be saved and restored when swapping processes in and out of the CPU. CPU-Scheduling information - Such as priority information and pointers to scheduling queues. Memory-Management information - E.g. page tables or segment tables. Accounting information - user and kernel CPU time consumed, account numbers, limits, etc. I/O Status information - Devices allocated, open file tables, etc.

13 Process Control Block (PCB)

14 Process Scheduling Its aim is to maximise CPU use by switching processes onto the CPU for time sharing The two main objectives of process scheduling are to keep the CPU busy at all times deliver "acceptable" response times for all programs, particularly for interactive ones. The process scheduler must meet these objectives by implementing suitable policies for swapping processes in and out of the CPU.

15 Scheduling Queues Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, waiting to execute Device queues –set of processes waiting for an I/O device to become available or to deliver data are placed in. There is generally a separate device queue for each device. ***Processes migrate among the various queues

16 Schedulers Long-term scheduler ( or Job Scheduler) – selects which processes should be brought into the ready queue . Invoked very infrequently Short-term scheduler ( or CPU Scheduler) – selects which process should be executed next and allocates CPU. Invoked very frequently.

17 Types of Processes Processes can be described as either:
I/O-bound processes Spend more time doing I/O than computations, many short CPU bursts CPU-bound processes Spend more time doing computations; few very long CPU bursts

18 Medium-term Scheduler
Some systems also employ a medium-term scheduler. When system loads get high, this scheduler will swap one or more processes out of the ready queue for a few seconds, in order to allow smaller faster jobs to finish up quickly and clear the system. An efficient scheduling system will select a good process mix of CPU-bound processes and I/O bound processes

19 Context Switch It is a process in which the CPU switches to another process In this case, the system must save the state of the old process and load the saved state for the new process (via a context switch) Context switch time is overhead; the system does no useful work while switching Time lost during context switching depends on the computer`s hardware support

20 Context Switch Whenever an interrupt arrives, the CPU must do a state-save of the currently running process, then switch into kernel mode to handle the interrupt, and then do a state-restore of the interrupted process. Similarly, a context switch occurs when the time slice for one process has expired and a new process is to be loaded from the ready queue. This will be instigated by a timer interrupt, which will then cause the current process's state to be saved and the new process's state to be restored. Saving and restoring states involves saving and restoring all of the registers and program counter(s), as well as the process control blocks described above.

21 Context Switching Contd…
Context switching happens VERY VERY frequently, and the overhead of doing the switching is just lost CPU time, so context switches ( state saves & restores ) need to be as fast as possible. Some hardware has special provisions for speeding this up, such as a single machine instruction for saving or restoring all registers at once. Some hardware actually has multiple sets of registers, so the context switching can be speeded up by merely switching which set of registers are currently in use.

22 THREADS

23 Threads A program has one or more locus of execution. Each execution is called a thread of execution. The set of threads comprise a process. Each thread contains: **an instruction pointer (IP), a register with next instruction. **a stack for temporary data (eg, return addresses, parameters) **a data area for data declared globally and statically A process/thread is active, while a program is not.

24 Threads Modern systems allow a single process to have multiple threads of execution, which execute concurrently.

25 The Thread Model (1) (a) Three processes each with one thread
(b) One process with three threads

26 The Thread Model (2) Items shared by all threads in a process
Items private to each thread

27 Each thread has its own stack
The Thread Model (3) Each thread has its own stack

28 A word processor with three threads
Thread Usage A word processor with three threads

29 Tutorial What are the benefits of having threads?
What is the relationship between threads multi-processor systems? Explain the differences between user-level threads and kernel-level threads.


Download ppt "Processes and threads."

Similar presentations


Ads by Google