DOC PREVIEW
UNO CSCI 8530 - Processes

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11The TempoOperating SystemPart 2ProcessesLast Edit: 9/5/20072Processes/Threads (1)Fundamental executable objectSome of these are really threads (except those created with the “run” system call)Each process instance has a "struct Proc" (defined in h/types.h)created by newproca or run system callsIdle process and first user process created at system startup3Processes/Threads (2)Each process has a state:PROCESS_FREE: unused struct Proc entryPROCESS_READY: process can runPROCESS_CURRENT: process is runningOthers: process is blocked for some reasonEach process has a priority (between LOWEST_PRIORITY and HIGHEST_PRIORITY).Each ready process on a FIFO queue based on its priority.4Processes/Threads (3)Each process identified by a unique int (of type pid_t) called its process ID.Each process has its own user-mode stack.Each process has a set of register values (including general regs, stack pointer, and instruction pointer)When running, these are found in the real registersWhen not running, these values are saved on a stack.There are also special-purpose registers (e.g. cr0 on an x86) that are managed exclusively by the OS.5Processes/Threads (4)Each process runs in user or supervisor (kernel, privileged) mode.User mode used for normal processingSupervisor mode used during system callsx86 architecture actually has 4 protection levels, or rings, but most operating systems use only two of these (0 – supervisor/kernel, and 3 – user).When executing a system call, a process uses a separate kernel stack to guarantee space is available, and to guarantee separation.6Understanding Stack UsageUnderstanding stack use is crucial to understanding process implementation.In IA-32, stacks grow downward in memoryStack pointer register (ESP) identifies the top element on the stackPUSH decrements ESP by item size and stores an itemPOP extracts item and increments ESP by item size27Stack Frames (1)Each function call has a dedicated region of stack called its stackframeBottom of stack frame (or other fixed point in the frame) pointed to by EBP register; top pointed to by ESP registerA C function call pushes arguments (in reverse order) and then the return addressCalled function pushes EBP, copies ESP to EBP, then decrements ESP by size of local variables.8Stack Frames (2)To return, a function places return value in EAX, copies EBP to ESP, pops EBP, and executes the ret instruction which pops the return address into the EIP register.GCC uses leave instruction which copies EBP to ESP and pops EBP.The calling function then reclaims the stack space used by the arguments and deals with the returned value.9Assembler Basics (1)Tempo assembler uses GNU assembler syntax (also called “AT&T” syntax)Operands in opposite order from Intel syntaxRegister names prefixed with ‘%’Constants prefixed with ‘$’Comments prefixed with ‘#’Opcodes suffixed with data type (i.e. b, w, l)Most instructions have two operands. Examples:movl $5, %eax # copies 5 to eax registersubl %eax, %ebx # subtract eax from ebx10Assembler Basics (2)Memory references take the formdisp(base,index,scale)disp is a constant (or label of instr/data)base is a permitted register nameindex is another permitted register namescale is 1, 2, 4, or 8; default is 1Result address is disp+base+index*scale11Assembler Basics (3)Observations:Accessing a parameter:• 8(%ebp) references the first 32-bit parameter• 12(%ebp) reference the second 32-bit parameterAccessing a local variable:• -4(%ebp) references first local 32-bit variable• -8(%ebp) reference second local 32-bit variable0(%ebp) contains caller’s ebp register value4(%ebp) contains return address12Example C Programint f (int a1, int *a2){int temp;temp = a1 * 3;*a2 = temp;return a1 - 5;}main ( ){int x, y, z;x = 12;z = f(x, &y);}313Assembler Code for Examplef:pushl %ebpmovl %esp, %ebpsubl $4, %espmovl 8(%ebp), %edxmovl %edx, %eaxsall %eaxaddl %edx, %eaxmovl %eax, -4(%ebp)movl 12(%ebp), %edxmovl -4(%ebp), %eaxmovl %eax, (%edx)movl 8(%ebp), %eaxsubl $5, %eaxleaveretmain:pushl %ebpmovl %esp, %ebpsubl $12,%espmovl $12, -4(%ebp)leal -8(%ebp), %eaxpushl %eaxpushl -4(%ebp)call faddl $8, %espmovl %eax, -12(%ebp)leaveret14Stack During Execution of fStorage for main’s x Storage for main’s y Storage for main’s z Addr of main’s y Value of main’s xReturn address from fmain’s EBP valueStorage for tempcaller’s EBP value main’s EBPf’s EBP15Initial Stack for a Process (1)As just seen, any function expects a return address at the bottom of the stack when it begins, preceded by any argumentsThe Main function expects two argumentsa count of the number of “command line” arguments (usually called ‘argc’)a pointer to an array containing ‘argc’ pointers to null-terminated strings followed by a null pointer; the strings contain the command line arguments, the first of which is usually the command name.16Initial Stack for a Process (2)These stack contents are created when the process is created (by the newproca system call).The return address is to the function ‘processDone’ which does nothing more than invoke the ‘exit’ system call.Stack space prior to Main’s arguments is used for copies of the newproca caller’s command line argument values.17struct Proc (1)struct Proc {tag_t tag; /* 0: self-identifying tag */pid_t pid; /* 4: unique process identification */Process next; /* 8: next Proc on this Queue */Process prev; /* 12: previous Proc on this Queue */struct Queue *queue; /* 16: on which Queue is this Proc found? */int priority; /* 20: process priority */int state; /* 24: process state */int stksize; /* 28: user stack size (pages) */unsigned int *stkbase; /* 32: base of user stack (from allocStack) *//* This is the address of the bottom *//* word on the stack, not like kstkbase */unsigned int *kstkbase; /* 36: base of kernel stack (1 page always) *//* Address of 1st byte of NEXT page */int signaledOn; /* 40: which event queue was signaled */18struct Proc (2)int timedOut; /* 44: set if process timed out */Process dnext; /* 48: next Proc on the delta queue */unsigned int delta; /* 52: clock ticks left before wakeup */unsigned int *ksp; /* 56: kernel %esp for ready/blocked process */pid_t waitingon; /* 60: process whose termination is awaited */int exitstatus; /* 64: exit status of process 'waitingon' */unsigned int eventaddr; /* 68: address of event for EVENT_WAIT */struct


View Full Document

UNO CSCI 8530 - Processes

Download Processes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Processes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Processes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?