Lab 10-OS
Lab 10-OS
Process managent
T / Ebtisam ALselwi 1
Linux Command Related with Process
Function
Command
T / Ebtisam ALselwi 2
Linux Command Related with
Process(con…)
Function
Command
T / Ebtisam ALselwi 3
Linux Command Related with
Process(con…)
Function
Command
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…)
T / Ebtisam ALselwi 6
ps command To see currently running Process
std@ubuntu:~$ ps
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
T / Ebtisam ALselwi 12
ps Command(con…)
The first proccess launched during takeoff called Init gives the
pgrep Init 1 This process will continue to run the rest of the
for running the rest of the services, and in turn the last
T / Ebtisam ALselwi 13
ps Command(con…)
T / Ebtisam ALselwi 14
ps Command(con…)
T / Ebtisam ALselwi 15
T / Ebtisam ALselwi 16
pidof process Command(con…)
3505
T / Ebtisam ALselwi 17
ps pid Command(con…)
std@ubuntu:~$ ps 3510
T / Ebtisam ALselwi 18
ps Command(con…)
To get information about all running process.
std@ubuntu:~$ ps -ag
T / Ebtisam ALselwi 19
To stop any process
kill Command
T / Ebtisam ALselwi 20
Background process List background jobs
std@ubuntu:~$ bg
T / Ebtisam ALselwi 21
Background process List stopped or background jobs, resume
a stopped job in the background
2-press ctrl +z
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
T / Ebtisam ALselwi 28
renice command Changes priority of an already running process
T / Ebtisam ALselwi 29
df command Gives free hard disk space on your system
std@ubuntu:~$ df
T / Ebtisam ALselwi 30
free command Gives free RAM on your system
std@ubuntu:~$ free
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
T / Ebtisam ALselwi 34
The steps
1-nano fork.cpp
2-g++ fork.cpp -o fork1.cpp
3-./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
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:~$ ./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