Unix-v5 Process Structure

62
06/14/22 . 1 Unix-v5 Process Structure

description

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. - PowerPoint PPT Presentation

Transcript of Unix-v5 Process Structure

Page 1: Unix-v5 Process Structure

04/21/23 . 1

Unix-v5 Process Structure

Page 2: Unix-v5 Process Structure

04/21/23 . 2

• 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

1.Machine instruction.

2.Data

3.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

Page 3: Unix-v5 Process Structure

04/21/23 . 3

Process State Transition Diagram

created

fork

Ready run,memory

Enough memory

Ready run,swapped

Swap In

Swap out

Sleep, memorywakeup

Sleep, swappedwakeup

Swap out

Kernel Running

Reschedule Process

Zombieexit

preemptedpreempt

User running

Return to user

Sys call, interrupt return

Interrupt, interrupt return

sleep

Page 4: Unix-v5 Process Structure

04/21/23 . 4

Process State Transition Diagram

• 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

Page 5: Unix-v5 Process Structure

04/21/23 . 5

Process Structure

text

Data

Stack

Process consists of 3 regions. Region is a contagious area of the virtual address space

Page 6: Unix-v5 Process Structure

04/21/23 . 6

User Area - U

text

Data

Stack

•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.

U Area

text

Data

Stack

Process A

Process B

U Area

Same virtual address

Physical memory

Physical memory

Page 7: Unix-v5 Process Structure

04/21/23 . 7

Data structure for a process

U Area

Process table

Per process region table allows independent processes to share regions.

text

data

stack

Per process region table

Region table

memory

Page 8: Unix-v5 Process Structure

04/21/23 . 8

exec a process

U Area A

Process table

text

data

stack

Per process region table

Region table

memory

text

data

stack

U Area B

Old process deallocate

File descriptor table

Page 9: Unix-v5 Process Structure

04/21/23 . 9

fork a process

U Area Parent

Process table

text

data

stack

Per process region table

Region table

memory

text

data

stack

U Area Child

File descriptor table

File descriptor table

Page 10: Unix-v5 Process Structure

04/21/23 . 10

create a thread

U Area A

Process table

text

data

stack

Per process region table Region table

memory

text

data

stack

File descriptor table

Page 11: Unix-v5 Process Structure

04/21/23 . 11

Paging memory layout

Memory management hardware divides physical memory into a set of equal sized pages (typical 512-4K bytes).

Paging overcomes fragmentation problem.

Page 0 Page 1 Page 2

(page number, byte offset in page)

Every memory location is addressed as

Page 12: Unix-v5 Process Structure

04/21/23 . 12

Page table

Page table maps virtual address into physical address. Also, it contains access privileges.

87k

1096k

941k

552k

2001k

727k

Page n Page n+1 Page n+2

Page table

Memory

Page 13: Unix-v5 Process Structure

04/21/23 . 13

Process virtual space

Process virtual space is divided into two classes

• Kernel space and associated kernel mode.

• User space and associated user mode.

text 0

data 1M

stack 2M

text 4M

data

stack

kernel

user

Page 14: Unix-v5 Process Structure

04/21/23 . 14

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

941k

552k

727k

empty

764k

137k

852k

541k

783k

986k

128kk

139k

97k

135k

256k

279k

292k

304k

0k

4k

17k

text 0

data 1M

stack 2M

text 4M

data

stack

Page table address Virtual address no of pages

kernel

user

Page 15: Unix-v5 Process Structure

04/21/23 . 15

User Area

• 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.

text 0

data 1M

stack 2M

U area 4K

text 4M

data

stack

Page table address Virtual address no of pages

kernel

user

text 0

data 1M

stack 2M

U area 4M

text

data

stack

Per process Region table

114k

565k

708k

143k

87k

941k

552k

727k

P B

Page tablestext 0

data 1M

stack 2M

U area 4M

text

data

stack

P A

Page 16: Unix-v5 Process Structure

04/21/23 . 16

Region Entry Structure

Inode pointer to the file

Size of the region

status

Count number of processes that reference the region

The file will be loaded into the region

• locked

•In demand

•In the process of being loaded to memory

•Loaded in to memory

Page 17: Unix-v5 Process Structure

04/21/23 . 17

Region Table

Active list

free list

Region table is divided into:

•A linked list of active elements.

•A linked list of free elements.

