Unit 5 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIX SHELL PROGRAMMING (21CS482)

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.

2. The Parent PID (PPID)

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.

Parents and Children

Every process has one parent. This parent itself is another process. A process born from it is said
to be its child.

When user runs the command

cat emp.lst

SDMIT, Ujire Page 1


UNIX SHELL PROGRAMMING (21CS482)

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.

UNIX permits a process to generate (or spawn) one or more children.

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.

Mechanism of Process Creation

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

SDMIT, Ujire Page 2


UNIX SHELL PROGRAMMING (21CS482)

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.

The important attributes that are inherited are:

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

2. The effective UID and effective GID of the process.

3. The current directory from where the process was run.

4. The descriptors of all files opened by the parent process.

5. Environment variables (like HOME and PATH)

How the Shell is Created

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.

SDMIT, Ujire Page 3


UNIX SHELL PROGRAMMING (21CS482)

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.

init getty login shell

fork fork-exec fork-exec

ps: Process Status

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.

If user execute the ps command immediately after logging in

$ ps

PID TTY TIME CMD

291 console 0:00 bash The login shell of user

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.

SDMIT, Ujire Page 4


UNIX SHELL PROGRAMMING (21CS482)

Table 1

POSIX Option BSD Option Significance

-f f Full listing showing the PPID of each


process

-e or –A aux All processes including user and system


processes

-u usr U usr Process of user usr only

-a Process of all users excluding processes not


associated with terminal

-l l Long listing showing memory-related


information

-t term t term Processes running on terminal term

Full Listing (-f)

To get a detailed listing which also shows the parents of every process, -f(full) option is used.

$ ps -f

UID PID PPID C STIME TTY TIME CMD

sumit 367 291 0 12:35:16 console 0:00 vi create_user.sh

sumit 291 1 0 10:24:58 console 0:00 -bash

sumit 368 367 0 12:35:18 console 0:00 /usr/bin/bash -i

SDMIT, Ujire Page 5


UNIX SHELL PROGRAMMING (21CS482)

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.

Displaying Processes of a User (-u)

The system administrator needs to use the –u(user) option to know the activities of any user.

$ ps -u sumit

PID TTY TIME CMD

378 ? 0:05 Xsun

403 ? 0:00 Xsession

339 pts/3 0:00 bash

Displaying All User Processes (-a)

The –a (all) option lists processes of all users but doesn’t the system processes.

$ ps -a

PID TTY TIME CMD

662 pts/01 00:00:00 ksh

705 pts/04 00:00:00 sh

1005 pts/01 00:00:00 ksh

1017 pts/01 00:00:04 vi

680 pts/03 00:00:00 ksh

1056 pts/02 00:00:00 sort

1058 pts/05 00:00:00 bash

SDMIT, Ujire Page 6


UNIX SHELL PROGRAMMING (21CS482)

1069 pts/02 00:00::00 ps

System Processes (-e or –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

PID TTY TIME CMD

0 ? 0:01 sched Take care of swapping

1 ? 0:00 init Parent of all shells

2 ? 0:00 pageout Part of the kernel – not exec’d

3 ? 4:36 fsflush Part of the kernel – not exec’d

194 ? 0:00 syslogd

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 : One Time Execution

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:

SDMIT, Ujire Page 7


UNIX SHELL PROGRAMMING (21CS482)

$ at 14:08

at> empawk2.sh

[Ctrl-d]

Commands will be executed using /usr/bin/bash

job 1041188880.a at Sun Dec 29 14:08:00 2002

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.

at doesn’t indicate the name of the script to be executed.

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

empawk2.sh > rep.lst

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.

The use of keywords and operators are shown below:

at 15 24- hour format assumed

at 5pm

at 3:08pm

SDMIT, Ujire Page 8


UNIX SHELL PROGRAMMING (21CS482)

at noon At 12:00 hours today

at now+1 year At current time after one year

at 3:08pm+1 day At 3:08 p.m. tomorrow

at 15:08 December 18,2001

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.

batch : Execute in Batch Queue

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:

$ batch < empawk2.sh

commands will be executed using /usr/bin/bash

Job 1041185673.b at Sun Dec 29 13:14:33 2002

cron: Running Jobs Periodically

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.

A entry in the file /var/spool/cron/crontabs/kumar looks as below:

SDMIT, Ujire Page 9


UNIX SHELL PROGRAMMING (21CS482)

00-10 17 * 3,6,9,12 5 find / -newer .last_time –print >backuplist

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)

crontab : Creating a crontab File

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.

crontab cron.txt cron.txt contains cron commands

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

SDMIT, Ujire Page 10


UNIX SHELL PROGRAMMING (21CS482)

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.

SDMIT, Ujire Page 11


UNIX SHELL PROGRAMMING (21CS482)

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.

Table 1: Signals and Default Disposition

Signal Name Significance Default Action


SIGINT Terminal interrupt ([Ctrl-c]) Terminate
SIGQUIT Terminal quit ([Ctrl\]) Terminate with core dump
SIGTSTP Terminal stop ([Ctrl-z]) Stop
SIGABRT Abnormal termination (abort call) Terminate
SIGCHLD Change of child’s status Ignore
SIGCONT Continue stopped child Continue
SIGALRM Timer(set by alarm call); sends signal after Terminate
expiry of timeout period
SIGHUP Hangup Signal Terminate
SIGTERM Software termination Terminate
SIGPIPE Write to pipe with read ends closed Terminate
SIGILL Illegal instruction Terminate with core dump
SIGFPE Arithmetic exception Terminate with core dump
SIGSEGV Segmentation violation; process attempts to Terminate with core dump
access memory outside its own address
space
SIGKILL Sure kill Terminate, can’t be caught
SIGSTOP Process stop Stop, can’t be caught

SDMIT, Ujire Page 12

You might also like