Unformatted text preview:

Chapter 8: Process ControlProcess IdentifiersRelated FunctionsForkFork detailsFile Sharing between Parent and ChildNormal casesOther Shared InfoThings that are Not Inherited by the ChildExitProcess Termination DetailsWhen a Process Terminateswait() and waitpid()Race ConditionsRunning a Different Programexec()Variations of exec()Changing User and Group IDsFacts about the three user IDsOther functionsInterpreter FilesSystem()Process Times/procAccessing /procInformation and Control Operations/proc Directory StructureChapter 8: Process ControlCMPS 105: Systems ProgrammingProf. Scott BrandtT Th 2-3:45Soc Sci 2, Rm. 167Process Identifiers Guaranteed to be unique for each currently executing process on a single computer Usually sequentially allocated Some systems services have PIDs as well 0: scheduler/swapper 1: init 2: pagedaemonRelated Functions #include <sys/types.h> #include <unistd.h> pid_t getpid(void); Returns PID of calling process pid_t getppid(void); Returns PID of parent of calling process uid_t getuid(void); uid_t geteuid(void); gid_t getgid(void); gid_t getegid(void) Return real or effective UID or GID for calling processFork #include <sys/types.h> #include <unistd.h> pid_t fork(void); Fork creates a new process The new process is an exact clone of the calling process This is the only way to create a process in Unix Fork returns The PID of the newly created process to the parentprocess 0 to the newly created childprocessFork details The child is a clone of the parent It has a copyof the parent’s Address space (heap, stack, variables, stdio bufs) File descriptors Code (may be shared, since it is read-only) After the fork() call, each process executes as though it was the one that called fork() The only difference is the return value from fork() Usually, different code paths are taken based on a test of the PID returnedFile Sharing between Parent and Child Each process has its own file descriptors The underlying kernel structures for managing the files are shared Specifically, the offsets are shared This means that shared output to the same file will work correctly Important if stdout has been redirected to a fileNormal cases Input and output isn’t redirected, so it doesn’t matter Parent waits for child to finish Parent gets updated file pointers when it resumes executing Child redirects it’s input/output so no shared file pointersOther Shared Info Real & effective user and group IDs Supplementary group IDs Session ID Controlling terminal set-user-ID and set-group-ID flags Current working directory Root directory File mode creation mask Signal mask and dispositions The close-on-exec flag for any open file descriptors Environment Attached shared memory segments Resource limitsThings that are Not Inherited by the Child The return value from fork() The process IDs The process ID of the parent The accumulated CPU time File locks Pending alarms Pending signalsExit Three ways to terminate normally and two ways to terminate abnormally Normal Termination Return from main() Call exit() (C library function) Cleans up standard I/O then calls _exit() Call _exit() (underlying system call) Abnormal Termination Receive certain signals from parent or kernel Call abort (sends SIGABRT to self) Termination status: exit parameter or other kernel-generated statusProcess Termination Details When a process terminates, the parent receives SIGCHLD wait() allows a parent process to wait for a child process to terminate When a process terminates, the kernel maintains a small amount of info until the parent calls wait() Such a process is a zombieuntil the parent calls wait() If the parent terminates first, child is inherited by initWhen a Process Terminates The parent process receives a SIGCHLD Parent can Ignore the signal (the default action), or Set up a signal handler to be called when the signal arrives Use wait() to wait for the child to finish Blocks parent Returns when a child process terminates Returns immediately if any child is a zombie Returns child’s PIDwait() and waitpid() #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *statloc); Wait for any child process to terminate pid_t waitpid(pid_tpid, int *statloc, intoptions); Wait for a specific child process to terminate Statloc contains the child’s termination status (the child’s parameter to exit(), possibly with extra information – see the man page (section 2) for wait())Race Conditions A race condition is any situation where two (or more) processes access shared data, AND The outcome of the processing depends upon the order in which the processes execute Example: two processes do x=x+1, where x is a shared variable Need some form of synchronization Signals File locks Semaphores …Running a Different Program fork() allows us to clone a process The clone is a duplicate of the parent It runs the same program as the parent We want to be able to run different programs, not just clones exec() allows us to run a different program in the current process Often closely follows a call to fork() fork() creates a new process, and exec() makes it run a new program Same PID, new text, data, BSS, stack, heapexec() #include <unistd.h> int execl(const char *pathname, const char *arg0, … /* (char *)0 */); int execv(const char *pathname, char const *argv[]); int execle(const char *pathname, const char *arg0, …/*(char *)0 */, char *const envp[]); int execve(const char *pathname, char const *argv[], char *const envp[]); int execlp(const char *filename, const char *arg0, … /* (char *) 0 */); int execvp(const char *filename, char *const argv[]);Variations of exec() l versions use a list of parameters v versions use an argv[] parameter e versions include an environment parameter p versions search PATH for executable Probably the one you want for assignment 4Changing User and Group IDs #include <sys/types.h> #include <unistd.h> int setuid(uid_tuid); int setgid(gid_tgid); If the process has superuser privileges setuid() sets the real user ID, effective user ID, and saved set-user-ID to uid If the process does not have superuser privileges, but uid=


View Full Document

UCSC CMPS 105 - 01 - Process Control

Download Process Control
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 Process Control 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 Process Control 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?