Page 18: Unix-v5 Process Structure

04/21/23 . 18

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.

Page 19: Unix-v5 Process Structure

04/21/23 . 19

allocreg -allocate region

1. Allocate a new region during fork, exec, shmget (shared memory) system calls.

2. Remove a region entry from a free list and add it to the active list.

3. Mark the region to be shared (e.g. text) or private (e.g. stack).

4. Set the inode field in the region to the inode of executable file.

5. Increment the inode reference count to prevent other processes from removing its contents when unlinking it.

6. Return a locked region.

Page 20: Unix-v5 Process Structure

04/21/23 . 20

allocreg-allocate region (continue)

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 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.

Page 21: Unix-v5 Process Structure

04/21/23 . 21

Allocreg -allocate region (exec example)

Active list

free list

Active list

free list

Allocate first free element in region table

1

Active list

free list

Set region to point to file

inode

2 Inode table

Region table

Page 22: Unix-v5 Process Structure

04/21/23 . 22

attachreg -attach region

•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.

Page 23: Unix-v5 Process Structure

04/21/23 . 23

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 typeOutput : 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 regionReturn (per process region table entry)

}

Page 24: Unix-v5 Process Structure

04/21/23 . 24

attachreg -attach region (exec example)

0 4

Per process Region table

Allocate new text entry (1)

114k

565k

708k

143k

Allocate new page table

Region table

process table

Attach an existing shared text region of size 4 Kbytes to virtual address 0 of the process.

Page 25: Unix-v5 Process Structure

04/21/23 . 25

growreg – grow the region size

• Kernel invoke growreg to change the size of the region.

1. Process expands size by executing sbrk system call.

2. 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.

Page 26: Unix-v5 Process Structure

04/21/23 . 26

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.

Page 27: Unix-v5 Process Structure

04/21/23 . 27

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 tablesIf (not system supporting demand paging)

transfer pages;else

free physical memory for pages.free page table entries

Set size field in process table

}

Page 28: Unix-v5 Process Structure

04/21/23 . 28

growreg – grow the region size

128 4

Per process Region table

114k

565k

708k

143k

128 5

Per process Region table

stack stack

114k

565k

708k

143k

976kNew page

Page 29: Unix-v5 Process Structure

04/21/23 . 29

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.

Page 30: Unix-v5 Process Structure

04/21/23 . 30

freereg – free region

freereg Input: 1 – pointer to the locked region

if (region reference count > 0) unlock region

returnrelease inode if it exists (iput)Free page table entriesFree memory associated with pagesPlace region in region free listUnlock region

}

Page 31: Unix-v5 Process Structure

04/21/23 . 31

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:

1. Allocate a new region entry.

2. Allocate page map table.

3. Allocate physical memory for the region.

Page 32: Unix-v5 Process Structure

04/21/23 . 32

dupreg – duplicate region

dupreg Input: 1 – pointer to region table entryOutput: a region which is identical to input region {

if (region type shared)// caller will increment reference count with subsequent attachregreturn input region pointer

Allocate new region (allocreg)Allocate page map table and physical memory for pages.Copy contents from input region to output regionReturn pointer to allocated region

}

Page 33: Unix-v5 Process Structure

04/21/23 . 33

dupreg – duplicate a region

Per process Region table

Per process Region table

stack

stack

TextData

DataText

Process A

Process B

Shared text

Private data

Private stack

Private data

Private stack

copy

Page 34: Unix-v5 Process Structure

04/21/23 . 34

loadreg – load region

• 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.

Page 35: Unix-v5 Process Structure

04/21/23 . 35

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

}

Page 36: Unix-v5 Process Structure

04/21/23 . 36

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.

Page 37: Unix-v5 Process Structure

04/21/23 . 37

loadreg – load region

Page table addr

Proc virtual address

Size and protect

--------- 0

Per process Region table

• allocreg: allocate a region for the executable file.

• attachreg: attach the new region to the process.

Region table

1

2

Page 38: Unix-v5 Process Structure

04/21/23 . 38

loadreg – load region

Page table addr

Proc virtual address

Size and protect

0 1

Per process Region table

empty

growreg: Allocate page map table of one empty entry. The size will be increased by one.

Region table

Page 39: Unix-v5 Process Structure

04/21/23 . 39

loadreg – load region

Page table addr

Proc virtual address

Size and protect

0 8

Per process Region table

