Processes
The Basics |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
main()
:
-a
all processes associated with terminals-A
all processes-l
generates a long listing
name=value
export
name
export
name=value
setenv
name
value
printenv
unset
(or
unsetenv
in the C shell)int setenv(const char *name, const char* value, int overwrite)
name
|
A pointer to the name of the environment variable |
value
|
A pointer to the value of the environment variable |
ovewrite
|
0 not to overwrite an existing variable; nonzero to always set/reset |
Return | 0 on success; -1 on error |
Note: There is also a putenv()
function but you must be much more
careful when using it because it does not duplicate the string it
just points to it. Hence, the string must not be an automatic
variable (i.e., a character on the stack) or an array that might change.
pid_t fork(void)
Return | Parent: PID of the child or -1. Child: 0 |
Note: fork()
is called from the parent process but creates
a duplicate child process (containing the same instructions). So, there
is a return in both the parent and the child.
fork()
)fork()
:
main()
void exit(int status)
status
|
0 for a successful termination; positive otherwise |
Return | Does not return |
Note: exit()
calls exit handlers (in reverse order of
registration), flushes the standard files, and calls _exit()
.
fork()
there are two.
Then, both the parent and child call fork()
resulting in two more.
commute_2000.dat
main()
functions)
exist for the tasks we want to perform in
the differentexec
familyint execve(const char *path, char *const argv[], char *const envp[]);
path
|
The path name of the new program |
argv
|
The NULL-terminated (so the length can be determined) array of arguments to be passed to the new program |
envp
|
The NULL-terminated (so the length can be determined) array of name-value pairs specifying the environment list |
Return | Does not return on success; returns -1 on error |
Note: The process ID remains the same and the file descriptors are left open.
errno
ENOENT
|
The file doesn't exist |
ENOEXEC
|
The file isn't in a recognizable format |
ETXTBSY
|
The file is open for writing |
E2BIG
|
The argument list and/or environment space are too big |
execve()
execve()
(cont.)fork()
there are two.
Then the text/code segment of both processes is changed
by execve()
so the second call
to fork()
is never executed.
fork()
creates a child
exec()
Familyexecve()
are inconvenientexec
functions with slightly
different signatrues (e.g., execle()
,
execlp()
, execvp()
,
execv()
, and execl()
)waitpid()
pid_t waitpid(pid_t pid, int *status, int options);
pid
|
The process to wait for (see below) |
status
|
An outbound parameter used to indicate how the child terminated |
options
|
A bit mask |
Return | The PID of the child or 0; -1 on error |
Note: If pid
is greater than 0 then this call returns
when the specific process ID terminates. If pid
is 0
then this call returns when any child in the same process as the
caller group terminates. If pid
is less than -1 then
this call returns when any child in the group of the absolute value
of the PID terminates. If pid
is -1 then it returns
when any child terminates.
WNOHANG
|
Return immediately (with a value of 0) if no member of the wait set has already terminated. |
WUNTRACED
|
Suspend execution of the calling process until a running member of the wait set is terminated or stopped. |
WCONTINUED
|
Suspend execution of the calling process until a cunning member of the wait set is terminated or a stopped member has been resumed. |
pid_t wait(int *status);
status
|
An outbound parameter used to indicate how the child terminated |
Return | The PID of the child or 0; -1 on error |
Note: Calling wait(&status)
is equivalent to calling
waitpid(-1, &status; 0)
.
WIFEXITED(status)
|
Returns true if the child exited normally. |
WIFSIGNALED(status)
|
Returns true if the child was
killed by a signal (and WTERMSIG(status) returns the
signal number).
|
WIFSTOPPED(status)
|
Returns true if the child
that caused the return is stopped (and WSTOPSIG(status)
then returns the signal number).
|
WIFCONTINUED(status)
|
Returns true if the child that caused the return was resumed. |
init
- the ancestor of all processes)wait()
(and has most of its resources returned
to the system but the process ID and termination status
are maintained)wait()
/waitpid()
wait()
/waitpid()
init
)wait()
/waitpid()
to
ensure that they don't create long-lived zombies
(this is known as reaping)