Linux(Chapter 4)
Linux(Chapter 4)
Linux(Chapter 4)
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).
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.
3. Each process has its own working directory, initially inherited from its
parent process
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.
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 :
[root@rhel7 ~]# ps
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
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.
1. kill -l :To display all the available signals you can use below command
option:
Syntax:
$kill -l
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
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 %-.
$ sleep 10000
To get the list of jobs either in running or stopped state you can
run the below command.
$ jobs -l
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.
Syntax:
at [OPTION...] runtime
or
atq
2. Schedule a job for the coming Monday at a time twenty minutes later
than the current time:
at Monday +20 minutes
at now +5 hours
or
atrm 11
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.
$ 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.
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.
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.
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.
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
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.
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
INTER-PROCESS COMMUNICATION
The types of inter-process communication are:
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.
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.
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
bg COMMAND
bg command in linux is used to place foreground jobs in background.
Syntax:
bg [job_spec ...]
fg COMMAND
fg command in linux used to put a background job in foreground.
Syntax:
fg [job_spec]
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
Syntax:
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
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
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.
Linux Kernel schedules the process and allocates CPU time accordingly
for each of them.
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.