Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linux(Chapter 4)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

LINUX OPERATING SYSTEM

PROCESS IN LINUX
 Linux is a multi-user as well as a multi-tasking operating system, which
means that more than one task can be active at once. The Process is one
of the fundamental abstractions in Linux/UNIX Operating Systems, the
other fundamental abstraction being files.”
 A process is a program in execution.
 It consists of the executing program code, a set of resources such as
open files, internal kernel data, an address space, one or more threads of
execution and a data section containing global variables.
 Each program running on a system is assigned a unique number by the
kernel which is called process ID (PID).

Process Fundamental
 When a program is started on Linux/UNIX, it creates what is known as a
“process” on the system.
 Every process is assigned a unique serial number called its process id or
PID for short.
 Processes can be created by any user, but can only be destroyed by
someone with the permission to do so - usually the user that created the
process or the system administrator.
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

 This ensures that the compute jobs you start on the system will not be
disturbed by any other user of the system until they are complete or you
decide to stop them yourself.
 Processes and process management become important on Linux/UNIX
systems that are shared between a number of users.
 The concept of users and PIDs is the main tool by which the available
system resources are shared fairly among everybody who needs access
to them.
 Processes can be suspended or given lower priority in cases where one
or more users should step out of the way for someone else, but wish to
do so without losing their work up to that point.
 One further consideration on this topic is the fact that a running
Linux/UNIX process can generate “child” processes. For example, any
program or command (forexample cat commant) you run from inside a
Linux/UNIX shell will be a child process of that shell.
 Conversely, the shell is the parent process of this child. Child processes
have associated with them both their own process id (PID) as well as
their parent’s process id (PPID).

Parent and Child Process


 In computing, a parent process is a process that has created one or more
child processes. A child process is a process created by another process
(the parent process).

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 In Linux, a process is responsible for generating another process.


 A process that generates another process is called the parent process of
the newly generated process, called the child.
 For example, when a command like $cat rshukla.txt is given; the shell
creates a process for running the cat command. Thus the shell sh (ksh or
bsh) being a process, generates another process (cat). Here the shell
process is the parent process and the cat process is the child process.
 Each process may create many child processes but will have at most one
parent process; if a process does not have a parent this usually indicates
that it was created directly by the kernel.
 In some systems, including UNIX-based systems such as Linux, the very
first process (called init) is started by the kernel at booting time and
never terminates; other parentless processes may be launched to carry
out various daemon tasks in user space.
 The operating system kernel identifies each process by its process
identifier.
 Process 0 is a special process that is created when the system boots;
after forking a child process (process 1), process 0 becomes the
swapper process.
 The swapper is a kernel daemon. Swapper moves whole processes
between main memory and secondary storage (swapping out and
swapping in) as part of the operating system's virtual memory
system.
 Process 1, known as init, is the ancestor of every other process in the
system.
 The init process is the ancestor of all other processes.
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

 Apache starts many child processes so that they can serve HTTP
requests at the same time.

Process Properties
1. A process has a user id (uid) and a group id (gid) which together specify
what permissions it has.

2. A process has a parent process id (ppid) — the pid of the process which
created it.

• The kernel starts an init process with pid 1 at boot-up.

• Every other process is a descendant of pid 1.

3. Each process has its own working directory, initially inherited from its
parent process

4. There is an environment for each process — a collection of named environ-

ment variables and their associated values.

• A process’s environment is normally inherited from its parent process.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Characteristics of Linux Processes


 Linux processes have the following characteristics:

1. Policy - normal or real time. Real time processes have a higher


priority than normal processes.

2. Priority - The process priority. It is a number between -20 and


19. The value of -20 is the highest, and 19 is the lowest priority.
Process priority can be set with the nice(1) command and
changed using the renice(8) command.

ps command
 As we all know Linux is a multitasking and multi-user systems.
 So, it allows multiple processes to operate simultaneously without
interfering with each other.
 Process is one of the important fundamental concept of the Linux OS. A
