Tuesday 4 April 2017

Starting a Process

1. Foreground Processes
2. Background Processes

Whenever you execute a command in UNIX, it creates a new process. 
The operating system tracks processes through a five digit ID number known as the pid or process ID. Each process in the system has a unique pid. Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system. You can use the ps command to see what processes you are running and all processes on the system. When you start a process (run a command), there are two ways you can run it in the foreground or background. The difference is how the process interacts with you at the terminal.

Foreground Processes
By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen. The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input, it waits for it from the keyboard. While command is running, I cannot run any other commands. For the commands, which usually runs very quickly, this is not a problem. But if there is a command that runs for a long time terminal will be tied up.

Background Processes
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits. The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand ( &) at the end of the command.

Suspending foreground processes
While a foreground process runs, the shell does not process any new commands. Before you can enter any commands, you have to suspend the foreground process to get a command prompt. The suspend key on most UNIX systems is Ctrl+Z. If Ctrl+Z does not work for you, use the stty command  to determine the key for your system.

Moving processes between the foreground and background
When a foreground process is suspended, a command prompt enables you to enter more commands; the original process is still in memory but is not getting any CPU time. To resume the foreground process, you have two options background and foreground. The bg command enables you to resume the suspended process in the background; the fg command returns it to the foreground. Put the job number on the bg or fg command, prefixed with a percent sign.

Nohup Command
You can prevent a background process from terminating, which is the default action, when you sign off or are disconnected. The nohup command prevents your process from getting the HUP (Hang UP) signal and enables it to continue processing. Add nohup command before the command you actually want to run. Because nohup is designed to run when there is no terminal attached, it wants you to redirect output to a file. If you do not, nohup redirects it automatically to a file known as nohup.out.

Wait Command
There are three ways to use the wait command with no options (the default), with a process ID, or with a job number prefixed with a percent sign. The command will wait for the completion of the job or process you specify.If you do not specify a job or process (the default ), the wait command waits for all background jobs to  finish. Using wait without any options is useful in a shell script that starts a series of background jobs.When they are all done, it can continue further processing.

No comments:

Post a Comment