|
|
1.1 ! root 1: .\" Copyright (c) 1983 Regents of the University of California. ! 2: .\" All rights reserved. The Berkeley software License Agreement ! 3: .\" specifies the terms and conditions for redistribution. ! 4: .\" ! 5: .\" @(#)1.1.t 6.2 (Berkeley) 5/12/86 ! 6: .\" ! 7: .sh "Processes and protection ! 8: .NH 3 ! 9: Host and process identifiers ! 10: .PP ! 11: Each UNIX host has associated with it a 32-bit host id, and a host ! 12: name of up to 64 characters (as defined by MAXHOSTNAMELEN in ! 13: \fI<sys/param.h>\fP). ! 14: These are set (by a privileged user) ! 15: and returned by the calls: ! 16: .DS ! 17: sethostid(hostid) ! 18: long hostid; ! 19: ! 20: hostid = gethostid(); ! 21: result long hostid; ! 22: ! 23: sethostname(name, len) ! 24: char *name; int len; ! 25: ! 26: len = gethostname(buf, buflen) ! 27: result int len; result char *buf; int buflen; ! 28: .DE ! 29: On each host runs a set of \fIprocesses\fP. ! 30: Each process is largely independent of other processes, ! 31: having its own protection domain, address space, timers, and ! 32: an independent set of references to system or user implemented objects. ! 33: .PP ! 34: Each process in a host is named by an integer ! 35: called the \fIprocess id\fP. This number is ! 36: in the range 1-30000 ! 37: and is returned by ! 38: the \fIgetpid\fP routine: ! 39: .DS ! 40: pid = getpid(); ! 41: result int pid; ! 42: .DE ! 43: On each UNIX host this identifier is guaranteed to be unique; ! 44: in a multi-host environment, the (hostid, process id) pairs are ! 45: guaranteed unique. ! 46: .NH 3 ! 47: Process creation and termination ! 48: .PP ! 49: A new process is created by making a logical duplicate of an ! 50: existing process: ! 51: .DS ! 52: pid = fork(); ! 53: result int pid; ! 54: .DE ! 55: The \fIfork\fP call returns twice, once in the parent process, where ! 56: \fIpid\fP is the process identifier of the child, ! 57: and once in the child process where \fIpid\fP is 0. ! 58: The parent-child relationship induces a hierarchical structure on ! 59: the set of processes in the system. ! 60: .PP ! 61: A process may terminate by executing an \fIexit\fP call: ! 62: .DS ! 63: exit(status) ! 64: int status; ! 65: .DE ! 66: returning 8 bits of exit status to its parent. ! 67: .PP ! 68: When a child process exits or ! 69: terminates abnormally, the parent process receives ! 70: information about any ! 71: event which caused termination of the child process. A ! 72: second call provides a non-blocking interface and may also be used ! 73: to retrieve information about resources consumed by the process during its ! 74: lifetime. ! 75: .DS ! 76: #include <sys/wait.h> ! 77: ! 78: pid = wait(astatus); ! 79: result int pid; result union wait *astatus; ! 80: ! 81: pid = wait3(astatus, options, arusage); ! 82: result int pid; result union waitstatus *astatus; ! 83: int options; result struct rusage *arusage; ! 84: .DE ! 85: .PP ! 86: A process can overlay itself with the memory image of another process, ! 87: passing the newly created process a set of parameters, using the call: ! 88: .DS ! 89: execve(name, argv, envp) ! 90: char *name, **argv, **envp; ! 91: .DE ! 92: The specified \fIname\fP must be a file which is in a format recognized ! 93: by the system, either a binary executable file or a file which causes ! 94: the execution of a specified interpreter program to process its contents. ! 95: .NH 3 ! 96: User and group ids ! 97: .PP ! 98: Each process in the system has associated with it two user-id's: ! 99: a \fIreal user id\fP and a \fIeffective user id\fP, both 16 bit ! 100: unsigned integers (type \fBuid_t\fP). ! 101: Each process has an \fIreal accounting group id\fP and an \fIeffective ! 102: accounting group id\fP and a set of ! 103: \fIaccess group id's\fP. The group id's are 16 bit unsigned integers ! 104: (type \fBgid_t\fP). ! 105: Each process may be in several different access groups, with the maximum ! 106: concurrent number of access groups a system compilation parameter, ! 107: the constant NGROUPS in the file \fI<sys/param.h>\fP, ! 108: guaranteed to be at least 8. ! 109: .PP ! 110: The real and effective user ids associated with a process are returned by: ! 111: .DS ! 112: ruid = getuid(); ! 113: result uid_t ruid; ! 114: ! 115: euid = geteuid(); ! 116: result uid_t euid; ! 117: .DE ! 118: the real and effective accounting group ids by: ! 119: .DS ! 120: rgid = getgid(); ! 121: result gid_t rgid; ! 122: ! 123: egid = getegid(); ! 124: result gid_t egid; ! 125: .DE ! 126: The access group id set is returned by a \fIgetgroups\fP call*: ! 127: .DS ! 128: ngroups = getgroups(gidsetsize, gidset); ! 129: result int ngroups; int gidsetsize; result int gidset[gidsetsize]; ! 130: .DE ! 131: .FS ! 132: * The type of the gidset array in getgroups and setgroups ! 133: remains integer for compatibility with 4.2BSD. ! 134: It may change to \fBgid_t\fP in future releases. ! 135: .FE ! 136: .PP ! 137: The user and group id's ! 138: are assigned at login time using the \fIsetreuid\fP, \fIsetregid\fP, ! 139: and \fIsetgroups\fP calls: ! 140: .DS ! 141: setreuid(ruid, euid); ! 142: int ruid, euid; ! 143: ! 144: setregid(rgid, egid); ! 145: int rgid, egid; ! 146: ! 147: setgroups(gidsetsize, gidset) ! 148: int gidsetsize; int gidset[gidsetsize]; ! 149: .DE ! 150: The \fIsetreuid\fP call sets both the real and effective user-id's, ! 151: while the \fIsetregid\fP call sets both the real ! 152: and effective accounting group id's. ! 153: Unless the caller is the super-user, \fIruid\fP ! 154: must be equal to either the current real or effective user-id, ! 155: and \fIrgid\fP equal to either the current real or effective ! 156: accounting group id. The \fIsetgroups\fP call is restricted ! 157: to the super-user. ! 158: .NH 3 ! 159: Process groups ! 160: .PP ! 161: Each process in the system is also normally associated with a \fIprocess ! 162: group\fP. The group of processes in a process group is sometimes ! 163: referred to as a \fIjob\fP and manipulated by high-level system ! 164: software (such as the shell). ! 165: The current process group of a process is returned by the ! 166: \fIgetpgrp\fP call: ! 167: .DS ! 168: pgrp = getpgrp(pid); ! 169: result int pgrp; int pid; ! 170: .DE ! 171: When a process is in a specific process group it may receive ! 172: software interrupts affecting the group, causing the group to ! 173: suspend or resume execution or to be interrupted or terminated. ! 174: In particular, a system terminal has a process group and only processes ! 175: which are in the process group of the terminal may read from the ! 176: terminal, allowing arbitration of terminals among several different jobs. ! 177: .PP ! 178: The process group associated with a process may be changed by ! 179: the \fIsetpgrp\fP call: ! 180: .DS ! 181: setpgrp(pid, pgrp); ! 182: int pid, pgrp; ! 183: .DE ! 184: Newly created processes are assigned process id's distinct from all ! 185: processes and process groups, and the same process group as their ! 186: parent. A normal (unprivileged) process may set its process group equal ! 187: to its process id. A privileged process may set the process group of any ! 188: process to any value.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.