process is an executing instance of a program and carry out different
tasks within the operating system.
 Linux provides us a utility called ps for viewing information related
with the processes on a system which stands as abbreviation
for “Process Status”.
 ps command is used to list the currently running processes and their
PIDs along with some other information depends on different options.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 It reads the process information from the virtual files in /proc file-
system. /proc contains virtual files, this is the reason it’s referred as a
virtual file system.
 ps provides numerous options for manipulating the output according
to our need.

Syntax –

ps [options]
Options for ps Command :

 Simple process selection :


Shows the processes for the current shell –

[root@rhel7 ~]# ps

PID TTY TIME CMD

12330 pts/0 00:00:00 bash

21621 pts/0 00:00:00 ps

Result contains four columns of information.


Where,
PID – the unique process ID
TTY – terminal type that the user is logged into
TIME – amount of CPU in minutes and seconds that the process has
been running
CMD – name of the command that launched the process.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Note – Sometimes when we execute ps command, it shows TIME as


00:00:00. It is nothing but the total accumulated CPU utilization time
for any process and 00:00:00 indicates no CPU time has been given by
the kernel till now. In above example we found that, for bash no CPU
time has been given. This is because bash is just a parent process for
different processes which needs bash for their execution and bash
itself is not utilizing any CPU time till now.

 mintty is a free and open source terminal emulator for Cygwin, the Unix-
like environment for Windows. It features a native Windows user
interface and does not require a display server; its terminal emulation is
aimed to be compatible with xterm.

sleep COMMAND

 sleep command is used to create a dummy job.


 A dummy job helps in delaying the execution. It takes time in seconds
by default but a small suffix(s, m, h, d) can be added at the end to
convert it into any other format.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 This command pauses the execution for an amount of time which is


defined by NUMBER.
Syntax:
sleep NUMBER[SUFFIX]...

 Example:

 Note: If you will define more than one NUMBER with sleep command
then this command will delay for the sum of the values.

kill COMMAND
 kill command in Linux (located in /bin/kill), is a built-in command
which is used to terminate processes manually.
 kill command sends a signal to a process which terminates the process.
 If the user doesn’t specify any signal which is to be sent along with kill
command then default TERM signal is sent that terminates the process.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Options and examples

1. kill -l :To display all the available signals you can use below command
option:

Syntax:
$kill -l

 Signals can be specified in three ways:


 By number (e.g. -5)
 With SIG prefix (e.g. -SIGkill)
 Without SIG prefix (e.g. -kill)

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Note:
 Negative PID values are used to indicate the process group ID. If you
pass a process group ID then all the process within that group will
receive the signal.
 A PID of -1 is very special as it indicates all the processes except kill
and init, which is the parent process of all processes on the system.
 To display a list of running processes use the command ps and this
will show you running processes with their PID number. To specify
which process should receive the kill signal we need to provide the
PID.

Syntax:
$ps

2. kill pid : To show how to use a PID with the kill command.
Syntax:
$kill pid

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

3. kill -s : To show how to send signal to processes.


Syntax:
kill {-signal | -s signal} pid

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

jobs COMMAND
 Jobs Command : Jobs command is used to list the jobs that you are
running in the background and in the foreground.
 If the prompt is returned with no information no jobs are present. All
shells are not capable of running this command. This command is only
available in the csh, bash, tcsh, and ksh shells.

Syntax :
jobs [JOB]

 The character '+' identifies the job that would be used as default
for the fg or bg utilities; this job can also be specified using the job_id
%+ or "%%" .
 The character '-' identifies the job that would become the default if the
current default job were to exit; this job can also be specified using the
job_id %-.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Type of Process in Linux


There are generally three types of processes that run on Linux.

1. Interactive Processes- Foreground Process:


 Interactive processes are those processes that are invoked by a
user and can interact with the user.
 vi is an example of an interactive process.
 Interactive processes are initialized and controlled through a
terminal session.
 Typical interactive programs are command shells, text editors,
and graphical applications.
 A process when you start from the terminal by default runs as
a foreground process.
 The foreground process will not allow you to use the terminal
