0% found this document useful (0 votes)
26 views43 pages

Lab 10-OS

The document discusses several Linux commands related to managing processes, including ps, kill, nice, renice, bg, fg, and df. Ps displays information about running processes. Kill terminates a process. Nice and renice control process priorities. Bg and fg manage foreground and background processes. And df shows available disk space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views43 pages

Lab 10-OS

The document discusses several Linux commands related to managing processes, including ps, kill, nice, renice, bg, fg, and df. Ps displays information about running processes. Kill terminates a process. Nice and renice control process priorities. Bg and fg manage foreground and background processes. And df shows available disk space.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lab10

Process managent

T / Ebtisam ALselwi 1
Linux Command Related with Process

Function
Command

ps Give the status of processes running for a user .


Example: ps
kill {PID} To stop any process i.e. to kill process.
Example: kill 1012
Pidof<processname> Give the processes ID (PID)

ps –ag To get information about all running process.


Example: ps –ag

T / Ebtisam ALselwi 2
Linux Command Related with
Process(con…)
Function
Command

top Details on all Active process

bg To send a process to background

fg To run a stopped process in foreground.

ps PID Gives the status of a particular process

T / Ebtisam ALselwi 3
Linux Command Related with
Process(con…)

Function
Command

nice Starts a process with a given priority


nice –n ‘nice value’ process name
Example: nice –n 19 top
renice Changes priority of an already running process
renice ‘nice value’ –p ‘PID’
df Gives free hard disk space on your system

free Gives free RAM on your system

T / Ebtisam ALselwi 4
top Command Display all running process
std@ubuntu:~$ top
The first lines of the previous
output include general
information about the system,
such as its running time,
processor usage rate, number of
operations, and others, and you
can see that there is one active
operation in our example, as well
as 55 processes in the idle sleep
state that do not use any of the
CPU processor resources. The
other partition and the distributor
in a table include active processes,
as well as different information
about them (such as how much
they consume for memory or
processor).

T / Ebtisam ALselwi 5
top Command)con…)

As we saw top displays processes similar to Task Manager


in graphical interfaces, they are not flexible enough to
cover all needs, and here comes the role of the PS tool to
compensate for this shortcoming. At first glance we may be
disappointed if we call it PS directly and without additional
Arguments data, as the number of output lines will not
exceed two with three columns:

T / Ebtisam ALselwi 6
ps command To see currently running Process

std@ubuntu:~$ ps

PID TTY TIME CMD


3685 pts/11 00:00:00 bash
3719 pts/11 00:00:00 ps

The reason is that the PS command simply displays the


processes associated with the current user and the terminal
session only, and given that we only operated terminal with
the PS command, the previous output seems normal. To
review more details about our current system operations we
can run the following command:
T / Ebtisam ALselwi 7
ps Command(con…)
std@ubuntu:~$ ps x
PID TTY STAT TIME COMMAND
2847 ? Sl 0:00 /usr/bin/gnome-keyring-daemon --
daemonize --l
2856 ? Ss 0:00 upstart --user
2955 ? S 0:00 dbus-launch --
autolaunch=813e56ef1c4f171bda95
2961 ? Ss 0:00 //bin/dbus-daemon --fork --print-pid 5
--prin
2977 ? Ss 0:00 dbus-daemon --fork --session --
address=unix:a
2987 ? Ss 0:00 /usr/lib/x86_64-linux-
gnu/hud/window-stack-br
.
T / Ebtisam ALselwi 8
ps Command(con…)

Column Description

UID User ID
PID Process ID.
PPID Parent process ID
C CPU utilization of process.
STIME Process start time.
TTY Terminal type associated with the process
TIME CPU time taken by the process.
CMD The command that started this process.

