Untitled Document

Chapter - 9 The processes

In our Previous discussion we have seen that , whenever we log in to the system a program called shell runs at the background which is nothing but the process . Processes are living Organism , processes are born , they give birth to other processes and also die. processes makes thing "Happen" in Unix/Linux.

In this chapter we will see the process attributes and understand the process creation mechanism . We will see how to control the processes by moving them between abckground and foreground and killing them when they got out of control .

About Processes -  

process is nothing but an instance of a running program . A program is said to be born when the program starts execution and remains alive as long as the program is active .After execution gets complete , the process is said to be die . A process has a name usually the name of the program running , if you run cat command a process name cat is created .However a process can't be considered synonums with a program. When two suers run the same program , there's one program on disk but two processes in memory.

Like file attributes processes also have attributes.Some attributes of every process are maintained by the kernel in memory in seprate structure called process table. We can say that it is the inode table of processes.

  • The Process id (PID) - Each process is uniquely identified by a unique integer call the Process-id (PID) that is allocated by the kernel when process is born . We need this PID to control a process or to kill it
  • The Parent PID ( PPID) - Is Nothing but the parent id of the process id's. When several processes have similar PPID it make sense to kill the parent rather than all its children seperately .

In our upcoming discussions we will see , how we can suspend , kill and move the process to background . Unix/linux provides different tools to control the processes .

The Shell process - 

when you log on to system a process start running at your system which is nothing but the shell. Like other processes this process also has process id . We have seen diferent shell varibales like PATH and SHELL. The pid of the shell process is stored in a special variable , $$ .To Know the pid of your current shell , type -

echo $$

The pid of your loging doesn't change as long as you don't log out from your session. When you log in again the New PID will be generated for the session .

Pocess and its relationship -  

Every process has parent process which gives birth to another process called as a child process.Consider following example -

more sample.txt

A process representng more command , is started by the shell process. This more process remains active as long as the command is active .The shell is said to be parent and more is child process of shell.

A process can have only one parent but due to multitasking nature of Linux it can generate one or more children . The command

more sample.txt | grep "mangesh"

will set up two processes for the two commands . These processes have the names more and grep both are spawned by the shell.

Wait or Not Wait  -  

While we are discussing about the parent process and it child process , you could be amused by two different attitudes that can be taken by the parent towards its child -

  • The parent may wait to die the child process so that it spawn other processes. The death is informed by the kernel to the parent . When we execute a command from shell , the shell process waits for the child to get die , before it starts the next command .
  • The parent may not wait for the child to die and my continue to spawn the other processes. This is what the init process does . Your shell can also create a process without waiting for it to die.
All command don't set up Processes. Built-in commands of the shell like pwd,cd etc . Don't create processes

Process Status -  

Now lets run the ps command to see the process attributes . The command reads the kernel data structure and process table to fetch the required characteristics. By default the ps command displays the processes owned by the user running the command.If you run the ps command on your terminal you will find the result lik this .

$ ps

PID      TTY            TIME       CMD

511      Console      0:00        ksh

  • The header PID -> tells the id of the process running on the terminal
  • TTY -> terminal on which currently the user is logged in .
  • TIME ->The time consumed by the process since it has been started
  • CMD -> The process name

ps is very useful command in Linux/Unix System , Let's see some of the important and most useful options of it . The table below summarise the options supported by ps and its usage .

Command Function
-f Full listing showing the PPID of each processes
-e or -A All processes including user and system processes
-u To see the processes of users only
-a processes of all users excluding processes not associated with terminal
-l long listing showing memory- related information
-t Processes running on terminal
$ ps -f (full listing) - 
      UID   PID   PPID   C      STIME   TTY      TIME     CMD
  mangesh   118    91    0   08:43:11   Console  0:00     monitor.ksh
  nikhil    151    11    0   11:55:15   Console  0:00     vim compare.ksh
  sambk     315    215   0   20:40:43   Console  0:00     -ksh
ps -u mangesh (Dispalys processes of a user ) - 
  PID    TTY       TIME     CMD 
  351   pts/1      0:08     vi
  408   pts/2      0:00     ksh
  228   pts/3      0:00     sample.ksh
ps -a (Dispalying all user processes ) - The option lists processes of all users but doesn't displays the system processes.
  PID     TTY      TIME         CMD
  511     pts/01  00:00:00      ksh
  652     pts/03  00:00:00      vi
  423     pts/04  00:00:04      ps
  1023    pts/02  00:00:00      sort

System Processes ( -e or -A) -  

Apart from user processes , there are number of processes which keep on running all the time on system .Most of these processes are not associated with terminal . They are spawned during system startup and some of them start when system goes to multiuser state.To list all such processes running on our system use the -e or -A option to ps .

ps -e 
  PID     TTY      TIME         CMD
   0       ?       0:01         sched                        Takes care of swapping
   1       ?       0:00         init                         Parent of all shells
  125      ?       0:00         syslogd                      Logs all system messages
  201      ?       0:00         cron                         Schedules Your jobs
  215      ?       0:00         sendmail                     Handles all your mail 
