12 Processes in Linux

Mr. Hardik Joshi

epgp books
  1. Overview of Process

 

A process is an entity that is executed by the CPU. It can be considered as an instance of one or more related tasks(thread) that are executing on the computer. A process may create several child processes or threads. Usually a program may correspond to a single process or multiple processes. User programs created in C language usually creates a single process, however, if functions like fork() are invoked within the program, it may create multiple processes.

 

Processes are entities that require system resources like CPU cycles, memory, devices etc. The operating system is responsible to allocate the resources to each process by ensuring the overall optimum utilization of the system. To use the system resources effectively, OS may give priority to processes or may schedule the process execution based on certain algorithms.

 

Process Types

 

Processes can be classified into different types on the basis of the type of task they perform. Certain processes like terminal window runs in interactive environment and are attached to the shell while others can execute in detached mode. The processes that run in detached mode are known as background processes while the others are foreground processes. Depending on the type of task performed, processes and threads can be classified into various categories. Table 1 lists the types of processes/threads along with the examples of each of them.

 

 

Process Attributes

 

Attributes like state, priority, process-id, parent process-id etc. determine the operations of that process. A process may have various states like running, ready, suspended etc. If there is a single CPU, OS cannot run multiple processes simultaneously (provided it is a single core system). Under such circumstances, a single process might be in running state while rest of the processes can be in ready or suspended states. If a process is waiting for certain resources to be freed by other process, it is said to be in blocked state and is kept in wait queue.

 

Each process has a unique process-id to differentiate among others. A process is mostly created by some parent process, so the process has parent process-id and some priority associated with that process. The priority of a process is depicted in numeric form. Process are invoked by user or other services, so each process is associated with user-id who initiated that process. Few IDs related to processes are listed in table 2.

 

Processes can have priorities that helps us to schedule the processes. For instance, a critical process like disk management must be executed first compared to other non-critical processes like batch processes. In OS, scheduler is responsible to execute the process in CPU. The scheduler schedules processes based on algorithms that consider priority of processes. In Linux, priority of a process can be set by specifying nice value for the process. The lower the nice value, the higher the priority. In Linux, a nice value of -20 refers to the highest priority while the nice value of +19 represents the lowest priority. In Linux, a user can modify the nice value using nice command, but the user can only increase niceness of any process (that is decrease the priority of a process). A user cannot increase the priority.

  1. Displaying the processes that are in system

Processes that are created in the system at any instance can be listed using ps, top or pstree commands. The output of ps command is non-interactive and it lists various attributes of the processes by specifying different options. The top command is interactive and also has variants like htop or atop. Without any option, the ps command will simply display the processes running in the current shell for that user. Figure 1 illustrates the ps command without any option.

 

Figure 1: ps command executed without any options.

 

When ps command is executed using ps -ef option, it displays all the processes within the system. The command ps -eLf displays processes within the system in full detail that also includes the thread information. In figure 2, the ps option is shown using different options. We have limited the display of number of processes to 15 by using head command. It must be noted that the output of ps -ef command lists various attributes of processes. The right most column indicates the name of process with the heading “CMD” or “COMMAND”. The ps -ef option uses System V style of representation while the command ps aux uses BSD style of representation. These are different variants of Unix system.

 

Figure 2: ps command with various options

 

If a user wants to specify few selected attributes while displaying the processes, he can use the axo option to ps command. Figure 3 represents the use of ps axo command with selected attributes to be listed for any process. In figure 3, we have limited to display the top 15 processes within the system. By removing the pipe command, it will display all processes within the system at that instance.

 

Figure 3: ps command with the option ‘axo’

pstree command

 

The pstree command displays the processes running on the system in tree structure. It shows the relationship between the process and its parent process and the siblings of the same parent. While using the pstree command, the threads are displayed in curly braces. Figure 4 illustrates the snapshot of pstree command. It must be noted that all the processes are forked from the init process. The pid of init process is 1.

 

 

Figure 4: output of pstree command

 

 

top command

 

