Unit 5 Notes
Unit 5 Notes
Unit 5 Notes
Module-5
Process
A process is an instance of a running program. A process is said to be born when the program
starts execution and remains alive as long as the program is active. After execution is complete,
the process is said to die. A process has a name, usually the name of the program being executed.
For example, when grep command is executed, then a process named grep is created. But
process is not same as program. When two users run the same program, there’s one program on
the disk but two processes in the memory. The kernel is responsible for the management of
processes. It determines the time and priorities that are allocated to processes so that multiple
processes are able to share CPU resources. It provides a mechanism by which a process is able to
execute for a finite period of time and then relinquish control to another process.
Process has attributes. These attributes are maintained by the kernel in memory in a separate
structure called process table. Two important attributes of a process are:
1. The process-id(PID)
Each process is uniquely identified by a unique integer called the Process-id (PID) that is
allotted by the kernel when the process is born. PID is need to control a process, for instance to
kill it.
The PID of the parent is also available as a process attribue. When several processes have the
same PPID, it often makes sense to kill the parent rather than all its children separately.
Every process has one parent. This parent itself is another process. A process born from it is said
to be its child.
cat emp.lst
From the keyboard, a process representing the cat command is created by the shell process. This
cat process remains active as long as the command is active. The shell (sh,ksh, bash or csh) is
said to be the parent of cat, while the cat is said to be the child of the shell.
The first process has PID 0. The first process is set up when the system is booted. A process can
have only one parent.
1. The parent may wait for the child to die so that it can spawn the next process. The death
is informed by the kernel to the parent.
2. It may not wait for the child to die at all and may continue to spawn other processes.
There are three distinct phases in creation of a process and uses three important system calls or
functions.
1. fork
2. exec
3. wait
Fork
A process in UNIX is created with the fork system call, which creates a copy of the process that
invokes it. The process image is practically identical to that of the calling process, except for a
few parameters like the PID. When a process is forked in this way, the child gets a new PID. The
forking mechanism is responsible for the multiplication of processes in the system.
Exec
Forking creates a process but it is not enough to run a new program. Forked child needs to
overwrite its own image with the code and data of the new program. This mechanism is called
exec, and the child process is said to be exec a new program. No new process is created here.
The PID and PPID of the exec’d process remain unchanged.
Wait
The parent then executes the wait system call to wait for the child process to complete. It picks
up the exit status of the child and then continues with its other functions.
When user run a command (cat) from the shell, the shell first forks another shell process. The
newly forked shell then overlays itself with the executable image of the cat, which then starts to
run. The parent(the shell) waits for cat to terminate and then picks up the exit status of the child.
This is the number returned by the child to the kernel.
When a process is forked, the child has different PID and PPID from its parent. The child
inherits most of the environment of its parent.
1. The real UID and real GID of the process. They represent the UID and GID of the user
running the program. These parameters are stored in the entry for the user in /etc/passwd
When the system moves to multiuser mode, init forks and execs a getty for every active
communication port(or line) Each one of these gettys prints the login prompt on the respective
terminal and then goes off to sleep. When a user attempts to log in, getty wakes up and fork-
execs the login program to verify the login name and password entered. On successful login,
login forks-execs the process representing the login shell. Repeated overlaying ultimately results
in init becoming the immediate ancestor of the shell.
init goes off to sleep, waiting for the death of its children. The other process getty and login
have extinguised themselves by overlaying. When the user logs out, his shell is killed and the
death is intimidated to init. init then wakes up and spawns another getty for that line to monitor
the next login.
ps command is used to display some process attributes. The command reads through the kernel’s
data structures and process tables to fetch the characteristics of the process. By default, ps
displays the processes owned by the user running the command.
$ ps
Each line shows the PID, the terminal(TTY) with which the process is associated(the controlling
terminal), the cumulative processor time(TIME) that has been consumed since the process has
been In above example, the login shell (bash) has the PID 291. The user is running the Bash
shell.
ps Options
The output of ps command varies across different UNIX flavors. Options to ps command are
highlighted in Table 1.
Table 1
To get a detailed listing which also shows the parents of every process, -f(full) option is used.
$ ps -f
The login shell (PID 291) has the PPID 1, the second process of the system. The first process has
the PID 0. init is the creator of all login shells and parent of all services offered by the system.
STIME shows the time the process started. CMD displays the entire command line with its
arguments.
The system administrator needs to use the –u(user) option to know the activities of any user.
$ ps -u sumit
The –a (all) option lists processes of all users but doesn’t the system processes.
$ ps -a
A number of system processes keep running all the time. Most of them are not associated with
any terminal. They are spawned during system startup and some of them start when the system
goes to the multiuser state. To list all processes running on your machine, use the –e or –A
option to ps.
$ ps –e
System processes are easily identified by the ? in the TTY column. They don’t have a controlling
terminal. The standard input and standard output of these processes are not connected to the
terminal.
Daemons are processes which are called without a specific request from the user. Many of
daemons are actually sleeping and wake up only when they receive input.Daemons do important
work for the system. The lpsched daemon controls all the printing activity. sendmail handles
both incoming and outgoing mail. TCP/IP network won’t run FTP and TELNET without the
inetd daemon. cron looks at its control file once a minute to decide what it should do.
at takes as its argument the time the job is to be executed and displays the at> prompt. Input has
to be supplied from the standard input:
$ at 14:08
at> empawk2.sh
[Ctrl-d]
The job goes to the queue and at 2:08 pm today, the script file empawk2.sh will be executed. at
shows the job number, the date and time of scheduled execution.This job number is derived from
the number of seconds that have elapsed since the Epoch.
The standard output and standard error of the program are mailed to the user, who can use any
mail reading program to view it. User may prefer to redirect the output of the command itself.
at 15:08
User can use –f option to take commands from a file. However, any error messages that may be
generated when executing a program will, in the absence of redirection , continue to be mailed to
the user. To mail job completion to the user, use the –m option.
at offers the keywords now, noon, midnight, today and tomorrow. at accepts the + symbol to
act as an operator. The words that can be used with this operator include hours, days, weeks,
months and years.
at 5pm
at 3:08pm
at 9am tomorrow
The month name and the day of the week, when used at all, must be either fully spelled out or
abbreviated to three letters. Jobs can be listed with the at –l command and removed with at –r.
The batch command also schedules job for later execution.But unlike at, jobs are executed as
soon as the system load permits. The command doesn’t take any arguments but uses an internal
algorithm to determine the execution time. This prevents too many CPU-hungry jobs from
running at the same time. The response of batch is similar to at otherwise:
cron executes programs at regular intervals. It is mostly dormant, but every minute it wakes up
and looks in a control file(the crontab file) in /var/spool/cron/crontabs for instructions to be
performed at that instant. After executing them, it goes back to sleep, only to wake up the next
minute.
A user may also be permitted to place a crontab file named after his login name in the crontabs
directory. kumar has to place his crontab commands in the file /var/spool/cron/crontabs/kumar
This location is system dependent.
Each line contains a set of six fields separated by whitespace. The complete command line is
shown in the last field. All of these fields together determine when and how often the command
will be executed. cron uses an unusual number matching system for matching fields. A set of
numbers is delimited by a comma. A * used in any of the first five fields implies that the
command to be executed every period depending on the field where it is placed.
The first field(legal values 00 to 59) specifies the number of minutes after the hour when the
command is to be executed. The range 00-10 schedules execution every minue in the first 10
minutes of the hour. The second field (17, i.e 5 p.m) indicates the hour in 24-hour format for
scheduling (legal values 1 to 24)
The third field (legal values 1 to 31) controls the day of the month. This field (here, an asterisk)
read with the other two, implies that the command is to be executed every minute, for the first 10
minutes, starting at 5 p.m everyday. The fourth field(3,6,9,12) specifies the month (legal values 1
to 12). The fifth field(5-Friday) indicates the day of the week (legal values 0 to 6), Sunday
having the value 0.
In above example, the find command will be executed every minute in the first 10 minutes after
5 p.m, every Friday of the months March,June,September and December (of every year)
User can create his own crontab files with vi editor. User need to use the crontab command to
place the file in the directory containing crontab files for cron to read the files again.
If kumar runs this command, a file named kumar will be created in /var/spool/cron/crontabs
containing the contents of cron.txt Different users can have crontab files named after their user-
ids. It is also possible to enter cron commands by using crontab with –e option.
crontab calls up the editor defined in the EDITOR variable. After you have edited your
commands and quit vi, the commands are automatically scheduled for execution. User can see
the contents of crontab file with crontab –l and remove them with crontab –r. cron is mainly
used by the system administrator to perform housekeeping chores, like removing outdated files
or collecting data on system performance. It’s also extremely useful to periodically dial up to an
Internet mail server to send and retrieve mail.
Signals
Signals are software interrupts. Signals provide a way of handling asynchronous events: a user at
a terminal typing the interrupt key to stop a program or the next program in a pipeline
terminating prematurely. A signal is represented by an integer and its symbolic name. For
instance, SIGINT represents the signal number 2. The event that generates a signal can take
place in the hardware, in the operating system or elsewhere. Signals can be generated from the
following sources.
The Keyboard : There are three signals that have their origin in the keyboard and they all affect
the current foreground job. [Ctrl-c] generates the SIGINT signal which terminates the process.
[Ctrl-\] generates SIGQUIT which directs a process to produce a core dump( a file named core in
the current directory) [Ctrl-z], the suspend key, sends SIGSTP to all processes of the foreground
processes group in a shell that supports job control.
From the hardware: Signals can be generated on account of an arithmetic exception (SIGFPE),
illegal instruction(SIGILL) or memory access violation (SIGSEGV). They all results in
termination of the process.
From a C program : The system call library offers some functions that generate signals. The
alaram system call generates SIGALRM after the expiry of a specified time. The abort call
generates SIGABRT which terminates a process. The raise and kill functions can generate any
signal.
From other sources : When a child dies, the kernel sends the SIGCHLD signal to the parent.
When a background job tries to read from the terminal, the terminal driver generates the
SIGTTIN signal and stops the process.
Signal Disposition
How a process responds to a signal depends on how things are set up in the program representing
the process. If any of them receives signal, there are mainly three things it can do with it:
Ignore the Signal: The process continues running as if nothing has happened. This is true of
SIGCHLD signal that is sent to the parent when a child dies. The parent actually ignores the
signal.
Terminate the process: This is the default disposition of most signals. For instance, SIGINT
and SIGHUP terminate the process. Both [Ctrl-C] and kill –s INT generate SIGINT. Signals
generated by hardware errors also usually terminate a process.
Suspend (stop) the process: This happens when you press [Ctrl-z] from the keyboard ( which
generates SIGTSTP) or send the SIGSTOP signal from your program.