Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure of Processes

Similar presentations


Presentation on theme: "Structure of Processes"— Presentation transcript:

1 Structure of Processes
Mr. M D Jamadar

2 Process States and Transitions
1 2 9 7 3 4 6 5 8 User Running Preempted Zombie Asleep in Memory Sleep, Swapped Ready to Run, Swapped fork Created Ready to Run in Memory Kernel Running not enough mem (swapping system only) swap out wakeup enough mem sleep reschedule process preempt return to user system call, interrupt interrupt, interrupt return

3 (swapping system only)
1 2 9 7 3 4 6 5 8 User Running Preempted Zombie Asleep in Memory Sleep, Swapped Ready to Run, Swapped fork Created Ready to Run in Memory Kernel Running not enough mem (swapping system only) swap out wakeup enough mem sleep reschedule process preempt return to user system call, interrupt interrupt, interrupt return

4 State of a Process Process table entry U area
Contains general fields of processes that must be always be accessible to the kernel U area Further characteristics of the process only need to be accessible to the running process itself

5 Data Structures for Process

6 Data Structure for Process (contd.)
u area main memory Kernel process table per process region table Kernel region table

7 Process table entry State field: user running, kernel running etc.
Fields that allow the kernel to locate the process and u area. Requires while context switch Process size : kernel know how much space to allocate for the process. User ID Process ID Event descriptor. Used when the process is in the "sleep" state. Scheduling parameters. Allow the kernel to determine the order in which processes move to the states "kernel running" and "user running” A signal field. keeps the signals sent to a process but not yet handled. Various timers: process execution time, resource utilization etc.

8 U Area A pointer to the process table entry User IDs various Timer:
Execution time in user mode Execution Time in kernel mode An error field: keeps error during system call Return value field: result of system call I/O parameters Amount of data transfer Address of source and target etc. The current directory and current root User file descriptor table Limit fields Restrict process size Restrict size of the file it can write The control terminal field: login terminal associated with the process, if one exists An array indicates how the process wishes to react to signal

9 Layout of System Memory
A process on the UNIX system consist of three logical sections Text Data (shared memory is considered as a part of data section) Stack The text section contains the set of instruction the machine executes for the process; address in the text section includes text addresses (for branch instructions or subroutine calls),data addresses (for access to global data variables), or stack addresses (for access to data structures local to a subroutines) The compiler generates addresses for a virtual address space with given address range, and the machine memory’s memory management unit translates the virtual addresses into physical memory The subsystem of the kernel and the hardware that cooprate to translate virtual to physical addresses comprise the memory management subsystem

10 <Processes and Regions>
The System V kernel divides the virtual address space of a process into logical regions A region is a contiguous area of the virtual address space of a process that can be treated as a distinct object to be shared or protected. Text, data and stack usually form separate regions of a process Several process can share a region and may execute the same program and they share one copy of text region and also may cooperate to share a common shared memory region. The kernel contains a region table and allocates an entry from the table for each active region in the system Each process contains a private per process region table (U area or a part of process table entry) 8K 16K 32K b a d c e 4K Text Data Stack Process A B Per Proc Region Tables (Virtual Addresses) Region <Processes and Regions>

11 Per Process Region Table (Pregion)
Each pregion entry points to the kernel region table Starting virtual (absolute) address of the region Permission filed: read-only, read-write, read-execute

12 Region Table entry A ptr to the inode of the file whose content were originally load into the region Region type (text, data, ..) Region size Location of region in PhyMem Status: Locked, in demand, valid, being loaded into memory Ref count: number of processes referencing the region

13 Pages and Page Tables In memory management architecture based on pages, the memory management hardware divides physical memory into a set of equal sized blocks called pages. Page sizes range from 512 bytes to 4K bytes and are defined by the hardware. Every addressable memory is contained in a page and every memory location can be addressed by a The purpose of paged memory is to allow greater flexibility in assigning physical memory, analogous to the assignment of disk blocks to files in a file system (page number, byte offset in page)

