0% found this document useful (0 votes)
46 views

Lab Manual 02

The document provides instructions for installing the G++ C++ compiler on Ubuntu, writing and running a basic "Hello World" C++ program, and describes the core input/output system calls - open(), read(), write(), and close(). It also lists two lab tasks: [1] writing a program to display the first 10 prime numbers, and [2] taking specifications for 5 people and writing them to a file using system calls.

Uploaded by

bazla khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Lab Manual 02

The document provides instructions for installing the G++ C++ compiler on Ubuntu, writing and running a basic "Hello World" C++ program, and describes the core input/output system calls - open(), read(), write(), and close(). It also lists two lab tasks: [1] writing a program to display the first 10 prime numbers, and [2] taking specifications for 5 people and writing them to a file using system calls.

Uploaded by

bazla khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operating System

Lab Manual 02

Topic: Run C++ Programs

Course Instructor: Mr. Fakhar Abbas


Lab Instructor: Ms. Romana Ali

Session: Spring 2022


School of Systems and Technology
UMT Lahore Pakistan

1
Install G++ the C++ Compiler on Ubuntu
The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++. Many
open-source projects, including the GNU tools and the Linux kernel, are compiled with GCC.
To able to add new repositories and install packages on your Ubuntu system, you must be logged
in as root or user with sudo privileges.

Installing G++ on Ubuntu

The default Ubuntu repositories contain a meta-package named build-essential that


contains the GCC compiler and a lot of libraries and other utilities required for compiling
software.
1. Start by updating the packages list:
$ sudo apt update

2. Install the build-essential package by typing:


$ sudo apt install build-essential

3. Check the version of g++ compiler by following command.


$ g++ --version

2
Basic C++ Program
let’s create hello world C++ program. Save the following code as hello.cpp text file and run it.
Perform the steps below.
1. Create a text file hello.cpp by following command and write simple code as shown in figure
$ nano hello.cpp

2. Close the editor and compile it by using

$ program-source-code.cpp -o executable-file-name

3. To run or execute the program use following command

$ ./executable-file-name

3
I/O System Call
The system call is a way for programs to interact with the operating system. When the program
makes a system call at that time it makes a request to the operating system's kernel.

There are 5 different categories of system calls:

1. Process Control
2. File Management
3. Device Management
4. Information Management
5. Communication

Here, we will discuss about the system calls for file management. There are four system calls for
file management.

1. open ()
2. read ()
3. write ()
4. close ()

1. open ()

open() system call is used to know the file descriptor of user-created files. Since read and write
use file descriptor as their 1st parameter so to know the file descriptor open() system call is used.

Syntax:

fd = open (file_name, mode, permission);


Example:
fd = open ("file", O_CREAT | O_RDWR, 0777);

Here,

 file_name is the name to the file to open.


 mode is used to define the file opening modes such as create, read, write modes.
 permission is used to define the file permissions.

Return value: Function returns the file descriptor.

4
2. read ()

read() system call is used to read the content from the file. It can also be used to read the input
from the keyboard by specifying the 0 as file descriptor (see in the program given below).

Syntax:

length = read(file_descriptor , buffer, max_len);


Example:
n = read(0, buff, 50);

Here,

 file_descriptor is the file descriptor of the file.


 buffer is the name of the buffer where data is to be stored.
 max_len is the number specifying the maximum amount of that data can be read

Return value: If successful read returns the number of bytes actually read.

3. write ()

write() system call is used to write the content to the file.

Syntax:

length = write(file_descriptor , buffer, len);


Example:
n = write(fd, "Hello world!", 12);

Here,

 file_descriptor is the file descriptor of the file.


 buffer is the name of the buffer to be stored.
 len is the length of the data to be written.

Return value: If successful write() returns the number of bytes actually written.

5
4. close ()

close() system call is used to close the opened file, it tells the operating system that you are done
with the file and close the file.

Syntax:

int close(int fd);

Here,

 fd is the file descriptor of the file to be closed.

Return value: If file closed successfully it returns 0, else it returns -1.

Example:

6
Lab Tasks

1. Write a basic C++ program which displays first 10 prime numbers.


2. Take 5 Persons specification (Name, Gender, Age, Height, Weight) and write them to the file using
system calls.

You might also like