Nothing Special   »   [go: up one dir, main page]

Module 5 Unix

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

MODULE – 5

INTRODUCTION TO UNIX SYSTEM PROCESS

Process Basics:
A process is an instance of 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 also has a name, usually the name of the program being
executed.
For example, when you execute the grep command, a process named grep is created. However,
a process can’t be considered synonymous with a program, when two users run the same
program, there’s one program on disk but two process in memory.
A process in a UNIX system is created by fork system call. Every process except process 0 is
created. The process that invokes the fork system call is parent process and the newly created
process is the child process.

Every process has one parent process but a parent can have many child process. The kernel
identifies it process by its process identification number (PID).

1. Mechanism of Process Creation:

There are 3 distinct phase in mechanism of process creation and uses 3 system calls: fork(),
exec() and wait().

 fork(): Creates a child process. A new process is created because an existing process
creates an exact copy of itself. This child process has the same environment as its parent
but only the PID is different. This procedure is known as forking.

 exec(): After forking the process, the address space of the child process is overwritten
by the new process data. This is done through exec call to the system. No new process
is created over here. The PID & PPID remains unchanged.

 wait(): The parent then executes wait system call to wait for the child process to
complete its execution.

All this means that when you run a command (say, cat) from the shell, the shell first forks
another shell process. The newly forked shell the overlays itself with the executable image of
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 a number returned by the child to the kernel, and has great
significance in both shell programming and systems programming.

The important attributes that are inherited by the child process from its parents are: Real UID
and GID, PGID, Nice value, Environment setting, Current working directory, memory
segments etc.
The process remembers the directory from where it was run. This attribute acquires importance
when a process also changes the directory.

1.1 When Real UID differs from effective UID

Every user in Unix like operating system is identified by a different integer number, this
unique number is called as UserID.
Every process have two Uids and two GIDS, i.e,
1. Real UserID : For a process, Real UserId is simply the UserID of the user that has started
it. It defines which files that this process has access to.
2. Effective UserID : It is normally the same as Real UserID, but sometimes it is changed to
enable a non-privileged user to access files that can only be accessed by a privileged user like
root.

Consider the listing of two programs:

$ ls -1/bin/cat/usr/bin/passwd

-rwsr-xr-x 1 root root 14264 2002-09-10 18:43/bin/cat


-rwsr-xr-x 1 root shadow 68680 2002-09-11 00:43/usr/bin/passwd
When kumar runs cat, the real and effective UID of the cat process are the same-kumar.
Being a non-privileged user, kumar can’t use cat to open the file that is readable only by
root.
The bit marked s in the permission field of passwd called the set-user-id(SUID), changes the
normal ownership scheme. When kumar runs passwd, the real UID is still kumar, but the
effective UID is root, the owner of the program. Because it’s the effective UID, not the real
UID, that determines the access rights of the process, the passwd process run by kumar can
open any file that is readable by root.

1.2 How the Shell is Created


When the system moved to multiuser mode, init forks and execs a getty for every
active communication line. Each one of the getty prints the login prompt on the
respective terminal and then goes to sleep.

When a user attempts to log in, getty wakes up and forks-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 as can be seen from this sequence.

init getty login shell


fork fork-exec fork-exec
init goes off to sleep, waiting for the death of its children. The other processes getty
and login have extinguished themselves by overlaying. When the user logs out, her
shell is killed and the death is intimated to init. init the wakes up and spawns another
getty for the line to monitor the next login.

2. Parent and Child Process:


Parent Process: All the processes are created when a process executes the fork() system call
except the startup process. The process that executes the fork() system call is the parent
process

Child Process: A child process is created by a parent process in an operating system using a
fork() system call

When you run the command


cat emp. lst
from the keyboard, a process representing the cat command, is started by the shell process,
This cat process remains active as long s the command is active. The shell is said to be the
parent of cat, while cat is said to be the child of the shell.
The ancestry of every process is ultimately traced to the first process(PID 0) that is set up when
the system is booted.
The analogy with the files and directories doesn’t stop here. Like a file, a process can have only
one parent, Moreover, just as a directory can have more than one file under it, the multitasking
nature of UNIX permits a process to generate one or more children. The command
cat emp.lst | grep ‘director
Sets up two processes for the two commands, These processes have the names cat and grep,
and both are spawned by the shell.

3. The ps command with its options:


ps can be seen as the process counterpart of the file system’s 1s command. The command reads
to through the kernel’s data structures and process tables to fetch the characteristics of
processes.
By default, ps displays the processes owned by the user running the command . If you execute
the command immediately after logging in, it would look like this:
$ ps