14 Pages and Page Tables Logical Page Number Physical Page Number 0 177
8K 32K 64K Text Data Stack 541K 783K 986K 897K 87K 552K 727K 941K empty 137K 852K 764K . 1096K 2001K 433K 333K Per Proc Region Table Page Tables(Physical Addresses) Virtual Addresses <Mapping Virtual Addresses to Physical Address> Logical Page Number Physical Page Number Figure shows a sample mapping of a process into physical memory Assume that the size of a page is 1K bytes, and suppose the process wants to access virtual memory address 68,432 The pregion entry shows that the virtual address is in the stack region(stack starts at 64K=64*1024=65536) Subtracting, address is at byte offset 2896 in the region As each page consist of 1K bytes, the address is contained at byte offset 848 in page 2 (counting from 0) of the region, located at physical address 986K

15 Layout of the Kernel The virtual mapping associated with the kernel is independanat of all processes. The code and data resides in the system permanently, and all processes share it. When the system is booted, it loads the kernel code into memory and sets up the necessary tables and registers to map its virtual addresses into physical memory addresses. The mechanism used t map kernel virtual addresses are similar to those used for user addresses. The virtual address space of a process is divided into several classes, including system and user, and each class has its own page tables. When executing kernel mode, the system permits access to kernel addresses, but it prohibits such when executing in user mode.

16

17 Manipulation of process address space
Locking and unlocking a region Allocating a region Attaching a region to process Changing the size of a region Loading the region Freeing a region Detaching a region from process Duplicating a region

18 Allocating a region The kernel allocates a new region(algorithm allocreg) during fork, exec, and shmget system calls. The kernel contains a region table whose entries appears either on free linked list or on an active linked list. When it allocates a region table entry, the kernel removes the first available entry from the free list, places it on the active list, locks the region, and marks its type (shared or private). With few exceptions, every process is associated with an executable file as a result of a prior exec call, and allocreg sets the inode field in the region table entry to point to the inode of executable file. Increases reference count of inode

19 Attaching a region to process
The kernel attaches a region during the fork, exec, and shmat system calls to connect it to the address space of a process. The region may be a newly allocated region or an existing region thet the process will share with other processes. The kernel allocates a free pregion entry, sets its type field to text, data or stack or shared memory, and records the virtual address where the region will exist in the process address space. The process must not exceed the system imposed limits for the highest vitual address, and the virtual addresses of new region must not overlap addresses of the existing regions. Attachreg then initializes a new set of memory management register triples for the process: If the region is not already attached to another process, the kernel allocates page table for it in subsequent call to grow region Finally Attachreg returns a pointer to the pregion entry for the newly attached region

20

21 Changing the size of a region

22 Loading the region

23

24 Freeing a region

25 Detaching a region from process

26 Duplicating a region

27

28 Context of a Process The context of a process consists of the contents of its address space and the contents of hardware registers and kernel data structures that relate to the process. The context of a process is the union of following User-level context Register-level context System-level context

29 User-level Context Text Data User stack Shared memory
These may be in main memory or in the swap space

30 Register-level Context
Program counter (address of next instruction to be executed) Processor Status (PS) register Specifies the hardware status of the machine Usually contains subfields to indicate that the result of a recent computation resulted in 0, positive or negative or that registered overflowed and so on Stack pointer Contains the current address of the next entry in the kernel or user stack General purpose registers Contains data generated by the process during its execution

31 System-level Context Static part Dynamic part Process table entry
U area of a process Pregion entries, region tables, page tables: Dynamic part Kernel stack: Private copy of kernel stack (of a process) A set of layers Each system-level context layer contains necessary info to recover previous layer (including register-level context of previous layer)

32 Components of context of a process

33 Saving the context of a process
Interrupts and exception System call interface Context Switch

34 Interrupts and exception

35

36

37 Context Switch Context switch under four circumstances:
When a process puts its self to sleep When it exits When it return from system call to user mode When it returns to user mode after the kernel completes handling an interrupt

38 Sleep and Wakeup

39

40 Algorithm for sleep

41 Algorithm for wakeup


Download ppt "Structure of Processes"

Similar presentations


Ads by Google