1ProcessesCS 2172Operating System• Supports virtual machines Promises each process the illusion of having whole machine to itself• Provides services: Protection Scheduling Memory management File systems Synchronization etc.HardwareOS KernelUserProcessUserProcess3What is a Process?• A process is a running program with its own … Processor state– EIP, EFLAGS, registers Address space (memory)– Text, bss, data, heap, stackHardwareOS KernelUserProcessUserProcess4Operating System• Common implementation strategies Chop up resources into small pieces andallocate small pieces at fine-grain level Introduce level of indirection and provide mapping from virtual resources to physical ones Use past history to predict future behaviorHardwareOS KernelUserProcessUserProcess5Life Cycle of a Process• Running: instructions are being executed• Waiting: waiting for some event (e.g., i/o finish) • Ready: ready to be assigned to a processorCreate Ready Running TerminationWaiting6Context SwitchRunningRunningSave contextLoad contextSave contextLoad context......RunningWaitingWaitingWaitingProcess AProcess B7Overlap CPU with I/O operations• Schedule CPU for process B while process A is waiting for I/O Better utilize CPUCPUCPUCPUI/OI/O I/OA:CPUCPUCPUI/OI/O I/OB:8Process Control Block• For each process, the kernel keeps track of ... Process state (new, ready, waiting, halted) CPU registers (EIP, EFLAGS, EAX, EBX, …) CPU scheduling information (priority, queues, ...) Memory management information (page tables, ...) Accounting information (time limits, group ID, ...) I/O status information (open files, I/O requests, ...)9Fork• Create a new process (system call) child process inherits state from parent process parent and child have separate copies of that state parent and child share access to any open filespid = fork();if (pid != 0) {/* in parent */...} else {/* in child */...}ParentChild10Wait• Parent waits for a child (system call) blocks until a child terminates returns pid of the child process returns –1 if no children exists (already exited) status#include <sys/types.h>#include <sys/wait.h>pid_t wait(int *status);• Parent waits for a specific child to terminate#include <sys/types.h>#include <sys/wait.h>pid_t waitpid(pid_t pid, int *status, int options);11Fork•Inherited: user and group IDs signal handling settings stdio file pointers current working directory root directory file mode creation mask resource limits controlling terminal all machine register states control register(s) . . .•Separate in child process ID address space (memory) file descriptors parent process ID pending signals timer signal reset times . . .12Exec• Overlay current process image with a specified image file (system call) affects process memory and registers has no affect on file table• Example:execlp(“ls”, “ls”, “-l”, NULL);fprintf(stderr, “exec failed\n”);exit(1);13Exec (cont)• Many variations of execint execlp(const char *file,const char *arg, ...)int execl(const char *path,const char *arg, ...)int execv(const char *path,char * const argv[])int execle(const char *path,const char *arg, ...,char * const envp[])• Also execve and execvp14Fork/Exec• Commonly used together by the shell... parse command line ...pid = fork() if (pid == -1)fprintf(stderr, “fork failed\n”);else if (pid == 0) {/* in child */execvp(file, argv);fprintf(stderr, “exec failed\n”);} else {/* in parent */pid = wait(&status);}... return to top of loop ...cshforkexecvplswait15System• Convenient way to invoke fork/exec/wait Forks new process Execs command Waits until it is completeint system(const char *cmd);• Example:int main(){system(“echo Hello world”);}16Summary• Operating systems manage resources Divide up resources (e.g., quantum time slides) Allocate them (e.g., process scheduling)• A processes is a running program with its own … Processor state Address space (memory)• Create and manage processes with ... fork exec wait system}Used in
View Full Document