unless the process is completed.
 In this case, if you need access back to your terminal then you
have to open a new terminal window or stop the running process
or kill it.
 It is also important to note, any process that is created via the
terminal is attached to the terminal session and the process will
be terminated once the terminal is closed.
 This is because bash is the parent process and any command you
run in the terminal will be the child process to the bash so when

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

you close the terminal parent process (bash) will automatically


terminate all its child processes and its own.
 Below is an example of the foreground process. This is just a
simple sleep command that will return the prompt to the user
only after it is completed.

$ sleep 10000

Linux Sleep Command

 Now I have few options to gain control back to the terminal


prompt.
 Either I have to cancel or stop the process or open a new terminal
tab. When you stop a running process by pressing (CTRL+Z) you
will get output as shown in the below image.

Stop Running Linux Process

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 To get the list of jobs either in running or stopped state you can
run the below command.

$ jobs -l

List Running Linux Jobs

 From the above image, you can see the jobs command gives
you the process ID, what command you submitted, what is the
status of it. You will also get a job ID for every process that you
submit ( [1], [2] [3], etc..).
 To start a job that is in the stopped state or bring the
background job to the foreground run the following command.

$ fg %4

 Where %4 denotes the job ID that you can get from the “jobs -
l” command.

Start Job in Foreground

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

2. Non- Interactive Processes (Batch Processes) - background


processes:
 These do not need user interaction, and hence they often run in
the background.
 Since such processes do not need to be very responsive, they
are often penalized by the scheduler.
 Automatic or batch processes are not connected to a terminal.
Rather, these are tasks that can be queued into a spooler area,
where they wait to be executed on a FIFO (first-in, first-out)
basis. Such tasks can be executed using one of two criteria:

I. At a certain date and time: done using the at command.

 at command is a command-line utility that is used to schedule a


command to be executed at a particular time in the future.
 Jobs created with at command are executed only once.
 The at command can be used to execute any program or mail at any
time in the future.
 It executes commands at a particular time and accepts times of the
form HH:MM to run a job at a specific time of day.
 The following expression like noon, midnight, teatime, tomorrow, next
week, next Monday, etc. could be used with at command to schedule a
job.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Syntax:
at [OPTION...] runtime

1. Command to list the user’s pending jobs:


at -l

or

atq

2. Schedule a job for the coming Monday at a time twenty minutes later
than the current time:
at Monday +20 minutes

3. Schedule a job to run at 1:45 Aug 12 2020:


at 1:45 081220

4. Schedule a job to run at 3pm four days from now:


at 3pm + 4 days

5. Schedule a job to shutdown the system at 4:30 today:


# echo "shutdown -h now" | at -m 4:30

6. Schedule a job to run five hour from now:

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

at now +5 hours

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

7. at -r or atrm command is used to deletes job , here used to deletes


job 11 .
at -r 11

or

atrm 11

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

II. At times when the total system load is low enough to accept extra
jobs: done using the batch command.
 In large environments, the system administrator may prefer batch
processing when large amounts of data have to be processed or
when tasks demanding a lot of system resources have to be executed
on an already loaded system.
 Batch processing is also used for optimizing system performance.
 Definition: “Processing that a system performs without requiring
interaction with the user is called background processing. In Linux,
append an ampersand to the command line to request background
processing.”
 Background process runs your process in the background and will
not take control of your terminal prompt.
 You can start a session and you are free to use your terminal. To
submit a command as a background process you have to
add & symbol at the end of the command.

$ sleep 50000 &

Run Linux Command in Background

 Run jobs command to get the list of jobs.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

$ jobs -l

 From the below image you can see Job ID [5] is assigned to the job
and & symbol tells it is submitted and running as a background job.

List Running Linux Commands (Jobs)

 You can start any stopped job in the background directly by running the
following command.

$ bg %2

 Where %2 denotes the job ID that you can get from the “jobs -l”
command.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

3. Process or Daemon Process:


 The third general type of process that runs on Linux is a System Process