ps command gives the static view of the system. If a user wants to monitor the live performance of the system (at every 2 seconds), under those conditions, top command is useful. top is an interactive command that shows the processes in real time mode and updates the list in every 2 seconds. To exit the top command, user has to type ‘q’.

 

Figure 5: output of top command

 

Figure 5 is a snapshot of top command. It is an interactive command which helps the user to order the list of processes by CPU usage, memory usage etc. using keys. Let us try to understand the output of the top command as displayed in figure 5.

  • The first line displays a quick summary of the system that includes how long the system has been running, how many users are logged on and what is the load average of the system (load average is discussed in extra reading notes)
  • The second line displays the statistics of the states of the processes within the system, that is how many processes are running, sleeping or in zombie state.
  • The third line displays how the CPU time is divided between the users (us) and the system (sy). It also lists the user jobs at low priority (ni), percentage of processes in idle mode (id), percentage of jobs waiting for I/O (wa), percentage of hardware interrupts (hi), percentage of software interrupts (si) and steal time (st). Steal time is generally used in virtual machines.
  • The fourth and fifth line indicate the memory usage in terms of physical memory (RAM) and swap space (virtual memory)
  • The sixth is the header and subsequent lines indicate the list of processes in sorted manner. The header of sixth line indicates:
  •  Process identification number of all processes in list o Process owners of the processes in list\  Priority and nice values of the processes in list\Virtual, physical and shared memory of the processes in list o Status like running, suspended etc.
  • Percentage of CPU and memory used by each process o Execution time
  •  Command/program name of the process

Interactive keys that can be used with top command are listed in table 2. By typing a single letter, the output of the top command gets organized as per the command key selected.

 

3. Background and Foreground jobs

Traditionally, jobs were known as the commands that were executed at terminal. Linux supports execution of commands/programs in interactive mode or foreground mode. If a user wants the terminal to be released for other commands and still wants few other programs to keep on executing, he may select the programs to run in background mode. It is believed that the background jobs take considerable time while the foreground jobs are usually interactive. Earlier days, in the absence of GUI, there used to be a single terminal for each user and hence the notion of foreground and background jobs was introduced.

 

Linux supports executing programs in background mode. We simply suffix the command with ‘&’ sign to indicate that it must run in background. The execution of such command will free up the shell for other programs to be executed.

Figures 6 & 7 displays the execution of background and foreground jobs. In figure 6, sleep command is executed in foreground mode, however by typing CTRL-Z, the command is suspended and executes in background mode. By issuing the ‘bg’ command, it displays the jobs running in background while issuing ‘fg’ command brings in the background jobs into foreground for execution. Finally, we terminate the sleep command by issuing the command CTRL-C. In figure 7, we are executing the sort command in background mode by suffixing ‘&’ at the end of the command. To avoid the intermediate output, we are redirection the output of sort command into /dev/null file.

 

  • Generally, the updatedb command is executed in background using the following format:
  • #updatedb &
  • The background jobs run at lower priority to ensure smooth functioning of the system.
  • Programs like downloading data or updating the system can be executed in background.
  1. Scheduling the execution of programs sleep command

sleep command is used when the job must be delayed or suspended for specific amount of time. Sleep command can also be used when we need to periodically check for system status or backup files at given interval. The syntax of sleep command is as follows:

sleep NUMBER [SUFFIX]

The values of SUFFIX can be :

  • S for seconds (default)
  • M for minutes
  • H for hours
  • D for days

Example:

 

$sleep 100 will suspend the shell for 100 seconds

 

at command

 

Suppose if we want to perform a particular task on a specific day or time, we can schedule that job using the at command. The at command maintains a queue of jobs that are scheduled. We can schedule the jobs on specific day (using dd/mm/yy format) or time (using HH:MM format). We can also specify the time using noon, midnight keywords or combinations like now + time_units format. Readers are requested to browse the manual to see various options in detail.

 

