Module 5 Unix
Module 5 Unix
Module 5 Unix
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).
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.
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.
$ ls -1/bin/cat/usr/bin/passwd
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.
Child Process: A child process is created by a parent process in an operating system using a
fork() system call
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.
$ 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
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 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
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
5.2 Crontab:
Crontab is used to create the crontab file (the list) and later used to change the
previously created crontab file.
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.