Processes Management in Unix:
In Unix, all the instructions outside the kernel are executed in the context of a process.
A process is a sequence of instructions and each process has a block of controlled data associated with it. Processes can be manipulated in a way similar to how files can be manipulated.
For Example, like ‘ls’ is can be used to see the list of files, ‘ps’ can be used to see the list of processes.
Unix Video #23:
In this tutorial, we will cover what process related information is stored and also describe some commands that are used to manage processes. We will also provide a brief description of how command scripts can be debugged.
What You Will Learn:
Unix Process Information
The process table contains information of all the processes that are running currently. The main purpose of this table is to manage all the running processes effectively.
Note: As a multitasking and multi-user OS, Unix will have many processes running at the same time.
The table contains information like:
- Process Id
- Parent Process ID
- State of the processes
- CPU usage
Processes in Unix go through various states depending on various circumstances. The state changes may be triggered by whether a process needs to wait for a read or write operation to complete, or when a different more urgent process needs to be given a chance to run.
Process Control Commands
There are a number of commands that are available to review and manipulate processes. The ‘ps’ command can be used to view the process status information.
|Command||ps - gives information of all the processes currently running.|
|Common Syntax||$ ps [OPTIONS]|
Gives information about the process including the PID, terminal name, time of creation and name of the processes.
As discussed in an earlier tutorial, processes may be classified as foreground, background, or daemon. When a command is run from the terminal it runs normal in the foreground. In order to run a process in the background, the ‘&’ button at the end of the command can be used.
$ grep “how” file1 > file2 &
This command will search for the lines containing the pattern “how” and will store the output in file2. If file1 is reasonably large, we may want to proceed with other actions, and run this command in the background. With the ‘&’ at the end, this command will run as a process in the background.
It is possible to suspend a foreground process by using the ‘^Z’ command. This command moves the current foreground process to a suspended state in the background. From there, the process can either be pushed to resume in the background using the ‘bg’ command, or it can be brought back to the foreground using the ‘fg’ command.
Each process is also associated with a priority. This is used to ensure that the OS is able to fairly allocate time to various processing tasks. The ‘nice’ command can be used to reduced the priority of a process and thus be ‘nice’ to the other processes, i.e.
$ nice <command>
This line will run the specified command at a lower priority – by default, the priority will be reduced by 10. The command also takes a parameter that can be used to use a different level of ‘niceness’.
$ nice -20 ls
This command runs ‘ls’ with the priority reduced by 20.
It is also possible to increase the priority with a negative ‘niceness’. However, this needs superuser permission.
When a terminal or login session is closed, it sends the SIGHUP signal to thr child processes. By default, this signal will cause the child processes to terminate. The ‘nohup’ command can be used to allow commands to continue running even when the login session is terminated.
$ nohup soft file1 > file2
With this command, sorting of file1 and saving in file2 process will continue even if we have logged out of the system.
The ‘kill’ command can be used to terminate any of the processes depending on permissions.
$ kill [options] <pid>
This command will terminate a process with the process id <pid>. The PID of a process can be obtained using the ‘ps’ command.
This ‘at’ command is used to execute commands at a particular date and time in the future.
$ at 8pm sort file1>file2
Unix provides a number of mechanisms to help find bugs in your command scripts. These mechanisms can be used to view a trace of what is being executed i.e. the sequence in which commands are executed. The trace can be used to understand and verify the logic and control flow of the script.
=> set -v
verbose mode: Setting this option before running a command will ensure that the command that will be executed is printed to stdout before it is actually executed.
=> set -x
execution trace mode: Setting this option will display each command as it is executed along with its arguments.
=> set -n
no-exec mode: Setting this option shows any errors without actually running any commands.
Check out our upcoming tutorial to know more about Advanced Shell Scripting.