System processes are easily identified by the ? symbol in the TTY column .

These processes don't have a controlling terminal which means that the standard input and output of these processes are not connected to the terminal. You can't press [crl-c] to inetrupt these processes either.These processes are called as deamon processes because they are called without any specific request from a user .Many of these commands are actually slepping and wakes up when they receives input

Internal and External Commands 

Shell recognizes three types of commands , from process point of view -

  • External Commands - The most commonly used commands like cat , ls , grep creates a process for each of these commands that it executes while remaining their present .
  • shell Scripts - The shell executes the scripts by creating another shell, which then executes the commands listed in the scripts..
  • Internal Commands - Shell have some built in commands which doesn't generate a process and are directly executed by the shell.like cd and echo are built in commands . Similarly variable assignment i=2 , doesn't generate a process either.

Running Jobs in Background -  

In a multitasking system we often need to run more than one job at a time . However on Unix termnal we can run only one job at a time on foreground , the rest of the jobs we can run at the bacground . To run the jobs in background we have to option - one is shell's operator & and other is nohup command .

The & operator doesn't allow you to logout wereas nohup allows you to logout when the jobs are running in background.

& : No Log out -

The shell's & operator allows you to run the jobs in background , condition that you shouldn't log out . Here is the example to set the job in background .

sort - o mangep.txt mangep.txt &

The shell will immediately returns thr pid for the job . The prompt is return back and shell is ready to take other jobs .The shell , is the parent for this job . You can submit as many job as you can , of course depends on the load permitted by system .

nohup: Allows Log out -

Background processes gets killed when users logout , this happens becuase users shell is killed. And when parents are killed , its children are also normally killed. We have one variation for this behaviour , the nohup(no hang up) command , when prefix to a command , permits execution of the process even after user has logged out . We must use & with this as well .

nohup sort mangep.txt &

The shell returns the PID for the job and also it dispalys some message . When the nohup command is run , nohup sends the standard output of the command to the file nohup.out . If we don't get this message then we need to take care of the output , using redirection if necessary .Once you get the message , now you can successfully log out from the system .

Now run the ps command from another terminal , you notice something quite interesting

ps -f -u mangesh 
  UID      PID    PPID   C      STIME     TTY   TIME      COMMAND
 mangesh   511     1    34    13:50:15    03    0:15     sort mangep.txt

What happend here is - the parent i.e. the shell was died but its child didn't, it has now turned to orphan. The kernel handles such situation by re-assigning the PPID to the system's init process - the parent of all shells. When users logout , init takes over the parentage of any process run with nohup. In this way we can kill a parent without killing its child .

If you run more than one command in a pipeline , you should use the nohup command at the begining of each command in the pipeline:

nohup grep 'shell' mangep.txt & | nohup sort &

Job Execution With Low Priority ( nice) -  

 Usually processes in the Unix/Linux are executed with equal priority . There are some instance when we want to run a job with high priority compare to other jobs , in such a case Unix/Linux offers us nice command , which is used with & operator to reduce the priority of other jobs .

To run a job with low priority , the command name should be prefixed with nice:

   nice wc -l shell_progs or better run 
   nice wc -l shell_progs

nice is built in command in C shell whose values are system dependent and typically range from 1 to 19 .Commands execute with a nice value whcih is generally in middle of range of 10 . A higher nice value implies lower priority .nice reduces the priority of any process thereby raising its nice value. We can also specify nice value explicitly with -n option :

nice -n 5 wc -l shell_progs &

A non priviledged user can't increase the priority of a process ; that power is reserved for super user . The nice value is displayed with the ps -o nice command

Killing Processes -  

Some times a process may run for long time and consumes the system resources , in such a case we need to terminate/kill the unwanted processes. We will now see kill command which performs this operation for us . The kill command takes process id (PID) as an argument and sends a signal.kill is an internal command in most of the shells .

kill 415

kills / Terminate the job having PID 415 . If you have forgot or don't know the PID of the process , run the ps command ; get the pid and run the kill command.You can even pass multiple PID to kill command ..

kill 415 109 329

If all of these processes have the same parent , then rather than killing indiviual child we can directly kill its parent and all of its children processes will be killed .If you have started the processes in background with nohup command and logged out , then you cannot kill its parent as init is the parent of these processes and you can't kill init . You need to indivually need to kill each child process.

Like files , you owned the process and the processes spwaned by it .It's Obvious that you can kill the processes owned by you and can't kill the others processes. Moreover certain system processes having the PID's 0,1,2,3 and 4 simply can't be killed in this manner .

Killing Last Background Job -  

The system variable $! stores the PID of last background job . You can kill the last background job without using the PID of the process.

kill $!

Kill with other signal - 

Some times we run simple kill 109 , command to kill process haveing PID , by default kill uses SIGTERM signal (15) to terminate the process , but some processes simply ignores this signal and continues to execute . In that case , the process can be killed with SIGKILL signal (9) . This signal can't be generated with kill command so we expilictly need to pass this signal to kill command.

  kill -s KILL 121
  kill -9 121 

