630 likes | 760 Views
Unix-v5 Process Structure. A process is an entity which is created by the operating system and consists of a sequence of bytes which is interpreted by the CPU as Machine instruction. Data Stack.
E N D
A process is an entity which is created by the operating system and consists of a sequence of bytes which is interpreted by the CPU as • Machine instruction. • Data • Stack. • Many processes appear to execute simultaneously as the kernel schedules them for execution and several processes may be an instance of one program. In UNIX fork is used to create a process. Process Definition .
Interrupt, interrupt return User running Sys call, interrupt return Return to user exit preempt Zombie Kernel Running preempted Process State Transition Diagram sleep Reschedule Process wakeup Ready run, memory Sleep, memory Swap In Enough memory Swap out fork Swap out created wakeup Ready run, swapped Sleep, swapped .
Created : parent execute system call. • Ready run, memory: move from created when enough memory. • Ready run, no memory: move from created when no memory. • Kernel Running: process is syscall or it is interrupted. • Sleep, memory: process is waiting for completion of I/O. • Sleep, swapped: process is swapped for lack of memory while waiting or I/O completion. • User Running: user process is executing user’s code. • Ready run, swapped: process is ready to run (e.g. when I/O completes) while it is swapped. • Preempted: the process is returning from kernel to user mode, but the kernel preempts it and does a context switching to schedule another process Process State Transition Diagram .
Process Structure Process consists of 3 regions. Region is a contagious area of the virtual address space .
Process A U Area Physical memory User Area - U Same virtual address Process B U Area Physical memory • Each process has a user area. • User area (U) has a fixed virtual address; it is mapped to different physical address. • Each user area is mapped to a physical memory when process is loaded to memory. .
Per process region table U Area Region table Process table Data structure for a process text data stack memory Per process region table allows independent processes to share regions. .
text text data data stack stack Old process deallocate U Area A Region table Per process region table Process table exec a process U Area B memory File descriptor table .
text text data data stack stack U Area Parent Region table File descriptor table Process table fork a process Per process region table U Area Child memory File descriptor table .
text text data data stack stack U Area A File descriptor table create a thread Process table Per process region table Region table memory .
Page 0 Page 1 Page 2 Paging memory layout Every memory location is addressed as (page number, byte offset in page) Memory management hardware divides physical memory into a set of equal sized pages (typical 512-4K bytes). Paging overcomes fragmentation problem. .
87k 552k 727k 941k 1096k 2001k Page table Page table Page n Page n+1 Page n+2 Memory Page table maps virtual address into physical address. Also, it contains access privileges. .
kernel Process virtual space user • Process virtual space is divided into two classes • Kernel space and associated kernel mode. • User space and associated user mode. .
empty 87k 128kk 256k 552k 97k 137k 292k 135k 727k 852k 304k 139k 941k 764k 279k 0k 541k 4k 783k 17k 986k Page table address Virtual address no of pages kernel user Layout of the kernel • Kernel code & data resides in memory permanently, all processes shares it. • When executing in user mode you can’t execute kernel code. • user accesses kernel mode through interrupts; changes mode from user to kernel. .
87k 114k 552k 708k 727k 143k 565k 941k Per process Region table Page table address Virtual address no of pages P A kernel User Area user Page tables P B • Loader assign a U area at fixed virtual location. • The proper region user entry contents will be loaded to U area register at context switching. This region is accessed only in kernel mode. .
The file will be loaded into the region • locked • In demand • In the process of being loaded to memory • Loaded in to memory Region Entry Structure .
Active list free list Region Table • Region table is divided into: • A linked list of active elements. • A linked list of free elements. .
Operations on Region • Lock and unlock • Allocate region and free region. • Attach a region and detach a region. • Load a region from a file into memory space of a process • Duplicate a region. .
Allocate a new region during fork, exec, shmget (shared memory) system calls. • Remove a region entry from a free list and add it to the active list. • Mark the region to be shared (e.g. text) or private (e.g. stack). • Set the inode field in the region to the inode of executable file. • Increment the inode reference count to prevent other processes from removing its contents when unlinking it. • Return a locked region. allocreg -allocate region .
locked region allocreg (inode pointer, region type) output locked region { remove region from linked list of free regions; assign the free region into active list and lock the region mark the region type to be shared or private. if (inode pointer is not null) increment inode reference count to prevent other processes from removing the inode when executes unlink. place region on linked list of active regions. return locked region } allocreg-allocate region (continue) • allocreg is called by shmtget, fork and exec. • In case of fork and exec the operation is associated with a file. The reference count for inode of this file must be incremented. .
1 Region table Active list Active list free list free list Allocreg -allocate region (exec example) Allocate first free element in region table 2 Inode table Active list free list Set region to point to file inode .
Attach a region to a process during fork. Exec, and shmat system calls. • It connects the region to the process address space. • Kernel allocate per process region entry for the new region. • Initialize its type to text, data, shared memory or stack. • The region’s virtual address space shouldn't overlap with other regions. • The process virtual address space shouldn’t exceed the limits. • Region maximum space is 8M, we can’t attach 1 M region with a process of size 7.5 M. • Kernel increase size field in the process table with the region size. • Kernel increase the reference count in the region table. attachreg -attach region .
attachreg • Input: 1 – pointer to locked region to be attached • 2- process to which region is being attached • 3- virtual address in process where region is attached • 4- region type • Output : per process region table entry • { • allocate per process region table entry for process. • initialize per process region table entry: • Set pointer to region being attached. • Set type field. • Set virtual address field. • Increment region reference count. • Increment process size according to growth region • Return (per process region table entry) • } attachreg -attach region .
114k 708k 143k 565k process table Region table attachreg -attach region (exec example) Per process Region table Allocate new text entry (1) Allocate new page table Attach an existing shared text region of size 4 Kbytes to virtual address 0 of the process. .
Kernel invoke growreg to change the size of the region. • Process expands size by executing sbrk system call. • Stack expand explicitly according to the depth of nested procedure call. • The virtual space of the expanded region shouldn't overlap with others. • The process size shouldn’t exceed the maximum size. • The shared region never increase in size if it is attached to other processes. growreg – grow the region size .
In case of more memory required, kernel allocate new page table or expand existing page table. Allocate physical memory for pages on systems do not support demand pages. • If the process contracts the region, the kernel simply release memory assigned to region. • Adjust process size, region size, and per process region entry to reflect the new mapping. growreg – grow the region size .
growreg • Input: 1 – pointer to per process region table • 2- change in size region (+ or -) • if (Region size is +) • Check legality of new size • Allocate page tables • If (not system supporting demand paging) • transfer pages; • else • free physical memory for pages. • free page table entries • Set size field in process table • } growreg – grow the region size .
114k 114k 708k 708k 143k 143k 565k 565k 976k Per process Region table Per process Region table stack stack growreg – grow the region size New page .
freereg – freeing a region • The region will be freed when it is not attached to any process (ref count =0). • Free the inode associated with region using iput. • free the page map table entries and memory pages. .
freereg • Input: 1 – pointer to the locked region • if (region reference count > 0) • unlock region • return • release inode if it exists (iput) • Free page table entries • Free memory associated with pages • Place region in region free list • Unlock region • } freereg – free region .
dupreg – duplicate a region • fork requires the kernel to duplicate data and stack regions. • the region reference count is incremented in case of shared text & memory, allowing the parent & child processes to share regions. • In case of stack & data regions are copied: • Allocate a new region entry. • Allocate page map table. • Allocate physical memory for the region. .
dupreg • Input: 1 – pointer to region table entry • Output: a region which is identical to input region • { • if (region type shared) • // caller will increment reference count with subsequent attachreg • return input region pointer • Allocate new region (allocreg) • Allocate page map table and physical memory for pages. • Copy contents from input region to output region • Return pointer to allocated region • } dupreg – duplicate region .
Per process Region table Text Data Shared text stack dupreg – duplicate a region Process A Private data Per process Region table Private stack Text copy Data stack Process B Private data Private stack .
Allocate memory to load a file (growreg). • Load a file on demand if on demand paging is supported. • Copy the file into memory if on demand not supported. loadreg – load region .
loadreg • Input: 1- pointer to per process region table entry • 2- target virtual address to load region • 3- inode pointer of file for loading region • 4- byte offset in file for start of region • 5- byte count for amount of data to load • { • increase region size to accommodate file size (growreg) • set up u area parameters for reading file • Target virtual address where data is read to • Start offset value for reading file. • Count of bytes to read from file. • Read file into region • Awaken all processes waiting for region to be loaded • } loadreg – load region .
exec system calls load a text of size 7k into memory with a gap of 1K bytes in the beginning. The page containing address 0 will be protected such that access 0 will incur page fault and abort. loadreg – load region .
Region table Per process Region table loadreg – load region 1 2 • allocreg: allocate a region for the executable file. • attachreg: attach the new region to the process. .
Region table Per process Region table loadreg – load region empty growreg: Allocate page map table of one empty entry. The size will be increased by one. .
empty 708k 143k 565k 976k Region table Per process Region table loadreg – load region • loadreg: • growreg: to allocate memory for the file to be loaded. • Load the file to be executed. .
The kernel detaches regions in the exec, exit, and shmdt (detach shared memory). • Decrement process size. • Decrement region reference count.. • Call free region to-free page map table, physical memory if necessary. detachreg – detach region .
detachreg – detach region detachreg Input : pointer to per process region table. Output: none decrement process size; Decrement region reference count; release per process region table; if (region reference count is 0) free region (algorithm freereg); .
Process invoke fork() to create a new process. • Process invoke fork () is a parent and new process is child process. • pid = fork (); • pid in the parent process is the child process ID, while pid in the child process is 0. fork – system call .
The kernel does the following: • Allocate a new slot in the process table for child process. • Assign a unique ID number for the child (Child ID). • Make copy for the following: • User area (U area), this include copy of file descriptor table, and kernel stack. • Increment the reference counts in the file table and inode table for files associated with the process. • data area of parent and stack area of parent • Return the process id for the child process to parent otherwise return 0. fork – system call (continue) .
U Area U Area Open Files Current Directory Changed Directory Open Files Current Directory Changed Directory Per process region table Per process region table Parent data Child data Parent user stack child user stack text text Kernel Stack Kernel Stack data data stack stack Parent Process Fork Creating a new process context File Table Shared Text Child Process inode Table .
pid =777 gid =456 pid =123 gid =456 pid =555 gid =555 pid =666 gid =666 Process group leader process group A pid =456 gid =456 B C . The kernel uses a group id to identify the set of processes which receive common signal. If process A sends a signal kill (0,SIGINT), it will be caught by all processes which have the same gid number(e.g. B,C). .
#include <signal.h> • main (argc,argv){ • int i; • Setpgrp (); // set group id equal to process id • For (i=0;i<4;i++){ • if (fork() == 0) • { • // child process • if (I & 1) // if process is odd set group id • setpgrp (); • printf (“pid %d pgrp %d \n”,getpid(),getpgrp ()); • pause (); //suspend execution until you get signal • while (1) printf (“pid %d pgrp %d \n”,getpid(), getpgrp ()); • } • } • kill (0,SIGINT); // send a termination signal to all processes in group • } set group id example Create 4 child processes. Even number processes has the same group id like parent. Processes created during odd iterations of the loop reset their process group number. When kill signal is sent it will terminate the even number while the odd will continue to execute. .
fork Input : none Output: to parent process, child pid, to child process, 0 allocate process table entry, allocate PID number; copy data from parent process table slot into new child slot; copy data region of parent process (dupreg, attachreg); copy user stack region of parent process, (dupreg, attachreg); share text for parent process (attachreg); increment inode count for current directory and changed root; increment file counts in file table; if (executing process is parent process) change the child state in memory into “ready to run” return (child PID); else // the child process is executing return (0) fork algorithm .
#include <fcntl.h> • int fdrd, fdwt; • char c; • main (argc,argv){ • int argc; • char *argv []; • fdrd = open (argv[1], O_RDONLY); • fdwt = creat (argv[2],0666); • fork (); • rdwrt (); • exit (0); • } • rdwrt (){ • for (;;) • { • if (read (fdrd,&c,1) != 1) • return; • write (fdwt,&c,1); • } • } fork example .
input : abcde fork example (continue) Output : abcde .