Super user can view the scheduled jobs of various users using atq command. The super user can see jobs of all users, whereas if the at command is issued by the user, he can see his own jobs that are in queue. To remove the jobs from queue, atrm command can be used by specifying the job id.

 

Figure 8 demonstrates the use of at command. In the figure, we are scheduling the display of content of file /etc/passwd after 2 days. The job is scheduled with id=13, which can be seen by using the atq command. Later, we are deleting the scheduled job using atrm command and subsequently displaying the empty queue.

 

batch command

 

batch command executes the job when the system is relatively idle. That is when the load average of the system falls below some threshold value, the command specified using batch command will be executed. Jobs that require more processing cycles and are not so urgent can be scheduled using the batch command.

 

jobs command

 

To view the jobs that are running in background, jobs utility is helpful. When jobs command is used with the -l option, it will list all background jobs along with their job ids. Figure 9 demonstrates the use of jobs command.

Figure 9: Demonstration of jobs command

 

As shown in the figure 9, there are two jobs running in background (sleep commands).These jobs are listed using the jobs -l command.

cron utility

 

Cron is a scheduling utility that helps in executing jobs on cyclic basis. For instance, if we want to take backup of a folder at 6 pm on daily basis, we can schedule the backup program using cron. cron program is a time-based scheduling program that uses configuration file /etc/crontab. The /etc/crontab file contains list of commands/programs that the user intents to execute at a specific time/day. To insert the program in schedule or to modify the schedule, one must use the cron editor.

 

The crontab file maintains list of programs with specific time to be executed. This file can be edited using the crontab -e command. The fields used in crontab file are shown in table 3.

 

Let us study few example entries of crontab file. Suppose if the file contains an entry as:

  • “30 05 12 01 * /home/demouser/mybackup.sh”, then the script mybackup.sh will execute on 12th January at 5:30 am.
  • “* * * * * /home/demouser/myscript.sh”, then the script myscript.sh will keep on executing every day of every month and every year at every minute of every hour.
  1. Terminating a process

kill command is used to terminate a process. A user can terminate his own process; however, a super user can terminate processes of other users. The usage of kill command is as follows:

$kill -SIGKILL <pid>

or

$kill -9 <pid>

 

Keywords:

 

Process, threads, process attributes, background and foreground processes, nice value, scheduler, process termination commands: ps, pstree, top, sleep, fg, bg, kill, at, atq, atrm, jobs, batch, cron

 

Summary

  • Processes are used to perform various tasks, processes can be single threaded or multi-threaded
  • Processes have attributes like state, priority etc.
  • Listing of processes can be done using ps, top, pstree command.
  • Processed can be managed using kill, bg, fg, nice commands.
  • Scheduling of processes can be done using at, batch, cron utilities.
you can view video on Processes in Linux

Extra Reading References

 

[1]“Job Scheduling with cron and at.” [Online]. Available: https://www.ibm.com/developerworks/library/l-job-scheduling/index.html.

[2]“Scheduling tasks with Linux.” [Online]. Available: https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/.

[3]“Examples of ps command.” [Online]. Available: https://linoxide.com/how-tos/linux-ps-command-examples/.

[4]“Manual of ps command.” [Online]. Available: https://www.pslinux.online/ps-manual.html.

[5]K. Thomas, Beginning ubuntu linux: From novice to professional. Apress, 2006.

[6]M. G. Sobell, A practical guide to Linux commands, editors, and shell programming. Prentice Hall Professional Technical Reference, 2005.

[7]E. Siever, A. Weber, S. Figgins, R. Love, and A. Robbins, Linux in a Nutshell. O’Reilly Media, Inc., 2005.

[8]N. Matthew and R. Stones, Beginning linux programming. John Wiley & Sons, 2008.

[9]N. Matthew and R. Stones, Professional Linux Programming. Wrox Press Ltd., 2000.

[10]P. Bovet and M. Cesati, Understanding the Linux Kernel: from I/O ports to process management.O’Reilly Media, Inc., 2005.

[11]Love, Linux Kernel Development (Novell Press). Novell Press, 2005.