loadreg:

• growreg: to allocate memory for the file to be loaded.

• Load the file to be executed.

Region table

empty

565k

708k

143k

976k

Page 40: Unix-v5 Process Structure

04/21/23 . 40

detachreg – detach region

• 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.

Page 41: Unix-v5 Process Structure

04/21/23 . 41

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);

Page 42: Unix-v5 Process Structure

04/21/23 . 42

Process Control

Page 43: Unix-v5 Process Structure

04/21/23 . 43

fork – system call

• 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.

Page 44: Unix-v5 Process Structure

04/21/23 . 44

fork – system call (continue)

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:

1. User area (U area), this include copy of file descriptor table, and kernel stack.

2. Increment the reference counts in the file table and inode table for files associated with the process.

3. data area of parent and stack area of parent

• Return the process id for the child process to parent otherwise return 0.

Page 45: Unix-v5 Process Structure

04/21/23 . 45

Fork Creating a new process context

Shared Text

textdatastack

Per process region tableParent data

Parent user stack

Open Files

Current Directory

Changed Directory

U Area

Kernel Stack

textdatastack

Per process region tableChild data

child user stack

Open Files

Current Directory

Changed Directory

U Area

Kernel Stack

File Table

inode Table

Parent Process

Child Process

Page 46: Unix-v5 Process Structure

04/21/23 . 46

process group

pid =123 gid =456

Process group leader

pid =456 gid =456

pid =777 gid =456

. 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).

A

B C

pid =555 gid =555 pid =666 gid =666

Page 47: Unix-v5 Process Structure

04/21/23 . 47

set group id example

#include <signal.h>main (argc,argv){

int i;Setpgrp (); // set group id equal to process idFor (i=0;i<4;i++){

if (fork() == 0){

// child processif (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

}

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.

Page 48: Unix-v5 Process Structure

04/21/23 . 48

fork algorithm

fork Input : noneOutput: 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 executingreturn (0)

Page 49: Unix-v5 Process Structure

04/21/23 . 49

fork example

#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);

}}

Page 50: Unix-v5 Process Structure

04/21/23 . 50

fork example (continue)

input : abcde

Output : abcde

Page 51: Unix-v5 Process Structure

04/21/23 . 51

exit – system call

Process is terminated by executing exit system call. An exiting process will enter zombie state, relinquishes its resources, and dismantle its context except for its slot in the process table.

• terminates the calling process "immediately".

• Any open file descriptors belonging to the process are closed

• any children of the process are inherited by process 1, init,

• the process's par ent is sent a SIGCHLD signal. The value status is returned to the parent process as the process's exit status, and can be collected using one of the wait family of calls)

exit (status)

Where the value of status is returned to the parent process for examination. The exit might be called implicitly or explicitly.

Page 52: Unix-v5 Process Structure

04/21/23 . 52

exit – system callexitInput: return code for parent processOutput: none{

if (process is a group leader)send an hangup signal to all members of process groupreset process group for all members to 0

close all open files (internal version of close)release current directory (iput)release current changed root, if exists (iput);free region; (freereg)make process state zombie;assign parent to all children processes to be init (PPID = 1);

send death of child (SIGCHLD) to parent processif (child process in zombie state)

// init remove child from process table send death of child (SIGCHLD) to parent

}

Page 53: Unix-v5 Process Structure

04/21/23 . 53

exit – system call-example

main (){

int child;if ((child = fork ()) == 0){

printf (“CHILD pid %D \n”, getpid ());pause (); //suspend execution until signal

}// parent

printf (“child PID %d\n”, child);}

Page 54: Unix-v5 Process Structure

04/21/23 . 54

process group

• Processes on UNIX are identified by a unique ID number and by group id number. Both ids are saved in the process table.

• Kernel uses process group number to identify group of related processes that should receive a common signal.

• Processes that have a common ancestor process that is a login shell receives a common signal when the user hits control-d character.

• setpgrp system call sets the group id equal to the process id.

grp = setpgrp ();

Page 55: Unix-v5 Process Structure

04/21/23 . 55

exec – system call

• exec invoke another program and overlay the memory space of the process with the copy of the executable file.

• The old user context is no longer accessible except for the exec’s parameters.

exec (filename, argv, envp)

1. filename is the name of the file to be executable

2. argv is a pointer to an array of characters that are parameters to the program.

