0% found this document useful (0 votes)
11 views18 pages

OS-Lab 5

The lab manual for the Operating Systems Lab at UET Taxila outlines a structured learning procedure focusing on process management in Linux and C++ programming. It covers essential topics such as managing processes, compiling and executing C++ programs, and various commands for process control. The manual includes practical activities to apply the learned concepts, guided by course instructor Dr. Sanay Muhammad and lab instructor Sheharyar Khan.

Uploaded by

nabihashahid47
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)
11 views18 pages

OS-Lab 5

The lab manual for the Operating Systems Lab at UET Taxila outlines a structured learning procedure focusing on process management in Linux and C++ programming. It covers essential topics such as managing processes, compiling and executing C++ programs, and various commands for process control. The manual includes practical activities to apply the learned concepts, guided by course instructor Dr. Sanay Muhammad and lab instructor Sheharyar Khan.

Uploaded by

nabihashahid47
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

LAB

MANUAL
Course: Operating Systems Lab

Department of Computer Engineering

Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
3) Stage v (Verify the accuracy)
4) Stage a2 (Assess your work)

Course Teacher: Dr. Sanay Muhammad


Lab Instructor: Sheharyar Khan

University of Engineering and Technology (UET TAXILA)


1

Created by Sheharyar Khan (sherykhan186@[Link])


LAB # 05

Statement of Purpose:
This lab will introduce the basic concepts related to process management through and
Writing C++ programs in Linux.

Activity Outcomes:
This lab teaches you the following topics:

1. How to manage processes in Linux.


2. Compiling and Executing C++ programs in Linux.

50

Created by Sheharyar Khan (sherykhan186@[Link])


1) Stage J (Journey)

1. Process management in Linux


A process is the instance of a computer program that is being executed. While a computer
program is a passive collection of instructions, a process is the actual execution of those
instructions. Several processes may be associated with the same program; for example,
opening up several instances of the same program often results in more than one process
being executed. When a system starts up, the kernel initiates a few of its own activities as
processes and launches a program called init. init, in turn, runs a series of shell scripts
(located in /etc) called init scripts, which start all the system services. Many of these services
are implemented as daemon programs, programs that just sit in the background and do
their thing without having any user interface. So even if we are not logged in, the system is
at least a little busy performing routine stuff.

The kernel maintains information about each process to help keep things organized. For
example, each process is assigned a number called a process ID or PID. PIDs are assigned in
ascending order, with init always getting PID 1. The kernel also keeps track of the memory
assigned to each process, as well as the processes' readiness to resume execution. Like files,
processes also have owners and user IDs, effective user IDs, etc. In the following, we will
discuss the most common commands; available in Linux to mange processes.

b. Displaying Processes in the System


1.1.1 Static view

The most commonly used command to view processes is ps. This command displays the
processes for the current shell. We can use the ps command as given below

We can display all of the processes owned by the current user by using the x option.

51

Created by Sheharyar Khan (sherykhan186@[Link])


Here, a new column is also added in the output that is STAT. This column shows the current
state of the process. The most common states are given below.

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
Following are the most common option that can be used 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

In the following example, we display processes that are related to the user ubuntu.

52

Created by Sheharyar Khan (sherykhan186@[Link])


Now, we select a process with id 1602

1.1.2 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.

In the following example we display the dynamic view of the system process and resource usage.

The output of the above command is given below

Some of the basic options available with top commands are given below

Options Description
-u display specific User process details
-d To set the screen refresh frequency

53

Created by Sheharyar Khan (sherykhan186@[Link])


1.1.3 Displaying processes in Treelike structure

The pstree command is used to display processes in tree-like structure showing the parent/child
relationships between processes.

1.2 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.

1.3 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.
In the following example, first we open the gedit program normally as given below.

54

Created by Sheharyar Khan (sherykhan186@[Link])


It can be noted that gedit is opened as foreground process and control does not returns to terminal
unless it is closed. Now, we start the gedit again as a background process.