or Daemon (day-mon).
 Daemon is the term used to refer to processes that are running on the
computer and provide services but do not interact with the console.
 Daemons are server processes that run continuously. Most of the time,
they are initialized at system startup and then wait in the background
until their service is required.
 Most server software is implemented as a daemon.
 Apache and init are all examples of daemons.
 A typical example is the networking daemon, xinetd, which is started in
almost every boot procedure. After the system is booted, the network
daemon just sits and waits until a client program, such as an FTP client,
needs to connect.

service COMMAND
 The service command is part of the init daemon. It was used commonly
in earlier distributions of Linux based systems to manage services on a
system. Let us have a look at how to start, stop and restart services
using the service command.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

1. Listing all the services


 Before we learn how to start, stop and restart services on Linux using
the service command, we need to know how to list all the services using
the init daemon. To do so, we use the following command.

service --status –all

Service Status

 Using this command in the command line will give an output similar to
the one seen below. Notice that we get a list of all the services on our
system. In this list, services preceded by a [+] sign are enabled while the
ones preceded by a [-] sign are disabled.

 This command can be used to find out the current status of a service on
our system. Else, you can use it to verify that your signal to the system
to start, stop or restart a service took effect.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

2. Starting a service
 Now we will discuss how to start a service. To do so, we use
the start option of the service command. This option is preceded by the
name of the service which we wish to start. We type the following
command to stop a service on a Linux system.
service <ServiceName> start

3. Stopping a service
 To stop a service using the service command, we utilise the stop option.
This option is preceded by the name of the service which we wish to
stop. Typing the following command will stop the specified service on
your Linux system.
service <ServiceName> stop

4. Restarting a service
 To restart a service using the service command, we make use of the
restart option. Like previous examples, this option is typed after the
name of the service which we wish to start. The following command
helps us restart a service on Linux.
service <ServiceName> restart

 In the following example, stars, restart and stop.

1. service httpd start


2. service httpd stop
3. service httpd restart

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

systemctl COMMAND
 The systemctl command is part of the systemd daemon. It is used
commonly in newer distributions of Linux based systems to manage
services on a system.
 The systemd daemon was designed to replace the init daemon by
offering better service management efficiency. Let us have a look at how
to start, stop and restart services using the systemctl command.

1. Listing all the services


 Before we have a look at how to start, stop and restart services on Linux
using the systemctl command, we need to know how to list all the
services using the systemd daemon.
 To do so, we type the following command in our terminal.

systemctl list-unit-files --type service -all

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Using the systemctl command will give an output similar to the one seen
below.
 Notice that we get a list of all the services on our system. In this list,
unlike the symbolic representation seen in case of the services
command, we get a new column that holds the status of the services on
our system.
 This command can be used to find out the current status of a service on
our system. Else, you can use it to verify that your signal to the system
to start, stop or restart a service took effect.

2. Starting a service
 To start a service using the systemctl command, we utilise
the start option.
 This option is followed by the name of the service which we wish to
start. Type the following command to start a service on your Linux
system.
systemctl start <ServiceName>

3. Stopping a service
 Now we will discuss how to stop a service. To do so, we use
the stop option of the systemctl command.
 This option is followed by the name of the service which we wish to
start. We use the following command to stop a service on a Linux
system.
systemctl stop <ServiceName>

4. Restarting a service

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 To restart a service using the systemctl command, we utilise the


restart option.
 Like previous examples, this option is followed by the name of the
service which we wish to start. The following command helps us restart
a service on Linux.
systemctl restart <ServiceName>

List of daemon services for Unix and Linux like


systems
o dhcpd: It stands for Dynamic Host Configuration Protocol. Also, it is
an Internet Bootstrap Protocol Server.
o ftpd: It stands for FTP Server Daemon.
o httpd: It is a Web Server Daemon.
o lpd: It stands for Line Printer Daemon.
o mysql: It is a database server daemon.
o named: It is a DNS server daemon.

o smtpd: It stands for Simple Mail Transfer Protocol Daemon.


