


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Topics covered in this lecture handout are: Process, Signals, UNIX, Zombies, Structure, PID, Command, PSTREE.
Typology: Study notes
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















1
A process consists of the executable program, its data and stack, variables(occupying system memory), open files(file descrip- tor) and an environment.
UNIX allows many users to access the system at the same time. Each user can run many programs, or even many instances of the same program, at the same time. The system itself runs other programs to manage system resources and control user access.
Typically, UNIX system will share code and system libraries between processes, so that there is only one copy of the code in memory at any one time.
For example: two users, Neil and rick both run the command “grep” on the shell at the same time to look for dif- ferent strings in different files. This pro- duces out two processes, each has it own process identifier, PID. And one copy of program code of “grep” is loaded into
the memory as read only, two users share this code. And the system library is also shared(remind you the shared library).
$ ps PID TTY TIME CMD 25089 pts/1 00:00:00 tcsh 25115 pts/1 00:00:00 ps
$ps -au USER PID %CPU %MEM VSZ RSS TTY lchang 25089 0.0 0.5 2860 1380 pts/ lchang 25116 0.0 0.3 2856 896 pts/
Explanation: USER: user’ name; PID: process identifier; CPU: cputime/real
|-gnome-name-serv |-gnome-smproxy |-gnome-terminal-+-gnome-pty-helpe | |-tcsh | ‘-tcsh---pstree |-identd---identd---3[identd] |-jserver |-keventd |-khubd |-klogd |-kreclaimd |-kswapd |-kupdated |-lockd |-login---tcsh---startx---xinit-+-.xini | ‘-X |-magicdev |-mdrecoveryd |-5[mingetty] |-panel |-portmap |-rpc.statd |-rpciod
|-sawfish |-sshd |-syslogd |-tasklist_applet |-xemacs |-xfs |-xinetd |-xscreensaver ‘-ypbind---ypbind---2*[ypbind]
Explanation: pstree command displays all process as a tree with its root being the first process that runs, called init. If the user name is specified, then that user’s processes are at the root of the tree.If a process creates more than one process of the same name, pstree visu- ally merges the identical branches by putting them in square brackets and prefixing them with the number of times the process is repeated.
processes share CPU, the child pro- cess has a copy of the parents’ en- vironment, open files, user identifier, current working directory and signals. The call to fork in the parent returns the PID of the new child process. At the same time, the call return to the new child a 0 to indicate it is a child.
pid_t new_pid; new_pid = fork();
switch(new_pid){ case -1: /* erro / break; case 0: /we are child/ break; default: /we are parent*/ break; }
#include <sys/types.h>
#include <unistd.h> #include <stdio.h>
int main() { pid_t pid; char *message; int n;
printf("fork program starting\n"); pid = fork();
switch(pid){ case -1: fprintf(stderr, "fork failed"); exit(1); case 0: n=3; message = "Child"; break; default: n=4; message = "Parent";
exit status. The status information is written to location that “stat loc” points to. If the parent doesn’t wait and child still exits, the child is put in a zombie state, it will stay in the state until ei- ther the parent call wait or the parent dies. If the parent dies before child, the init process adopts the orphaned zombie processes. The wait call is not only used to put parent go to sleep, also to make sure that the process terminates properly.
The exit status are defined in < sys/wait.h > as followed:
WIFEXITED(stat_val): Non-zero if the child is terminated normally. WEXITSTATUS(stat_val): If WIFEXITED is non-zero, this returns the exit code. WIFSIGNALED(stat_val):Non-zero if the child
is terminated on an uncaught signal. WTERMSIG(stat_val):If WIFSIGNAL is non-zero, this returns the signal number. WIFSTOPPED(stat_val):Non-zero if the child has stopped. WSTOPPED(stat_val): If WIFSTOPPED is non-zero, this returns the signal number.
#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h>
int main() { pid_t pid; char *message; int n; int stat_val;
printf("fork program starting\n");
printf("Child %d has finished\n", child_pid); if(WIFEXITED(stat_val)) printf("Child exit with code %d\n", WEXITSTATUS(stat_val)); else printf("Child terminate abnormally\n"); } exit(WEXITSTATUS(stat_val)); }
Signals
A signal is an event generated by the UNIX system in response to some con- dition, upon which a process may in turn take some action.
Signals are generated by some error con- ditions, such as memory segment viola- tions, floating point processor errors or il- legal instructions. They are generated by the shell and terminal handlers to cause interrupts.
Signals can also be explicitly sent from one process to another as a way of pass- ing information or modifying behavior.
Generally, Signals can be generated, caught and acted upon, or ignored.
Signal names are defined by including the header file < signal.h > as followed: 3
The signal is to be caught or ignored is given as argument sig. The function to be called when the specified sig- nal is received is given as func. This function must take a single int argu- ment(the signal received) and is of type void. This function can be one of these two special values:
SIG_IGN: Ignore the signal SIG_DFL : Restore the default behavoir
#include <signal.h> #include <stdio.h> #include <unistd.h>
void ouch(int sig) { printf("Ouch! -I got the signal %d\n",sig); (void) signal(SIGINT, SIG_DFL); }
int main() { (void) signal(SIGINT, ouch); while(1){ printf("Hello World!\n"); sleep(1); } }
Explanation: the function ouch reacts to the signal which is passed in the pa- rameter sig. And this function is called when a signal is occurred. It prints out a message, and then handle the signal SIGINT(by default generated by press- ing Ctrl-C), reset it back to the default behavior. The main function has an infinite loop of printing a message, unless a signal is received and handled by call for sig- nal(). Execute this program to see what is happened.