System ProgrammingLearn how to Work with Managing Processes in Red Hat Linux Part...

Learn how to Work with Managing Processes in Red Hat Linux Part – 2

Managing Processes
In the previous article, we had an introduction to Process Management. We have learned how to display active processes, how to search for specific process(es), and how to manipulate processes using the kill, killall and pkill commands. In this new article, we will continue with process manipulation, learning how to control process priority (increasing and decreasing), how to start processes in the background, and how to control them. Wish you nice reading.

Process Priority
In a multi-tasking system, CPUs serve multiple tasks (programs or processes) in almost the same time. For the multiple programs to appear to the user as if they are working simultaneously, the CPU actually divides its time into small time slots, and assigns each process a time slot. Naturally, processes are not equal in importance, and hence they should have different precedence (priority) values. For example, a backup process running in the background should have smaller priority than an interactive GUI application. An NTP service that synchronizes date and time information should not have the same priority as an update query in the database of a core banking application.
Programs with higher priority will be allocated more time slots so that they get better chance to achieve their work in a timely manner.

– OK, that dictates having a mechanism, a way, or a value upon which the system could decide to which process(es) it should give preference.

That is absolutely right!!! And for this reason, the Niceness value exists.

The Niceness Value
Consider two processes A and B. Process A is more hunger to CPU than process B. So, process A will have higher priority, and hence will take more CPU slots than process B. In this case, Process B will be considered to be nicer than process A. In other words, process B will have higher niceness value than process A. So, take it as a rule:
The niceness value of a process is inversely proportional to its priority.
Niceness ranges from -20 (least nice = highest priority) to 19 (most nice = lowest priority). The default niceness value for a new process is 0.

The top command displays the niceness value for each running process.
Img1
The ps command also displays the niceness value when run with –l (long format) option:
Img2
Notice the NI column.

Adjusting Process Priority
A process priority could be set and changed on need by modifying the niceness value. For this purpose, the nice and renice commands come into scene.

  • The nice command is used to run a program with modified scheduling priority.
  • Syntax
    nice –n NICENESS [COMMAND [ARG]…]
    Example
    The following runs the sleep command with niceness value of 5:
    nice –n 5 sleep 600
    Img3
    From another terminal, we could easily verify that the process has been started with the niceness value we specified:
    Img4
    You can write more than one command on the same line, by separating them with semi-colons;

  • The renice command does the same job for the running processes. It alters the scheduling priority of one or more running processes.
  • Syntax
    renice –n NEWNICEVALUE pid…
    Example
    For the sleep process we started in the last example with niceness value of 5:
    Img5
    We need to give it higher priority by decreasing the niceness value to 5.
    Img6
    The ps command will display the current priority.

Running Processes in Background
Some processes take very long time (may be several hours or half a day) to finish. A backup job for example may take a whole day. It is not wise to start such a process and wait in front of the screen for it to finish. Instead, such long processes could be run in the background. For a process to start in the background, it should have a little or no interaction at all. To run a command in the background, use the following syntax:
COMMAND &
The ampersand & following the command tells the system to start this program in the background.
Example
To run a shell script backup.sh that takes a backup of your application’s data in the background:
bash backup.sh &
Note
The nohup (no hangup) command is commonly used with long processes that will run in the background. A process started by nohup will be immune to hang-ups.

Listing Background Processes
To list processes running in the background, use the command jobs. Consider two commands run in the background:
Img7
To list the background jobs:
Img8

Controlling Background Processes
Three commands control the background processes: fg, bg, and kill.

  • To bring the 2nd background process to the foreground:
  • fg %2

  • To terminate it while it is in the foreground, press Ctrl+c.
  • To suspend a foreground process, press Ctrl+z. This will pause the process and move it to the background.
  • Img9

  • To resume a stopped background process, use the bg command:
  • Img10

  • To kill one or more background processes, use the kill command:
  • Img11

Summary

  • A process priority determines how often a process will be given CPU time slots.
  • Process priority is controlled by a value called Niceness. The higher a process priority is, the lower niceness a process has.
  • The nice command is used to run a process with specific priority, while renice modifies priority of an already-running process.
  • To start a process in the background, follow the command with &.
  • Three commands could be used to control background processes: fg, bg, and kill.

I hope you find this article useful. See you in the next article.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -