swapper
process.
init
process, which
is the ancestor of all further processes.
In particular, init
forks off a process
getty
, which monitors terminal lines and allows users
to log in.
/etc/passwd
file.
From thereon, any process may fork
to produce new processes, considered to be children of
the forking process.
init
process.
getpid
and getppid
calls.
int getpid(void)
int getppid(void)
int getpgrp(void)
int setpgid(pid_t pid, pid_t pgid)
Sets the process group ID of the process with ID pid to pgid.
If pid is equal to 0, the process ID of the calling process
is used.
/etc/passwd
.
This is assigned to the user's shell as its UID and EUID, and is
inherited by processes spawned from the shell.
int getuid(void)
which gets the UID of the process, and
int geteuid(void)
which gets the EUID of the process.
/etc/passwd
also has a group ID which
is given to the shell as its initial GID and EGID.
int getgid(void)
which gets the GID of the process and
int getegid()
which gets the EGID of the process.
int setuid(int uid)
which sets the UID and EUID, and
int setgid(int gid)
which sets the GID and EGID.
Both calls return 0 on success, or -1 on error.
HOME=/usr/spg/geoff
TERM=/tvi950
PATH=/usr/bin:/usr/spg/geoff/bin
environ
, which is an array
of char pointers:
extern char *environ[];
A NULL
pointer signifies the last such string.
int main(int argc,char *argv[],char *env[])
char *getenv(char keyword[]);
getenv
returns a pointer to the string value, or
NULL
if no value is specified.
int setenv(char *name,char *value,int overwrite);
unsetenv(char *name);
fork
system call
int fork(void)
fork
call creates an (almost exact) copy of
the parent.
In particular they share all open files.
fork
call returns the new PID to the
parent and 0 to the child, or -ve to the parent if it fails.
fork
call merely duplicates the parent process.
To switch to a different program a process uses one of the
exec
system calls.
exec
calls replace the code and data areas of
the current program and jumps to the start of the new program.
exec
calls.
void execl(char *program_path,char *arg0,...,char *argn,
NULL
);
program_path
is the name of the file to
execute.
/
.
argi
are the arguments to the program, which in
C are accessible through the parameters to main.
arg0
is the name of the program.
NULL
argument ends the list.
void execv(char *program_path,char *arguments[]);
Here the arguments are stored as an array of pointers, in
the same way as they are received by main.
The last pointer must be NULL
.
execl
and execv
calls pass on the
current environment to new programs.
environment
argument to execle
and
execve
.
void execle(char *program_path,char *arg0,...,char *argn,
NULL
, char *environment[]);
The environment is an array of pointers to strings that
describe the environment to be passed to the new program.
void execve(char *program_path,char *arguments[],char *environment[]);
This is the true exec
, the others end up calling
this one.
void exit(int status)
exit
system call.
SIGCHLD
signal.
init
process
adopts all orphan processes.
wait
system call allows parents to wait for
their children to terminate.
int wait(int *status_pointer);
wait
suspends the process until a child process
terminates and sends a SIGCHLD
signal.
wait
returns the id of that process, or -1
immediately if there are no children.
*status_pointer
.
int waitpid(pid_t pid, int *status_pointer, int options);
signal.h
. Basic ones are:
SIGHUP
: #1 - hangup (modem)
SIGINT
: #2 - interrupt (rubout key)
SIGQUIT
: #3 - quit (FS control \ key)
SIGILL
: #4 - illegal instruction
SIGTRAP
: #5 - trace or breakpoint
SIGABRT
: #6 - abort()
SIGEMT
: #7 - emulator trap instruction
SIGFPE
: #8 - floating point exception
SIGKILL
: #9 - kill, uncatchable quit
SIGBUS
: #10 - bus error
SIGSEGV
: #11 - segmentation violation
SIGSYS
: #12 - bad system call
SIGPIPE
: #13 - end of pipe
SIGALRM
: #14 - alarm clock
SIGTERM
: #15 - catchable termination
SIGURG
: #16 - urgent condition on IO channel
SIGSTOP
: #17 - sendable stop signal not from tty
SIGTSTP
: #18 - stop signal from tty
SIGCONT
: #19 - continue a stopped process
SIGCHLD
: #20 - child death (or stopped under Linux)
SIGTTIN
: #21 - to readers pgrp upon background tty read
SIGTTOU
: #22 - like TTIN for output
SIGIO
: #23 - input/output possible signal
SIGXCPU
: #24 - exceeded CPU time limit
SIGXFSZ
: #25 - exceeded file size limit
SIGVTALRM
: #26 - virtual time alarm
SIGPROF
: #27 - profiling time alarm
SIGWINCH
: #28 - window size changes
SIGINFO
: #29 - information request
SIGUSR1
: #30 - user defined signal 1
SIGUSR2
: #31 - user defined signal 2
SIGHUP
, SIGINT
,
SIGQUIT
.
SIGILL
, SIGTRAP
,
SIGABRT
, SIGFPE
, SIGSYS
,
SIGPIPE
.
SIGEMT
, SIGBUS
,
SIGSEGV
.
SIGALRM
SIGCHLD
, SIGURG
,
SIGIO
, SIGXCPU
, SIGXFSZ
,
SIGWINCH
, SIGINFO
.
signal
system call
The signal
system call can be used to instruct
receiving processes on how to deal with a signal.
This associates a function with a given signal.
void (*signal(int TheSignal,void (*Function)(int)))(int);
This is a function that returns a pointer to a function
that returns an int
.
The second argument is the same.
signal
associates TheSignal
with the
Function
.
Function
may be supplied by the user or one of
SIG_DFL
, which resets to the default, and
SIG_IGN
, which ignores the signal.
signal
returns the previous function.
pause
system call
void pause(void);
causes the calling process to suspend until a non-ignored
signal is received.
SIGKILL
and SIGSTOP signals cannot
be caught or ignored
kill
system call
int kill(int PID,int Signal);
kill
sends the Signal
to all
processes identified by the PID
, where the
values are:
PID
alarm
system call
unsigned alarm(unsigned Time)
SIGALRM
to occur Time
seconds later in the calling process.
alarm(0)
turns off the alarm
SIGCHLD
signal, signals
are not queued.
signum specified which signal to handle
*act specifies what to do
*oldact, if not NULL, saves the old action
A struct sigaction contains:
which is PRIO_PROCESS for process priorities
who is the PID, or 0 for the current process (user, group)
prio is the new priority value.
struct rlimit is defined as:
struct rlimit {
rlim_t rlim_cur;
rlim_t rlim_max;
};
If the process exceeds a soft limit it may be sent a signal:
A process cannot exceed its hardlimit.
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
and a struct timeval is defined as:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
date ls -l cat fred.cNotes:
if (fork() == 0) { printf("Here comes the directory listing\n"); execlp("/bin/ls","ls",NULL); printf("That is the end of the listing\n"); } else { ... /*----Some legal stuff here */ }