Displaying Processes in the System
- Static view (ps command)
- Dynamic view (top command)
ps command
- displays the processes for the current shell.
- We can display all of the processes owned by the current user by using the x option.
ps -x
State of the Process
- A column in output is shown that is STAT.
- This column shows the current state of the process.
State | Meaning |
R | Running |
S | Sleeping or waiting for an event such as keystroke |
D | Uninterruptible Sleep. Process is waiting for I/O such as a disk drive |
T | Stopped |
Z | A difunctional or zombie process |
l | multithreaded |
s | Session leader |
+ | Foreground process |
< | A high priority process |
N | A low priority process |
Option with ps command
Option | Description |
-A or -e | Display every active process on a Linux system
|
-x | Display all of the processes owned by the user |
-F | Perform a full-format listing |
-U | Select by real user ID or name |
-u | Select by effective user ID or name |
-p | Select process by pid |
-r | Display only running processes |
-L | Show number of threads in a process |
-G | Show processes by Group |
Example:
- Display processes that are related to the user ubuntu
- Select a process with id 1302
Dynamic view
- The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources.
- Top displays a list of processes, with the ones using the most CPU at the top.
- An improved version of top command is htop but it is usually not pre-installed in most distributions.
- When a top program is running, we can highlight the running programs by pressing z.
- we can quit the top program by press q or Ctrl + c.
Option with top command
Options | Description |
-u | display specific User process details |
-d | To set the screen refresh frequency |
Displaying processes in Tree like structure
- pstree command is used to display processes in tree-like structure.
- showing the parent/child relationships between processes.
Interrupting A Process
- A program can be interrupted by pressing Ctrl + C.
- This will interrupt the given processes and stop the process.
- Ctrl+C essentially sends a SIGINT signal from the controlling terminal to the process, causing it to be killed
Putting a Process in the Background
- A foreground process is any command or task you run directly and wait for it to complete.
- Unlike with a foreground process, the shell does not have to wait for a background process to end before it can run more processes.
- Normally, we start a program by entering its name in the CLI. However, if we want to start a program in background, we will put an & after its name.
Example
- open the gedit program (gedit) as foreground process.
- Now start again gedit as a background process.(gedit &)
jobs command
- To list the jobs that have been launched from our terminal.
- Using the jobs command, we can see this list.
Example
we first launch two jobs in background(gedit &,firefox &) and then use the jobs command to view the running jobs
Bringing a process to the foreground
- A process in the background is immune from keyboard input, including any attempt to interrupt it with a Ctrl-c.
- fg command is used to bring a process to the foreground.
Example
- start the gedit editor in background(gedit &).
- Then use the jobs command to see the list of jobs launched and then, bring this process to the foreground(fg %1).
Killing a process
- We can kill a process using the kill command.
- To kill a process, we provide the process id as an argument.
Example
we start the gedit program and then kill it using kill command.
Signals
- The kill command doesn’t exactly “kill” processes, rather it sends them signals.
- Signals are one of several ways that the operating system communicates with programs.
- Programs listen for signals and may act upon them as they are received.
Common signals with kill command
Following are most common signals that can be send with kill command.
Signal | Meaning |
INT | INT Interrupt. Performs the same function as the Ctrl-c key sent from the terminal. It will usually terminate a program. |
TERM | Terminate. This is the default signal sent by the kill command. If a program is still “alive” enough to receive signals, it will terminate. |
STOP | Stop. This signal causes a process to pause without terminating. |
CONT | Continue. This will restore a process after a STOP signal. |
Pausing a process
- Linux allows you to pause a running process rather than quitting or killing it.
- Pausing a process just suspends all of its operation so it stops using any of your processor power even while it still resides in memory.
- This may be useful when you want to run some sort of a processor intensive task, but don’t wish to completely terminate another process you may have running.
- Pausing it would free up valuable processing time while you need it, and then continue it afterwards.
kill command with STOP option
- We can pause a process by using kill command with STOP option.
- The process id is required as an argument in kill command.
Example
- Start the gedit program in the background(gedit &).
- Find the pid of gedit using ps command(ps).
- Pause the process using kill command(kill –STOP 5008).
- Resume the process by using the kill command with CONT option(kill –CONT 5008).
Changing process priority
- Every running process in Linux has a priority assigned to it.
- We can change the process priority using nice and renice utility.
- Nice command will launch a process with a user defined scheduling priority.
- Renice command will modify the scheduling priority of a running process.
system priorities
In Linux system priorities are 0 to 139
- 0 to 99 for real time
- 100 to 139 for users.
nice value range is -20 to +19
- -20 is highest
- 0 default
- +19 is lowest.
Relation between nice value and priority is:
PR = 20 + NI
So, the value of PR = 20 + (-20 to +19) is 0 to 39 that maps to 100-139
Example
- we start the gedit program in background and see its nice value using ps command(ps -l).
- Now, we start the gedit program again using nice command. (nice 10 gedit &).
- 3.Then we change its priority using renice command. (sudo renice -n -12 -p 5161)
Change the priority of all of the processes belonging to a user or a group
- We can also use the renice command to change the priority of all of the processes belonging to a user or a group.
- e.g: process belonging to user computer(sudo renice -n -12 -u computer).
LAb Task
- Display all of the process in current shell
- Display every active process in the system
- Provide a full-format listing of process owned by you
- Display a full-format listing of processes owned by user ubuntu
- Display a process with id 1
- Display the dynamic view of the current processes in the system and set the refresh interval 0.5 second
- Display the dynamic view of the processes owned by user with id 999 (or name ubuntu)
- Start the gedit program in background and then bring it foreground
- Suspend the gedit program 1
- Resume the gedit program
- Start the gedit program with priority 90
- Reset the priority of gedit to 65
Related Links to Operating System topics
Operating system Course content
- Operating System-Functions and History
- Generations of Operating System
- Functions of an Operating System
- Components of Operating System
- Types of Operating System
- Services of Operating System
- Properties of Operating System
- Processes in Operating System
- Process Scheduling in Operating System
Lab Practice Task
- Introduction to Linux Ubunto
- Installation with virtual Box
- Writing Linux Commands
- Navigation in File System and Directory Management in Ubunto using CLI
- File Handling and I/O Redirection In Ubunto
- File Access Permission in Linux
- Text Processing Tools and Basic System Configuration Tools in Linux
- Package Management in Linux
- How to manage processes in Linux
- Compiling and Executing C++ programs in Linux
- System Calls
- Introduction To Shell Programming
#Operating System complete course #Operating System past paper #Operating System-project #Computer Science all courses #operating system Problem with source code#University Past Paper #Programming language #Question paper #old paper #Operating System-Functions and History #Generations of Operating System #Functions of an Operating System #Components of Operating System #Types of Operating System #Services of Operating System #Properties of Operating System #Processes in Operating System #Process Scheduling in Operating System #Introduction to Linux Ubunto #Installation with virtual Box #Writing Linux Commands #Navigation in File System and Directory Management in Ubunto using CLI #File Handling and I/O Redirection In Ubunto #File Access Permission in Linux #Text Processing Tools and Basic System Configuration Tools in Linux #Package Management in Linux #How to manage processes in Linux #Compiling and Executing C++ programs in Linux #System Calls #Introduction To Shell Programming