o snmpd: It stands for Simple Network Management Protocol Daemon.
o sshd: It is a Secure Shell Server Daemon.
o xinetd: It is an Enhanced Internet Superserver Daemon.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Zombie processes in Linux are sometimes also referred to as defunct


or dead processes. They’re processes that have completed their
execution, but their entries are not removed from the process table.

INTER-PROCESS COMMUNICATION
 The types of inter-process communication are:

1. Signals - Sent by other processes or the kernel to a specific


process to indicate various conditions.

2. Pipes - Unnamed pipes set up by the shell normally with the “|”
character to route output from one program to the input of
another.

3. FIFOS - Named pipes operating on the basis of first data in, first
data out.

A FIFO, also known as a named pipe, is a special file similar to a


pipe but with a name on the filesystem. Multiple processes can
access this special file for reading and writing like any ordinary
file. Thus, the name works only as a reference point for processes
that need to use a name in the filesystem.

An unnamed pipe is only used for communication between a


child and it's parent process, while a named pipe can be used
for communication between two unnamed process as well.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Processes of different ancestry can share data through a named


pipe. A named pipe exists in the file system.

4. Message Queues - Message queues are a mechanism set up to


allow one or more processes to write messages that can be read
by one or more other processes.

5. Semaphores - Counters that are used to control access to shared


resources.

These counters are used as a locking mechanism to prevent more


than one process from using the resource at a time.

6. Shared Memory - The mapping of a memory area to be shared


by multiple processes.

Message queues, semaphores, and shared memory can be


accessed by the processes if they have access permission to the
resource as set up by the object’s creator. The process must pass
an identifier to the kernel to be able to get access.

CONNECTING PROCESS WITH PIPES


 Linux provides you with a wide array of utilities to manipulate data.
 You can search, sort, slice, dice, and transform data stored in files in
many different ways.
 A pipe (also called a pipeline) is a powerful shell feature that allows you
to pump the output of one program directly into another.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 In other words, a pipe is a communication device that permits


unidirectional communication.
 Data written to the “write end” of the pipe is read back from the “read
end.”
 Pipes are serial devices; the data is always read from the pipe in the
same order it was written.
 Typically, a pipe is used to communicate between two threads in a
single process or between parent and child processes.
 In a shell, the symbol | creates a pipe. For example, this shell command
causes the shell to produce two child processes, one for ls and one for
less:
$ls | less
 The shell also creates a pipe connecting the standard output of the ls
sub process with the standard input of the less process.
 The filenames listed by ls are sent to less in exactly the same order as if
they were sent directly to the terminal. A pipe’s data capacity is limited.
 If the writer process writes faster than the reader process consumes the
data, and if the pipe cannot store more data, the writer process blocks
until more capacity becomes available. If the reader tries to read but no
data is available, it blocks until data becomes available.
 Thus, the pipe automatically synchronizes the two processes.
 Linux also supports named pipes, also known as FIFOs because pipes
operate on a First In, First Out principle.
 The first data written into the pipe is the first data read from the pipe.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Unlike pipes, FIFOs are not temporary objects, they are entities in the
file system and can be created using the mkfifo command. Processes are
free to use a FIFO so long as they have appropriate access rights to it.
 The way that FIFOs are opened is a little different from pipes. A pipe is
created in one go whereas a FIFO already exists and is opened and
closed by its users. Linux must handle readers opening the FIFO before
writers open it as well as readers reading before any writers have
written to it. That aside,
 FIFOs are handled almost exactly the same way as pipes and they use
the same data structures and operations.

 Note: A very useful Linux feature is named pipes which enable different
processes to communicate. The | (pipe) character takes standard output
of the command that appears on the left, and passes it to standard input
of the command that appears on the right.

Redirecting Input Output


 Most command-line UNIX programs can read from something called
standard input for their input and write to something called standard
output for their output.
 Standard input (STDIN for short) is normally connected to the keyboard
- that’s how you normally provide input to programs via the shell.
 Standard output (STDOUT for short) is normally connected to the