It can be seen that after starting the gedit program control returns to the terminal and user can
interact with both terminal and gedit.

1.4 jobs command


The shell's job control facility also gives us a way to list the jobs that have been launched
from our terminal. Using the jobs command, we can see this list.
In the following example, we first launch two jobs in background and then use the jobs
command to view the running jobs.

1.5 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. In the
following example, we start the gedit editor in background. Then use the jobs command to
see the list of jobs launched and then, bring this process to the foreground.

1.6 Killing a process


55

Created by Sheharyar Khan (sherykhan186@[Link])


We can kill a process using the kill command. To kill a process, we provide the process id as an
argument (We could have also specified the process using a jobspec). In the following example, we
start the gedit program and then kill it using kill command.

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, in
turn, “listen” for signals and may act upon them as they are received. 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.

1.7 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.

We can paus a process by using kill command with STOP option. The process id is required
as an argument in kill command. In the following example, we start the gedit program in the
background. Then we find the pid of gedit using ps command and then; we pause the
process using kill command. Later, we can resume the process by using the kill command
with CONT option.

56

Created by Sheharyar Khan (sherykhan186@[Link])


1.8 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. In Linux system priorities are 0 to 139 in which 0 to 99 for real time and 100 to 139
for users. nice value range is -20 to +19 where -20 is highest, 0 default and +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.

In the following example, we start the gedit program in background and see its nice value using ps
command.

Now, we start the gedit program again using nice command.

57

Created by Sheharyar Khan (sherykhan186@[Link])


We can change the priority of a running process using renice command. In the following example,
we first start the gedit program in background. Then we change its priority using renice command.

We can also use the renice command to change the priority of all of the processes belonging to a
user or a group. In the following example, we change the nice value of all of the process belonging to
user ubuntu.

1.9 Few more useful commands related to processes


1.9.1 vmstat command
Outputs a snapshot of system resource usage including, memory, swap and disk I/O. To see a
continuous display, follow the command with a time delay (in seconds) for updates. For example:
vmstat 5.

58

Created by Sheharyar Khan (sherykhan186@[Link])


1.9.2 xload command
Displays the system load over time.

1.9.3 tload command


This command is similar to xload but displays output in the terminal.

2. Compiling and Executing C++ Programs


Compiling is the way toward making an interpretation of source code into the local language
of the PC's processor. The PC's processor works at an extremely basic level, executing
programs in what is called machine language. This is a numeric code that portrays extremely
little activities, for example, "include this byte," "point to this area in memory," or
"duplicate this byte”. Each of these instructions is expressed in binary which were hard to
write. This issue was overwhelmed by the appearance of assembly language, which
supplanted the numeric codes with (marginally) simpler to utilize character mnemonics, for
example, CPY (for duplicate) and MOV (for move). Programs written in assembly language
are processed into machine language by a program called an assembler.

We next come to what are called high-level languages. They are called this since they enable
the developer to be less worried about the details of what the processor is doing and more
with taking care of the current issue. Programs written in high-level programming languages
are converted into machine language by processing them with another program, called a
compiler.

a. C++ compiler for Linux


C++ is a general-purpose programming language and widely used nowadays for competitive
programming. It has imperative, object-oriented and generic programming features. C++ runs on lots
of platform like Windows, Linux, Unix, Mac etc. Before we start programming with C++. We will need
an environment to be set-up on our local computer to compile and run our C++ programs
successfully. GNU C++ Compiler (g++) is a compiler in Linux which is used to compile C++ programs.
It compiles both files with extension .c and .cpp as C++ files.

i. Installing g++ compiler


By default, g++ is provided with most of the Linux distributions. We can find the details of
installed g++ compiler by writing the following command:

59

Created by Sheharyar Khan (sherykhan186@[Link])


If g++ is not installed on your system; then it can be installed by writing the following commands

$ sudo apt-get update


$ sudo apt install g++

2.2 Compiling C++ program