PID TTY TIME CMD

291 console 00:00:00 bash

Ignoring the header, each line shows the PID, the terminal (TTY) with which the process is
associated, the cumulative process time(TIME) that has been consumed since the process has
been started, and the process name(CMD).

3.1 ps Options:
ps is highly variant command; its actual output varies across different UNIX flavours.

a) Full Listing(-f) : To get a detailed listing which also shows the parent of every process,
use the –f full option:

$ 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/ash-1

The login shell (PID 291) has the PPID 1, the second process of the system.(The first process has
the PID 0), and the –f option easily identifies a login shell by the hyphen preceding the command
name. STIME shows the process started. CMD displays the entire command line with its
arguments.

Table: 3.1 Options to ps

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 Processes of user usr only
-a Processes 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(say,/dev/console)
b) Displaying Processes of User(-u): It is used to know the activities of any user. The
following output shows a user working on the X window system rather than a character
terminal and X running a number of processes on the user’s behalf:

$ ps –u sumit
PID TTY TIME CMD
378 ? 0:05 Xsun
403 ? 0:00 Xsession
339 pts/3 0:00 bash
346 pts/3 0:00 vi
347 pts/3 0:00 bash
478 ? 0:00 dtfile
460 pts/5 0:00 dtsessio
467 ? 0:01 dtwm

c) Displaying All user Processes(-a) : The –a (all) option lists processes of all users but
doesn’t display 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:00 vi
680 pts/03 00:00:00 ksh
1056 pts/02 00:00:00 sort
1058 pts/05 00:00:00 bash
1069 pts/02 00:00:00 ps

4. Executing command at a specified point of time: at command

at command is a command-line utility that is used to schedule a command to be executed at


a particular time in the future.
Jobs created with at command are executed only once.
The at command can be used to execute any program or mail at any time in the future. It
executes commands at a particular time and accepts times of the form HH:MM to run a job
at a specific time of day.

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]
Commands will be executed using /usr/bin/bash
Job 1041188880.a Sun Dec 29 14:08:00 2002
The job goes to the queue, and at 2:08 p.m today, the script file empawk2.sh will be executed.
at shows the job number, the date and time scheduled execution.
at doesn’t indicate the name of the script to be executed; that is something the user has to
remember. The standard outpot and standard error of the program are mailed to the user, who
can use any mail regarding program to view it. Alternatively, a user may prefer to redirect the
output of the command itself:
at 15:08
empawk2.sh>rep.lst

at also offers the keywords now, noon, midnight,today and tomorrow.


The following forms show the use of some of the keywords and operators:

at 15
at 5pm
at 3:08pm
at noon At 12:00 hours today
at now + 1 day At current time after 1 year
at 3:08pm + 1 day At 3:08 p.m tomorrow
at 15:08 December 18,2001
at 9am tomorrow

Jobs can be listed with the at – l command and removed with at – r.

5. Executing a command periodically: Cron command and the


crontab.file.. Signals

5.1 Cron:

The cron is a software utility, offered by a Linux-like operating system that automates
the scheduled task at a predetermined time.
It is a daemon process, which runs as a background process and performs the
specified operations at the predefined time

A user is permitted to place a crontab file named after her login name in the cron tabs
directory, kumar has to place his crontab commands in the file
/var/spool/cron/crontabs/kumar. The location is, however, system-dependent. A
specimen entry in the file

\var\spool\cron\crontabs\kumar can look like this:


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 a 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 is 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 the
execution every minutes in the first 10 minutes of the hour. The second field
(17, i.e, 5 pm) indicates the hour in 24-hour format for the scheduling(legal
values 1 to 24).
 The third field(legal values 1 to 31) controls the day of the month. The forth
field(3,6,9,12) specifies the month (legal values 1 to 12). The fifth field (5-
Friday)indicates the days of the week (legal values 0 to 6), Sunday having the
value 0.

5.2 Crontab:

Crontab is used to create the crontab file (the list) and later used to change the
previously created crontab file.

Crontab cron.txt cron.txt contains cron commands

If kumar runs the command, a file named kumar will be created in


/var/spool/cron/crontabs containing the contents of cron.txt. In this way different users
can have crontab files named after their user-ids.

Crontab calls up the editor defined in the EDITOR variable(often vi) After editing the
command and quit vi, the commands are automatically schedules for execution.

The contents of the crontab file can be seen with crontab -1 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.

You might also like