terminal window - the terminal is normally where the output will
appear from commands you issued via the shell.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 However, the shell also allows you to redirect STDIN and STDOUT with
the special characters >, >>, <, and |.
 For example, STDOUT in our above example can be redirected to a file
instead of the terminal window by typing:
$ ls -l > list.txt

 In this example, the output of ls -l is redirected into a file called list.txt


instead of being printed to your terminal. If list.txt does not exist, it is
created. If it does exist, its contents are replaced with the contents of
STDOUT. Note that > is a special character to the shell.

 As such, it and subsequent characters on the command line are not


interpreted as arguments to the ls command. A variant of the above is:
$ls -l >> list.txt
 >> works just like > except that if list.txt already exists, STDOUT is
appended to the bottom of the file rather than replacing its contents.
 STDIN can be taken from a file instead of the keyboard in an analagous
way:
$sort < list.txt
 In this example, the sort program reads the file list.txt on STDIN and
sorts it into alphabetical order on STDOUT, which will be output to our
terminal. If we instead type:
$sort < list.txt > sorted_list.txt
 then list.txt will be passed to sort on STDIN, and the sorted result will be
placed in the file sorted_list.txt on STDOUT.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Finally, we introduce the | (pipe) character, which is used to make so-


called pipelines.
 The | character takes STDOUT of the command that appears on the left,
and passes it to STDIN of the command that appears on the right. For
example:
$ls -l | sort | lpr
 takes STDOUT of ls -l, sends it to STDIN of sort, and finally sends
STDOUT of sort to STDIN of lpr which prints out a hardcopy of the result
via the default printer. This is the equivalent of:
$ls -l > list.txt
$sort < list.txt > sorted_list.txt
$lpr sorted_list.txt
 Except that no intermediate files are used to pass the output of the
preceding commands to the input of the succeeding commands.

MANAGING MULTIPLE PROCESSES


 As with any multitasking operating system, Linux executes multiple,
simultaneous processes. Well, they appear simultaneous, anyway.
 Actually, a single processor computer can only execute one process at
time but the Linux kernel manages to give each process its turn at the
processor and each appears to be running at the same time.
 There are several commands that can be used to control processes.
These are as follows:
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

• ps - List the processes running on the system.


• kill - send a signal to one or more processes (usually to “kill” a process
by number)
• jobs - an alternate way of listing your own processes.
• bg - put a process in the background
• fg - put a process in the forground
• killall - Send a signal to a process by name
• nice - Set process priority of new processes.
• renice(8) - Can be used to change the process priority of a currently
running
process.
• top - Displays the processes that are using the most CPU resources.

bg COMMAND
 bg command in linux is used to place foreground jobs in background.
Syntax:
bg [job_spec ...]

 bg [JOB_SPEC] : This command is used to put the mentioned job in


background. In the below screenshot, we do following :
1. We use jobs command to list all jobs
2. We create a process using sleep command, we get its ID as 1.
3. We put it in background by providing its ID to bg.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 ‘sleep 500’ is used to create dummy foreground job.

fg COMMAND
 fg command in linux used to put a background job in foreground.
Syntax:
fg [job_spec]

 fg [JOB_SPEC] : This command is used to put the mentioned job


running inbackground to foreground.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 “sleep 500” is a command which is used to create a dummy job which


runs for 500 seconds.

free COMMNAD

 The free command gives information about used and unused memory
usage and swap memory of a system. By default, it displays memory
in kb (kilobytes).
 Memory mainly consists of RAM (random access memory) and swap
memory.
 Swap memory is a part of hard disk drive that acts like a virtual RAM.

Syntax: free

 Look at the above snapshot,


 Line1tells about the memory details like total RAM available in our
system, used RAM, free RAM, shared RAM, buffered RAM and cached
RAM.
 Line2indicates used and free buffer/cache memory.
 Line3indicates total, used and free swap memory.
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

 Displaying RAM in different format


o By default, free command displays RAM information in kb format.
You can display it in different format as per your wish.

Syntax:

1. free -b display information in Bytes