We can compile a C++ program using the following command

$ sudo g++ [Link]

The above command will generate an executable file [Link]. We can use -o option to specify the
output file name for the executable.

$ sudo g++ [Link] -o executable-file

2.3 Running a C++ program


Onc a C++ program is successfully compiled, we can execute the created executable file by
using the following command:

$ ./executable-file

In the following example, we write a simple C++ program that displays a Hello World
message and the then compile and execute this program using the above commands. To
write the program, we use the gedit text editor. The source code file is saved with .cpp
extension.
Open the gedit editor and pass the name of the file ([Link]) to be created

Write the following code

60

Created by Sheharyar Khan (sherykhan186@[Link])


Now, close the source file and write the following command to compile [Link]

To execute the hello executable file, write the following command.

2.4 Passing command-line arguments to a C++ program


Command line argument is a parameter supplied to the program when it is invoked.
Command line argument is an important concept in C++ programming. It is mostly used
when you need to control your program from outside. Command line arguments are passed
to the main () method.
To pass command line arguments, we typically define main () with two arguments: first
argument counts the number of arguments on the command line and the second is a
pointer array which holds pointers of type char which points to the arguments passed to the
program. The syntax to define the main method is

int main (int argc, char *argv[])

61

Created by Sheharyar Khan (sherykhan186@[Link])


Here, argc variable will hold the number of arguments pass to the program while the argv
will contain pointers to those variables. argv[0] holds the name of the program while argv[1]
to argv[argc] hold the arguments. Command-line arguments are given after the name of the
program in command-line shell of Operating Systems. Each argument separated by a space.
If a space is included in the argument, then it is written in “”.
In the following example, we calculate the average of numbers; passed in the command-
line. Write the following code and save the file named as [Link]. Compile the [Link] and
create an executable file named avg and execute it. While writing the command to execute
avg pass the numbers whose average is required. It is shown below.

62

Created by Sheharyar Khan (sherykhan186@[Link])


2) Stage a1 (apply)

Lab Activities

Activity 1:
This activity is related to process monitoring in Linux
1. Display all of the process in current shell
2. Display every active process in the system
3. Provide a full-format listing of process owned by you
4. Display a full-format listing of processes owned by user ubuntu
5. Display a process with id 1
6. Display the dynamic view of the current processes in the system and set the refresh
interval 0.5 second
7. Display the dynamic view of the processes owned by user with id 999 (or name
ubuntu)
8. Start the gedit program in background and then bring it foreground
9. Suspend the gedit program
10. Resume the gedit program

Solution:

1.

2.

63

Created by Sheharyar Khan (sherykhan186@[Link])


3.

4.

5.

6.

7.

8.

9.

10.

Activity 2:
Perform the following tasks

64

Created by Sheharyar Khan (sherykhan186@[Link])


11. Start the gedit program with priority 90
12. Reset the priority of gedit to 65

Solution:
1.

2.

Activity 3:
Write a program in C++ that find the maximum and minimum number from an array.

Solution:
#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;
cout << "Enter the elements of the array : ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];

65

Created by Sheharyar Khan (sherykhan186@[Link])


}
cout << "Largest element : " << max;
cout << "Smallest element : " << min;
return 0;
}

Activity 4:
Change the above program such that it accepts the input at command-line.
Solution:
#include<iostream>
#include<cstdlib>
using namespace std;
int main (int a_c, char *arr[ ])
{
int arr[10], n, i, max, min,cout,num;
count=a_c;
max = atoi(arr[1]);
for (i = 0; i < n; i++)
{
num=atoi(arr[i]);
if (max <num)
max = num;
}
min = atoi(arr[1]);
for (i = 0; i < n; i++)
{
num=atoi(arr[i]);
if (min > num)
min = num;
}
cout << "Largest element : " << max;
cout << "Smallest element : " << min;
return 0;
}

66

Created by Sheharyar Khan (sherykhan186@[Link])

You might also like