T / Ebtisam ALselwi 9
ps Command(con…) With the aux option, the PS
command will display processes that
std@ubuntu:~$ ps aux belong to all users in an easy-to-
understand table format.
USER PID %CPU %MEM VSZ RSS TTY STAT
START TIME COMMAND
root 1 1.0 0.2 29520 4180 ? Ss 09:57 0:04 /sbin/i
root 2 0.0 0.0 0 0 ? S 09:57 0:00 [kthrea
root 3 0.0 0.0 0 0 ? S 09:57 0:00 [ksofti
root 5 0.0 0.0 0 0 ? S< 09:57 0:00 [kworke
root 7 0.2 0.0 0 0 ? S 09:57 0:00 [rcu_sc
root 8 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
root 9 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
root 10 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
root 11 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
root 12 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
root 13 0.0 0.0 0 0 ? S 09:57 0:00 [rcuos/
T / Ebtisam ALselwi 10
We can also arrange the display of
ps Command(con…) operations sequentially, showing the
relationships between them by
std@ubuntu:~$ ps axjf adding the AXJF option to the
command.

T / Ebtisam ALselwi 11
ps Command(con…)

std@ubuntu:~$ ps pid

Notes about Process IDs


Linux and Unix-like systems are assigned a number defined
for each process ID or PID, where the system can recognize
and track processes during playback. The Pgrep command
allows an easy way to get the PID number for any operation,
which it queries and returns as output on the screen:

T / Ebtisam ALselwi 12
ps Command(con…)

The first proccess launched during takeoff called Init gives the

PID number equals one.

pgrep Init 1 This process will continue to run the rest of the

operations while the system is running, and it is responsible

for running the rest of the services, and in turn the last

operation takes the largest PID number.

T / Ebtisam ALselwi 13
ps Command(con…)

std@ubuntu:~$ std@ubuntu:~$ pgrep init

std@ubuntu:~$ pgrep bash


3552

T / Ebtisam ALselwi 14
ps Command(con…)

We say about the process it is process Parent if it runs


another process, which means that if the process parent is
forcibly stopped (killing it) the child process will collapse, in
which case the PID number of the process Parent ID is
indicated by the PPID number, processes are managed
between the user and the system by calling the process
number instead of its name.

T / Ebtisam ALselwi 15
T / Ebtisam ALselwi 16
pidof process Command(con…)

std@ubuntu:~$ pidof firefox

3505

T / Ebtisam ALselwi 17
ps pid Command(con…)

std@ubuntu:~$ ps 3510

PID TTY STAT TIME COMMAND


3510 ? Sl 0:09 /usr/lib/firefox/firefox

T / Ebtisam ALselwi 18
ps Command(con…)
To get information about all running process.
std@ubuntu:~$ ps -ag

PID TTY STAT TIME COMMAND


1306 tty4 Ss+ 0:00 /sbin/getty -8 38400 tty4
1313 tty5 Ss+ 0:00 /sbin/getty -8 38400 tty5
1322 tty2 Ss+ 0:00 /sbin/getty -8 38400 tty2
1324 tty3 Ss+ 0:00 /sbin/getty -8 38400 tty3
1328 tty6 Ss+ 0:00 /sbin/getty -8 38400 tty6
1515 tty7 Ss+ 0:08 /usr/bin/X -core :0 -seat seat0 -auth /var/run/light
1870 tty1 Ss+ 0:00 /sbin/getty -8 38400 tty1
3409 pts/8 Ss 0:00 bash
3622 pts/9 Ss+ 0:00 bash
3684 pts/8 R+ 0:00 ps -ag

T / Ebtisam ALselwi 19
To stop any process
kill Command

std@ubuntu:~$ kill 3510

T / Ebtisam ALselwi 20
Background process List background jobs

std@ubuntu:~$ bg

[1]+ top &

T / Ebtisam ALselwi 21
Background process List stopped or background jobs, resume
a stopped job in the background

1- start the program

2-press ctrl +z

3-type bg to send process to background .

T / Ebtisam ALselwi 22
bg command (con…) 1- start the program

T / Ebtisam ALselwi 23
bg command (con…) Press ctrl +z (stop process)

T / Ebtisam ALselwi 24
bg command (con…)
type bg to send process to background .

T / Ebtisam ALselwi 25
fg command Brings the most recent job to foreground.

std@ubuntu:~$fg

T / Ebtisam ALselwi 26
Adjust operations priorities
In your management of your server you will often
need to be able to prioritize the process, to determine
which one you want to give the highest priority, in
isolated cases some process are sensitive and of high
importance, while the rest of the process can wait a
little while resources are available. The process
priorities are controlled in Linux through a value
called niceness.
T / Ebtisam ALselwi 27
nice command Starts a process with a given priority

std@ubuntu:~$ nice -n 19 top

T / Ebtisam ALselwi 28
renice command Changes priority of an already running process

std@ubuntu:~$ pidof firefox


4209
std@ubuntu:~$ sudo renice -20 -p 4209

4209 (process ID) old priority 0, new priority -20

T / Ebtisam ALselwi 29
df command Gives free hard disk space on your system

std@ubuntu:~$ df

Filesystem 1K-blocks Used Available Use% Mounted on


/dev/sda1 18582980 3692076 13923892 21% /
none 4 0 4 0% /sys/fs/cgroup
udev 937640 4 937636 1% /dev
tmpfs 189752 1048 188704 1% /run
none 5120 4 5116 1% /run/lock
none 948756 488 948268 1% /run/shm
none 102400 52 102348 1% /run/user

T / Ebtisam ALselwi 30
free command Gives free RAM on your system

std@ubuntu:~$ free

total used free shared buffers cached


Mem: 1897512 1806988 90524 4836 64880 905848
-/+ buffers/cache: 836260 1061252
Swap: 1955836 0 1955836

T / Ebtisam ALselwi 31
Creating a new process
using fork()
System call

T / Ebtisam ALselwi 32
PROCESS - SYSTEM CALL
fork()
• The fork system call is used to create a new process
called child process.
• The return value is 0 for a child process.
• The return value is negative if process creation is
unsuccessful.
• For the parent process, return value is positive

• The child process is an exact copy of the parent


process.
• Both the child and parent continue to execute the
instructions following fork call.
• The child can start execution before the parent or vice-
versa.
T / Ebtisam ALselwi 33
PROCESS - SYSTEM CALL(con…)

getpid() and getppid()

• The getpid system call returns process ID of the


calling process

• The getppid system call returns parent process ID of


the calling process

T / Ebtisam ALselwi 34
The steps
1-nano fork.cpp
2-g++ fork.cpp -o fork1.cpp
3-./fork1.cpp

std@ubuntu:~$ nano fork.cpp

std@ubuntu:~$ g++ fork.cpp -o fork1.cpp

std@ubuntu:~$ ./fork1.cpp

T / Ebtisam ALselwi 35
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout<<"Current process Id : "<<getpid()<<endl;
pid_t childProcessId = fork();
if(childProcessId < 0)
{
cout<<"Failed to Create a new Process"<<endl;
}
else if (childProcessId == 0)
{
// This code will be executed in Child Process Only
cout<<"Child Process Id : "<<getpid()<< " Its
parent ID : "<<getppid()<<endl;
}
T / Ebtisam ALselwi 36
else if (childProcessId > 0)
{
// This code will be executed in Parent Process Only
cout<<"Parent Process Id : "<<getpid()<< " Its
Child Process ID : "<<childProcessId<<endl;
}
}

T / Ebtisam ALselwi 37
Output

Current process Id : 4320


Parent Process Id : 4320 Its Child Process ID : 4321
Child Process Id : 4321 Its parent ID : 4320

T / Ebtisam ALselwi 38
#include <iostream> std@ubuntu:~$ nano fork2.cpp
#include <unistd.h>
using namespace std;
int main()
{
int x = 6;
pid_t childProcessId = fork();
if(childProcessId < 0)
{
cout<<"Failed to Create a new Process"<<endl;
}
else if (childProcessId == 0)
{
// This code will be executed in Child Process Only
cout<<"Child Process :: x = "<<x<<endl;
x = 10;
cout<<"Child Process :: x = "<<x<<endl;
T / Ebtisam ALselwi cout<<"Child Process
39 exists"<<endl;
else if (childProcessId > 0)
{
// This code will be executed in Parent Process
Only
sleep(2);
cout<<"Parent Process :: x = "<<x<<endl;
}
}

T / Ebtisam ALselwi 40
std@ubuntu:~$ g++ fork2.cpp -o fork22.cpp

std@ubuntu:~$ sudo chmod +x fork2.cpp

std@ubuntu:~$ ./fork22.cpp

T / Ebtisam ALselwi 41
Output

Child Process :: x = 6
Child Process :: x = 10
Child Process exists
Parent Process :: x = 6

T / Ebtisam ALselwi 42
End…
T / Ebtisam ALselwi 43

You might also like