A simple kill command won't kill the login shell . You can kill your login shell by using any of these commands.

  kill -9 $$ -------------------------------------- stores PID of current shell
  kill -9 KILL 0 --------------------------------- kills all processes including the login shell 

Job Control -  

 A job is start or created when 2 -3 processes or commands combine together. If your jobs supposed to be completed within 10 minutes and it has taken more than expected time , Now the job has blocked the terminal and you can't run any job on the terminal .To over come this issue Unix/ Linux has provided us method of Job Control mechanism which mean you can do -

  • (bg) - Moves a job to the background
  • (fg) - Brings the job to the foreground
  • (jobs) - List the active jobs
  • [ctrl-z] - Suspend a foreground job
  • (kill) - Kill a job

 at AND batch command -

Unix/Linux provides you a facility of scheduling of you jobs at a specified time of the day . If the system is overloaded throughout the day and you want to run the less priority jobs when the system is at low load , then you can schedule your jobs using at and batch command .

at command - at takes its argument as time at which the job has to be executed and displays at > prompt

  at 14:00
  at> job1.ksh 
   Commands will be executed using/usr/bin/ksh
   job 1021102123.a at Mo May 2 14:00:00 2016

The job goes to queue , and todat at 2 pm the job job1.ksh wills be executed.at doesn't shows the the name of the script to be excuted this is something user has to remeber. The standard output and standard error of the programs are mailed to the user. Or user can alternatively redirect the output of command itself.

  at 15:05
  job1.ksh > rep.txt

at also offers the key words now , moon , midnight , today and tomorrow . moreover it accepts the + synbol to act as an operator .The words hat can be used with this operator include hours , days , weeks,months,years. The following forms show the use of the key words and operators.

  • at 14 ( 24 hour assumed).
  • at 5pm
  • at 4:05pms
  • at noon ( At 12:00 hours today)
  • at now + 1 year ( At current time after one year)
  • at 4:05 + 1 day ( At 3:08 pm tomorrrow)
  • at 16:05 November 18,2015
  • at 9am tomorrow

The month name and day of the week , when used at all , must be either fully spelled out or abbreviated to three letters.

Jobs can be listed with at -l and removed with at -r . Unfortunately there is no way you can find out the name of the program scheduled to be executed . This can create a prblem when you have scheduled a job and does not recall the name of it .

batch command -

The batch command also schedules jobs for later execution, but unlike at , jobs are executed as soon as the system load permits . The command doesn't take any argument but uses an internal algorithm to determine the execution time .

batch < job1.ksh

Any job scheduled with batch goes to a special at queue from where it can be removed with at -r

The system administrator controls the use of at and batch on your system .

Cron : running Jobs Periodically -

We have seen the at and batch command which runs the scheduled jobs for one time execution .To run the job periodically we have cron command which runs our job at regular intervals . The ps -e command shows the cron daemon running.This the Unix/Linux system chronograph.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 time.

A user may also be permitted to place a crontab file named after her login name in the crontabs directory .mangep has to place his crontab commands in the file /var/spool/cron/crontabs/mangep. this location is however system dependent . A specimen entry in the file /var/spool/cron/crontabs/mangep 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 seprated 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 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 execution every minute 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 aestrick) , 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 every day . 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 .

So what is the frequency of execution of this command ? Even though the third field uses a * to indicate execution every day, the fifth field override the entry and limits execution to every friday . The find command will thus be executed every minute in the first 10 minutes after 5 pm , every riday of the month March , June ,September and December ( of every year ).

crontab : creating a crontab file -

You can also create your crontab file with vi in the format shown above.We will then need to use crontab command to place the file in the directory containing crontab files for cron to read the file again  -

crontab cron.txt ---------------- cron.txt contains cron commands     

If mangesh runs this commands , a file named mangesh will be created in /var/spool/cron/crontabs containing the contents of cron.txt . In this way different users have crontab files named after their user-ids.

It's 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 our command and quit vi , the commands are automatically scheduled for execuution .

You can see the contents of your 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 or collecting data on system performance. It's also extremly useful to periodically dial up to an Internet mail server to send an receive mail .

Like at and batch , the use of cron is controlled by some system files , which can be edited only by the system administrator.

time : Timing Processes - 

When you have multiple versions of a program , you will want to find out how effeciently they use the system resources.The time command accepts the entire command line to be time as its argument and does this work . It executes the program and also displays the time usage on the terminal . This enables programmers to tune their programmes to keep CPU usage at an optimum level .

You can find out the time taken to perform a sorting operation by precceding the sort command with time .

time sort -o mangep.txt sample.txt     

real 0m29.812s

user 0m1.370s

sys 0m9.990s

The real time shown is the clock elapsed time from the invocation of the command until its termination. The user time shows the time spent by the program in executing itself.The sum of user time and the sys time actually represents the CPU time. This could be significantly less than the real time on a heavily loaded system .

Conclusion :

This chapter has covered how the process gets birth and how they give birth to their subsequent childs.Different commands and operations on process .Moving process to foreground and background, we will see some more operations on processes in our further discussions.

Untitled Document Scroll To Top Untitled Document