Unformatted text preview:

ProcessesProcess ID (pid)PID ExampleUser/Group IDUser/Group ID SynopsisUser/Group ID ExampleProcess Statespsps Fieldsfork System Callfork return valuesFork Attributesfork Examplefork (Chain)fork (Fan)fork (Tree)wait Functionwait errnopid_t wait(int *stat_loc)Chain with waitwait_rStatus ValuesUse of Status Valueswaitpid (pid,status,options)waitpid Status Parameterwaitpid Options Parameterwaitpid Return Valueswaitpid exampleswaitpid – WNOHANGexecl, execlp, execleexecv, execvp,execveexecl Exampleexecvp Exampleerrno for execUse of makeargvAttributes Preserved by Calls to execexec Attributes (Continued)Background ProcessesDaemonBackground ProcessesCritical SectionsProcessesProcess ID (pid)Synopsis#include <unistd.h>pid_t getpid(void) – returns the pid of the currently running process.pid_t getppid(void) – returns the pid of the parent of the currently running process.POSIXPID Example/* Example 2.1 */#include <stdio.h>#include <unistd.h>void main(void){printf("Process ID: %ld\n", (long)getpid());printf("Parent process ID: %ld\n", (long)getppid());return 0;}User/Group ID• System managers assign a unique integral user ID and an integral group ID to each user when creating the user’s account• The system uses the user and group Ids to retrieve privileges from the system database for that user• Each process is identified with a particular user called the owner• Each user has a unique ID (uid)• Effective User ID (euid) can change during execution and determine access permissions to files• System manager has a UID of 0POSIXUser/Group ID SynopsisSYNOPSIS#include <unistd.h>gid_t getegid(void);uid_t geteuid(void);gid_t getgid(void);uid_t getuid(void);POSIXUser/Group ID Example#include <stdio.h>#include <unistd.h>int main (void) {printf(“My real user ID is %5ld\n”, (long) getuid());printf(“My effective user ID is %5ld\n”, (long) geteuid());printf(“My real group ID is %5ld\n”, (long) getgid());printf(“My effective group ID is %5ld\n”, (long) getuid());return 0;}Process Statesnewblockedready donerunningpsDisplays information about processes. The –a option displays information for processes associated with terminalsSYNOPSISps [-aA] [-G grouplist] [-o format]…[-p proclist] [-t termlist] [-U userlist]POSIX Shells and Utilities-ps FieldsFlags associated with the processThe process stateThe user ID of the process ownerThe process IDThe parent process IDThe processor utilization used for schedulingThe priority of the processThe nice valueThe memory address of the processThe size of the process imageThe address of the event if the process is sleepingThe controlling terminalCumulative execution timeCommand nameFSUIDPIDPPIDCPRINIADDRSZWCHANTTYTIMECOMMANDMeaningheaderfork System CallCreates child process by copying parent’s memory imageSYNOPSIS#include <unistd.h>pid_t fork(void)POSIXfork return values• Returns 0 to child• Returns child PID to parent• Returns –1 on errorFork AttributesChild inherits:• Parent’s memory image • Most of the parent’s attributes including environment and privilege.• Some of parent’s resources such as open files.Child does not inherit:• Parent pid.• Parent time clock (child clock is set to 0).fork Example#include <stdio.h>#include <unistd.h>#include <sys/types.h>void main(void){pid_t childpid;childpid = fork()) if(childpid == -1 {perror(“failed to fork”);return 1; }if(childpid == 0) {fprintf(stderr, "I am the child, ID = %ld\n", (long)getpid());/* child code goes here */} else if (childpid > 0) {fprintf(stderr, "I am the parent, ID = %ld\n", (long)getpid());/* parent code goes here */}}fork (Chain)#include <stdio.h>#include <unistd.h>#include <sys/types.h>void main(void){int i;int n;pid_t childpid;n = 4;for (i = 1; i < n; ++i)if (childpid = fork())break;fprintf(stderr,"This is process %ld with parent %ld\n",(long)getpid(), (long)getppid());sleep(1);}fork (Fan)#include <stdio.h>#include <sys/types.h>#include <unistd.h>void main(void){int i;int n;pid_t childpid;n = 4;for (i = 1; i < n; ++i)if ((childpid = fork()) <= 0)break;fprintf(stderr, "This is process %ld with parent %ld\n",(long)getpid(), (long)getppid());sleep(1);}fork (Tree)#include <stdio.h>#include <sys/types.h>#include <unistd.h>void main(void){int i;int n;pid_t childpid;for (i = 1; i < 4; ++i)if ((childpid = fork()) == -1)break;fprintf(stderr, "This is process %ld with parent %ld\n",(long)getpid(), (long)getppid());sleep(1);}wait FunctionSYNOPSIS#include <sys/wait.h>pid_t wait (int *stat_loc);pid_t waitpid(pid_t pid, int *stat_loc, int options);POSIXwait errnooptions parameter of waitpid was invalidEINVALfunction was interrupted by a signalEINTRcaller has no unwaited-for children (wait) or process or process group specified by pid does not exist (waitpid), or process group specified by pid does not have a member that is a child of caller (waitpid)ECHILDcauseerrnopid_t wait(int *stat_loc)• Causes caller to pause until a child terminates, or stops until the caller receives a signal.• If wait returns because a child terminates, the return value (of type pid_t) is positive and is the pid of that child.• Otherwise wait returns –1 and sets errno.• stat_loc is a pointer to an integer variable.• If caller passes something other than NULL, wait stores the return status (terminate status?) of the child.• POSIX specifies the following macros for testing the return status: WIFEXITED, WEXITSTUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, and WSTOPSIG.• Child returns status by calling exit, _exit, or return.Chain with wait#include …void main(void) {int i;int n;pid_t childpid;int status;pid_t waitreturn;n = 4;for (i = 1; i < n; ++i)if (childpid = fork())break;while(childpid != (waitreturn = wait(&status)))if ((waitreturn == -1) && (errno != EINTR))break;fprintf(stderr, "I am process %ld, my parent is %ld\n",(long)getpid(), (long)getppid()); }wait_rRestarts the wait function if it is interrupted by a signalStatus ValuesSYNOPSIS#include <sys/wait.h>WIFEXITED (int stat_val)WIFEXITSTATUS (int stat_val)WIFSIGNALED (int stat_val)WTERMSIG (int stat_val)WIFSTOPPED (int stat_val)WIFSTOPSIG (int stat_val)Use of Status Valueswait(&status);if(WIFSTOPPED(status)) printf(“Child stopped due to a signal”);waitpid (pid,status,options)If the pid paramater is:– greater than 0, wait for child with pid number– equal to 0, wait for any child in the same process group as the caller– equal to –1, wait for any child– less than –1, wait for any child in


View Full Document

Chico CSCI 372 - 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?