3. envp is a pointer to an array of characters which have the environment of the executable program (e.g. name = value).

4. execl. execv, execle etc are different versions of exec.

• exec access the file’s inode via algorithm namei, Determine that it is executable, user has permission to execute it.

• Since parameters to exec is part of the old memory space about to be freed, the kernel copy argv and envp to holding place such as the kernel stack.

Page 56: Unix-v5 Process Structure

04/21/23 . 56

exec – system call (continue)

• Detach the old region using detachreg.

• The kernel allocates and attaches regions for text and data, load the contents of the executable file into memory (allocreg, attachreg, and loadreg).

• The data regions is divided into two parts: initialized at compile time and not initialized data regions. The kernel allocate region (allocreg) for the initialized data region, attach it (attachreg) and initializes the value of memory to 0, for the not initialized data region it increase the size of data region using the growreg.

• allocate (allocreg) and attach (attachreg) a user stack to the process. Copy the exec parameters into the user stack.

• Initialize the stack and program counter registers.

• Release the inode which are allocated by namei in the beginning of exec using iput.

• The process id stays the same and its position in the process hierarchy stays the same, but only the user contexts change.

Page 57: Unix-v5 Process Structure

04/21/23 . 57

exec – algorithm

exec Input : file name parameter list environment variable list{

get file inode (namei)verify file executable, user has permission to executeread file headers, check that it is load modulecopy exec parameters from old address space to system spacefor (every region attached to the process)

detach all regions (detachreg)for (every region specified in load module)

allocate new region (allocreg)attach the region (attachreg)load region to memory if appropriate (loadreg)

copy exec parameters into new user stack regioninitialize registers (e.g. program counter & stack register)

release inode of file (iput)}

Page 58: Unix-v5 Process Structure

04/21/23 . 58

exec – system call-example

main (){

int status;if (fork () == 0)

exec (“/bin/date”,”date”,0);wait (&status)

}

• The kernel finds that the /bin/date is an executable file and all users can execute it.

• The kernel copy “/bin/date”, “date”, into a holding place (e.g. kernel stack).

• Free text, data, and stack regions occupied by the process.

• Allocate new text, data, and stack . Copy the instructions of /bin/date/ into the text region, and copy the data area into the data region.

• The kernel copies the argument “date” into the user stack.

• After the exec the child process is executing the “date” program.

• When the date program terminates, the parent process receives its exit status from the wait call.

Page 59: Unix-v5 Process Structure

04/21/23 . 59

exec – executable file structure

• primary header: describe how many sections in the file, the start address for the process execution, the magic number which identifies the executable file.

• Section Headers: describe the section size, type and virtual address for the section.

• Data: the section data contains information such as the text that is initially loaded in the process address space. Section data could contains symbol table or debugging information.

Magic NumberNumber Of Sections

Initial Register Values

Section TypeSection Size

Virtual Address

Section TypeSection Size

Virtual Address

Data (e.g. text)

Data (e.g. text)

Primary Header

Section 1 Header

Section n Header

Section 1

Section n

Page 60: Unix-v5 Process Structure

04/21/23 . 60

changing the size of the process’s data region

Two functions to change the size of the process

1. brk (ends), ends becomes value of the highest virtual address of the data region of process and is called its break value

2. oldends = sbrk (inc) inc, change the current break value by inc number of bytes, oldends is the break value before the call.

• Kernel checks if the new process size is less than the system maximum.

• The new data region doesn’t overlap with other regions.

• If all checks pass the kernel invoke growreg to allocate auxiliary memory (e.g. page tables) for the data region and increments the process size.

• It tries to allocate memory for the new space and initialize it to 0. If not able to allocate memory, it swaps the process out until the new space is available.

• The new increased space is virtually contagious with the old one.

Page 61: Unix-v5 Process Structure

04/21/23 . 61

brk – algorithm

brk Input : new break valueOutput: old break value lock process data region

if (region size is increasing & new region size is illegal)unlock data regionreturn error

change region size (growreg)zero out addresses in new data spaceunlock process data region

Page 62: Unix-v5 Process Structure

04/21/23 . 62

brk – example

Main (){

char *endpt;endpt = sbrk(0);printf (“endpt = %ud \n”, endpt);while (endpt -- ){

if (brk (endpt) == -1){

printf (“brk of %ud failed\n”,endpt);exit ();

}}

}