newproca Process Creation The Tempo Operating System Part 6 System Call Implementation Processes and Timers Conceptually simple validate parameters allocate an unused process table entry fill in the appropriate values allocate user and kernel stack space initialize stacks for initial return from kernel No code or data needs to be initialized since these are already linked and are present in memory The most complicated part of newproca is getting the initial user and kernel stack contents correct Last update 2 16 2009 newproca pseudocode 1 Find an unused process table entry if not found return error PROCTBLFULL Extract system call arguments Setup tag priority and process ID Allocate user mode stack handle errors Determine size of string arguments Set user stack info args ptr to args return address processDone Copy string arguments and pointers to user mode stack newproca pseudocode 3 Mark all other file descriptors as available for use Mark process as not signaled see signal discussion for details Increase number of processes in the system Add process to appropriate ready queue by calling addready If new process has priority greater than current creating process add current process to the appropriate ready queue and set rs to 1 Return the ID of the new process newproca pseudocode 2 Allocate kernel mode stack always 1 page Initialize kernel stack user mode stack segment and stack pointer eflags register code segment and instruction pointer remaining segment and general purpose registers Initialize event queues empty unmasked Setup file descriptors 0 1 for keyboard display User Stack at Process Startup String N String 2 Bottom of stack String 1 Null Ptr Addr Str N Addr Str 2 Addr Str 1 Addr Array Initial stack pointer N processDone 1 Kernel Stack at Process Startup User Stack Sel User Stack Ptr eflags Reg User Code Sel Instr Pointer Other Segment Registers 4 Bottom of the kernel stack Popped restored by the iret instruction after isr30z in kernel s Initial kernel stack pointer run create load process from disk Contrary to its name newproca does not really create a process Instead it creates what is usually called a thread The run system call on the other hand creates a real process and initializes its address space appropriately code data comes from a disk file Gen Purpose Registers 8 run pseudocode part 1 Find an unused process table entry if not found return error PROCTBLFULL Extract system call arguments Expand runinf structure s components or copy caller s information for new process Validate new process priority Allocate one page of RAM for the new process page table recall this allows for mapping a 4MB process addr space Open the file containing the executable image Load the executable file into RAM described later Close the file Loading an executable file part 1 There are two actually three but one is funky forms of executable files handled by the run system call ELF Executable and Loadable Format and PE Portable Executable ELF is most common in the Linux envirnment while PE executables are used in the Windows world Perhaps MACH O format will be added later for executables produced using MAC OS X tools Executable files contain completely linked applications packaged in multiple sections or segments each preceded by a section header Typical are text and data run pseudocode part 2 Initialize the stack for the process This is done in a manner similar to that of newproca Allocate a page for the kernel stack Initialize the kernel stack Set user system RW and presence bits appropriately for the page table entries Initialize event queues and file descriptors Copy the environment environ and working directory workdir information to the process table entry Add the process to the appropriate ready queue addready set rs to 1 if appropriate and return Loading an executable file part 2 Loading takes place as follows Validate the file format as one we can handle For each section Extract information from a section header Virtual address where the section should be loaded Size of the section Allocate pages of memory real and virtual for the section update the page table Copy the section from disk to memory Set the program break top of allocated memory Allocate and clear a page for the process stack 2 addready The addready function has a single parameter a pointer to a process table entry The state member of that process table entry is set to PROCESS READY The process table entry is linked using the next and prev members onto the appropriate ready queue getpstate pseudocode Getpstate like getpid just returns the contents of a member in this case state of the process table entry For this call we must map from the pid to a pointer to the process table entry This is done by the pid2a function in sys c which just does a linear search of the entire array returning the appropriate pointer if the pid was found or NULL 0 otherwise Once the pointer is located or not the appropriate value is returned to the caller setprio psuedocode Verify argument is within allowed range for process priorities if not return error If the new priority is the same as the current priority return without change Modify the process table entry If there now exists a ready process with a priority higher than the current process priority Move the current process to the ready state by calling addready Set rs to 1 to force a context switch Return getpid pseudocode Getpid almost warrants no explanation since it only extracts and returns the pid member of the process table entry for the current process The only thing worth noting is that it is always assumed that the global variable activeProcess points to the process table entry for the running process getprio getquantum setquantum Each of these system calls just return a field from the process table for the currently running process or get set the value of a global variable No further explanation should be necessary to understand their implementation yield pseudocode Recall that the yield system call causes the running process to voluntarily relinquish the CPU to another process at the same priority The steps taken are add the current process the one yielding the CPU to the appropriate ready queue by calling addready set rs to 1 to force a context switch return 3 exit pseudocode Exit does little direct work The exitstatus member of the process table entry of the terminating process is set to the value of the argument to exit this value can be retrieved by a waiting process that
View Full Document
Unlocking...