2. free -m display information in Megabytes
3. free -g display information in Gigabytes

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

top UTILITY
 top command is used to show the Linux processes.
 It provides a dynamic real-time view of the running system.
 Usually, this command shows the summary information of the system
and the list of processes or threads which are currently managed by
the Linux Kernel.
 As soon as you will run this command it will open an interactive
command mode where the top half portion will contain the statistics of
processes and resource usage.
 And Lower half contains a list of the currently running processes.
Pressing q will simply exit the command
mode.

Here,
o PID: Shows task’s unique process id.
o PR: The process’s priority. The lower the number, the higher the
priority.
o VIRT: Total virtual memory used by the task.
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

o USER: User name of owner of task.


o %CPU: Represents the CPU usage.
o TIME+: CPU Time, the same as ‘TIME’, but reflecting more
granularity through hundredths of a second.
o SHR: Represents the Shared Memory size (kb) used by a task.
o NI: Represents a Nice Value of task. A Negative nice value implies
higher priority, and positive Nice value means lower priority.
o %MEM: Shows the Memory usage of task.
o RES: How much physical RAM the process is using, measured in
kilobytes.
o COMMAND: The name of the command that started the process.
 Examples:
1) Exit Top Command After Specific repetition: Top output keep
refreshing until you press ‘q‘. With below command top command will
automatically exit after 10 number of repetition.
top -n 10

2) Display Specific User Process


top -u paras

3) Kill running process: You can kill a process after finding PID of
process by pressing ‘k‘ option in running top command without exiting
from top window as shown below.
4) Shows top command syntax:
top -h

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

CHANGING PROCESS PRIORITY


 The Linux kernel is responsible for making all running processes
interact well with each other.
 One of the ways in which it does this is by determining which
applications should get more CPU time than others.
 By default, the kernel handles this without interaction from the user, but
there may be cases where you want to increase or decrease a process or
application’s priority.
 When talking about processes priority is all about managing processor
time.
 The Processor or CPU is like a human juggling multiple tasks at the same
time. Sometimes we can have enough room to take on multiple projects.
 Sometimes we can only focus on one thing at a time. Other times
something important pops up and we want to devote all of our energy
into solving that problem while putting less important tasks on the back
burner.
ASST. PROF. ZEENAT SULTANA
LINUX OPERATING SYSTEM

 In Linux we can set guidelines for the CPU to follow when it is looking at
all the tasks it has to do.
 These guidelines are called niceness or nice value.
 The Linux niceness scale goes from -20 to 19.
 The lower the number the more priority that task gets.
 If the niceness value is high number like 19 the task will be set to the
lowest priority and the CPU will process it whenever it gets a chance.
 The default nice value is zero.
 By using this scale we can allocate our CPU resources more
appropriately.
 Lower priority programs that are not important can be set to a higher
nice value, while high priority programs like daemons and services can
be set to receive more of the CPU’s focus.
 You can even give a specific user a lower nice value for all of his/her
processes so you can limit their ability to slow down the computer’s
core services.

nice and renice COMMAND


 nice command in Linux helps in execution of a program/process with
modified scheduling priority.
 It launches a process with a user-defined scheduling priority. In this, if
we give a process a higher priority, then Kernel will allocate more CPU
time to that process.
 Whereas the renice command allows you to change and modify the
scheduling priority of an already running process.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

 Linux Kernel schedules the process and allocates CPU time accordingly
for each of them.

To set the priority of a process

To check the nice value of a process.

Changing priority of the running process.

ASST. PROF. ZEENAT SULTANA


LINUX OPERATING SYSTEM

Creating process
 A new process can be created by the fork() system call.
 The new process consists of a copy of the address space of the original
process.
 fork() creates new process from existing process.
 Existing process is called the parent process and the process is created
newly is called child process.
 The function is called from parent process.
 Both the parent and the child processes continue execution at the
instruction after the fork(), the return code for the fork() is zero for the
new process, whereas the process identifier of the child is returned to
the parent.

ASST. PROF. ZEENAT SULTANA

You might also like