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

System Programming

Uploaded by

adimj257
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)
12 views

System Programming

Uploaded by

adimj257
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/ 115

What is UNIX ?

• Unix is a Computer operating system that


– Sits between the hardware and the user/applications
– Manages resources
Course Name :
– provides high-level abstractions (e.g., files) and services (e.g., multiprogramming)
Systems Programming – provides Stability
– provides Multiuser capability
– provides Multitasking capability
– provides Communication
– provides Security
– Provides portability
– is Open source(most flavors)
5
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Systems Programming Little History of Unix


• The UNIX operating system was born in the late 1960s.
Introduction to Systems Programming
Text Book(s) • It originally began as a one man project led by Ken Thompson of Bell Labs, and has
since grown to become the most widely used operating system.
T1 Richard Blum, Christine Bresnahan, “Linux Command Line and
Shell Scripting Bible”, 3rd Edition, Wiley Publication, 2015. • Ken Thompson with ideas and support from Rudd Canaday,
T2 Maurice J. Bach, “The Design of the Unix Operating System”, PHI, Doug McIlroy, Joe Ossanna and Dennis Ritchie, wrote a small
1993 general-purpose time-sharing system PDP-7 as MULTICS
(MULTiplexed Information and Computing Service).
T3 Brian. W. Kernighan and Rob Pike, “The UNIX Programming
Environment”, Prentice – Hall of India, Pvt. Ltd • In the time since UNIX was first developed, it was named as
UNICS (UNiplexed Information and Computing Service), later
T4 Leland L. Beck “An Introduction to Systems Programming” 3rd changed to UNIX and it has gone through many different
edition. generations and even mutations.
6
2
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Course Title : Systems Programming Little History of Unix


Course No(s): SSWT ZC327 / CSIW ZC327
• Ritchie also designed and wrote a compiler for the C Programming language.
No Course Objective • In 1973, Ritchie and Thompson rewrote the UNIX Kernel in C, breaking the
tradition that system software is written in Assembly level language on PDP-11
CO1 To provide understanding of the files, directory and file
machine.
systems.
• Around 1974 it was licensed to Universities for “Educational purposes” and a few
CO2 To provide insight into UNIX commands and Shell years later became available for commercial purpose.
scripting. • In 1979, the “seventh edition” (V7) version of Unix was released, grandfather of all
To provide understanding of the System calls and its extant Unix systems.
CO3
usage with C programming. • After this point, the history of Unix becomes somewhat convoluted.

CO4 To provide understanding of assemblers, linkers and • Since then, it has spread world-wide, with tens of thousands of systems installed,
from Micro-Computers to Main-Frames.
loaders
7
3
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Agenda

What is an Operating System (OS)? • What is OS ?


Functions of an OS

UNIX Operating System

UNIX Architecture, Feature of UNIX,

A Brief History of UNIX, Command Structure and Usage

Basic UNIX commands – passwd, date, who, who am i,


mail, write, man, pwd, ls.
4 SS ZG516 -Computer Organization and Software Systems 8
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
What is OS ?

• An Operating system (OS) A program that acts as an intermediary between a


user of a computer and the computer hardware.
• Operating system goals:
– Execute user programs and make solving user problems easier.
– Make the computer system convenient to use.
• In simple terms, an operating system is a manager. It manages all the available Common Services of an OS ?
resources on a computer, from the CPU, to memory, to hard disk accesses.

• Tasks the operating system must perform:


– Control Hardware - The operating system controls all the parts of the computer and attempts
to get everything working together.
– Run Applications - Another job the OS does is run application software.
– Manage Data and Files - The OS makes it easy for you to organize your computer. Through
the OS you are able to do a number of things to data, including copy, move, delete, and
rename it.
9
<Course Code> 13
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Computer System Components Common Services of an OS

1. Hardware – provides basic computing resources (CPU, • Process Management


memory, I/O devices).
• Main Memory Management
2. Operating system – controls and coordinates the use of the
hardware among the various application programs for the • File Management
various users.
• Secondary Storage Management
3. Applications programs – define the ways in which the system
resources are used to solve the computing problems of the • I/O Subsystem Management
users (compilers, database systems, video games, business
programs).
4. Users (people, machines, other computers).

10

BITS Pilani, Pilani Campus •14 BITS Pilani, Pilani Campus

Abstract View of System Components Process Management

• A process is a program in execution. A process


needs certain resources, including CPU time,
memory, files, and I/O devices, to accomplish its task.

• The operating system is responsible for the following


activities in connection with process management.
– Process creation and deletion.
– process suspension and resumption.
– Provision of mechanisms for:
• process synchronization
• process communication
15
11
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Main-Memory Management
Operating System is Responsible
• Memory is a large array of words or bytes, each with its own
• Resource allocator – manages and allocates address. It is a repository of quickly accessible data shared
resources. by the CPU and I/O devices.
• Main memory is a volatile storage device. It loses its
• Control program – controls the execution of user contents in the case of system failure.
programs and operations of I/O devices .
• The operating system is responsible for the following
• Kernel – the one program running at all times (all activities in connections with memory management:
else being application programs). – Keep track of which parts of memory are currently being used and by
whom.
– Decide which processes to load when memory space becomes
available.
– Allocate and deallocate memory space as needed.
12 16
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
File Management Unix Architecture

•The main concept that unites all the versions of Unix is the following four basics
• A file is a collection of related information defined by its •Kernel − The kernel is the heart of the operating system, It interacts with the
hardware and most of the tasks like memory management, task scheduling and
creator. Commonly, files represent programs (both source file management.
and object forms) and data.
• The operating system is responsible for the following •Shell − The shell is the utility that processes your requests.
activities in connections with file management: When you type in a command at your terminal, the shell interprets the
command and calls the program that you want.
– File creation and deletion.
Ex: C Shell, Bourne Shell and Korn Shell are the most famous shells which are
– Directory creation and deletion. available with most of the Unix variants.
– Support of primitives for manipulating files and directories.
– Mapping files onto secondary storage. •Commands and Utilities − There are various commands and utilities which you
can make use of in your day to day activities.
Ex: cp, mv, cat and grep, etc. are few examples of commands and utilities.
•Files and Directories − All the data of Unix is organized into files.
17
•All files are then organized into directories. These directories are further
BITS Pilani, Pilani Campus organized into a tree-like structure called the filesystem. BITS Pilani, Pilani Campus

Unix Architecture
Secondary-Storage Management

• Since main memory (primary storage) is volatile and too


small to accommodate all data and programs
permanently, the computer system must provide
secondary storage to back up main memory.
• Most modern computer systems use disks as the
principle on-line storage medium, for both programs and
data.
• The operating system is responsible for the following
activities in connection with disk management:
– Free space management
– Storage allocation

18

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Unix Architecture
I/O Subsystem Management
•The block diagram of the UNIX OS
kernel shows various modules and their
relationships with each other
• Keeping track of status of the I/O devices •The system call and library interface
made the border between user
• Deciding who gets the device, for how long and programs and the kernel.
when •System calls look like ordinary function
calls in C programs.
• Allocating a device to a process and de-allocation •Libraries map these function calls to
the primitives needed to enter the
operating system.
•Assembly language programs may
invoke system calls directly without a
system call library.
•Programs frequently use other libraries
such as the standard I/O library to
19 provide a more sophisticated use of the
BITS Pilani, Pilani Campus
system calls. BITS Pilani, Pilani Campus

The UNIX Operating System Command Structure

A UNIX command is an action request given to the UNIX shell for execution.
• Simple and yet powerful
All UNIX commands apply an action or a series of actions to some input data and
• A multiuser, multitasking, portable operating create some output data.
All UNIX commands are case sensitive.
system
The command format is
• Interaction with the system via a command $ verb [options] [arguments]
interpreter – shell where
verb is the command name
options modify how the action is applied and
On the lighter side, arguments provide additional information to the command
UNIX is simple. But, you need to be a genius to understand
its simplicity! There are a few commands (like pwd) that don’t take any arguments,
some commands (like who, ls) that may or may not take any arguments and
some commands (like cut) that compulsorily take arguments.

20 24
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Internal and External Commands When things go wrong
Terminal settings directly impact keyboard operation, and you should know
The commands that are built-in as part of the shell are called internal commands. which keys to press when things don’t quite work as expected.
Example: echo command
Keystroke or Function
A program or file having an independent existence in the /bin directory (or /usr/bin), command
is called as an external command. [Ctrl-h] Erases text
Example: ls, wc, cal, bc, more … [Ctrl-c] or Delete Interrupts a command
[Ctrl-d] Terminates login session or a program that expects its input from
PATH variable: Contains the sequence of directories that the shell searches to look for keyboard
a command. [Ctrl-s] Stops scrolling of screen output and locks keyboard
$ echo $PATH [Ctrl-q] Resumes scrolling of screen output and unlocks keyboard
/bin:/usr/bin:/usr/local/bin:/usr/local/java/bin:. [Ctrl-u] Kills command line without executing it
[Ctrl-\] Kills running program but creates a core file containing the memory
When you issue a command, image of the program
The shell sees if it is a built-in command. If so, it executes it directly. [Ctrl-z] Suspends process and returns shell prompt; use fg to resume job
If not a built-in, it searches the associated file in the directories specified in PATH. If [Ctrl-j] Alternative to [Enter]
found, it executes the file. [[Ctrl-m] Alternative to [Enter]
Otherwise, it triggers a error message. stty sane Restores terminal to normal status
25 29
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

UNIX files and directories


Flexibility of Command Usage
•All data in Unix is organized into files.
•All files are organized into directories.
A command can often be entered in more than one way.
•These directories are organized into a tree-like structure called the
If used judiciously, you can restrict the number of keystrokes to a minimum.
filesystem.
Examples: •In Unix, there are three basic types of files −
Combining Commands
$ wc chap1 ; ls –l chap1 •Ordinary Files − An ordinary file is a file on the system that contains
Command line that overflows data, text, or program instructions. In this tutorial, you look at working with
$ echo “This is ordinary files.
> a three line
> text message”
•Directories − Directories store both special and ordinary files.
UNIX lets you type commands without waiting for a prompt; The commands
are passed onto the shell for interpretation after the previous command has •For users familiar with Windows or Mac OS, Unix directories are
completed. equivalent to folders.

•Special Files − Some special files provide access to hardware such as


26 hard drives, CD-ROM drives, modems, and Ethernet adapters.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

man pages Directories and Files

• A file is a set of data that has a name. The information can


UNIX offers an online help facility in the man command. be an ordinary text, a user-written computer program, results
man displays the documentation of the specified command. of a computation, a picture, and so on.
Example:
$ man wc displays help on wc command • The file name may consist of ordinary characters, digits and
special tokens like the underscore, except the forward slash
man uses a pager program, which displays this documentation one (/). It is permitted to use special tokens like the ampersand
page at a time (&) in a filename.
man is configured to be used with a specific pager.
Two available pagers are: • Unix organizes files in a tree-like hierarchical structure, with
more, a Berkeley pager, as an alternative to the AT&T pg command the root directory, indicated
less, the standard pager on Linux systems, also available on UNIX. It is
modeled after vi editor and is more powerful than more..
• by a forward slash (/), at the top of the tree. See the Figure
27 below, in which part of the hierarchy of files and directories 31
BITS Pilani, Pilani Campus
on the computer is shown. BITS Pilani, Pilani Campus

UNIX files and directories


A sample man Page
User Commands wc(1)
NAME
wc – displays a count of lines, words and characters in a file
SYNOPSIS
wc [-c | -m | -C] [-lw] [file ...]
DESCRIPTION
The wc utility reads one or more input files and, by default, writes the number of
newline characters, words and bytes contained in each input file to the
standard output. The utility also writes a total count for all named files, if
more than one input file is specified.
OPTIONS
The following options are supported:
-c Count bytes.
-m Count characters.
-C same as –m.
-l Count lines.
-w Count words delimited by white spaces or new line characters ...
OPERANDS
The following operand is supported:
file A path name of an input file. If no file operands are specified,
the standard input will be used.
EXIT STATUS
See largefile(5) for the description of the behavior of wc when encountering files
greater than or equal to 2 Gbyte (2 **31 bytes)
SEE ALSO
cksum(1), isspace(3C), iswalpha(3C), iswspace(3C), largefile(5), ... 28
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Regular file • date - displays current date and time
$date +%m
• It may be text or binary file $date +%h
• Both the files are executable provided execution rights $date +”%m %h”
are set
• They can be read or written by users with appropriate Other format specifiers:
permissions – d-the day of month
• To remove regular files use rm command – y-the last two digits of the year
– H,M,S – Hr, Min & Sec respectively,
– D-date in format mm/dd/yy
– T-time in format hh:mm:ss

33 37
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Directory file

• Folder that contains other files and • Echo - displays message on screen of
subdirectories issue prompt for input.
• Provides a means to organize files in Usage
hierarchical structure $echo GNU/Linux
• To create : mkdir command $echo $SHELL $PATH
• To remove : rmdir command $echo “Enter input value: \c”
Enter input value:$

34 38
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Some commands have large number of option. Don't


panic. Escape sequences in echo and printf

Initiallly keep in mind : • Escape sequence significance


\a Bell
• where to give whitespaces. ?
\b Backspace
– Between command and options \c No new line
– Between command and arguemts \f Formfeed
• How options should be used? \n New Line
\r Carriage Return
Exceptions : \t Tab
• Some commands do not take any arguments/ \v Vertical tab
options (pwd, whoami)‫‏‬ \\ Backslash
\0n ASCII char represented by the
• Some commands may or may not take arguments/ octal value n
options (ls, who, uname)‫‏‬ $ echo ‘\07’ beep
• Some command compulsorily need options (cut)‫‏‬ 35 39
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Basic Commands
• banner- prints a banner
• In Linux bash shell interprets escape sequences
– Usage - $ banner Wipro
only with the –e option.
• cal- calendar of the month. • Bash has –n option as substitute for \c
– Syntax – cal [ [ month] year ] • Korn shell doesn’t use –n.
• Takes 2 arguments
• Echo’s behaviour varies across shells so POSIX
• Month (2 args) and year (1 arg)‫‏‬
recommends printf as a replacement of echo.
– Usage - $cal 9 2009
– $cal 2010

36 40
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
passwd – changing your password
• Passwd – note the spelling
$who –H
• user can change password only for his/her
account. -H gives the header option
• Superuser can change the password of every $who –Hu
NAME LINE TIME IDLE PID COMMENTS
user using the same command.
root console Sep 19 12:50 0:48 11041 (:0)
• The command expects you to respond three Krishna pts/5 Sep 19 12:50 0:33 11090 (sys1.lab1.com)
times. Rama pts/2 Sep 19 13:01 . 13456 (sys5.lab2.com)
– Prompts for old password
$who am i
– validates the current password
– If correct prompts for new password
– Prompts for re-entering new password 41 45
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

uname – knowing your machine


• Incase you forget password contact the system Displays the operating system features of the current machine
administrator. $uname -- displays the name of the O/S
• When you enter a password, the string is Linux
encrypted by the system, which is used by UNIX $uname -r -- displays the release/version
system to authenticate the password. 2.6.32-21-generic -- kernel version 2.6
• The encryption is stored in a file named shadow $uname -s -- implementation name
in the /etc directory. Linux

• Super user can see the encrypted password but


The complete name of the O/S is the combination of the output of the
two options
cannot get back the original password. $uname -rs
Linux 2.6.3-21-generic
42 46
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

password framing rules

• Don’t choose a password similar to the old one. $uname -n -- displays system name
• Don’t use commonly used names like name of friend, Lab1sys2 -- which is a part of the
pets etc. A system may check with its own dictionary domain -- name
and throw out those password that are easily guessed. $hostname -- an alternate command
• Use a mixture of alphabetic and non alphabetic chars. Lab1sys2
• Enterprise UNIX won’t allow passwords that are fully • Hostname is very much essential to identify your
numeric or fully alphabetic. computer on the network.
• Don’t write the password in an easily accessible
• Hostname is a part of your machine's domain
document.
name(sys1.lab2.com).
43 47
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Who – who are the users bc – The calculator


• UNIX maintains an account of all users who are
• Unix has 2 types of calculator
logged on to the system.
• Graphical – xcalc
• $who – Available in x-window system, friendly
root console Sep 19 12:50 (:0)‫‏‬ • Text based – bc
Krishna pts/5 Sep 19 12:50 (lab1.sys1.com) – Less user friendly but extremely powerful.
Rama pts/2 Sep 19 13:01 (lab2.sys5.com)‫‏‬ • bc is a pseudo-programming language featuring arrays,
Column 1 : Krishna - username conditionals and loops. It also comes with a library for
Column 2 : pts/2 device name of their respective terminal. These are performing scientific calculations.
actually the filenames associated with the terminals. Pts/2 indicates a • It can handle very very large numbers.
file named 2 in the pts directory. • If a computation results in a 900 digit numbers, bc will
Column 3 ,4, 5 – month, date, login time respectively. show each and every digit,
Column 6 : shows the machine name from where the user has
logged in 44 48
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
4. Using the cc and bcc option You can copy the emails to
Bc belongs to a class of filters that expects input more number of users by using the -c and -b options. An
from the keyboard when used without arguments. example is shown below:

• $bc mail -s "Mail subject" -c "[email protected]" -b


12 + 12 "[email protected]" "[email protected]"
< body.txt
24
[Ctrl+d] the eof character 5. Specifying the from address So far the above examples
send the emails with from address as the logged in user. You
can explicitly specify the from-address using the -r option.
bc shows output/result in next line.
cat body.txt | mail -s "Mail subject" "[email protected]" -- -
49 r [email protected] 53
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

$bc
12*12 ; 2^32 multiple calculations
144
4294967296

12/5
2 Decimal portion truncated

scale =2 truncated to 2 decimal places


32/3
10.66 Not rounded off, result is actually 10.66666
50
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Mail Command Examples in Unix

The Mail command in unix or linux system is used to send emails to the
users, to read the received emails, to delete the emails etc.
• The syntax of mail command is:
• mail [options] to-address [-- sendmail-options]
• 1. Sending sample email to user

The basic functionality of the mail command in unix or linux system is to System Pro
send an email to the user.

echo "Mail body" | mail -s "Mail subject" [email protected]



Here the echo statement is used for specifying the body of the email. The -
s option is used for specifying the mail subject. The mail command sends
the email to the user [email protected]
51
BITS Pilani, Pilani Campus

2. Specifying the body in a file


IMP Note to Self
You want to compose a mail which contains 100 lines in the body. Specifying
the body with the echo statement is a tedious process. So write the contents
of the body in a file and send the mail using one of the following options:

Using cat statement:

cat body.txt | mail -s "Mail subject" [email protected]

Using input redirection operator

mail -s "Mail subject" [email protected] < body.txt

Here the body.txt file contains the body of the email.

3. Send mail to more than one user You can send email to more than one user
by specifying the users in comma separated list.

mail -s "Mail subject" "[email protected], [email protected]" < body.txt


52
BITS Pilani, Pilani Campus
IMP Note to Students What is Unix ?
• The process control subsystem

 It is important to know that just login to the session does


• The not
process control subsystem is responsible for process synchronization, interpro
memory management, and process scheduling.
guarantee the attendance. • The file subsystem and the process control subsystem interact when loading a file
execution.
 Once you join the session, continue till the end to consider you
• Some of the system calls for controlling processes are fork (create a new process),
as present in the class.
• exec (overlay the image of a program onto the running process),
 IMPORTANTLY, you need to make the class more interactive
• exit(finish
by executing a process), wait (synchronize process execution with the exit o
process),
responding to Professors queries in the session. • brk (control the size of memory allocated to a process), and
• signal (control process response to extraordinary events).
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT • The memory management module controls the allocation of memory.
• The kernel moves them between main memory and secondary memory so that all
chance to execute.3
Systems Programming BITS Pilani, Pilani Campus

Unix History What is Unix ?


• Memory swapping and demand paging

• The swapper process is sometimes called the scheduler because it "schedu


memory for processes and influences the operation of the CPU scheduler.

• The scheduler module allocates the CPU to processes.

• It schedules them to run in turn until they voluntarily relinquish the CPU w
or until the kernel preempts them when their recent run time exceeds a tim

• The scheduler then chooses the highest priority eligible process to run; the
again when it is the highest priority eligible process available

Unix Architecture
The UNIX Operating System
• The main concept that unites all the versions of Unix is the following four basics −
• Kernel − The kernel is the heart of the operating system.

• fileSimple
• It interacts with the hardware and most of the tasks like memory management, task scheduling and management.
and yet powerful
• Shell − The shell is the utility that processes your requests.
• A multiuser, multitasking, portable operating system
• • Interaction
When you type in a command at your terminal, the shell interprets the command and calls the program that you want. with the system via a command interpret
• C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants.

• Commands and Utilities − There are various commands and utilities which you can make use of in your day to day
activities. On the lighter side,
• cp, mv, cat and grep, etc. are few examples of commands and utilities. UNIX is simple. But, you need to be a genius to understa
its simplicity!
• Files and Directories − All the data of Unix is organized into files.
• All files are then organized into directories. These directories are further organized into a tree-like structure called the
filesystem.

What is Unix ? Unix Architecture


• The file subsystem
• The file subsystem manages files, allocating file space, administering free space, controlling access
to files, and retrieving data for users.

• Processes interact with the file subsystem via a specific set of system calls, such as
• open (to open a file for reading or writing), close, read, write,
• stat (query the attributes of a file),
• chown (change the record of who owns the file), and
• chmod (change the access permissions of a file).

• Device drivers are the kernel modules that control the operation of peripheral devices.
Unix Architecture man pages
•The main concept that unites all the versions of Unix is the following four basics
•Kernel − The kernel is the heart of the operating system, It interacts with the hardware and most of the
UNIX offers an online help facility in the man command.
tasks like memory management, task scheduling and file management. man displays the documentation of the specified command.

•Shell − The shell is the utility that processes your requests. Example:
$ man wc displays help on wc command
When you type in a command at your terminal, the shell interprets the command and calls the
program that you want.
man uses a pager program, which displays this documentati
Ex: C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the at a time
page
Unix variants.
man is configured to be used with a specific pager.
•Commands and Utilities − There are various commands and utilities which you can make use of in your
Two available pagers are:
day to day activities. more, a Berkeley pager, as an alternative to the AT&T pg comma
Ex: cp, mv, cat and grep, etc. are few examples of commands and utilities. less, the standard pager on Linux systems, also available on UN
•Files and Directories − All the data of Unix is organized into files. modeled after vi editor and is more powerful than more..
•All files are then organized into directories. These directories are further organized into a tree-like
structure called the filesystem.
BITS Pilani, Pilani Campus

Unix Architecture
A sample man Page
•The block diagram of the UNIX OS kernel shows various
modules and their relationships with each other User Commands wc(1)
•The system call and library interface made the border NAME
wc – displays a count of lines, words and characters in a file
between user programs and the kernel. SYNOPSIS
wc [-c | -m | -C] [-lw] [file ...]
•System calls look like ordinary function calls in C DESCRIPTION
The wc utility reads one or more input files and, by default, writes the
programs. newline characters, words and bytes contained in each input file
•Libraries map these function calls to the primitives standard output. The utility also writes a total count for all named
more than one input file is specified.
needed to enter the operating system. OPTIONS
The following options are supported:
•Assembly language programs may invoke system calls -c Count bytes.
directly without a system call library. -m Count characters.
-C same as –m.
•Programs frequently use other libraries such as the -l Count lines.
-w Count words delimited by white spaces or new line characters ...
standard I/O library to provide a more sophisticated use of OPERANDS
the system calls. The following operand is supported:
file A path name of an input file. If no file operands are specified,
the standard input will be used.
EXIT STATUS
See largefile(5) for the description of the behavior of wc when encounter
greater than or equal to 2 Gbyte (2 **31 bytes)
SEE ALSO
cksum(1), isspace(3C), iswalpha(3C), iswspace(3C), largefile(5), ...
BITS Pilani, Pilani Campus

Command Structure When things go wrong


Terminal settings directly impact keyboard operation, and you shou
A UNIX command is an action request given to the UNIX shell for execution.
which keys to press when things don’t quite work as expected.
All UNIX commands apply an action or a series of actions to some input data and create some output
data. Keystroke or Function
command
All UNIX commands are case sensitive. [Ctrl-h] Erases text
The command format is [Ctrl-c] or Delete Interrupts a command
$ verb [options] [arguments] [Ctrl-d] Terminates login session or a program that expects its i
where keyboard
verb is the command name [Ctrl-s] Stops scrolling of screen output and locks keyboard
options modify how the action is applied and [Ctrl-q] Resumes scrolling of screen output and unlocks keyboard
arguments provide additional information to the command [Ctrl-u] Kills command line without executing it
[Ctrl-\] Kills running program but creates a core file containing th
image of the program
There are a few commands (like pwd) that don’t take any arguments, [Ctrl-z] Suspends process and returns shell prompt; use fg to resum
some commands (like who, ls) that may or may not take any arguments and [Ctrl-j] Alternative to [Enter]
some commands (like cut) that compulsorily take arguments. [[Ctrl-m] Alternative to [Enter]
stty sane Restores terminal to normal status
13
BITS Pilani, Pilani Campus

Internal and External Commands Shell Commands of UNIX

The commands that are built-in as part of the shell are called internal commands. • Unix Commands
Example: echo command
• When you first log into a unix system, you are prese
A program or file having an independent existence in the /bin directory (or /usr/bin), is called
something that looks like the following:
as an external command.
Example: ls, wc, cal, bc, more …
/home/jaya#
PATH variable: Contains the sequence of directories that the shell searches to look for a
command.
• That “something” is called a prompt. As its name would sug
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/local/java/bin:.
prompting you to enter a command.
When you issue a command,
The shell sees if it is a built-in command. If so, it executes it directly. • Every unix command is a sequence of letters, numb
If not a built-in, it searches the associated file in the directories specified in PATH. If found, it
executes the file. characters. But there are no spaces.
Otherwise, it triggers a error message.
14 18

BITS Pilani, Pilani Campus


Unix Commands Unix - Basics
• Change Password
• Unix is also case-sensitive. This means that cat and Cat are different
• All Unix systems require passwords to help ensure that your files and data remain
commands. system itself is secure from hackers and crackers.
• Following are the steps to change your password −
• The prompt is displayed by a special program called the• Step 1 − To start, type password at the command prompt as shown below.
shell.
• Step 2 − Enter your old password, the one you're currently using.
• Step 3 − Type in your new password.
• Shells accept commands, and run those commands. • Step 4 − You must verify the password by typing it again.

• They can also be programmed in their own language. These


• $ passwd
programs
are called “shell scripts”. • Changing password for krishna
• (current) Unix password:******
• New UNIX password:*******
• Retype new UNIX password:*******
• passwd: all authentication tokens updated successfully
19

BITS Pilani

Unix Commands
Unix - Basics
• Who Are You?
• While you're logged into the system, you might be willing to know : Who am I?
 There are two major types of shells in unix: • $ whoami
 Bourne shells • krishna
 C shells.
• Who is Logged in?
 Steven Bourne wrote the original unix shell sh,and most shells
• Sometime since
you might be interested to know who is logged in to the computer at the sa
then end in the letters sh to indicate they are extentions on are
• There thethree
original
commands available to get you this information, based on how much
idea. other users:
• users, who, and w.
• $ users
 Linux comes with a Bourne shell called bash written by the Free
• krishna bablu qadir
Software Foundation.
• $ who
 Bash stands for Bourne Again Shell and is the default• krishna
shellttyp0toOctuse
8 14:10 (limbo)
running linux • bablu ttyp2 Oct 4 09:08 (calliope)
• qadir ttyp4 Oct 8 12:09 (dent)
20

BITS Pilani

Unix Commands UNIX files and directories


• All data in Unix is organized into files.
• All files are organized into directories.
The man command displays reference pages for the command you specify.
• These directories are organized into a tree-like structure called the file syst
• In available.
The UNIX man pages (man is short for manual ) cover every command Unix, there are three basic types of files −

To search for a man page, enter man followed by the name of the command to find .
• Ordinary Files − An ordinary file is a file on the system that contains data, te
instructions. In this tutorial, you look at working with ordinary files.
For example:

• Directories − Directories store both special and ordinary files.


$ man ls • For users familiar with Windows or Mac OS, Unix directories are equivalent

• Special Files − Some special files provide access to hardware such as hard d
21 modems, and Ethernet adapters.
BITS Pilani

Unix - Basics UNIX files and directories


• Login Unix
• When you first connect to a Unix system, you usually see a prompt such as the following −
• login:
• To log in
• Type your userid at the login prompt, then press ENTER.
• Your userid is case-sensitive, so be sure you type it exactly as your system administrator has instructed.

• Type your password at the password prompt, then press ENTER.


• Your password is also case-sensitive.

• login : krishna
• krishna's password:
• Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
• $
UNIX files and directories UNIX files and directories
• Listing Files • Creating Files:
• To list the files and directories stored in the current directory, use the following
• Youcommand − vi editor to create ordinary files on any Unix system.
can use the
• $ ls • $ vi filename
• bin hosts lib res.03
• The above command will open a file with the given filename.
• ch07 hw1 pub test_results
• ch07.bak hw2 res.01 users • Now, press the key i to come into the edit mode.
• docs hw3 res.02 work • Once you are in the edit mode, you can start writing your content in the file as in t
• This is about
• The command ls supports the -l option which would help you to get more information unix file....I
thecreated it for the first time.....
listed files − • I'm going to save this content in this file.
• $ ls -l • Once you are done with the program, follow these steps −
• drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml • Press the key esc to come out of the edit mode.
• -rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg • Press two keys Shift + ZZ together / wq to come out of the file completely.
• drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
• drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia • Display Contents of a File
• -rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar
• $ cat filename

UNIX files and directories UNIX files and directories


• Listing Files • Counting Words in a File
• $ ls – • You can use the wc command to get a count of the total number of lines, words, a
a file. Following is a simple example to see the information about the file created a
• $ wc filename
• Prefix & Description
• 2 19 103 filename
• - Regular file, such as an ASCII text file, binary executable, or hard link. • Here is the detail of all the four columns −
• -b • First Column − Represents the total number of lines in the file.
- Block special file. Block input/output device file such as a physical hard drive.
• -c - Character special file. Raw input/output device file such as a physical hard •drive.
Second Column − Represents the total number of words in the file.
• Third Column − Represents the total number of bytes in the file. This is the actual size
• -d - Directory file that contains a listing of other files and directories. • Fourth Column − Represents the file name.
• - l - Symbolic link file. Links on any regular file
• - p - Named pipe. A mechanism for interprocess communications. • You can give multiple files and get information about those files at a time. Followin
• - s - Socket used for interprocess communication.
• $ wc filename1 filename2 filename3

UNIX files and directories UNIX files and directories


• Metacharacters • Copying Files
• To make a copy of a file use the cp command. The basic syntax of the command is −
• Metacharacters have a special meaning in Unix. • $ cp source_file destination_file
• For example, * and ? are metacharacters. • $ cp filename copyfile

• We use * to match 0 or more characters, a question mark (?) matches with a single character.
• Renaming Files
• $ ls ch*.doc • To change the name of a file, use the mv command. Following is the basic syntax −
• Displays all the files, the names of which start with ch and end with .doc − • $ mv old_file new_file
• The mv command will move the existing file completely into the new file.
• ch01-1.doc ch010.doc ch02.doc ch03-2.doc
• ch04-1.doc ch040.doc ch05.doc ch06-2.doc • Deleting Files
• ch01-2.doc ch02-1.doc c • To delete an existing file, use the rm command. Following is the basic syntax −
• $ rm filename
• Here, * works as meta character which matches with any character. • Caution − A file may contain useful information. It is always recommended to be careful while u
• If you want to display all the files ending with just .doc, then you can use the• following
It is better tocommand − along with rm command. It will ask for a confirmation before dele
use the -i option
• You can remove multiple files at a time with the command given below −
• $ls *.doc
• $ rm filename1 filename2 filename3

UNIX files and directories What’s in the File?


• Hidden Files
• An invisible file is one, the first character of which is the dot or the period character (.).
There are two major commands used in unix for listing files, cat, a
• Unix programs (including the shell) use most of these files to store configuration information.
• Some common examples of the hidden files include the files − cat
• .profile − The Bourne shell ( sh) initialization script
• .kshrc − The Korn shell ( ksh) initialization script
• .cshrc − The C shell ( csh) initialization script • cat shows the contents of the file.
• .rhosts − The remote shell configuration file cat [-nA] [file1 file2 . . . fileN]
• To list the invisible files, specify the -a
• $ ls -a
• . .profile docs lib test_results • cat is not a user friendly command-it doesn’t wait for you to read
• .. .rhosts hosts pub users mostly used in conjuction with pipes.
• .emacs bin hw1 res.01 work
• .exrc ch07 hw2 res.02
• .kshrc ch07.bak hw3 res.03
• However, cat does have some useful command-line options.
• Single dot (.) − This represents the current directory.
• Double dot (..) − This represents the parent directory. • For instance, n will number all the lines in the file, and A will show
34
Unix / Linux - Directory Management
• more
• A directory is a file the solo job of which is to store the file names and the related
• more is much more useful, and is the command that you’ll want to use
• All when
the files,browsing
whether ordinary, special, or directory, are contained in directories.
ASCII text files • Unix uses a hierarchical structure for organizing files and directories.
more [-l] [+linenumber}] [file1 file2 ... fileN] • This structure is often referred to as a directory tree.
• The tree has a single root node, the slash character (/), and all other directories ar
• The only interesting option is l, which will tell more that you aren't interested in treating
the character Ctrl-L} as a ``new page'' character. more will start on •a Home
specified
Directory
linenumber. • The directory in which you find yourself when you first login is called your home d
• head • You will be doing much of your work in your home directory and subdirectories th
organize your files.
head will display the first ten lines in the listed files. • You can go in your home directory anytime using the following command −
head [- lines}] [l file1 file2 ... fileN]
• Any numeric option will be taken as the number of lines to print, so
• $cd
head
~ -15 frog will
print the first fifteen lines of the file frog • Here ~ indicates the home directory
35

BITS Pilani

Unix / Linux - Directory Management


• tail
• Absolute/Relative Pathnames

• Like head, tail display only a fraction of the file. Directories are arranged in a hierarchy with root (/) at the top.
• The position of any file within the hierarchy is described by its pathname.
• tail also accepts an option specifying the number of lines. • Elements of a pathname are separated by a /.
tail [-lines] [l file1 file2 ... fileN] • A pathname is absolute, if it is described in relation to root, thus absolute pathnames always begin w
• Following are some examples of absolute filenames.
• /etc/passwd
• /users/sjones/chem/notes
• file • /dev/rdsk/Os3
• A pathname can also be relative to your current working directory.
• file command attempts to identify what format a particular file is • Relativein.
written pathnames never begin with /.
• Relative to user krishna's home directory, some pathnames might look like this −
• chem/notes
file [file1 file2 ... fileN] • personal/res
• Since not all files have extentions or other easy to identify marks, the file command
• To determine
performs some rudimentary checks to try and figure out exactly what it where you are within the filesystem hierarchy at any time, enter the command pwd to p
contains.
directory −
• $pwd
36
• /user0/home/krishna

BITS Pilani

Unix / Linux - Directory Management


• grep • Listing Directories
• grep is the generalized regular expression parser. • To list the files in a directory, you can use the following syntax −
• $ls dirname (e.g. $ls demo)
• This is a fancy name for a utility which can only search a text file.
• • Creating Directories
grep [-nvwx] [-number] { expression} [file1 file2 ... fileN] • We will now understand how to create directories. Directories are created by the f
• $mkdir dirname

• Creates the directory mydir in the current directory. Here is another example −
• $mkdir /tmp/test-dir
• This command creates the directory test-dir in the /tmp directory. The mkdir comm
if it successfully creates the requested directory.
• If you give more than one directory on the command line, mkdir creates each of th
example, −
• $mkdir docs pub
37
• Creates the directories docs and pub under the current directory.
BITS Pilani

UNIX files and directories Unix / Linux - Directory Management


• Standard Unix Streams • Removing Directories
• Directories can be deleted using the rmdir command as follows −
• Under normal circumstances, every Unix program has three streams opened• for it when it starts up
$rmdir dirname
• stdin − This is referred to as the standard input and the associated file descriptor
• Note is 0.remove a directory, make sure it is empty which means there should not be any file or sub-
− To
• This is also represented as STDIN. • You can remove multiple directories at a time as follows −
• $rmdir dirname1 dirname2 dirname3
• The Unix program will read the default input from STDIN. • The above command removes the directories dirname1, dirname2, and dirname3, if they are empty.
output if it is successful.

• stdout − This is referred to as the standard output and the associated file descriptor is 1.
• Changing Directories
• This is also represented as STDOUT. • You can use the cd command to do more than just change to a home directory. You can use it to chan
a valid absolute or relative path. The syntax is as given below −
• The Unix program will write the default output at STDOUT • $cd dirname
• Here, dirname is the name of the directory that you want to change to. For example, the command −
• stderr − This is referred to as the standard error and the associated file • $cd /usr/local/bin
descriptor is 2.
• Changes to the directory /usr/local/bin.
• This is also represented as STDERR. • From this directory, you can cd to the directory /usr/home/amrood using the following relative path −
• The Unix program will write all the error messages at STDERR. • $cd ../../home/amrood
Unix / Linux - Directory Management Unix / Linux - File Permission / Access Mod
• Renaming Directories • Changing Permissions
• The mv (move) command can also be used to rename a directory. The syntax is as follows • $chmod − o+wx testfile
• $mv olddir newdir • $ls -l testfile
• You can rename a directory mydir to yourdir as follows − • -rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
• $mv mydir yourdir • $chmod u-x testfile
• The directories . (dot) and .. (dot dot) • $ls -l testfile
• -rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
• The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the
directory one level above the current working directory, often referred to as the parent • $chmod g = rx testfile
directory.
• If we enter the command to show a listing of the current working directories/files and • $lsuse
-l testfile
the -a option to
list all the files and the -l option to provide the long listing, we will receive the following• result.
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
• $ls -la
• drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 . • Here's how you can combine these commands on a single line −
• drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
• ---------- 1 teacher class 4210 May 1 08:27 .profile • $chmod o+wx,u-x,g = rx testfile
• -rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo • $ls -l testfile
• -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Mod
• File ownership is an important component of Unix that provides a secure method for storing files. • Using chmod with Absolute Permissions
• Every file in Unix has the following attributes −
• Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file.
• The second way to modify permissions with the chmod command is to use
each set of permissions for the file.
• Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the
file. • Each permission is assigned a value, as the following table shows, and the t
• Other (world) permissions − The permissions for others indicate what action all other users can perform on the file. permissions provides a number for that set.
• The Permission Indicators
• While using ls -l command, it displays various information related to file permission as follows −
• $ls -l /home/amrood
• Number Octal Permission Representation Ref
• -rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile • 0 No permission ---
• drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
• 1 Execute permission --x
• Here, the first column represents different access modes, i.e., the permission associated with a file or a directory. • 2 Write permission -w-

• 3
The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute
(x) − Execute and write permission: 1 (execute) + 2 (write) = 3 -wx
• The first three characters (2-4) represent the permissions for the file's owner. • 4 Read permission r--
• For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.
• The second group of three characters (5-7) consists of the permissions for the group to which the file belongs.
• 5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
• For example, -rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write permission. • 6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
• The last group of three characters (8-10) represents the permissions for everyone else. • 7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx
• For example, -rwxr-xr-- represents that there is read (r) only permission

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Mod
• File Access Modes • Using chmod with Absolute Permissions
• Here's an example using the testfile.
• The permissions of a file are the first line of defense in the security of a Unix• system. The basic
Running ls -l on the testfile shows that the file's permissions are as follows −
building blocks of Unix permissions are the read, write, and execute permissions, which have been
described below − • $ls -l testfile
• -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
• Read - Grants the capability to read, i.e., view the contents of the file.
• Write - Grants the capability to modify, or remove the content of the file. • Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can se
• Execute - User with execute permissions can run a file as a program. • $ chmod 755 testfile
• $ls -l testfile
• -rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
• Directory Access Modes
• Directory access modes are listed and organized in the same manner as any• other $chmodfile. There are a
743 testfile
few differences that need to be mentioned − • $ls -l testfile

• Read - Access to a directory means that the user can read the contents. The• user-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
can look at the
filenames inside the directory.
• $chmod 043 testfile
• Write - Access means that the user can add or delete files from the directory.• $ls -l testfile
• Execute - Executing a directory doesn't really make sense, so think of this as• a----r---wx
traverse permission.
1 amrood users 1024 Nov 2 00:10 testfile
• A user must have execute access to the bin directory in order to execute the ls or the cd command.

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Mod
• Changing Permissions • Changing Owners and Groups
• To change the file or the directory permissions, you use the chmod (change mode) command. • While creating an account on Unix, it assigns a owner ID and a group ID to each user. All th
• There are two ways to use chmod — the symbolic mode and the absolute mode. above are also assigned based on the Owner and the Groups.
• Two commands are available to change the owner and the group of files −
• Using chmod in Symbolic Mode • chown − The chown command stands for "change owner" and is used to change the owner
• The easiest way for a beginner to modify file or directory permissions is to use the symbolic•mode.
chgrp − The chgrp command stands for "change group" and is used to change the group of
• With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the
following table. • Changing Ownership
• The chown command changes the ownership of a file. The basic syntax is as follows −
• Chmod operator & Description • $ chown user filelist
• + Adds the designated permission(s) to a file or directory. • The value of the user can be either the name of a user on the system or the user id (uid) of
• - Removes the designated permission(s) from a file or directory. • The following example will help you understand the concept −
• = Sets the designated permission(s). • $ chown amrood testfile
• Changes the owner of the given file to the user amrood.
• Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions are as−follows
• NOTE − user, root, has the unrestricted capability to change the ownership of an
The super
• $ls -l testfile change the ownership of only those files that they own.
• -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Mod
• Changing Group Ownership • Setting the Terminal Type
• The chgrp command changes the group ownership of a file. • Usually, the type of terminal you are using is automatically configured by either the login or gett
• Sometimes, the auto configuration process guesses your terminal incorrectly.
• The basic syntax is as follows − • If your terminal is set incorrectly, the output of the commands might look strange, or you might
the shell properly.
• To make sure that this is not the case, most users set their terminal to the lowest common deno
• $ chgrp group filelist • $TERM=vt100
• The value of group can be the name of a group on the system or the group ID (GID) of a group
on the system. • Setting the PATH
• When you type any command on the command prompt, the shell has to locate the command b

• Following example helps you understand the concept − The PATH variable specifies the locations in which the shell should look for commands.
• Usually the Path variable is set as follows −
• $PATH=/bin:/usr/bin
• $ chgrp special testfile • Here, each of the individual entries separated by the colon character (:) are directories.

• Changes the group of the given file to special group. If you request the shell to execute a command and it cannot find it in any of the directories give
message similar to the following appears −
• $hello
• hello: not found

Unix / Linux - File Permission / Access Modes


• Unix / Linux - Environment
wc
• wc (word count) simply counts the number of words, lines, and cha
• An important Unix concept is the environment, which is defined by environment variables.
$ wc [-clw] [file1 file2 ... fileN]
• Some are set by the system, others by you, yet others by the shell, or any program that loads another program.
• The three parameters, clw, stand for character, line, and word res
• A variable is a character string to which we assign a value. tell wc which of the three to count.
• The value assigned could be a number, text, filename, device, or any other type of data.

• For example, first we set a variable TEST and then we access its value using the echo command −
spell
• $TEST="Unix Programming"
• $echo $TEST spell is very simple unix spelling program, usually for American Eng
• It produces the following result. spell is a filter, like most of the other programs we’ve talked about.
• Unix Programming
$ spell [file1 file2 ... fileN]
• Note that the environment variables are set without using the $ sign but while accessing them we use the $ sign as
prefix.
• These variables retain their values until we come out of the shell. 56

Unix / Linux - File Permission / Access Modes


cmp
• Unix / Linux - Environment
• When you log in to the system, the shell undergoes a phase called initialization to set up • thecmp
environment.
compares two files.
• This is usually a two-step process that involves the shell reading the following files −
• /etc/profile
• profile • The first must be listed on command line, while the sec
• The process is as follows − as the second parameter or is read in form standard inp
• The shell checks to see whether the file /etc/profile exists. •
• If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
• The shell checks to see whether the file .profile exists in your home directory.
• cmp is very simple, and merely tells you where the two
• Your home directory is the directory that you start out in after you log in. • $ cmp file1 [ file2]
• If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
• As soon as both of these files have been read, the shell displays a prompt −
• $
• This is the prompt where you can enter commands in order to have them executed.
• Note − The shell initialization process detailed here applies to all Bourne type shells, but some additional files are
used by bash and ksh.
57

Unix / Linux - File Permission / Access Modes


diff
• The .profile File
• The file /etc/profile is maintained by the system administrator of your Unix
• One
machine andmost complicated standard unix commands is called d
of the
contains shell initialization information required by all users on a system.
• The GNU version of diff has over twenty command line options.
• The file .profile is under your control.
• It is a much more powerful version of cmp and shows you what th
• You can add as much shell customization information as you want to this file.
instead of merely telling you where the first one is.
• The minimum set of information that you need to configure includes −
• The type of terminal you are using. • $diff file1 file2
• A list of directories in which to locate the commands.
• A list of variables affecting the look and feel of your terminal.
• You can check your .profile available in your home directory.
• Open it using the vi editor and check all the variables set for your environment.
58
tr pr command

• The “translate characters” command operates on standard input-it doesn’t accept a


filename as a parameter. • The -m option reads all files on the command line simultaneously
own column, like this:
• Instead, it’s two parameters are arbitrary strings.
• % pr -m -t file1 file2 file3
• It replaces all occurences of string1 in the input string2.
• The lines The lines The lines
• In addition to relatively simple commands such as tr frog toad, • of file1 of file2 of file3
• tr can accept more complicated commands. • are here are here are here
• ... ... ...
$tr string1 string2

59 63

BITS Pilani

ed command pr command

• ed is a line-oriented text editor. • An option that's a number will print a file in that number of columns.
• For instance, the -3 option prints a file in three columns.
• The file is read, line by line, until the first column is full (by default, that takes 5
• It is used to create, display, modify and otherwise manipulate text files.
• Next, the second column is filled.
• Then, the third column is filled.
• red is a restricted ed: it can only edit files in the current directory• and cannot
If there's execute
more of the file, the first column of page 2 is filled -- and the cycle rep
shell commands.
• % pr -3 file1
• If invoked with a file argument, then a copy of file is read into the editor’s
• Oct 1buffer.
19:44 2017 file1 Page 1

• Changes are made to this copy and not directly to file itself. • Line 1 here Line 57 here Line 115 here
• Line 2 here Line 58 here Line 116 here
• Line 3 here Line 59 here Line 117 here
• Upon quitting ed, any changes not explicitly saved with a ‘w’ command
• ...are lost. ... ...
60

BITS Pilani

ed command
pr command
 Editing is done in two distinct modes: command and input.  +n starts displaying with the nth page of each file. The default for n i
 When first invoked, ed is in command mode.  -n displays n columns of output. This is the obsolete equivalent of -c
 In this mode commands are read from the standard input and executed  -atoorders
manipulate the across the page on output, instead of down. Yo
input lines
contents of the editor buffer. option with -n.
 A typical command might look like:
 ,s/old/new/g  -c n displays n columns of output. When you specify this option,
you had also specified the
 which replaces all occurrences of the string old with new.  -e and -i options. When you specify both this option and
 When an input command, such as ‘a’ (append), ‘i’ (insert) or ‘c’ (change), is given, ed enters
input mode.  -t, pr uses the minimum number of lines possible to display the o
 This is the primary means of adding text to a file. specify this option with -m.
 In this mode, no commands are available; instead, the standard input is written directly to the
editor buffer.  -d produces double-spaced output.
61

BITS Pilani

pr command
pr command
• Lines consist of text up to and including a newline character.  -e[char][gap] expands each occurrence of the input tab characte
• Input mode is terminated by entering a single period (.) on a line. such that the following character has the next column position
multiple of gap, plus 1.
• pr command:
 -F uses form feeds to separate pages.
• The pr command is famous for printing a file neatly on a page -- with
pr normally separates
margins at top pages by sending a series of newline
and bottom, filename, date, and page numbers. length of a page.

 -f for
• It can also print text in columns: one file per column or many columns uses form
each feeds to separate pages.
file.
 When output is to a terminal, pr sounds the bell and waits for y
returnofbefore
• The -t option takes away the heading and margins at the top and bottom displaying the text.
each page.
 pr normally separates pages by sending a series of newline
That's useful when "pasting" data into columns with no interruptions.
length of a page.
62

BITS Pilani
which cal
• Displays a path name of a command. • Calendar • % cal current m
• Searches a path environmental variable for the command and • for month • % cal 2 2000 Feb 2000
displays the absolute path. • entire year • % cal 2 2100 not a lea
• % cal 2 2400
• To find which tcsh and bash are actually in use, type: • Years range: 1 - 9999
leap yea

% which tcsh • % cal 9 1752 11 days


• No year 0 • % cal 0 error
% which bash
• Calendar was corrected in • % cal 2002 whole year
• % man which for more details 1752 - removed 11 days

chsh clear
• Change Login Shell
• Login shell is the shell that interprets commands after you logged in • Clears the screen
by default. • There’s an alias for it: Ctrl+L
• You can change it with chsh (provided that your system admin • Example sequence:
allowed you to do so).
• % cal
• To list all possible shells, depending on implementation: • % clear
% chsh -l
% cat /etc/shells • % cal
• Ctrl+L
• % chsh with no arguments will prompt you for the shell.

whereis sleep
• Display all locations of a command (or some other binary, man page, or a
source file). • “Sleeping” is doing nothing for some time.
• Searchers all directories to find commands that match whereis’• Usually used for delays in shell scripts.
argument • % sleep 2 2 seconds pause
• % whereis tcsh

date Command Grouping


• Guess what :-) • Semicolon: “;”
• Displays dates in various formats • Often grouping acts as if it were a single command, so a
• % date different commands can be redirected to a file:
• % date -u • % (date; cal; date) > out.txt
• in GMT
• % man date
alias exit / logout
• Defined a new name for a command • Exit from your login session.
• % alias • % exit
• with no arguments lists currently active aliases • % logout
• % alias newcommand oldcommand
• defines a newcommand
• % alias cl cal 2003
• % cl

unalias shutdown
• Removes alias • Causes system to shutdown or reboot cleanly.
• Requires an argument. • May require superuser privileges
• % unalias cl • % shutdown -h now - stop
• % shutdown -r now - reboot

history
• Display a history of recently used • % !n
commands • repeat command n in the history
• % history • % !-1 Files
• all commands in the history • repeat last command = !!
• % history 10 • % !-2
• last 10 • repeat second last command
• % history -r 10 • % !ca
• reverse order • repeat last command that begins with
‘ca’
• % !!
• repeat last command

apropos ls

• Search man pages for a • % apropos date • List directory contents • % ls -F


• append “/” to dirs an
substring. • % man -k date • Has whole bunch of options, see man executables
• % apropos word ls for details.
• % apropos password • % ls -l
• % ls • long format
• Equivalent: • all files except those starting with a “.” • % ls -al
• % man -k word • % ls -a • % ls -lt
• all • sort by modification t
earliest)
• % ls -A
• all without “.” and “..” • % ls -ltr
• reverse
cat mv
• Display and concatenate files. • Moves or renames files/directories.
• % cat • % mv <source> <destination>
• Will read from STDIN and print to STDOT every line you enter.
• The <source> gets removed
• % cat file1 [file2] ...
• Will concatenate all files in one and print them to STDOUT
• % mv file1 dir/
• % cat > filename • % mv file1 file2
• Will take whatever you type from STDIN and will put it into the file filename • rename
• % mv file1 file2 dir/
• To exit cat or cat > filename type Ctrl+D to indicate EOF (End of File).
• % mv dir1 dir2

less rm
• less ("less is more") a bit more smart than the more command
• Removes file(s) and/or directories.
• to display contents of a file:
• % less filename • % rm file1 [file2] ...
• To display line numbers: • % rm -r dir1 [dir2] ...
• % less -N filename
• To display a prompt: • % rm -r file1 dir1 dir2 file4 ...
• % less -P"Press 'q' to quit" filename
• Combine the two:
• % less -NP"Blah-blah-blah" filename
• For more information:
• % man less

touch script
• By touching a file you either create it if it did not exists (with 0• Writes a log (a typescript) of whatever happened in the
length). file.
• Or you update it’s last modification and access times. • % script [file]
• There are options to override the default behavior. • % script
• % touch file • all log is saved into a file named typescript
• % man touch • % script file
• all log is saved into a file named file
• To exit logging, type:
• % exit

cp find
• Looks up a file in a directory tree.
• Copies files / directories.
• % find . -name name
• % cp [options] <source> <destination>
• % find . \(-name ‘w*’ -or -name ‘W*’ \)
• % cp file1 file2
• % cp file1 [file2] … /directory
• Useful option: -i to prevent overwriting existing files
and prompt the user to confirm.
mkdir ln
• Creates a directory. • Symbolic link or a “shortcut” in M$ terminology.
• % mkdir newdir • % ln –s <real-name> <fake-name>
• Often people make an alias of md for it.

cd chmod
• Changes your current directory to a new one. • Changes file permissions
• % cd /some/other/dir • Possible invocations
• Absolute path • % chmod 600 filename
• % cd subdir • -rw------- 1 user group 2785 Feb 8 14:18 filename
(a bit not intuitive where 600 comes from)
• Assuming subdir is in the current directory.
• % chmod u+rw filename
• % cd (the same thing, more readable)
• Returns you to your home directory. • For the assignment:
• % chmod u+x myshellscript
(mysshellscript is now executable)
• -rwx------ 1 user group 2785 Feb 8 14:18 myshellscript

pwd grep
• Displays personal working directory, i.e. your current directory.• Searches its input for a pattern.
• % pwd • The pattern can be a simple substring or a complex regular expre
• If a line matches, it’s directed to STDOUT; otherwise, it’s discarde
• % echo “blah-foo” | grep blah
• Will print the matching line
• % echo “blah-foo” | grep zee
• Will not.
• See a separate grep tutorial.

rmdir Pipes
• Removes a directory. • What's a pipe?
• % rmdir dirname • is a method of interprocess communication (IPC)
• in shells a '|' symbol used
• Equivalent: • it means that the output of one program (on one side of a pip
• % rm -r dirname input for the program on another end.
• a set of "piped" commands is often called a pipeline
• Why it's useful?
• Because by combining simple OS utilities one can easily solve
tasks
IMP Note to Students
 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Agenda

 Files and directories in UNIX, file system.


 Basics of UNIX File System (UFS).
 What is inode in UFS?
 Discussion about inode structure in UNIX
 Discussion about various types of links [hard, symbolic] in
UFS. Different between these links.
 Discussion on choosing hard link Vs Symbolic link

4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus

What is a file system?

• Speaking broadly, a file system is the logical


means for an operating system to store and
Course Name : retrieve data on the computers hard disks, be
Systems Programming they local drives, network-available volumes,
or exported shares in a storage area network
(SAN)

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self What is a file system?

A file system can refer to the methods and data


structures that an operating system uses to keep track
of files on a disk or partition
• Linux keeps regular files and directories on block
devices such as disks
• A Linux installation may have several physical disk
units, each containing one or more file system types
• Partitioning a disk into several file system instances
makes it easier for administrators to manage the data
stored there
2 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
What is a file system? INTERNAL REPRESENTATION OF FILES

Overhead
• Inodes
sector track
view • Structure of a regular file
cylinder • Directories
• Conversion of a path name to an inode
• Super block
• Inode assignment to a new file
Disk blocks are composed of The same track on each platter
one or more contiguous in a disk makes a cylinder; • Allocation of disk blocks
partitions are groups of
sectors
contiguous cylinders
• Other file types
7 11
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

What is a file system? File System Implementation


(Disk Layout)

• File system instances reside on partitions


Entire Disk
• Partitioning is a means to divide a single hard Master Boot Partition Table Disk Partitions
drive into many logical drives Record

• A partition is a contiguous set of blocks on a drive


that are treated as an independent disk
• A partition table is an index that relates sections
of the hard drive to partitions Boot Block Super Block Inode List Data Blocks

A Possible File System Instance Layout


8 12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

The UNIX File System


• The root directory has many subdirectories. The following table describes some of the
subdirectories contained under root
File system layout
Directory Content

/bin Common programs, shared by the system, the system administrator and the users. • The central structural concepts of a file system
/dev
Contains references to all the CPU peripheral hardware, which are represented as type are:
files with special properties.
Most important system configuration files are in /etc, this directory contains data
- Boot Block
/etc
similar to those in the Control Panel in Windows - Super Block
/home Home directories of the common users.

/lib
Library files, includes files for all kinds of programs needed by the system and the - Inode List
users.
/sbin Programs for use by the system and the system administrator. - Data Block
Temporary space for use by the system, cleaned upon reboot, so don't use this for
/tmp
saving any work!
/usr Programs, libraries, documentation etc. for all user-related programs.
Storage for all variable files and temporary files created by users, such as log files,
/var the mail queue, the print spooler area, space for temporary storage of files. Boot Block Super Block Inode List Data Blocks
9 13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

UNIX and POSIX file attributes Boot Block


• File type : Type of file
• Access permission : The file access permission (owner, group, others) Boot Block:
• Hard link count : Number of hard links of a file - Occupies the beginning of a file system
• UID : The file owner user ID - Typically residing at the first sector, it may also
• GID : The file group ID
contain the bootstrap code that is read into the
• File size : The file size in bytes
machine at boot time
• Last access time : The time the file was last accessed
• Last modify time : The time the file was last modified - Although only one boot block is required to boot
• Last change time : The time the file access permission the system, every file system may contain a boot
UID ,GID or hard link count was last changed block
• Inode number : The system inode number of the file
• File system ID : The file system ID where the file is stored Boot Block
10 14
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Super Block What is a file system?
Super Block:
- Describes the state of a file system So what is a file system?
- How large it is
- How many files it can store
- A file system is a set of abstract data types that are
- Where to find free space in the file system implemented for the storage, hierarchical
organization, manipulation, navigation, access, and
retrieval of data

Boot Block Super Block


15 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Super Block Inodes in UNIX system

• UNIX system has an inode table which keeps track of all files
• Each entry in inode table is an inode record
• Inode record contains all attributes of file including inode
number and physical address of file
• Information of a file is accessed using its inode number
• Inode number is unique within a file system
• A file record is identified by a file system ID and inode number
• Inode record doesnot contain the name of the file
• The mapping of filenames to inode number is done via
directory files
Boot Block Super Block
16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Inode List What is a file system?

Inode List: • The kernel deals on a logical level with file


- An inode is the internal representation of a file systems rather than with disks
contains the description of the disk layout of the file
• The separate file systems that the system may
data
use are not accessed by device identifiers
- file owner
- permissions
• Instead they are combined into a single
hierarchical tree structure that represents the
- The inode list contains all of the inodes present in
an instance of a file system file systems as one whole single entity

Boot Block Super Block Inode List


17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Data Blocks File system consistency check

Data Blocks:
- Contain the file data in the file system
- Additional administrative data
- An allocated data block can belong to one and only
one file in the file system

Boot Block Super Block Inode List Data Blocks


18 22
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Kernel I/O structure Inode in Operating System

In Unix based operating system each file is indexed


by an Inode. Inode are special disk blocks they are
created when the file system is created. The number
of Inode limits the total number of files/directories that
can be stored in the file system.
An Inode is a data structure in UNIX operating
system that contains important information pertaining
to files within a file system. When a file system is
created in UNIX, a set amount of indoes is created as
well. Usually, about 1 percent of the file system disk
space is allocated to the inode table.
23
Systems Programming 27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

I/O in Unix ‐ Devices The Inode contains the following information:

1.Numeric UID of the owner.


2.Numeric GUID of the owner.
3.Size of the file.
4.File type: regular,directory,device etc…
5.Date and Time of Last modification of the file
data.
6.Date and Time of Last access of file data.
7.Date and Time of Last change of the I-node.

24
Systems Programming 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

I/O in Unix ‐ Devices


•Administrative information (permissions, timestamps, etc).
•A number of direct blocks (typically 12) that contains the first
12 blocks of the files.
•A single indirect pointer that points to a disk block which in
turn is used as an index block, if the file is too big to be
indexed entirely by the direct blocks.
•A double indirect pointer that points to a disk block which is a
collection of pointers to disk blocks which are index blocks,
used if the file is too big to beindexed by the direct and
single indirect blocks.
•A triple indirect pointer that points to an index block of index
blocks of index blocks.

25
Systems Programming 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

I/O in Unix ‐ Devices Inode Total Size:

•Number of disk block address possible to store in 1 disk


block = (Disk Block Size / Disk Block Address).

•Small files need only direct blocks, so there is little


waste in space or extra disk reads in those
cases. Medium-sized files may use indirect blocks. Only
large files use double or triple indirect blocks, which is
reasonable since those files are large anyway. The disk
is now broken into two different types of blocks: Inode
and Data Blocks.

26
Systems Programming 30
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
•There must be some way to determine where the Inodes
are, and to keep track of free Inodes and disk blocks. This is Hard Links
done by a Superblock. Superblock is located at a fixed They are the low-level links. It links more than
position in the file system. The Superblock is usually one filename with the same Inode and it
replicated on the disk to avoid catastrophic failure in case of represents the physical location of a file.
corruption of the main Superblock.
When hard link is created for a file, it directly
•Index allocation schemes suffer from some of the same points to the Inode of the original file in the disk
performance problems. As does linked allocation. For space, which means no new Inode is created.
example, the index blocks can be cached in memory, but the Directories are not created using hard links and
data blocks may be spread all over a partition. they can not cross filesystem boundaries. When
the source file is removed or moved, then hard
links are not affected.
Systems Programming 31 Systems Programming 35
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Inode Structure :
2) Soft Links (Symbolic Links)
Soft links are very common. It represents a virtual or abstract
location of the file. It is just like the shortcuts created in
Windows. A soft link doesn't contain any information or
content of the linked file, instead it has a pointer to the
location of the linked file. In other words, a new file is created
Inode Structure :
with new Inode, having a pointer to the Inode location of the
original file.
It is used to create link between directories and can cross file
system boundaries. When the source file is removed or
moved, then soft links are not updated.
We'll study in deep about both the links how to create it and
remove it.

Systems Programming 32 Systems Programming 36


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

WHERE TO USE HARD LINKS

Inode Table
The Inode table contains all the Inodes and is created
when file system is created. 1. ln data/ foo.txt input_files
2. Links provide some protection against accidental
The df -i command can be used to check how many deletion
inodes are free and left unused in the file system.
3. Because of links, we don’t need to maintain two
programs as two separate disk files.
Inode Number

Each Inode has a unique number and Inode number


can be seen with the help of ls -li command.

37
Systems Programming 33
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

LIMITATIONS OF HARD LINKS

1. We cant have two linked filenames in two file


A Linux filesystem has many hard links and systems.
symbolic links. A link is a connectivity between 2. We cant link a directory even within the same file
the filename and the actual data byte in the disk system
space. More than one filename can link to the
same data.
There are two types of links in Linux OS: This can be solved by using symbolic links (soft links)
1.Hard Links
2.Soft Links

38
Systems Programming 34
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
How do inodes locate files?

When are inodes created?

When a filesystem is created, space for inodes is allocated


as well. There are algorithms that take place to determine We know our data is out there on the disk somewhere,
how much inode space you need depending on the unfortunately it probably wasn't stored sequentially, so we
volume of the disk and more. You've probably at some have to use inodes. Inodes point to the actual data blocks of
point in your life seen errors for out of disk space issues. your files. In a typical filesystem (not all work the same), each
Well the same can occur for inodes as well (although less inode contains 15 pointers, the first 12 pointers point directly
common), you can run out of inodes and therefore be to the data blocks.
unable to create more files. Remember data storage
depends on both the data and the database (inodes).
To see how many inodes are left on your system, use the
command df -i
Systems Programming 39 Systems Programming 43
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Inode information The 13th pointer, points to a block containing pointers to


Inodes are identified by numbers, when a file gets more blocks, the 14th pointer points to another nested block
created it is assigned an inode number, the of pointers, and the 15th pointer points yet again to another
number is assigned in sequential order. However, block of pointers! Confusing, I know! The reason this is done
this way is to keep the inode structure the same for every
you may sometimes notice when you create a inode, but be able to reference files of different sizes. If you
new file, it gets an inode number that is lower than had a small file, you could find it quicker with the first 12
others, this is because once inodes are deleted, direct pointers, larger files can be found with the nests of
they can be reused by other files. To view inode pointers. Either way the structure of the inode is the same.
numbers run ls -li:

Systems Programming 40 Systems Programming 44


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

pete@icebox:~$ ls -li
140 drwxr-xr-x 2 pete pete 6 Jan 20 20:13 Desktop
141 drwxr-xr-x 2 pete pete 6 Jan 20 20:01 Documents

The first field in this command lists the inode number.


You can also see detailed information about a file with stat,
it tells you information about the inode as well.

Systems Programming 41 Systems Programming 45


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

pete@icebox:~$ stat ~/Desktop/


File: ‘/home/pete/Desktop/’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 806h/2054d Inode: 140 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 1000/ pete)
Gid: ( 1000/ pete)
Access: 2016-01-20 20:13:50.647435982 -0800
Modify: 2016-01-20 20:13:06.191675843 -0800
Change: 2016-01-20 20:13:06.191675843 -0800

Systems Programming 42 Systems Programming 46


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
IMP Note to Self

2
Systems Programming 47
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

48 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda

 File Access Permission


 Stat, df, & du commandas.
 I/O in UNIX – Block and Character devices, device drivers, I/O
queuing and interrupt handling
 Inodes
 Structure of a regular file
 Conversion of given path name to inode in UFS with more
examples.

49 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Unix / Linux - File Permission / Access Modes

• File ownership is an important component of Unix that provides a secure method for storing files.
• Every file in Unix has the following attributes −
• Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file.
• Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs
to, can perform on the file.
• Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.
Course Name : •

The Permission Indicators
While using ls -l command, it displays various information related to file permission as follows −

Systems Programming $ls -l /home/amrood
– -rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
– drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir

• Here, the first column represents different access modes, i.e., the permission associated with a file or a directory.
• The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order:
read (r), write (w), execute (x) −
• The first three characters (2-4) represent the permissions for the file's owner.
• For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.
• The second group of three characters (5-7) consists of the permissions for the group to which the file belongs.
• For example, -rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write permission.
• The last group of three characters (8-10) represents the permissions for everyone else.
• For example, -rwxr-xr-- represents that there is read (r) only permission

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus


Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Modes

• File Access Modes • Changing Owners and Groups


• The permissions of a file are the first line of defense in the security of a Unix system. The basic building • While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions
blocks of Unix permissions are the read, write, and execute permissions, which have been described mentioned above are also assigned based on the Owner and the Groups.
below − • Two commands are available to change the owner and the group of files −
• Read - Grants the capability to read, i.e., view the contents of the file. • chown − The chown command stands for "change owner" and is used to change the owner of a file.
• Write - Grants the capability to modify, or remove the content of the file. • chgrp − The chgrp command stands for "change group" and is used to change the group of a file.
• Execute - User with execute permissions can run a file as a program.
• Changing Ownership
• Directory Access Modes • The chown command changes the ownership of a file. The basic syntax is as follows −
• Directory access modes are listed and organized in the same manner as any other file. There are a few • $ chown user filelist
differences that need to be mentioned −
• The value of the user can be either the name of a user on the system or the user id (uid) of a user on the
• Read - Access to a directory means that the user can read the contents. The user can look at the system.
filenames inside the directory.
• The following example will help you understand the concept −
• Write - Access means that the user can add or delete files from the directory.
• $ chown amrood testfile
• Execute - Executing a directory doesn't really make sense, so think of this as a traverse permission.
• Changes the owner of the given file to the user amrood.
• A user must have execute access to the bin directory in order to execute the ls or the cd command.
• NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but
normal users can change the ownership of only those files that they own.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Modes



Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode) command.
• Changing Group Ownership
• There are two ways to use chmod — the symbolic mode and the absolute mode. • The chgrp command changes the group ownership of a file.
• The basic syntax is as follows −
• Using chmod in Symbolic Mode
• The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode.
• With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the • $ chgrp group filelist
following table.
• The value of group can be the name of a group on the system or the group ID (GID) of a
• Chmod operator & Description group on the system.
• + Adds the designated permission(s) to a file or directory.
• - Removes the designated permission(s) from a file or directory.
• = Sets the designated permission(s). • Following example helps you understand the concept −

• Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions are as follows −
• $ls -l testfile
• $ chgrp special testfile
• -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile • Changes the group of the given file to special group.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Modes

• Using chmod with Absolute Permissions • Unix / Linux - Environment


• The second way to modify permissions with the chmod command is to use a number to specify
each set of permissions for the file. • An important Unix concept is the environment, which is defined by environment variables.
• Each permission is assigned a value, as the following table shows, and the total of each set of • Some are set by the system, others by you, yet others by the shell, or any program that loads another program.
permissions provides a number for that set.
• A variable is a character string to which we assign a value.
• The value assigned could be a number, text, filename, device, or any other type of data.
• Number Octal Permission Representation Ref
• 0 No permission --- • For example, first we set a variable TEST and then we access its value using the echo command −
• 1 Execute permission --x
• $TEST="Unix Programming"
• 2 Write permission -w-
• $echo $TEST
• 3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx • It produces the following result.
• 4 Read permission r-- • Unix Programming
• 5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
• 6 Read and write permission: 4 (read) + 2 (write) = 6 rw- • Note that the environment variables are set without using the $ sign but while accessing them we use
• 7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx the $ sign as prefix.
• These variables retain their values until we come out of the shell.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Unix / Linux - File Permission / Access Modes Unix / Linux - File Permission / Access Modes

• Using chmod with Absolute Permissions • Unix / Linux - Environment


• Here's an example using the testfile.
• When you log in to the system, the shell undergoes a phase called initialization to set up the environment.
• Running ls -l on the testfile shows that the file's permissions are as follows −
• $ls -l testfile • This is usually a two-step process that involves the shell reading the following files −
– -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile • /etc/profile
• profile
• Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see • The process is as follows −
the permission changes −
• The shell checks to see whether the file /etc/profile exists.
• $ chmod 755 testfile
• $ls -l testfile • If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
• -rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile • The shell checks to see whether the file .profile exists in your home directory.
• Your home directory is the directory that you start out in after you log in.
• $chmod 743 testfile • If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
• $ls -l testfile • As soon as both of these files have been read, the shell displays a prompt −
• -rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
• $
• $chmod 043 testfile • This is the prompt where you can enter commands in order to have them executed.
• $ls -l testfile • Note − The shell initialization process detailed here applies to all Bourne type shells, but some additional files
• ----r---wx 1 amrood users 1024 Nov 2 00:10 testfile are used by bash and ksh.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus


Unix / Linux - File Permission / Access Modes
Information we get from stat
• File: The file name, If the provided file is a symlink, then the name will be
• The .profile File
different.
• The file /etc/profile is maintained by the system administrator of your Unix machine and
contains shell initialization information required by all users on a system. • Size: The size of a given file in Bytes.
• Blocks: Total number of allocated blocks to the file on hard disk.
• The file .profile is under your control. • File type: Regular files, special files, directories, or symbolic links.
• You can add as much shell customization information as you want to this file.
• Device: Device number in hexadecimal format.
• The minimum set of information that you need to configure includes −
• The type of terminal you are using. • Inode: Inode number of the file.
– A list of directories in which to locate the commands. • Links: Number of hard links of the file.
– A list of variables affecting the look and feel of your terminal. • Access: The last time at which the file was accessed.
– You can check your .profile available in your home directory.
– Open it using the vi editor and check all the variables set for your environment. • Modify: The last time at which file was modified.
• Change: The last time the at which file’s attribute or content was changed.
• Birth: The time at which the file was created.
18
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Unix / Linux - File Permission / Access Modes


du Command


Setting the Terminal Type
Usually, the type of terminal you are using is automatically configured by either the login or getty programs.
du command displays the number of blocks used for files. If


Sometimes, the auto configuration process guesses your terminal incorrectly.
If your terminal is set incorrectly, the output of the commands might look strange, or you might not be able to interact
the File parameter specified is actually a directory, all files

with the shell properly.
To make sure that this is not the case, most users set their terminal to the lowest common denominator in the
within the directory are reported on. If no File parameter
following way −
• $TERM=vt100 is provided, the du command uses the files in the current
• Setting the PATH directory.
• When you type any command on the command prompt, the shell has to locate the command before it can be


executed.
The PATH variable specifies the locations in which the shell should look for commands.
Notes:


Usually the Path variable is set as follows −
$PATH=/bin:/usr/bin • Files with multiple links are counted and written for only
• Here, each of the individual entries separated by the colon character (:) are directories.
• If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, one entry.
a message similar to the following appears −


$hello
hello: not found
• Block counts are based only on file size

19
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Stat command du Command


Flags
stat is a command which gives information about -a Displays disk usage for each file specified, or displays the individual
the file and filesystem. Stat command gives disk usage for each file in a directory.
information such as the size of the file, access -k Calculates the block count in 1024-byte units rather than the
default 512-byte units.
permissions and the user ID and group ID, birth
-l Allocates blocks evenly among the links for files with multiple links.
time access time of the file. By default, a file with two or more links is counted only once.
-s Displays the total disk usage for all specified files, or displays the
total disk usage for all files in a directory.
syntax of using stat command:
stat --options filenames syntax of du command
du [OPTION]... [FILE]...
16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Stat command df Command


-L, --dereference follow links • df command (short for disk filesystem) is used to
-f, --file-system display file system status instead of show disk utilization for a Linux system.
file status • Display information of device name, total blocks, total disk space,
used disk space, available disk space and mount points on a file
system
• Display all the file system, use -a option. df –a
• Use -h option to display size in power of 1024 df -h /home/WASE
• Use -H option to display sizes in power of 1000 df -H /home/WASE
• Use -T option to display file type df -T /home/WASE

17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
df Command Device special files
To display information about /boot file system, • Devices can be accessed via special files. A device special file
df /boot does not use any space on the filesystem. It is only an access
We can display information in various block format. For instance see point to the device driver.
1M-byte blocks, run: • Two types of special files exist: character and block special
df -m files. The former allows I/O operations in character mode
df -m /boot while the later requires data to be written in block mode via
Show statistics about the number of free and used inodes the buffer cache functions.
Pass the -i as follows to display index node (inodes) info: • When an I/O request is made on a special file, it is forwarded
df -i to a device driver. A special file is referenced by a major
df -i -H number, which identifies the device type, and a minor
number, which identifies the unit.
df -i -H -T
22 26
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Inodes I/O Queuing and Interrupt Handling

Each file is represented by a structure, called an inode.

Each inode contains the description of the file: file type,


access rights, owners, timestamps, size, pointers to data
blocks.
The addresses of data blocks allocated to a file are stored in
its inode. When a user requests an I/O operation on the
file, the kernel code converts the current offset to a block
number, uses this number as an index in the block
addresses table and reads or writes the physical block.
23 27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Directories I/O in Unix – Block Devices and I/O

• Directories are structured in a hierarchical tree. Each


directory can contain files and subdirectories.

• Directories are implemented as a special type of files.


Actually, a directory is a file containing a list of entries.
Each entry contains an inode number and a file name.
When a process uses a pathname, the kernel code
searchs in the directories to find the corresponding inode
number. After the name has been converted to an inode
number, the inode is loaded into memory and is used by
subsequent requests.
24 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Links I/O in Unix – Block Devices and I/O

The inode contains a field containing the number associated


with the file.

Adding a link simply consists in creating a directory entry,


where the inode number points to the inode, and in
incrementing the links count in the inode.

When a link is deleted, i.e. when one uses the rm command to


remove a filename, the kernel decrements the links count
and deallocates the inode if this count becomes zero.
25 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
inode

• Contains the information necessary for a


process to access a file

• Exits in a static form on disk and the kernel


reads them into an in-core inode

34
Systems Programming 30
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

inodes

Advantages of the Unix file System • Disk inodes consists of the following fields
•Hierarchical organization: - file owner identifier
•Robustness: - file type
•Security: . - file access permissions
•Compatibility: - file access times
- number of links to the file
- table of contents for the disk address of data in a file
- file size

35
Systems Programming 31
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

inodes

• in-core copy of the inode contains the following fields in addition


Disadvantages of the Unix file System
to the fields of the disk inode:
- status of the in-core inode (The inode is locked, a process is
•Complexity: waiting for the inode to become unlocked)
- logical device number of file system that contains the file
•Steep Learning Curve: - inode number are stored in a linear array on disk, the kernel
identifies the number of a disk mode by its position in the array.
•Lack of User-Friendly Interface - pointers to other in-core inodes
- reference count, indicating the number of instances of the file that
•Limited Support for Certain File Systems
are active (such as when opened).

36
Systems Programming 32
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File System Algorithms Accessing Inodes


The algorithm iget allocates an in-core copy of an inode.
If the inode is not found on a hash queue, it allocates an inode from
the free list and reads the disk copy into the in-core inode.
It already knows the inode number and device number. It calculates
the logical block number on which the disk inode resides according
to how many inodes fit into one disk block.

The formula for calculating the logical block number is:


Every file a UNIX system has a unique inode. Processes interact with files using
well defined system calls. The users specify a file with a character string which is
the file's path and then the system get the inode which is mapped to the file which block number = ((inode number - 1) / number of inodes per block) +
corresponds to the path.
start block of inode list
33 37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Algorithm iput
Inodes exist in a static form on disk, and the kernel reads them into an in- - The kernel locks the inode if it has not been already locked
core inode to manipulate them. Disk inodes consist of the following fields:
- The kernel decrements inode reference count
• File owner identifier. Ownership is divided between an individual owner
and a "group" owner and defines the set of users who have access rights - The kernel checks if reference count is 0 or not
to a file. The superuser has access rights to all files in the system. - If the reference count is 0 and the number of links to the file is 0,
• File type. Files may be of type regular, directory, character or block then the kernel releases disk blocks for file(algorithm free),
special, or FIFO (pipes). free the inode(algorithm ifree)
• File access permissions. The system protects files according to three
classes: the owner and the group owner of the file, and other users; each • If the file was accessed or the inode was changed or the
class has access rights to read, write and execute the file, which can be file was changed , then the kernel updates the disk inode
set individually. Because directories cannot be executed, execution • The kernel puts the inode on free list
permission for a directory gives the right to search the directory for a file
name.
- If the reference count is not 0, the kernel releases the inode lock

42
Systems Programming 38
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

iput (inode_no) //releaseIncoreInode


– lock inode if not locked
File access times, giving the time the file was last modified, when it
was last accessed, and when the inode was last modified.
– decrement inode refernece count
• Number of links to the file, representing the number of names the file – if (refernce count==0)
has in the directory hierarchy..
• Table of contents for the disk addresses of data in a file. Although • if (inode link==0)
users treat the data in a file as a logical stream of bytes, the kernel – free disk block
saves the data in discontiguous disk blocks. The inode identifies the – set file type to 0
disk blocks that contain the file's data. – free inode
• File size. Data in a file is addressable by the number of bytes from the
beginning of the file, starting from byte offset 0, and the file size is 1 • if (file accessed or inode changed or file changed)
greater than the highest byte offset of data in the file. For example, if a – update disk inode
user creates a file and writes only 1 byte of data at byte offset 1000 in • put inode on free list
the file, the size of the file is 1001 bytes.
– Release inode lock
43
Systems Programming 39
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

algorithm ¡alloc /* allocate


inode */ input: file system
The in-core copy of the inode contains the following fields in addition to
the fields of the disk inode: while (not done) {
if (super block locked) {
• The status of the in-core inode, indicating whether
sleep (event super block becomes free); continue; /* while loop */
— a process is waiting for the inode to become unlocked, }
— the in-core representation of the inode differs from the disk copy as if (inode list in super block is empty) {
a result of a change to the data in the inode, lock super block;
— the in-core representation of the file differs from the disk copy as a get remembered inode for free inode search; search disk for free inodes
result of a change to the file data, until super block full,
• The logical device number of the file system that contains the file. or no more free inodes (algorithms bread and brclsc); unlock super block;
• The inode number. Since inodes are stored in a linear array on disk, wake up (event super block becomes free); if (no free inodes found on
the kernel identifies the number of a disk inode by its position in the disk)
return (no inode); set remembered inode for next free inode search;
array. The disk inode does not need this field.
}

Systems Programming 40 Systems Programming 44


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Structure of a Regular File


Algorithm iget
algorithm iget input: file system inode number In UNIX, the data in files is not stored sequentially on disk.
output: locked
If it was to be stored sequentially, the file size would not be
inode { flexible without large fragmentation.
if (inode in inode cache) {
sleep (event inode becomes unlocked); In case of sequential storage, the inode would only need to
continue; /* loop back to while V
/* special processing for mount points */ if (inode on inode free list) store the starting address and size. Instead, the inode
remove from free list; increment inode reference count; return (inode); stores the disk block numbers on which the data is
/* inode not in inode cache */ if (no i nodes on free list)
return (error); present. But for such strategy, if a file had data across
remove new inode from free list;
reset inode number and file system;
1000 blocks, the inode would need to store the numbers
remove inode from old hash queue, of 1000 blocks and the size of the inode would differ
place on new one;
read inode from disk (algorithm bread);
according to the size of the file.
initialize inode (e.g. reference count to 1); return (inode);
45
Systems Programming 41
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Structure of a Regular File Structure of a regular file
The inodes have array of size 13 which for storing the block numbers,
although, the number of elements in array is independent of the - While it’s not at necessary level of indirection,
storage strategy. the kernel calculates index into inode or indirect block from
The first 10 members of the array are "direct addresses", meaning logical block number in file, gets disk block number from inode
that they store the block numbers of actual data. or indirect block and release buffer from previous disk read
The 11th member is "single indirect", it stores the block number of • If there is no more levels of indirection , the kernel stops
the block which has "direct addresses". conversing
The 12th member is "double indirect", it stores block number of a • Otherwise the kernel reads indirect disk block(bread) and
"single indirect" block. adjusts logical block number in file according to level of
And the 13th member is "triple indirect", it stores block number of a indirection
"double indirect" block.
This strategy can be extended to "quadruple" or "quintuple" indirect
addressing. 46 50
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Structure of a regular file Conversion of Path Name to an Inode

Inode Data Blocks


direct0 The kernel parses the path by accessing each inode in the
direct1 path and finally returning the inode of the required file.
direct2
Every process has a current directory. The current directory
direct3
of process 0 is the root directory.
direct4
direct5 For every other process, it is the current directory of its
direct6 parent process.
direct7
direct8
Later the process can change the current directory with the
direct9 system call chdir. The inode of the current directory is
single indirect
stored in the u-area.
double indirect
triple indirect
47 51
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Structure of a regular file Path conversion to an inode

Suppose System V UNIX • if (path name starts with root)


Assume that a logical on the file system holds 1K bytes and that a block – working inode= root inode
number is addressable by a 32 bit integer, then a block can hold up to 256 • else
block numbers – working inode= current directory inode
• while (there is more path name)
10 direct blocks with 1K bytes each=10K bytes – read next component from input
1 indirect block with 256 direct blocks= 1K*256=256K bytes – read directory content
1 double indirect block with 256 indirect blocks= – if (component matches an entry in directory)
256K*256=64M bytes • get inode number for matched component
• release working inode
1 triple indirect block with 256 double indirect blocks=
• working inode=inode of matched component
64M*256=16G bytes – else
– return no inode
• return (working inode)
48 52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Structure of a regular file Algorithm namei


• Processes access data in a file by byte offset and view a file as a - If path name starts from root, then the kernel assigns root
stream of bytes inode(iget) to working inode
• The kernel accesses the inode and converts the logical file block - Otherwise, the kernel assigns current directory inode to working
into the appropriate disk block inode
• algorithm bmap - While there is more path name, the kernel reads next path name
- The kernel calculates logical block number in file from byte component from input, and verifies that working inode is of
offset directory, access permissions OK
- The kernel calculates start byte in block for I/O • If working inode is of root and component is ‘..’, then the
- The kernel calculates number of bytes to copy to user kernel checks whether there is more path name or not
- The kernel checks if read-ahead is applicable, then marks inode • Otherwise the kernel reads directory by repeated use of
- The kernel determines level of indirection bmap,bread,brelse

49 53
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Path conversion to an inode

• If the kernel finds a match, it records the inode number


of the matched directory entry, releases the block and
the old working inode, and allocates the inode of the
match component

• If the kernel does not match the path name in the


block, it releases the block, adjusts the byte offset by
the number of bytes in a block, converts the new offset
to a disk block number and reads the new block

54
Systems Programming 58
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Superblock contents
• size of the file system.
• number of free blocks in the file system.
• list of free blocks in the file system.
• pointer to the next free block in the free blocks list
• size of the inodes list.
• number of free inodes in the file system.
• list of free inodes in the file system.
• pointer to the next free inode in the free inodes list.
• lock fields for the free blocks and free inodes list.
• a field indicating whether the super block has changed.
The kernel periodically writes the superblock to the disk if it had been
modified so that it is consistent with the data on the disk
55
Systems Programming 59
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Inode assignment to a new file

• algorithm ialloc : assigns a disk inode to a newly created file


-super block is unlocked
1.There are inodes in super block inode list and inode is free
get inode number from super block inode list
get inode(iget)
initialize inode
write inode to disk
decrement file system free inode count
2. There are inodes in super block inode list but inode is not free
get inode number from super block inode list
get inode (iget)
write inode to disk
release inode (iput)

56
Systems Programming 60
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Inode assignment to a new file

3. Inode list in super block is empty


lock super block
get remembered inode for free inode search
search disk for free inode until super block full or
no more free inodes(bread and brelse)
unlock super block
super block becomes free
if no free inodes found on disk , stop
otherwise, set remembered inode for next free inode
search
- If super block is locked, sleep
57
Systems Programming 61
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
IMP Note to Self

2
Systems Programming 62
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

63 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda


 The vi basics
 Input mode – entering and replacing text
 Saving text and quitting – the ex mode
 Navigation
 Editing text
 Undoing last editing instructions
 Repeating the last command
 Searching for a pattern
 Substitution – search and replace
64  cp, cut, paste commands. 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

vi basics
• To write and edit some programs and scripts, we require
editors
Course Name : • UNIX provides vi editor for BSD system – created by Bill
Systems Programming Joy
• Bram Moolenaar improved vi editor and called it as vim
(vi improved) on Linux OS

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
The vi Editor INPUT MODE – ENTERING AND REPLACING TEXT
Insertion of Text at Line Extremes (I and A)
The three modes of vi Editor:
1. Command Mode: The default mode of the editor where
every key pressed is interpreted as a command to run
on text. To copy, delete text & navigate, this mode is
used.
2. Input Mode: Every key pressed after switching to this
mode actually show up as text. To input text, this mode
is used.
3. ex Mode(Last Line Mode): The mode used to handle
files (like saving) and perform substitution.

6 10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

The vi Editor INPUT MODE – ENTERING AND REPLACING TEXT

Opening a New Line (o and O)

7 11
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

INPUT MODE – ENTERING AND INPUT MODE – ENTERING AND REPLACING


REPLACING TEXT TEXT
Replacing Text (r, s, R and S)

8 12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

INPUT MODE – ENTERING AND INPUT MODE – ENTERING AND REPLACING


REPLACING TEXT TEXT
Insertion of Text (i and a) Input Mode Commands

9 13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
ex MODE – SAVING TEXT AND QUITTING ex MODE – SAVING TEXT AND QUITTING
Saving the Work (:w) Writing Selected Lines

14 18
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE – SAVING TEXT AND QUITTING ex MODE – SAVING TEXT AND QUITTING
Saving and Quitting (:x and :wq) Escape to the UNIX Shell (:sh and [Ctrl-z])

15 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE – SAVING TEXT AND QUITTING ex MODE – SAVING TEXT AND QUITTING
Aborting Editing (:q) Recovering from a Crash (:recover and –r)

16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE – SAVING TEXT AND QUITTING ex MODE – SAVING TEXT AND QUITTING

Writing Selected Lines Save and Exit Commands of the ex Mode

17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – NAVIGATION Command MODE – NAVIGATION
Movement in the Four Directions (h, j, k and l) Moving to Line Extremes (0, | and $)

22 26
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – NAVIGATION Command MODE – NAVIGATION


Movement in the Four Directions (h, j, k and l) Scrolling ([Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u])

23 27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – NAVIGATION Command MODE – NAVIGATION


Word Navigation (b, e and w) Absolute Movement (G)

Example
5b takes the cursor 5 words back
3w takes the cursor 3 words forward

24 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – NAVIGATION Command MODE – EDITING TEXT


Moving to Line Extremes (0, | and $) Editing Text – Deleting characters (x, X)

25 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT Command MODE – EDITING TEXT
Editing Text – Deleting lines (dd) Editing Text – Joining Lines (J)

30 34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – EDITING TEXT Command MODE – EDITING TEXT

Editing Text – Moving Text (p, P) Undoing Last Editing Instructions (u and U)

31 35
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – EDITING TEXT Command MODE – EDITING TEXT

Editing Text) Repeating the Last Pattern Search (n and N)

They use operators, such as,


d delete
y yank (copy)
dd delete entire line
yy copy entire line
6dd deletes the current line and five lines below

32 36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Command MODE – EDITING TEXT SEARCHING FOR A PATTERN

Editing Text – Copying Text (y and p)


• / search forward
• ? search backward

/printf The search begins forward to position the


cursor on the first instance of the word
?pattern Searches backward for the most previous
instance of the pattern
n Repeats search in same direction of
original search

33 37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Command MODE – EDITING TEXT cp – COPYING A FILE
Search and Repeat Commands

38 42
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE cp – COPYING A FILE


Substitution – Search and Replace ( :s )

39 43
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE cp – COPYING A FILE


Substitution – Search and Replace ( :s )
We can perform search and replace in ex mode using :s
Its syntax is,
:address/source_pattern/target_pattern/flags
:1,$s/director/member/g
:1,50s/unsigned//g
:3,10s/director/member/g
:.s/director/member/g
:$s/director/member/g
40 44
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

ex MODE cut Command


Substitution – Search and Replace ( :s )
• It is used for slitting the file vertically
head -n 5 emp.lst | tee shortlist
will select the first five lines of emp.lst and saves it to
shortlist
• We can cut by using -c option with a list of column
numbers, delimited by a comma (cutting columns)

cut -c 6-22,24-32 shortlist


cut -c -3,6-22,28-34,55- shortlist

41 45
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cut Command paste Command

• Most files don’t contain fixed length lines, so we Joining lines (-s)
have to cut fields rather than columns (cutting fields) Let us consider that the file address book contains the
-d for the field delimiter details of three persons
-f for the field list cat addressbook
cut -d \ | -f 2,3 shortlist | tee cutlist1
paste -s addressbook -to print in one single line
will display the second and third columns of shortlist
and saves the output in cutlist1. here | is escaped to paste -s -d ”| | \n” addressbook -are used in a circular
prevent it as pipeline character manner

46 50
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

cut Command

• To print the remaining fields, we have


cut –d \ | -f 1,4- shortlist > cutlist2

47 51
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

paste Command IMP Note to Self

• When we cut with cut, it can be pasted back with the


paste command, vertically
paste cutlist1 cutlist2
We can view two files side by side

48 52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

paste: pasting file (contd.,)

• We can specify one or more delimiters with -d

paste -d “|” cutlist1 cutlist2 Course Name :


Systems Programming
Where each field will be separated by the delimiter |

49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
IMP Note to Self cp – COPYING A FILE

2 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students cp – COPYING A FILE

 It is important to know that just login to the session does not


guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

3 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Agenda cut Command


 Commands :
• cat - to create, view, concatenate files. • It is used for slitting the file vertically
• mv - mv is used to move one or more files or directories head -n 5 emp.lst | tee shortlist
• rm - removes the entries for a specified file, group of files.
will select the first five lines of emp.lst and saves it to
• cmp, comm, diff - to display the differences by comparing the files shortlist
• chmod- used to change the access permissions • We can cut by using -c option with a list of column
• top - Displays list of running processes numbers, delimited by a comma (cutting columns)
• head- Pick lines from the beginning
• tail - Pick lines from the end cut -c 6-22,24-32 shortlist
• sort - Sort, merge and remove cut -c -3,6-22,28-34,55- shortlist
• wc - to find number of lines, word count, byte and characters.
• tee - to view output and at the same time store it in a file 4 8
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

cp – COPYING A FILE cut Command

• Most files don’t contain fixed length lines, so we


have to cut fields rather than columns (cutting fields)
-d for the field delimiter
-f for the field list
cut -d \ | -f 2,3 shortlist | tee cutlist1
will display the second and third columns of shortlist
and saves the output in cutlist1. here | is escaped to
prevent it as pipeline character

5 9
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
cut Command cat – DISPLAYING AND CREATING FILES

• To print the remaining fields, we have


cut –d \ | -f 1,4- shortlist > cutlist2

10 14
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

paste Command cat – DISPLAYING AND CREATING FILES

cat options ( -v and –n)


• When we cut with cut, it can be pasted back with the
paste command, vertically
paste cutlist1 cutlist2
We can view two files side by side

11 15
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

paste: pasting file (contd.,) cat – DISPLAYING AND CREATING FILES

cat is also useful for creating files


• We can specify one or more delimiters with -d

paste -d “|” cutlist1 cutlist2

Where each field will be separated by the delimiter |

12 16
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

paste Command rm – DELETING FILES

Joining lines (-s)


Let us consider that the file address book contains the
details of three persons
cat addressbook
paste -s addressbook -to print in one single line
paste -s -d ”| | \n” addressbook -are used in a circular
manner

13 17
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
rm – DELETING FILES cmp – COMPARING TWO FILES

18 22
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

rm – DELETING FILES comm – WHAT IS COMMON?

19 23
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

rm – DELETING FILES comm – WHAT IS COMMON?

20 24
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

mv – RENAMING FILES comm – WHAT IS COMMON?

21 25
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
diff – CONVERTING ONE FILE TO OTHER CHANGING FILE PERMISSIONS

• A file or a directory is created with a default set of


permissions, which can be determined by umask

• Let us assume that the file permission for the created


file is -rw-r--r—

• Using chmod command, we can change the file


permissions and allow the owner to execute his file

26 30
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

umask: DEFAULT FILE AND DIRECTORY RELATIVE AND ABSOLUTE


PERMISSIONS PERMISSIONS

• Usually we have,
• In a relative manner, specify the changes to the
rw-rw-rw- (octal 666) for regular files current permissions
rwxrwxrwx (octal 777) for directories
• The default is transformed by subtracting the user
• In an absolute manner, specify the final permissions
mask from it to remove one or more permissions
• We can evaluate the current value of the mask as,
$ umask
022

27 31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

FILE OWNERSHIP RELATIVE PERMISSIONS

• When you create a file, you become its owner (third • chmod only changes the permissions specified in the
column) command line and leaves the other permissions
• Group owner of the file (fourth column) unchanged
• Several users may belong to a single group, but the • Syntax
privileges of the group are set by the owner of the file chmod category operation permission filename(s)
and not by the group members

28 32
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

FILE OWNERSHIP RELATIVE PERMISSIONS

• chmod takes an expression as its argument which


• UNIX follows a three-tiered file protection system that contains:
determines a file’s access rights 1. user category (user, group, others)
• Filetype owner (rwx) groupowner (rwx) others (rwx) 2. operation to be performed (assign or remove a
• Example: permission)
-rwxr-xr-- 1 kumar metal 20500 may 10 19:21 chap02 3. type of permission (read, write, execute)
rwx r-x r--
owner/user group owner others

29 33
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
RELATIVE PERMISSIONS ABSOLUTE PERMISSIONS

Category operation permission • Need not to know the current file permissions
u - user + assign r - read • Set all nine permissions explicitly
g - group - remove w - write • A string of three octal digits is used as an
expression
o - others = absolute x - execute
• The permission can be represented by one octal
a - all (ugo)
digit for each category
• For each category, we add octal digits

34 38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

RELATIVE PERMISSIONS ABSOLUTE PERMISSIONS

• Examples Octal Permissions Significance


-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart 0 --- no permissions
1 --x execute only
chmod u+x xstart 2 -w- write only
3 -wx write and execute
-rwxr--r-- 1 kumar metal 1906 sep 23:38 xstart 4 r-- read only
5 r-x read and execute
6 rw- read and write
The command assigns (+) execute (x) permission to the
user (u), other permissions remain unchanged 7 rwx read, write and execute

35 39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

RELATIVE PERMISSIONS ABSOLUTE PERMISSIONS

• chmod ugo+x xstart • Using relative permission


• chmod a+x xstart chmod a+rw xstart
• chmod +x xstart
• Using absolute permission
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart chmod 666 xstart
chmod 644 xstart
chmod 761 xstart
chmod accepts multiple file names in command line
will assign all permissions to the owner, read and write
• chmod u+x note note1 note3 permissions for the group and only execute permission to the
others
36 40
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

RELATIVE PERMISSIONS ABSOLUTE PERMISSIONS

Let initially
• 777 signifies all permissions for all category, but
still we can prevent a file from being deleted
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
• 000 signifies absence of all permissions for all
chmod go-r xstart category, but still we can delete a file

Then, it becomes • It is the directory permissions that determines


whether a file can be deleted or not
-rwx--x--x 1 kumar metal 1906 sep 23:38 xstart

37 41
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
PERMISSIONS DIRECTORY PERMISSIONS

This becomes 644 (666-022) for ordinary files and


• Only owner can change the file permissions
755 (777-022) for directories
• User can not change other user’s file’s permissions
umask 000
• But the system administrator can do anything
Indicates, we are not subtracting anything and the
default permissions will remain unchanged
Note that, changing system wide default
permission seetings is possible using chmod but
not by umask

42 46
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

THE SECURITY IMPLICATIONS CHANGING FILE OWNERSHIP

• Let the default permission for the file xstart is • On BSD and AT&T systems
• Kumar – owner (user)
-rw-r--r--
• Metal – group owner
• chmod u-rw, go-r xstart or
• If sharma copies a file of kumar, then sharma will
• chmod 000 xstart become its owner and he can manipulate the
---------- attributes
• This is simply useless but still the user can • chown and chgrp
delete this file • On BSD, only system administrator can use chown
• On other systems, only the owner can change both

43 47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

chmod RECURSIVELY chown Command

• Changing ownership requires superuser


chmod -R a+x shell_scripts permission, so use su command
ls -l note
This makes all the files and subdirectories found in -rwxr----x 1 kumar metal 347 may 10 20:30 note
the shell_scripts directory, executable by all • chown sharma note; ls -l note
users -rwxr----x 1 sharma metal 347 may 10 20:30 note

44 48
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

DIRECTORY PERMISSIONS chgrp Command

• This command changes the file’s group owner


• It is possible that a file cannot be accessed even though
it has read permission, and can be removed even when • No superuser permission is required
it is write protected
• The default permissions of a directory are ls –l dept.lst
rwxr-xr-x (755)
• A directory must never be writable by group and others
-rw-r--r-- 1 kumar metal 139 jun 8 16:43 dept.lst
chgrp dba dept.lst;
ls –l dept.lst
-rw-r--r-- 1 kumar dba 139 jun 8 16:43 dept.lst
45 49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
sort : ordering a file wc – COUNTING LINES, WORDS AND
CHARACTERS
Sorting is the ordering of data in ascending or descending sequence. The sort
command orders a file and by default, the entire line is sorted
This default sorting sequence can be altered by using certain options. We
can also sort one or more keys (fileds) or use a different ordering rule.
sort options
The important sort options are:

-k n sorts on nth field


-k m,n starts sort on mth field and ends sort on nth field
-k m.n starts sort on nth column of mth field
-u removes repeated lines
-n sorts numerically
-r reverses sort order
-m list merges sorted files in list
-c checks if file is sorted
-o flname places output in file flname
50 54
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

sort : ordering a file wc – COUNTING LINES, WORDS AND


CHARACTERS
sort –t“|” –k 2 shortlist
sorts the second field (name)

sort –t”|” –r –k 2 shortlist or


sort –t”|” –k 2r shortlist
sort order can be revered with this –r option.
sort –t”|” –k 3,3 –k 2,2 shortlist

sorting on secondary key is also possible as shown above.

sort –t”|” –k 5.7,5.8 shortlist

we can also specify a character position with in a field to be the beginning of sort
as shown above (sorting on columns).
51 55
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

head - display the start of a file wc – COUNTING LINES, WORDS AND


CHARACTERS

head displays the head, or start, of the file.


Syntax
head [options] file
Common Options
-n number number of lines to display, counting from the top of the file
-number same as above

Examples
By default head displays the first 10 lines. You can display more with the "-n
number", or "-number" options, e.g., to display the first 40 lines:
head -40 filename or head -n 40 filename

52 56
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

tail - display the end of a file Tee Command


tail displays the tail, or end, of the file. Tee command is used to store and view (both at the same time)
Syntax the output of any other command.
tail [options] file Tee command writes to the STDOUT, and to a file at a time as
Common Options shown in the examples below
-number number of lines to display, counting from the bottom of The following command writes the output only to the file and not
the file to the screen.
Examples $ ls > file
The default is to display the last 10 lines, but you can specify
different line or byte numbers, or a different starting point within The following command (with the help of tee command) writes
the file. To display the last 30 lines of a file use the -number the output both to the screen (stdout) and to the file.
style:
$ ls | tee file
tail -30 filename
53 57
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
top - display top CPU processes IMP Note to Self
SYNOPSIS
top [-] [d delay] [p pid] [q] [c] [C] [S] [s] [i] [n iter] [b] DESCRIPTION
top provides an ongoing look at processor activity in real time.
It displays a listing of the most CPU-intensive tasks on the system, and
can provide an interactive interface for manipulating processes.

It can sort the tasks by CPU usage, memory usage and runtime. can be
better configured than the standard top from the procps suite.

Most features can either be selected by an interactive command or by


specifying the feature in the personal or system-wide configuration file.
See below for more information.

58 2
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

59 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda

 Commands : grep
• Pattern matching and Shell Meta characters

• What is Shell Programming

• Need for Shell Programming

• Shell Programming Variants

• Writing some Scripts


• Interactive shell scripting
• Arithmetic in shell
• Variables in shell with more examples.
60 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

grep Command

grep to search a file(s) for a pattern and display.


grep options to display, count, line numbers or
Course Name : filenames
Systems Programming Regular expressions
Basic regular expressions (BRE)
Extended regular expressions (ERE)
sed to edit / manipulate an input stream
substitution features
repeated and remembered patterns

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
grep Command grep Command

grep : searching for a pattern • When grep is used with multiple filenames, it
grep options displays the filenames along with the output
Basic regular expressions (BRE)
An introduction
grep “director” emp1.lst emp2.lst
The character class
The *
The dot Where it shows filename followed by the contents
Specifying pattern locations Metacharacters

6 10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

grep Command grep frequently used options

• It scans the file / input for a pattern and displays -i ignores case for matching
lines containing the pattern, the line numbers or -v doesn’t display lines matching expression
filenames where the pattern occurs
-n displays line numbers along with lines
-c displays count of number of occurrences
• It’s a command from a special family in UNIX for -l displays list of filenames only
handling search requirements

grep options pattern filename(s)

7 11
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

emp.lst (Data used for grep) grep Command


• 2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
• 9876 | jai sharma | director | production | 12/03/50 | 7000
• 5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
• 2365 | barun sengupta | director | personnel | 11/05/47 | 7800 -e exp specifies expression with this option
• 5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400 -x matches pattern with entire line
• 1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
• 6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300 -f file takes pattrens from file, one per line
• 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600 -E treats pattren as an extended RE
• 4290 | jayant choudhury | executive | production | 07/09/50 | 6000
• 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000 -F matches multiple fixed strings
• 6521 | lalit chowdury | directir | marketing | 26/09/45 | 8200
• 3212 | shyam saksena | d.g.m. | accounts | 12/12/55 | 6000
• 3564 | sudhir agarwal | executive | personnel | 06/07/47 | 7500
• 2345 | j. b. sexena | g.m. | marketing | 12/03/45 | 8000
• 0110 | v.k.agrawal | g.m.| marketing | 31/12/40 | 9000 8 12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

grep Command grep Command

grep “sales” emp.lst


1. grep -i ‘agarwal’ emp.lst
2. grep -v ‘director’ emp.lst > otherlist
• Patterns with and without quotes is possible
wc -l otherlist will display 11 otherlist
• Its generally safe to quote the pattern
1. grep –n ‘marketing’ emp.lst
• Quote is mandatory when pattren involves more
than one word 2. grep –c ‘director’ emp.lst
• It returns the prompt in case the pattren can’t be 3. grep –c ‘director’ emp*.lst
located will print filenames prefixed to the line count
grep president emp.lst
9 13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
grep Command grep Command

grep “[aA]g[ar][ar]wal” emp.lst


1. grep -i ‘manager’ *.lst
will display filenames only
• A single pattern has matched two similar strings
1. grep -e 'Agarwal' -e 'aggarwal' -e 'agrawal'
emp.lst • The pattern [a-zA-Z0-9] matches a single
alphanumeric character. When we use range,
will print matching multiple patterns
make sure that the character on the left of the
hyphen has a lower ASCII value than the one on
the right
Negating a class (^) (caret)

14 18
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

BASIC REGULAR EXPRESSIONS THE *

• It is tedious to specify each pattern separately * Zero or more occurrences of the previous character
with the -e option
• grep uses an expression of a different type to g* nothing or g, gg, ggg, etc.
match a group of similar patterns
• if an expression uses meta characters, it is grep “[aA]gg*[ar][ar]wal” emp
termed a regular expression
• Some of the characters used by regular
Notice that we don’t require to use –e option three
expression are also meaningful to the shell
times to get the same output!!!!!

15 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

BRE character subset THE DOT

A dot matches a single character


* Zero or more occurrences
g* nothing or g, gg, ggg, etc.
.* signifies any number of characters or none
. A single character
.* nothing or any number of characters
grep “j.*saxena” emp.lst
[pqr] a single character p, q or r
[c1-c2] a single character within the ASCII
range represented by c1 and c2

16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

The character class ^ and $

• grep supports basic regular expressions (BRE) Most of the regular expression characters
by default and extended regular expressions are used for matching patterns, but there are two
(ERE) with the –E option that can match a pattern at the beginning or end
of a line
• A regular expression allows a group of
characters enclosed within a pair of [ ], in which ^ for matching at the beginning of a line
the match is performed for a single character in $ for matching at the end of a line
the group

17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
grep Command The ERE set

grep “^2” emp.lst ch+ matches one or more occurrences of


Selects lines where emp_id starting with 2 character ch
ch? Matches zero or one occurrence of
grep “7…$” emp.lst character ch
Selects lines where emp_salary ranges between
7000 to 7999 exp1|exp2 matches exp1 or exp2
(x1|x2)x3 matches x1x3 or x2x3
grep “^[^2]” emp.lst
Selects lines where emp_id doesn’t start with 2
22 26
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

When meta characters lose their meaning Matching multiple patterns

• It is possible that some of these special characters grep -E 'sengupta|dasgupta' emp.lst


actually exist as part of the text
• Sometimes, we need to escape these characters We can locate both without using –e option twice,
Eg: when looking for a pattern g*, we have to use \ or
To look for [, we use \[
To look for .*, we use \.\* grep -E '(sen|das)gupta' emp.lst

23 27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

EXTENDED RE (ERE) Essential Shell programming

• If current version of grep doesn’t support ERE, then


use egrep but without the –E option
• -E option treats pattern as an ERE

+ matches one or more occurrences of the


previous character
? Matches zero or one occurrence of the previous
character

24 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

grep ERE What is Shell?

b+ matches b, bb, bbb, etc. • A Shell Is a program that interprets commands


• It allows a user to execute commands
b? matches either a single instance of b or nothing • Either manually at a terminal or
automatically in programs called shell scripts
These characters restrict the scope of match as
• A shell is not an operating system
compared to the *
• It is a way to interface with the operating
grep –E “[aA]gg?arwal” emp.lst system and execute commands
• Login shell is BASH = Bourne Again Shell
25 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Advantage of using Shell Shell Meta characters

1. File name short hands


– Can pick up a whole set of file names as arguments to
a program by specifying a pattern for the names
– Shell will find all the filenames that match the pattern
2. Input – Output redirection
– Can take input from a file and can redirect output to a
file
3. Personalizing the environment
– Can define own commands and short hands
30 34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Bash Shell Meta characters


Shell provides a mechanism for generating a list
of file names that match a pattern
– Example: ls ‐l *.c
– All file names in the current directory that end in .c
– The character * is a pattern that will match any string
including the null string
– This mechanism is useful both to save typing and to
select names according to some pattern

31 35
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Wild cards Shell Scripts


Wild cards – Special characters interpreted by shell The shell may be used to read and execute
commands contained in a file
• Assume a scenario where commands who, ls and
date are executed one after the other
• To write a shell script to achieve this:
– Open a new file [say firstShellPgm.sh] in vi editor
– Type all the commands that you want to get
executed one after the other. i.e. who, ls and date
– Save the file using :wq option.
32 36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Wild card – “*” Wild card – “?”

33 37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Examples Shell Scripts/Shell Programs

• Group of commands have to be executed


regularly
• Stored in a file
• File itself executed as a shell script or a shell
program by the user.
• A shell program runs in interpretive mode.
• Shell scripts are executed in a separate child shell
process which may or may not be same as the
login shell.

38 42
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System Variables in Shell Shell Scripts

Example: script.sh
#! /bin/sh
# script.sh: Sample Shell Script
echo “Welcome to Shell Programming”
echo “Today’s date : `date`”
echo “This months calendar:”
cal `date “+%m 20%y”` This month’s calendar.

echo “My Shell :$ SHELL”

39 43
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

What is Shell Programming Shell Scripts

To run the script we need to first make it


executable. This is achieved by using the chmod
• Grouping a set commands
command as shown below:
• Programming constructs used $ chmod +x script.sh
Then invoke the script name as:
$ script.sh

40 44
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Need for Shell Programming Shell Scripts

• To execute a set of commands regularly


Explicitly spawn a child with script name as
• Typing every time every command is laborious & time argument:
consuming
• To have control on the sequence of
sh script.sh

commands to be executed based previous results Note: Here the script neither requires a executable
permission nor an interpreter line.

41 45
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Read: Making Scripts Interactive Using Command Line Arguments

#! /bin/sh
• Shell’s internal tool for making scripts interactive echo “Program Name : $0”
• Used with one or more variables. echo “No of Arguments : $#”
• Inputs supplied with the standard input are read echo “Arguments are : $*”
into these variables.
Ex: read name $ chmod +x 2.sh
causes the script to pause at that point to take $ 2.sh A B C
input from the keyboard. o/p Program Name : 2.sh
No of Arguments : 3
Arguments are : A B C
46 50
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Read: Making Scripts Interactive

47 51
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using Command Line Arguments IMP Note to Self

• Shell scripts accept arguments from the command


line.
• Run non interactively
• Arguments are assigned to special shell variables
(positional parameters).
• Represented by $1, $2, etc;

48 52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using Command Line Arguments Contact Session - 9

Course Name :
Systems Programming

Shell Scripting
49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
IMP Note to Self Using Command Line Arguments

• Shell scripts accept arguments from the command


line.
• Run non interactively
• Arguments are assigned to special shell variables
(positional parameters).
• Represented by $1, $2, etc;

2 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students Using Command Line Arguments

฀ It is important to know that just login to the session does not


guarantee the attendance.
฀ Once you join the session, continue till the end to consider you
as present in the class.
฀ IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
฀ Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

3 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Agenda Shell Scripts

#!/bin/bash
Current Session CS09 echo "Hello, Student”
▪ Decision making constructs
▪ Shell scripts with more examples # This script displays the date, time, username and
▪ Conditional structure # current directory.
echo "Date and time is "
date
echo
echo -e "Your username is: `whoami` \n"
echo -e "Your current directory is: \c"
pwd
4 8
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Shell Scripts/Shell Programs Shell Scripts


#Reading user input
• Group of commands have to be executed regularly #To read standard input into a shell script use the read command.
• Stored in a file echo "Please enter your name:"
read name
• File itself executed as a shell script or a shell program by echo "Welcome to Testing Department Mr. $name"
the user.
# This prompts the user for input, assigns this to the variable name and
• A shell program runs in interpretive mode. then displays the value of this variable to standard output.

• Shell scripts are executed in a separate child shell # If there is more than one word in the input, each word can be assigned
process which may or may not be same as the login shell. to a different variable.
# Any words left over are assigned to the last named variable.
echo -e "\nPlease enter your surname\n"
echo -e "Followed by your first name: \c"
read name1 name2
echo -e "\nWelcome to Wipro $name2 $name1“
5 9
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Shell Scripts Starting
# Script to print user information who currently login , current date & time $ echo '#!/bin/sh' > my-script.sh
#
$ echo 'echo Hello World' >> my-script.sh
clear
echo -e "\nHello $USER" $ chmod 755 my-script.sh
echo -e "\nToday is \c ";date $ ./my-script.sh
echo -e "\nNumber of user login : \c" ; who | wc -l Hello World
echo -e "\nCalendar\n" $
cal

# declaration and printing of string variable • Note that to make a file executable, you must set the
#!/bin/bash eXecutable bit, and for a shell script, the Readable bit
x='Beautiful World' must also be set:
echo $x
echo x # just string 'x' • chmod a+rx first.sh

10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Shell Scripts Commands

# printing of string constant • grep "mystring" /tmp/myfile


#!/bin/bash • cat /tmp/myfile | grep "mystring“
echo 'hello'
echo "hello"
Which is more efficient?
echo hello

#!/bin/bash
# Script that demos, command line args
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* "
11
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Shell Scripts My first script

# printing of string constant #!/bin/sh


#!/bin/bash # This is a comment!
echo 'hello' echo Hello World # This is a comment, too!
echo "hello"
echo hello • A new copy of the shell is invoked
• # is for command
#!/bin/bash
# Script that demos, command line args
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* "
12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Variables

#var.sh
#!/bin/sh
MY_MESSAGE="Hello World"
SOME SIMPLE SCRIPTS echo $MY_MESSAGE

• MY_MESSAGE is a variable
• echo is used to echo the value on console
• $ sign is used to signify a variable

BITS Pilani, Pilani Campus BITS Pilani,


BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956
Input Handling Variables

# var1.sh # var3.sh
#!/bin/sh #!/bin/sh
echo "What is your name?"
echo What is your name?
read USER_NAME
read MY_NAME echo "Hello $USER_NAME"
echo "Hello $MY_NAME - hope you're well." echo "I will create you a file called ${USER_NAME}_file"
touch "${USER_NAME}_file"
• MY_NAME is a variable
• read helps to read the value from console • The touch command changes attriobutes of a file and
creates a new file of 0 bytes if it does not exist
• echo is used to echo the value on console
• Note the use of {} arounf the variable name to separate it
• $ sign is used to signify a variable from text.

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Scope of Variables Escape Characters

# myvar2.sh $ echo Hello World


#!/bin/sh Hello World
echo "MYVAR is: $MYVAR" $ echo "Hello World"
MYVAR="hi there" Hello World
echo "MYVAR is: $MYVAR" $ echo "Hello \"World\"“
Output: Hello "World"
$ ./myvar2.sh
MYVAR is: • Note how SPACES are interpreted with and without
MYVAR is: hi there QUOTES.
Note: MYVAR hasn't been set to any value, so it's blank. • Note the use of \ to give character meaning to the
special character “

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Export *

$ MYVAR=hello echo * shows all files in the current directory.


$ ./myvar2.sh $ echo *
MYVAR is: case.shtml escape.shtml first.shtml
MYVAR is: hi there functions.shtml hints.shtml index.shtml
• Note: When you call myvar2.sh from your interactive shell, a new ip-primer.txt raid1+0.txt
shell is spawned to run the script. $ echo *txt
$ export MYVAR
ip-primer.txt raid1+0.txt
$ ./myvar2.sh
MYVAR is: hello $ echo "*"
MYVAR is: hi there *
• Note: We need to export the variable for it to be inherited by another $ echo "*txt"
program - including a shell script. *txt
• But changing the value does not export it back.

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Run in the same shell Special characters

$ MYVAR=hello $ echo "A quote is \", backslash is \\, backtick is \`."


$ echo $MYVAR A quote is ", backslash is \, backtick is `.
hello $ echo "A few spaces are ; dollar is \$. \$X is ${X}."
$ . ./myvar2.sh A few spaces are ; dollar is $. $X is 5.
MYVAR is: hello
MYVAR is: hi there
$ echo $MYVAR
hi there

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956
The Logical Operators && and ||- Conditional
Execution

The || delimits two commands: cmd2 is executed only when cmd1 fails.
Example:
$ grep ‘manager’ emp.lst || echo “Pattern not found”
Pattern not found

SOME MORE CONCEPTS This delimiter may be used in a shell script as follows:

grep “$1” $1 || exit 2 Do not continue the script if search fails


Echo “Pattern found – Job Over”

You have seen the for simple decision you can use && and ||. If statements need to
be considered only for more complex decisions.

30
Systems Programming 26
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

exit and Exit Status of Command The if Conditional

C programs and Shell Scripts have the same command ( function in C) to terminate
the process. exit in Shell and exit() in C.

exit 0 Used when everything went fine in the scriot


exit 1 Used to indicate that there was an error in processing.

Normally success does not need to be explicitly stated but the exit condition for
failure is often used explicitly.

Example if a grep command does not find a pattern the exit function with a non-zero
argument is invoked exit(1) command may be used.
$ cat foo
cat: can’t open foo
Returns a nonzero exit status because it could not open the file. The sheel offers a
variable ($?) and a command (test) that evaluates a command exit status.

27 31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

exit and Exit Status of Command Shell Scripts

Parameter $? And its use #!/bin/bash


The variable stores the exit status of the last commend.
• 0 for success.
#Script to print file
• Nonzero for failure if cat $1
The value of the parameter is set by the argument of exit. then
If no exit status is specified the value is 0 ( true or success)
Example:
echo -e "\n\nFile $1, found and successfully
$ grep director emp.lst > /dev/null: echo $? echo ed“
0 Success fi
$ grep manager emp.lst > /dev/null: echo $?
1 Failure – in pattern search
$ grep manager emp3.lst > /dev/null: echo $? #!/bin/bash
2 Failure – in opening file # Script to see whether argument is positive
2 These returns conditions are used by programmers to control the flow of the
if test $1 -ge 0
program. then
echo "$1 number is positive“
28 fi 32
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

The Logical Operators && and ||- Conditional The if Conditional


Execution

Shell provides two operators that allow conditional execution - && and || as follows:

cmd1 && cmd2


cmd1 || cmd2

The && delimits two commands: cmd2 is executed only when cmd1 succeeds.
Example:
$ grep ‘director’ emp.lst && echo “Pattern found in file”

29 33
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Using test and [] to evaluate
Shell Script
expressions
# if ... elif ... else

#!/bin/bash
echo 'Enter a number'
read x
if [ $x -eq 10 ]
then
echo "Entered value is 10"
elif [ $x -eq 100 ]
then
echo "Entered value is 100"
else
echo "Entered value is not 10 and not 100"
fi
34 38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions –


Shell Scripts
Numeric Comparison

# comparison -lt and -gt, nested if

#!/bin/bash
echo -n 'Enter a number'
read x
if [ $x -gt 10 ]
then
if [ $x -lt 100 ]
then
echo "Entered value is greatedthan 10 and lessthan 100"
fi
fi

35 39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions –


Shell Scripts
Numeric Comparison
#!/bin/bash
# Script to see whether argument is positive
if test $1 -gt 0
then
echo "$1 number is positive"
fi

# Script to test rm command and exist status

if rm $1
then
echo "$1 file deleted"
fi
36 40
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions –


Shell Scripts Numeric Comparison
# very simple 'if'
#!/bin/bash
echo 'Enter a number '
read x
if [ $x -eq 10 ]
then
echo "Entered value is 10"
fi #!/bin/bash
echo 'Enter a number'
read x
if [ $x -eq 10 ]
then
echo "Entered value is 10"
else
echo "Entered value is not 10"
fi
37 41
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Using test and [] to evaluate expressions – The case Conditional
Numeric Comparison

42 46
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions – The case Conditional


Numeric Comparison

43 47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions –


Numeric Comparison The case Conditional – Matching Multiple Patterns

44 48
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Using test and [] to evaluate expressions –


Numeric Comparison The case Conditional – Wild-Cards

45 49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
expr – Computation and String handling expr – String Handling

50 54
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

expr – Computation expr – String Handling

51 55
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

expr – Computation QUIZ

1) Which of the following code snippets correctly checks if


a file named report.txt exists and is not empty, and if it is
not, prints "File is available and has content"?
a) if [ -e report.txt ] && [ -s report.txt ]; then
echo "File is available and has content"
fi
b) if [ -e report.txt -a -s report.txt ]; then
echo "File is available and has content"
fi
c) if [[ -e report.txt ]] || [[ -s report.txt ]]; then
echo "File is available and has content"
fi
d) if [ -e report.txt ] && [ -n report.txt ]; then
echo "File is available and has content"
fi
52 Answer : a 56
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

expr – String Handling 2) What will be the output of the following script if the variable
num is set to 15?
num=15
if [ $num -lt 10 ]; then
echo "Number is less than 10"
elif [ $num -gt 10 ] && [ $num -lt 20 ]; then
echo "Number is between 10 and 20"
else
echo "Number is 20 or more"
fi
a) Number is less than 10
b) Number is between 10 and 20
c) Number is 20 or more
d) Syntax error
Answer : b
53
57
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
3) Consider the following case statement. What will be the output ANSWER
if the user inputs "y"?
#!/bin/bash
read -p "Continue? (y/n): " answer file="example.txt"
case $answer in
y|Y) echo "Continuing...";; if [ ! -e "$file" ]; then
n|N) echo "Stopping...";; echo "File does not exist. Creating $file..."
touch "$file"
*) echo "Invalid input";; fi
esac if [ ! -s "$file" ]; then
echo "This is a new file" > "$file"
a) Continuing... echo "Added text to new file."
b) Stopping... else
echo "File is not empty. Appending date and time..."
c) Invalid input echo "$(date)" >> "$file"
d) Syntax error fi
Answer : a echo "Displaying contents of $file:"
cat "$file"
58 62
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Which of the following correctly tests if a number stored in var IMP Note to Self
is either greater than 100 or less than 10?

a) if [ $var -gt 100 ] -o [ $var -lt 10 ]; then


echo "Condition met"
fi
b) if [[ $var -gt 100 || $var -lt 10 ]]; then
echo "Condition met"
fi
c) if [ $var -gt 100 ] || [ $var -lt 10 ]; then
echo "Condition met"
fi
d) if [ $var -gt 100 -o $var -lt 10 ]; then
echo "Condition met"
fi
Answer : b and c (both are correct )

63
59
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

What is the output of the following script if x=5 and y=10?

if (( x + y > 10 )); then


echo "Sum is greater than 10"
elif (( x + y == 10 )); then
echo "Sum is exactly 10"
Course Name :
else
echo "Sum is less than 10" Systems Programming
fi
a) Sum is greater than 10
b) Sum is exactly 10
c) Sum is less than 10
d) Syntax error CS09- March 31, 2024
Answer : a Shell Scripting

60
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

IMP Note to Self


Scenario
You need to write a shell script that performs different
operations on a file based on its attributes. The script should
 Check if the file exists. If not, create it.
 If the file is empty, write "This is a new file" to it.
 If the file is not empty, append the current date and time to
the file.
 Finally, display the contents of the file.
Task
 Implement the script using decision-making constructs.
 Consider edge cases, such as incorrect file paths or
permission issues.

2
61
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
IMP Note to Students
 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

3
Systems Programming 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

STARTING WITH LOOPS

Systems Programming 4 Systems Programming 8


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Systems Programming 5 Systems Programming 9


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Systems Programming 6 Systems Programming 10


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Systems Programming 11
BITS Pilani, Pilani Campus BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Systems Programming 12
BITS Pilani, Pilani Campus BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

More for ..

#for2.sh
#!/bin/sh
for i in hello 1 * 2 goodbye
do
echo "Looping ... i is set to $i"
done

• Note how * gets all file name.


• Try a variation in wild cards
• Note that I loops in a set.

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956
Loops - while

#while1.sh
#!/bin/sh
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done

BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956

Pyramid printing using while

rows=5 # Print stars


N digit Rev i=1 j=1
while [ $i -le $rows ] while [ $j -le $((2 * i - 1)) ]
do do
134 4 4 echo -n "*"
j=$i
j=$((j + 1))
13 3 43 while [ $j -lt $rows ] done
1 1 431 do echo ""
echo -n " "
0 j=$((j + 1)) i=$((i + 1))
Fibon n=5 done done
Output:
Ifbo0 =0
*
Ifbo =1 ***
Fibo2 =1display 0 1 1 2 3 5 8 …………… *****
*******
*********
24
BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Shell script to find sum of first N numbers


from number M (take N and M from the user)

25
BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 Systems Programming - V 3.0 BITS Pilani, Pilani Campus

26
Break and Continue
Break
• There are some situations where we want to jump out of a loop instantly,
without wanting to get back to control-command.
• The keyword break allows us to do this.
• When the keyword break is encountered inside any loop, control automatically
passes to the first statement after the loop.
• A break is usually associated with if.
• The loop comes to end on execution of break.
Continue
• In some situations we want to take the control to the beginning of the loop,
bypassing the statements inside the loop which have not yet been executed.
• The keyword continue allows us to do this.
• When the keyword continue is encountered inside the loop, control
automatically passes to the beginning of the loop.
• A continue is usually associated with and if.
for loop for command line arguments
BITS Pilani,
BITS Pilani, Deemed to be University under Section PilaniAct,
3 of UGC Campus
1956 4 Ja n 2 0 1 4 Prepa red f o r W I LP BITS Pilani, Pilani Campus
You can break out of a for loop using 'break' Shell script program to find factorial of a positive
integer N (take N from the user). fact(N)=1×2×…..×N
command as shown below
Using for loop

clear echo "Input N"


i=1 read N
fact=1
for day in Mon Tue Wed Thu Fri if [ $N -lt 0 ]
do then
echo "Weekday $((i++)) : $day" echo "Wrong arguments"
exit 1
if [ $i -eq 3 ]; then
fi
break; for ((i=1; i<=N; i++))
fi do
fact=`expr $fact \* $i` sh filename.sh
done Input N
O/P: done
5
Weekday 1 : Mon echo "The factorial of $N is $fact"
Weekday 2 : Tue
The factorial of 5 is 120
27 31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Example adds "(WEEKEND)" to Sat and Sun, and Script to print the first N numbers in Fibonacci series
"(weekday)" to rest of the days. Given, f0=0, f1=1, and fn=fn‐1+fn‐2 for all n>=2, N is taken from
the user by using read command, for loop
echo "Input N"
clear read N
i=1 fib0=0
fib1=1
for day in Mon Tue Wed Thu Fri Sat Sun fib2=1 O/P
do Input N
if [ $N -lt 0 ]
5
echo -n "Day $((i++)) : $day" then 0
echo "Wrong arg" 1
if [ $i -eq 7 -o $i -eq 8 ]; then exit 1 1
continue; O/P fi 2
Day 1 : Mon (weekday) for ((i=0; i<N; i++)) 3
fi Day 2 : Tue (weekday) do
echo " (weekday)" Day 3 : Wed (weekday) echo "$fib0 "
Day 4 : Thu (weekday) fib0=$fib1
done Day 5 : Fri (weekday)
fib1=$fib2
Day 6 : Sat (WEEKEND)
Day 7 : Sun (WEEKEND)
fib2=`expr $fib0 + $fib1`
28 done 32
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

# Shell script to compute the sum of numbers


# passed to its argument on command line and display the result. Using while loop
echo "Input N"
clear read N
if [ $# -eq 0 ] fib0=0
then fib1=1
fib2=1 O/P
echo "No arguments passed" Input N
if [ $N -lt 0 ]
exit then 5
fi echo "Wrong arg" 0
1
sum=0 exit 1 1
for i in $* fi 2
do for ((i=0; i<N; i++)) 3
O/P do
sum=`expr $sum + $i` echo "$fib0 "
sh filename.sh 20 30 40 10 50
done Sum of Numbers is : 150 fib0=$fib1
echo "Sum of Numbers is : $sum" fib1=$fib2
fib2=`expr $fib0 + $fib1`
29 done 33
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Shell script program to find factorial of a positive


integer N (take N from the user). fact(N)=1×2×…..×N Nesting of Loops [for and while]
Using while loop
echo "Input N"
read N
fact=1
if [ $N -lt 0 ]
then
echo "Wrong arguments"
exit 1
fi
i=1
while [ $i -le $N ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "The factorial of $N is $fact"
30 34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Nesting of Loops [for and while] Files using Shell Scripting
while read line
do
x=`expr $line % 2`
if [ $x ‐eq 0 ]
then
echo $line
fi
done < integernos.txt

35 39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Nesting of Loops [for and while]


Take two integer numbers N and M from user
Files using Shell Scripting
• Display all the Multiplication table between N and M

Task
• Copy numLine number of lines from the
starting line number startNum of the file
named data.txt
• Store the copied lines in a file named
datanew.txt
• Take numLine and startNum as user input

36 40
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Take start range as N and end range as M from user Files using Shell Scripting
• Find all prime numbers between N and M
Method I
echo "Enter the start line number:”
read startNum
echo “Enter the number of lines to be
displayed:”
read numLine
endLine=$((startNum + numLine – 1))
head ‐n$endLine integernos.txt | tail
‐n$numLine >
37 41
Systems Programming - V 3.0 BITS Pilani, Pilani Campus datanew.txt Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Files using Shell Scripting Files using Shell Scripting

[Create a text file called integernos.txt and


store some random numbers in it (one
number in one line).]
Write a shell script program to display all the
even numbers from the file.

38 42
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
set and shift: Manipulating the Positional
Files using Shell Scripting Parameters

Copy the odd lines of the file named data.txt


to a file named odd.txt and copy the even
lines of the file named even.txt

43 47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Files using Shell Scripting shift: Shifting Arguments left


i=1
while read line
do
if [ `expr $i % 2` ‐eq 0 ]
then
echo $line >> even.txt
else
echo $line >> odd.txt
fi
i=`expr $i + 1`
done<integernos.txt
44 48
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Write a shell script to find all prime numbers


from integernos.txt file and store in another file Set -- : Helps Command Substitution
called primes.txt

45 49
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

set and shift: Manipulating the Positional


Parameters

46 50
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
awk
awk is a powerful text-processing tool in
Unix/Linux. It is used for pattern scanning and
Course Name : processing.
Systems Programming Example 1: Print specific columns
Suppose you have a file sp.txt with the following
content:
 A Gautam 25 Developer
 Chandrashekhar 30 Manager
 Rajeev 22 Designer 5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self


awk
Example 1: You want to print the names and their
job titles:

awk '{print $1, $3}' sp.txt

O/P:
A Gautam Developer
Chandrashekhar Manager
2 Rajeev Designer 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


awk
 It is important to know that just login to the session does not Example 2: Sum of column
guarantee the attendance. If you have a file system.txt consists of following
 Once you join the session, continue till the end to consider you numbers: 10,20,30,40
as present in the class. To calculate the sum of the numbers:
 IMPORTANTLY, you need to make the class more interactive by awk '{s+=$1} END {print s}' system.txt
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to O/P:
respond, otherwise it will be considered as ABSENT 100
3 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Agenda
getopts
getopts is a built-in shell function used to parse
 awk command-line options. Here is example f1.sh
#!/bin/bash
 getopts
while getopts "a:b:c" opt; do
 automation using shell case $opt in
 crontab a) echo "Option a with value: $OPTARG" ;;
b) echo "Option b with value: $OPTARG" ;;
at command for scheduling
c) echo "Option c selected" ;;
\?) echo "Invalid option: -$OPTARG" ;;
esac
4
done 8
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
getopts Automation Using Shell
 getopts: This built-in command is used to parse command-line  $BACKUP_DIR/backup_$(date +%F_%T).tar.gz: The backup file is
options. It reads options and their arguments, one by one, from saved in the directory specified by BACKUP_DIR.
the command line. The filename is backup_$(date +%F_%T).tar.gz, where:
$(date +%F_%T) generates a timestamp using the date
 "a:b:c": The string "a:b:c" specifies the valid options: command.
a:: Option a requires an argument (indicated by the colon : after a). %F produces the date in YYYY-MM-DD format.
b:: Option b also requires an argument. %T produces the time in HH:MM:SS format.
c: Option c does not require an argument (no colon after c).
The final backup file will have a name like backup_2024-08-
 opt: This variable stores the current option being processed. 24_15:30:01.tar.gz.
 /path/to/data: This is the path to the directory or files you want
 The while loop will continue to process options until all options to back up. Replace /path/to/data with the actual directory or
from the command line are parsed. files you need to back up.
Systems Programming 9 Systems Programming 13
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

getopts crontab
Execute the following command crontab is used to schedule recurring tasks.
./f1.sh -a 10 -b 20 –c Example 1: Run a Script Every Day at 2 AM
Edit crontab file
O/P: crontab –e
Option a with value: 10 Add the following line:
Option b with value: 20 0 2 * * * /path/to/script.sh
Option c selected

10 14
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Automation Using Shell crontab


Automation often involves combining shell Example 2: Run a Command Every 5 Minutes
scripts with other tools like awk, sed, etc.
Example 1: Automated Backup Script */5 * * * * /path/to/command
#!/bin/bash
• */5: This means "every 5 minutes." The */
# Backup Directory
BACKUP_DIR="/backup"
syntax specifies that the command should run
# Create Backup with Timestamp at every interval of 5 minutes.
tar -czf $BACKUP_DIR/backup_$(date +%F_%T).tar.gz /path/to/data • *: The remaining asterisks represent "every
# Print Message hour," "every day," "every month," and "every
echo "Backup created at $BACKUP_DIR" day of the week," respectively.
11 15
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Automation Using Shell at


 BACKUP_DIR="/backup": This line defines the path to at is used to schedule a command to run
the directory where the backup will be stored. The once at a particular time.
variable BACKUP_DIR holds this path. You can change
/backup to any directory where you want to save the Example 1: Run a Command at 3:30 PM
backup. echo "tar -czf /backup/backup.tar.gz
/path/to/data" | at 3:30 PM
 tar -czf:tar is a Unix/Linux utility used for creating • echo "tar -czf /backup/backup.tar.gz /path/to/data":
archive files. This echoes the command that you want to execute at the
-c creates a new archive. scheduled time.
-z compresses the archive using gzip. • | at 3:30 PM: The at command schedules the echoed
command to run at 3:30 PM on the current day.
-f specifies the filename of the archive to be created.
16
Systems Programming 12
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Shell Scripts : Demonstrations
Advanced Example
 You can combine all these tools to create an Script 1
Script to read values from the user and perform arithmetic operations

automated system monitoring script that runs #!/bin/bash


##Arithmetic operations using inputs read from the user
periodically and alerts you if something goes echo "Enter the values : "
read -p 'Enter Value 1: ' a
wrong. read -p 'Enter Value 2: ' b
ADD=$(($a+$b))
SUB=$(($a-$b))
MUL=$(($a*$b))

 Monitor.sh script will check the disk usage every


DIV=$(($a/$b))
echo -e "Addition=$ADD\nSubtraction=$SUB\nMultiplication=$MUL\nDivision=$DIV"

30 minutes and send an email if the usage


exceeds 90%.

17 Systems Programming 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Script for arithmetic operations using case statement

Advanced Example: monitor.sh


#!/bin/bash Script 2
#!/bin/bash
# Check Disk Usage ###Arithmetic Operations using case statement
DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//g') read -p 'Enter Value1 : ' a
read -p 'Enter value2 : ' b
# Threshold read -p 'Enter the operation[ADD|SUB|MUL|DIV]: ' op
case $op in
THRESHOLD=90 ADD) ADD=$(($a+$b))
echo "Addition = $ADD" ;;
# If Disk Usage Exceeds Threshold, Send an Email SUB) SUB=$(($a-$b))
if [ $DISK_USAGE -gt $THRESHOLD ]; then echo "Subtraction = $SUB" ;;
MUL) MUL=$(($a*$b))
echo "Disk usage is critically high: $DISK_USAGE%" | mail -s "Disk echo "Multiplication = $MUL" ;;
Usage Alert" [email protected] DIV) DIV=$(($a/$b))
echo "Divide = $DIV" ;;
Fi *) echo "Invalid Operation!!" ;;
esac
crontab Entry: */30 * * * * /path/to/monitor.sh
18 Systems Programming 22
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Script 3
Script to demonstrate the useage of Array Variables
Advanced Example: monitor.sh
###Script to demonstrate the usage of Array Variables
 df /: The df command reports the disk space usage of ###Defining the array variables individually
name[0]=Smith
file systems. df / checks the root ("/") file system. name[1]=Steve
 grep /: Filters the output to include only the line name[2]=Rohit
### Accessing the variables
containing the root file system. echo 'echo ${name[0]}='${name[0]}

 awk '{print $5}': Extracts the fifth column, which echo 'echo ${name[1]}='${name[1]}
echo 'echo ${name[2]}='${name[2]}
represents the percentage of disk space used. ### Accessing all values at a time
echo 'echo ${name[*]}='${name[*]}
 sed 's/%//g': Removes the percentage symbol (%) from echo 'echo ${name[@]}='${name[@]}
the value so that it can be compared as an integer. ### To get the array size
echo 'echo ${#name[*]}='${#name[*]}
echo 'echo ${#name[@]}='${#name[@]}

19 Systems Programming 23
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Script 4 :- Conditional Control Structures

Advanced Example: monitor.sh


if – statement

 echo "Disk usage is critically high: $DISK_USAGE%": Prepares the ###Script to check the type of file
#!/bin/bash
message body, including the current disk usage percentage. ###File test using if--else statement
FILE='/dev/sda'
if [ -c $FILE ]; then
echo "$FILE is a character special file"
 mail -s "Disk Usage Alert" [email protected] else
echo "$FILE is not a character special file"
pilani.ac.in: Sends an email with the subject "Disk Usage Alert" to fi

the specified email address. if [ -b $FILE ]; then


echo "$FILE is a block special file"
else
echo "$FILE is not a block special file"

 */30 * * * *: This crontab entry means the script will run every 30 fi
if [ -d $FILE ]; then
minutes. echo "$FILE is a directory"
else
echo "$FILE is not a directory"
fi
 /path/to/monitor.sh: Replace this with the actual path to your
20 Systems Programming 24
script.
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Script 5 Files using Shell Scripting
###Option parsing using GETOPTS
#!/bin/bash
###Option Parsing using GETOPTS
Task
if [ "$#" -ne "6" ]; then
echo "Invalid Parameters" • Copy numLine number of lines from the
exit 1
fi
while getopts a:b:c: var >/dev/null 2>&1
starting line number startNum of the file
do
case $var in
a) a=$OPTARG;;
named data.txt
b) b=$OPTARG;;
c) c=$OPTARG;; • Store the copied lines in a file named
*) echo Invalid Suffix ; exit 1;;

done
esac
datanew.txt
echo "Values given to suffix a = $a"
echo "Values given to suffix b = $b"
• Take numLine and startNum as user input
echo "Values given to suffix c = $c"

Systems Programming 25 29
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Script 6
Shell script for counting the total number of lines across a set of files. Files using Shell Scripting
#!/bin/bash
Method I
echo "Enter the start line number:”
# Counting the number of lines in a list of files
# for loop over arguments
if [ $# -lt 1 ]
then
echo "Usage: $0 file ..." read startNum
exit 1
fi
echo "$0 counts the lines of code"
echo “Enter the number of lines to be
l=0
n=0
displayed:”
s=0
for f in $*
do
read numLine
endLine=$((startNum + numLine – 1))
l=`wc -l $f | sed 's/^\([0-9]*\).*$/\1/'`
echo "$f: $l"
n=$[ $n + 1 ]

Done
s=$[ $s + $l ]
head ‐n$endLine integernos.txt | tail
echo "$n files in total, with $s lines in total"
‐n$numLine >
Systems Programming 26 30
BITS Pilani, Pilani Campus datanew.txt Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Files using Shell Scripting Files using Shell Scripting

[Create a text file called integernos.txt and


store some random numbers in it (one
number in one line).]
Write a shell script program to display all the
even numbers from the file.

27 31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Files using Shell Scripting


Files using Shell Scripting
while read line
Copy the odd lines of the file named data.txt
do
to a file named odd.txt and copy the even
x=`expr $line % 2`
if [ $x ‐eq 0 ] lines of the file named even.txt
then
echo $line
fi
done < integernos.txt

28 32
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Files using Shell Scripting shift: Shifting Arguments left
i=1
while read line
do
if [ `expr $i % 2` ‐eq 0 ]
then
echo $line >> even.txt
else
echo $line >> odd.txt
fi
i=`expr $i + 1`
done<integernos.txt
33 37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Write a shell script to find all prime numbers


from integernos.txt file and store in another file Set -- : Helps Command Substitution
called primes.txt

34 38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

set and shift: Manipulating the Positional


Parameters
Agenda

Need for protection


System calls
Communication between user mode and
Kernel mode
Execution of a system calls
Difference between system call & function
call

35 39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

set and shift: Manipulating the Positional


Parameters
Protection

• Protection refers to a mechanism which controls the


access of programs, processes, or users to the
resources defined by a computer system.

• We can take protection as a helper to multi


programming operating system, so that many users
might safely share a common logical name space such
as directory or files.

36 40
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Why we need protection? fscanf system call execution
 To prevent the access of unauthorized users
 To ensure that each active programs or processes in
the system uses resources only as the stated policy
 To improve reliability by detecting latent errors
 In single task / programming environment
– To protect OS from incorrect program
 In multiprogramming / multitasking environment
– To protect OS and other programs from
incorrect program
41 45
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

How to Protect the system scanf() work inside the OS

scanf() and printf() are functions in libc (the C standard


library), and they call the read() and write() operating
system syscalls respectively,

Talking to the file descriptors stdin and stdout respectively


(fscanf and fprintf allow you to specify the file stream you
want to read/write from).

42 46
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls Invoking system call handler &


executing system call
A system call is a function that allows a process to
communicate with the Linux kernel.

System calls expose the operating system’s resources to


user programs through an API (Application Programming
Interface).

System calls can only access the kernel framework.

System calls are needed for all the services that need
resources.
43 47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Steps for Making a System Call (Example: read call)

System calls

44 48
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Steps for Making a System Call Examples of System Calls
(Example: read call)
 A system call is implemented by a ``software interrupt'' that
transfers control to kernel code; • Example:
 in Linux/i386 this is ``interrupt 0x80''. The specific system call – getuid() //get the user ID
being invoked is stored in the EAX register, and its arguments are
held in the other processor registers. – fork() //create a child process
 In our example, the number associated to read is __NR_read, – exec() //executing a program => execve (2)
defined in <asm/unistd.h>.
in Linux
 After the switch to kernel mode, the processor must save all of its
registers and dispatch execution to the proper kernel function, • Don’t confuse system calls with libc calls
after checking whether EAX is out of range.
– Differences?
 The system call we are looking at is implemented in the sys_read
function. – Is printf() a system call?
 The read finally performs the data transfer and all the previous – Is rand() a system call?
steps are unwound up to the calling user function. 49 53
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Steps for Making a System Call System Call Example


(Example: read call) #include <syscall.h>
#include <unistd.h>
Using a system call #include <stdio.h>
#include <sys/types.h>
int main(void)
Each arrow in the figure represents a jump in CPU {
instruction flow, and each jump may require flushing long ID1, ID2;
the prefetch queue and possibly a ``cache miss'' /* direct system call */
event. /* SYS_getpid (func no. is 20) */
ID1 = syscall(SYS_getpid);
printf ("syscall(SYS_getpid)=%ld\n“,ID1);
Transitions between user and kernel space are /* "libc" wrapped system call */
especially important, as they are the most expensive /* SYS_getpid (Func No. is 20) */
in processing time and prefetch behavior. ID2 = getpid();
printf ("getpid()=%ld\n", ID2);
return(0);
50 } 54
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Why do you need system calls in User mode and Kernel mode Switching
Operating System?
There are various situations where you must require system calls in The User mode is normal mode where the process has limited
the operating system. access. While the Kernel mode is the privileged mode where
the process has unrestricted access to system resources like
hardware, memory, etc. A process can access I/O Hardware
 It is must require when a file system wants to create or delete a registers to program it, can execute OS kernel code and access
file. kernel data in Kernel mode. Anything related to Process
 Network connections require the system calls to sending and management, IO hardware management, and Memory
receiving data packets. management requires process to execute in Kernel mode.
 If you want to read or write a file, you need to system calls.
 If you want to access hardware devices, including a printer, This is important to know that a process in Kernel mode get
scanner, you need a system call. power to access any device and memory, and same time any
 System calls are used to create and manage new processes. crash in kernel mode brings down the whole system. But any
crash in user mode brings down the faulty process only.
51 55
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls vs. libc How to communicate ?


Each I/O system call has corresponding procedure calls from
the standard I/O library.

System calls Library calls


open Fopen
close Fclose
read fread, getchar, scanf, fscanf,
getc, fgetc, gets, fgets
write fwrite, putchar, printf, fprintf
putc, fputc, puts, fputs
Lseek fseek

52 56
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Use of A System Call to Perform I/O Types of System Calls
Process Control
Process control is the system call that is used to direct the
processes. Some process control examples include creating,
load, abort, end, execute, process, terminate the process,
etc.

File Management
File management is a system call that is used to handle the
files. Some file management examples include creating
files, delete files, open, close, read, write, etc.

57 61
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Difference between System call Types of System Calls


and function call
Device Management
Device management is a system call that is used to deal with devices.
Some examples of device management include read, device, write,
get device attributes, release device, etc.

Information Maintenance
Information maintenance is a system call that is used to maintain
information. There are some examples of information maintenance,
including getting system data, set time or date, get time or date, set
system data, etc.

Communication
Communication is a system call that is used for communication. There
are some examples of communication, including create, delete, etc.
58 62
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System Calls versus Function Calls System Call


Function Call System Call

Process Process

fnCall() sysCall()

OS

Caller and callee are in the same


Process
- Same user - OS is trusted; user is not.
- Same “domain of trust” - OS has super-privileges; user does not
- Must take measures to prevent abuse
59 63
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Types of System Calls

60 64
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related system calls ‐ open System call program example

65 69
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – creat, close File related system calls – lseek

66 70
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – read File related system calls – lseek

67 71
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – write File related system calls – mkdir, rmdir

68 72
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related system calls – rename • System calls related to process
– fork
– wait
– exec
– exit
– signal
– kill
– raise

73
77
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File related system calls – link Process summary

74 78
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – symlink Process related system calls – fork

75 79
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – unlink Process related system calls – fork

76 80
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
#include <stdio.h>
Process related system calls – fork #include <sys/types.h>
#include <unistd.h>
Calling fork()
int main()
{
pid_t p;
p = fork();
if(p==-1)
{
printf("There is an error while calling fork()\n");
}
if(p==0)
{ OUTPUT:
printf("We are in the child process\n"); sysads@linuxhint $ gcc fork.c -o fork
} sysads@linuxhint $ ./fork
else We are in the parent process
We are in the child process
{
printf("We are in the parent process\n");
}
return 0;
81 } 85
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process Creation Example Calling fork()


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> Output:
//main function begins This is the parent process
int main(){
pid_t p= fork(); //calling of fork system call This is the child process
if(p==-1)
printf("Error occured while calling fork()");
else if(p==0)
printf("This is the child process");
else
printf("This is the parent process");
return 0;
}

82 86
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork()
Calling fork()
Consider the following example in which we have used the fork() system call to create
a new child process:

#include <stdio.h> OUTPUT:


#include <sys/types.h> sysads@linuxhint $ gcc fork.c -o fork
#include <unistd.h> sysads@linuxhint $ ./fork
int main()
{
fork(); Using fork() system call
Using fork() system call
printf("Using fork() system call\n");
return 0;
} In this program, we have used fork(), this will create a new child
process. When the child process is created, both the parent process
and the child process will point to the next instruction (same
Program Counter).
That is 2n times, where n is the number of fork() system calls.
83 87
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork() IMP Note to Self


#include <stdio.h>
#include <sys/types.h> Output:
sysads@linuxhint $ gcc fork.c -o fork
#include <unistd.h>
sysads@linuxhint $ ./fork
int main()
{
fork();
fork(); Now the total number of process
created are 24 = 16 and we have our
fork();
print statement executed 16 times.
fork();
printf("Using fork() system call\n");
return 0;
}

84 88
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Agenda

System calls
System calls for Process management
Execution of system calls

Thank You

89 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System Call

Course Name :
Systems Programming

21st APR 2024

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self System calls for low level I/O

2 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students File related system calls ‐ open


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

3 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related system calls – creat, close File related system calls – lseek

8 12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – read File related system calls – lseek

9 13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – write File related system calls – mkdir, rmdir

10 14
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System call program example File related system calls – rename

11 15
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related system calls – link Process related system calls – fork

16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – symlink Process related system calls – fork

17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – unlink Process related system calls – fork

18 22
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls related to process Calling fork()


fork :- For creating a duplicate process from the parent process. Calling fork()
Consider the following example in which we have used the fork() system call to create
wait :- Processes are supposed to wait for other processes to a new child process:
complete their work.
exec :- Loads the selected program into the memory. #include <stdio.h> OUTPUT:
#include <sys/types.h> sysads@linuxhint $ gcc fork.c -o fork
exit :- Terminates the process #include <unistd.h> sysads@linuxhint $ ./fork
signal :- signal() system call installs a new signal handler for the int main()
signal with number signum. {
fork(); Using fork() system call
kill :- Takes two arguments. The first, pid, is the process ID you Using fork() system call
printf("Using fork() system call\n");
want to send a signal to, and the second, sig, is the signal you return 0;
want to send. Therefore, you have to find some way to know } In this program, we have used fork(), this will create a new child
process. When the child process is created, both the parent process
the process ID of the other party. and the child process will point to the next instruction (same
raise :- The raise() function sends the signal sig to the running Program Counter).
That is 2n times, where n is the number of fork() system calls.
program. 19
23
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Calling fork() Process related system calls – wait
#include <stdio.h>
#include <sys/types.h> Output:
sysads@linuxhint $ gcc fork.c -o fork
#include <unistd.h>
sysads@linuxhint $ ./fork
int main()
{
fork();
fork(); Now the total number of process
created are 24 = 16 and we have our
fork();
print statement executed 16 times.
fork();
printf("Using fork() system call\n");
return 0;
}

24 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork()
Process related system calls – wait
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t p;
p = fork();
if(p==-1) {
printf("There is an error while calling fork()\n");
}
if(p==0){
printf("We are in the child process\n"); OUTPUT:
} sysads@linuxhint $ gcc fork.c -o fork
else { sysads@linuxhint $ ./fork
printf("We are in the parent process\n"); We are in the parent process
} We are in the child process
return 0;
} 25 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork() Process related system calls – wait


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> Output:
//main function begins This is the parent process
int main()
{ This is the child process
pid_t p= fork(); //calling of fork system call
if(p==-1)
printf("Error occured while calling fork()");
else if(p==0)
printf("This is the child process");
else
printf("This is the parent process");
return 0;
}
26 30
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process Creation Example Process related system calls – wait


#include<stdio.h> [vasudevarao@localhost Systemcalls]$ gcc
#include<sys/wait.h> Wait1.c -o Wait1
#include<unistd.h> [vasudevarao@localhost Systemcalls]$ ./Wait1
Hello from parent
int main(){ Hello from child
if (fork()== 0) Bye
printf("HC: hello from child\n"); Child has terminated
Bye
else {
printf("HP: hello from parent\n");
wait(NULL);
printf("CT: child has terminated\n");
}
printf("Bye\n");
return 0;
27 } 31
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Process related system calls – wait Working of exec
• Current process image is overwritten with a new process
image.
• New process image is the one you passed as exec argument
• The currently running process is ended
• New process image has same process ID, same environment,
and same file descriptor (because process is not replaced
process image is replaced)
• The CPU stat and virtual memory is affected. Virtual memory
mapping of the current process image is replaced by virtual
memory of new process image.

32 36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

We have two .c files here


Output of system calls – wait exec.c
[vasudevarao@localhost Systemcalls]$ gcc Wait.c -o Wait #include <stdio.h>
[vasudevarao@localhost Systemcalls]$ ./Wait #include <unistd.h>
fork program starting #include <stdlib.h>
This is from parent process int main(int argc, char *argv[])
This is from child process I am exiting {
Child exited already now with exit status 512 printf("PID of exec.c = %d\n", getpid());
char *args[] = {"Helloexec", "C", "Programming", NULL};
execv("./helloexec", args);
printf("Back to exec.c");
return 0;
}

33 37
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

helloexec.c
Process related system calls – exec
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("We are in helloexec.c\n");
printf("PID of helloexec.c = %d\n", getpid());
return 0;
} Output :
PID of exec.c = 4733
We are in helloexec.c
PID of helloexec.c = 4733

34 38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – exec

35 39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Process related system calls – exit Process related system calls – signal

40 44
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – exit Process related system calls – signal
#include <stdio.h> else if (pid == 0)
#include <unistd.h> {
#include<sys/types.h> execl("/bin/ls","ls","-l",0);
#include<sys/wait.h> printf("Exec failed\n");
int main() exit(2);
{
}
pid_t pid;
else
int status;
const char *ls_argv[] = {"ls", "-a",0}; {
const char *ls_envp[] = printf("parent process\n");
{"PATH=/bin:/usr/bin",0}; }
printf("fork program starting \n"); wait(&status); // waitpid(pid,
pid = fork(); &status,0);
if (pid < 0 ) printf("Child exited already now I am
{ exiting\n");
printf("fork failed\n");
return 0;
exit(1);
} 41 45
}
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – exit Process related system calls – signal
Output:

vasudevarao@localhost Systemcalls]$ ./exit


fork program starting
parent process
total 156
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:56 exec
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:49 exex
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:59 exit
-rw-r--r--. 1 vasudevarao vasudevarao 624 Feb 23 19:56 exit.c
Child exited already now I am exiting

42 46
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – signal Process related system calls – signal

43 47
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Process related system calls – signal Process related system calls – kill & raise

48 52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – signal System calls for IPC: pipe()
The pipe() system function is used to open file
descriptors, which are used to communicate
between different Linux processes. In short,
the pipe() function is used for inter-process
communication in Linux.

49 53
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – signal System calls for IPC: pipe()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
int pipefds[2];
if(pipe(pipefds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
printf("Read File Descriptor Value: %d\n", pipefds[0]);
printf("Write File Descriptor Value: %d\n", pipefds[1]);
return EXIT_SUCCESS;
}
50 54
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – kill System calls for IPC: pipe()
• The header file of pipe() unistd.h
• The main() function, defined the pipefds two element integer
array.
• Then, ran the pipe() function to initialize the file descriptors
array pipefds as follows. pipe(pipefds)
• also checked for errors using the return value of the pipe()
function.
• It used the exit() function to terminal the program in case the
pipe function fails.
• Then, printed the value of the read and write pipe file
descriptors pipefds[0] and pipefds[1] respectively.

51 55
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
System calls for IPC: dup()
• dup() and dup2() create a copy of the file
descriptor oldfd.
• After a successful return from dup() or dup2(),the old Course Name :
and new file descriptors may be used interchangeably. Systems Programming
They refer to the same open file description
(see open(2)) and thus share file offset and file status
flags; for example, if the file offset is modified by
using lseek(2) on one of the descriptors, the offset is
also changed for the other.

56
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

System calls for IPC: dup() IMP Note to Self


#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int old_fd, new_fd;
old_fd=open("test.txt",O_RDWR);
printf("File descriptor is %d\n",old_fd);
new_fd=dup(old_fd);
printf("New file descriptor is %d\n",new_fd);
}
57 2
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

58 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda


Compiler
Assemblers
Linker and Loader
C Compilation Process

59 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Software The Compiler job 2 – VALIDATION

• Rejects programs that have errors in them

• What sort of error may be detected by a compiler?

1. Static checks occur at compile time.

2. Dynamic checks occur at run time.


• A compiler cannot detect whether a program will
produce the output that the programmer expects.

5 9
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

System Software The Compiler job 3 – OPTIMIZATION

• System software is computer software designed to • Tries to produce as good a target program as it can
operate the computer hardware and to provide a • Criterion for goodness could be
platform for running application software. 1. How fast the target program runs (most common)
• Examples : OR
– Loaders 2. Consumption of power and/or memory
– Assembler
• Differences among compilers depends on
– Linkers
• Their source and target languages,
– Operating system
as well as
– Compilers
• on the relative importance of these three jobs
– Editors
6 10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Compiler

A program that translates the code written according to a


language specs, to the code executable on the target platform

Systems Programming 11
Systems Programming 7
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

The Compiler job 1 – TRANSLATION

• Must ensure that Source program and target


program are equivalent.
• Compile time: when the compiler program runs
• Run time: when the target program runs
• The programming language in which the compiler
is implemented is the implementation language
Systems Programming 12
8
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Interpreter – A Poor Cousin of Compiler
Applications of Assembly Language
• Like Compilers
Interpreters also do translation of
• Assembly language is used for direct hardware manipulation,
source code to Machine code access to specialized processor instructions, or to address
BUT critical performances issues.
Can you see a role
• Unlike Compilers • Typical uses are device drivers(CD, HDD). Low-level embedded
for the Interpreter systems (Keyboard, water tank indicator) and real-time
They do this translation:
? systems (computer, notepad).
1. One line at a time and
2. Execute that code immediately before taking up
next line for translation
Can not generate Object code that can be linked & run
independently
Systems Programming 17
13
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Assemblers Assembler’s functions


• Convert mnemonic operation codes to their machine language
equivalents
• Convert symbolic operands to their equivalent machine
addresses
• Build the machine instructions in the proper format
• Convert the data constants to internal machine representations
• Write the object program and the assembly listing
• The assembler identifies the symbolic names associated with
each instruction and allocates memory to each instruction. It
also maintains a program counter or location counter (LC) to
keep track of the memory addresses of every instruction. As it
processes these instructions, it updates the LC and generates
14
the target machine code. 18
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Assemblers Elements of Assembly Language

1. Mnemonic Operation Code: The mnemonic operation codes


for machine instructions (also called mnemonic opcodes) are
easier to remember and use numeric operation codes.
Example: ADD, SUB, MOVE etc.
2. Symbolic Operands: A programmer can associate symbolic
names with data or instructions and use these symbolic
names as operands in assembly statements. Example: ADD
R1, R2, R3
3. Data Declaration: Data can be declared in a variety of
notations including the decimal notation. Example: NUM1 03
Or NUM! 0011H

15 Systems Programming 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

What is an Assembler & Assembly Language? Assembly Process


• Convert .asm file into .obj file
• An assembler is a type of computer program that takes in basic
instructions written in assembly language and converts them into a 1. Pass 1: Complete scan .asm file. Find all labels,
pattern of bits (machine language) that the computer's processor instructions and calculating corresponding address.
can use to perform basic operations. 2. Pass2: Convert all the instructions into machine language
• Assembly Language: Assembly language is a kind of low level format.
programming language, which uses symbolic codes or mnemonics 3. Symbol Table: Stored all the information of assembly
as instruction. language data, variables, instructions, address etc.
• Some examples of mnemonics include ADD, SUB, LDA and STA that
stands for addition, subtraction, load accumulator ans store
accumulator, respectively.

Systems Programming 16 Systems Programming 20


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Pass 1 of Assembler (Analysis Phase)
1. Symbol Table(St or SYMTAB): Store value or
address assign to the Label.
Label Addres
Example: How Assembler John
s
200

Converts Assembly L1
X
202
204

Language code 2. Literal Table (LT or LITAB): Store each literals


or constants(= ‘3’) with its location

Index Literal Address


0 =‘3’ 200
1 =‘2’ 202

Systems Programming 25
Systems Programming 21
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Example of Assembly Language with


Pass 1 of Assembler (Analysis Phase)
Assembler Directives
• Operation Code Table(OPTAB): Store Mnemonic operation code
with there opcodes and length.
Inst. Opcode Length(Bytes)
MOVER 3 2
MOVEM X 1
MOVER 2 2

Systems Programming 22 Systems Programming 26


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Example of Assembly Language with


Pass 2 of Assembler (Synthesis Phase)
Assembler Directives
• START: This instruction starts the execution of program from • In the second pass the instruction are again read and are assembled
location 200 and label with START provides name for the using the symbol table.
program(John is name for program) • Basically, the assembler goes through the block of program and
generates machine code for that instruction.
• MOVER: it moves the content of literal(=‘3’) into register operand
• Then the assembler proceeds to the next instruction. In this way, the
R1.
entire machine code program is created.
• MOVEM: it moves the content of register into memory • Convert mnemonic operation code, symbolic table operands with there
operand(X). equivalent machine code.
• MOVER: It again moves the content of literal(=‘2’) into register • Convert data constants to internal machine representations.
operand R2 and it label is specified as L1. • Convert complete program into .obj file.
• LTORG: It assigns address to literals( current LC value).
• DS(Data Space): It assigns a data space of 1 to Symbol X.
• END: It finishes the program execution.
Systems Programming 23 Systems Programming 27
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Design of working Assembler Linker and Loader


1. Analysis Phase (PASS 1 of Assembler):
1. To build symbol table for synthesis phase to
• Linking which combines two or more separate object
proceed.
2. Determines address of each symbols called as
memory allocation. programs and supplies the information needed to allow
3. Location counter used to hold address of next references between them
instruction.
4. Isolated label, mnemonic opcode, operands,
constants etc. • Loading and Allocation which allocates memory location
5. Validate meaning and address of each statements. and brings the object program into memory for execution
2. Synthesis Phase: (Pass 2 of Assembler):
1. Use data structures generated by analysis phase. • Linkers combine many separate pieces of a program, re-
2. To build machine instructions for every assembly
statements as per mnemonic code and there
organize storage allocation. Typically invoked invisibly by
address allocation. compilers.
3. Synthesis machine instruction as per source code.

Systems Programming 24 28
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Basic Definition We Need…three Processes
Loading - which allocates memory location and brings the •Loading - which allocates memory location and brings the object
object program into memory for execution - (Loader) program into memory for execution
•Relocation - which modifies the object program so that it can be
Linking- which combines two or more separate object loaded at an address different from the location originally
programs and supplies the information needed to allow specified.
references between them - (Linker) •Linking- which combines two or more separate object programs
and supplies the information needed to allow references between
Relocation - which modifies the object program so that it can them
be loaded at an address different from the location originally
specified - (Linking Loader) •A loader is a system program that performs the loading
function. Many loaders also support relocation and
linking. Some systems have a linker to perform the linking
and a separate loader to handle relocation and loading.
29 33
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Role Of Loader And Linker Basic Loader Functions

• A Loader is a system program that performs the


Memory

Source Object loading function


Assembler Linker
Program
Program Object
•It brings object program into memory and starts its
program execution
ready for
Executable execution

Code

Loader

30 34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Bootstrap Loaders
• Source Program – Assembly Language How is the loader itself loaded into the memory ?
• Object Program - From assembler
• Contains translated instructions and data values from
the source program • When computer is started – with no program in memory,
• Executable Code - From Linker a program present in ROM ( absolute address) can be
made executed – may be OS itself or A Bootstrap loader,
• Loader - Loads the executable code to the specified which in turn loads OS and prepares it for execution.
memory locations and code gets executed.
• The first record ( or records) is generally referred to as a
bootstrap loader – makes the OS to be loaded
• Such a loader is added to the beginning of all object
programs that are to be loaded into an empty and idle
system
31 35
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Need For Linking And Loading Three functions of a linker

 Combine all the pieces of a program.


 Figure out a new memory organization so that all
the pieces fit together (combine like sections).
 Touch up addresses so that the program can run
under the new memory organization.

Result: a runnable program stored in a new object file


called an executable.

32 36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Problems linker must solve Assembling

Assembly level code (.s file) is converted into a


 Assembler doesn't know addresses of external machine-understandable code (in binary/hexadecimal
objects when assembling files separately. form) using an assembler. Assembler is a pre-written
E.g. where is printf routine? program that translates assembly code into machine
- Assembler just puts zero in the object file for each code.
unknown address
 Assembler doesn't know where the things it's
assembling will go in memory
- Assume that things start at address zero, let linker
re-arrange.

37
Systems Programming 41
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Compiling a C Program: Behind


the Scenes Linking

Linking is a process of including the library files into our


program. Library Files are some predefined files that
contain the definition of the functions in the machine
language and these files have an extension of .lib.

The linking process generates an executable file with


an extension of .exe in DOS and .out in UNIX OS.

Systems Programming 38 Systems Programming 42


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Pre-Processing

Pre-processing is the first step in the compilation


process in C performed using the pre-processor
tool (A pre-written program invoked by the system
during the compilation). All the statements starting
with the # symbol in a C program are processed by
the pre-processor, and it converts our program file
into an intermediate file with no # statements.

1. Comments Removal,
2. Macros Expansion,
3. File inclusion
Systems Programming 39 Systems Programming 43
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Compiling a Java Program: Behind the


Scenes

Source Code Compilation (javac) ----Bytecode (.class)


(HelloWorld.java) |(Converts .java to HelloWorld.class -
.class files) Platform-Independent

JVM Interpreter/JIT
Compiler Bytecode Verifier Class Loader Subsystem
(Interprets bytecode (Ensures bytecode is (Loads .class files into
or compiles it into | | valid and secure) memory)
native machine code
for execution)

Runtime Environment
| | (Includes memory
Execution Engine | | management, Output
(Manages execution of garbage | | collection, (Console/GUI)
bytecode) thread management,
Systems Programming 40 Systems Programming 44
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Linkage Editors
•Source Code:
•You write the Java program in a .java file. Object
•Compilation: Program (s)

•The Java compiler (javac) compiles the .java file to a .class file
containing bytecode. Linkage
•Bytecode: Library editor

•The .class file contains platform-independent bytecode.


•Class Loader Subsystem:
•The JVM's Class Loader loads the .class files into memory. This Processing of an Object
Linked
program
subsystem is responsible for loading, linking, and initializing classes program using LE
and interfaces. Relocating
•Bytecode Verifier: loader
•The Bytecode Verifier checks the loaded bytecode to ensure it is
valid and does not violate Java’s security constraints. This step is Memory
crucial for maintaining Java’s security model. 49
Systems Programming 45
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

•JVM Interpreter/JIT Compiler:


Some Useful Functions…
•The JVM interprets the bytecode or uses the Just-In-Time (JIT)
compiler to translate the bytecode into native machine code for
faster execution. The choice between interpreting and JIT • An absolute object program can be created, if starting
compilation depends on the JVM implementation and runtime address is already known
conditions. • New versions of the library can be included without
•Execution Engine: changing the source program
•The Execution Engine is responsible for executing the bytecode
• Linkage editors can also be used to build packages of
instructions. It manages the actual execution of the instructions and subroutines or other control sections that are generally
interacts with the runtime environment. used together
•Runtime Environment:
• Linkage editors often allow the user to specify that
•The JVM runtime environment includes various services like external references are not to be resolved by
memory management, garbage collection, thread management, automatic library search – linking will be done later by
etc., to support the execution of the Java program. linking loader – linkage editor + linking loader – savings
•Output: in space
•The program produces output, which can be displayed in the
console, a GUI, or another interface.
Systems Programming 46
50
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Write an assembly language program to find the sum of two numbers using LC3
and assemble, load and execute the same using the LC3Simulator. Dynamic Linking

Step1:Write the program in Linux using vi editor and save it as sum.asm • Linkage editors perform linking before the program is
Program: loaded for execution.
• Linking loaders perform these same operations at
.ORIG x3000 ; Starting address of the program
; Load the first number into register R0
LD R0, NUM1 load time.
; Load the second number into register R1
• Dynamic linking postpones the linking function until
LD R1, NUM2 execution time.
; Add the contents of R0 and R1, store the result in R2 – A subroutine is loaded and linked to the test of the
ADD R2, R0, R1 program when it is first called.
; Store the result from R2 into memory location SUM
ST R2, SUM

; Halt the program 51


HALT Systems Programming 47
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Linking Loaders Dynamic Linking Application

Object
The source
• Dynamic linking is often used to allow several executing
Program(s)
program is first programs to share one copy of a subroutine or library.
assembled or
compiled,
producing an
• For example, a single copy of the standard C library can
Library Linking
object program. A
loader linking loader
performs all be loaded into memory.
linking and loading
operations, and
loads the program
Memory into memory for • All C programs currently in execution can be linked to
execution
this one copy, instead of linking a separate copy into
each object program.
Processing of an Object program using LL
48 52
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Swapping
Dynamic Linking Application

• A process can be swapped temporarily out of memory to a


In an object‐oriented system, dynamic linking is often backing store, and then brought back into memory for
continued execution
used for references to software object. – Total physical memory space of processes can exceed
• This allows the implementation of the object and its physical memory
• Backing store – fast disk large enough to accommodate copies
method to be determined at the time the program is of all memory images for all users; must provide direct access
to these memory images
run. (e.g., C++) • Roll out, roll in – swapping variant used for priority-based
scheduling algorithms; lower-priority process is swapped out
• The implementation can be changed at any time, so higher-priority process can be loaded and executed
without affecting the program that makes use of the • Major part of swap time is transfer time; total transfer time is
directly proportional to the amount of memory swapped
object. • System maintains a ready queue of ready-to-run processes
which have memory images on disk

53
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Swapping (Cont.)
Dynamic Linking Advantage

• Allow several executing programs to share one copy of • Does the swapped out process need to swap back in
a subroutine or library to same physical addresses?
• In an object oriented system, dynamic linking makes it • Depends on address binding method
possible for one object to be shared by several – Plus consider pending I/O to / from process
programs memory space
• Modified versions of swapping are found on many
• However, without using dynamic linking, these systems (i.e., UNIX, Linux, and Windows)
subroutines must be loaded and linked every time the – Swapping normally disabled
program is run. – Started if more than threshold amount of memory
• Using dynamic linking can save both space for storing allocated
the object program on disk and in memory, and time – Disabled again once memory demand reduced
below threshold
for loading the bigger object program.
54
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Schematic View of Swapping


Dynamic Linking Implementation

A subroutine that is to be dynamically loaded must be called via


an operating system service request
– This method can also be thought of as a request to a part of
the loader that is kept in memory during execution of the
program
• Instead of executing a JSUB instruction to an external symbol,
the program makes a load‐and call service request to the OS
• The parameter of this request is the symbolic name of the
routine to be called

55
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Dynamic Linking Implementation Contiguous Allocation

• The OS examines its internal tables to determines whether • Main memory must support both OS and user
the subroutine is already loaded. processes
• If needed, the subroutine is loaded from the library. • Limited resource, must allocate efficiently
• Then control is passed from the OS to the subroutine being • Contiguous allocation is one early method
called.
• Main memory usually into two partitions:
• When the called subroutine completes its processing, it
returns to its caller (operating system). – Resident operating system, usually held in
• The OS then returns control to the program that issues the low memory with interrupt vector
request. – User processes then held in high memory
• After the subroutine is completed, the memory that was – Each process contained in single contiguous
allocated to it may be released.
section of memory
56
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Contiguous Allocation (Cont.)
Causes of Fragmentation

• Relocation registers used to protect user processes


from each other, and from changing operating-system
• User processes are loaded and unloaded from
code and data the main memory, and processes are kept in
– Base register contains value of smallest physical memory blocks in the main memory. Many
address spaces remain after process loading and
– Limit register contains range of logical addresses –
each logical address must be less than the limit
swapping that another process cannot load
register due to their size. Main memory is available,
– MMU maps logical address dynamically but its space is insufficient to load another
– Can then allow actions such as kernel code being process because of the dynamical allocation of
transient and kernel changing size
main memory processes.
Systems Programming 65
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Hardware Support for Relocation and Limit Registers


Types of Fragmentation

• There are mainly two types of fragmentation


in the operating system. These are as follows:
• Internal Fragmentation
• External Fragmentation

Systems Programming 66
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Dynamic Storage-Allocation Problem


Internal Fragmentation
How to satisfy a request of size n from a list of free holes?

• First-fit: Allocate the first hole that is big enough • When a process is allocated to a memory block, and if the
process is smaller than the amount of memory requested, a
• Best-fit: Allocate the smallest hole that is big enough; must search free space is created in the given memory block. Due to this,
entire list, unless ordered by size the free space of the memory block is unused, which
– Produces the smallest leftover hole
causes internal fragmentation.
• Worst-fit: Allocate the largest hole; must also search entire list • For Example:
– Produces the largest leftover hole • Assume that memory allocation in RAM is done using fixed
partitioning (i.e., memory blocks of fixed sizes). 2MB, 4MB,
First-fit and best-fit better than worst-fit in terms of speed and storage 4MB, and 8MB are the available sizes. The Operating System
utilization
uses a part of this RAM.

Systems Programming 67
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Fragmentation

• Fragmentation is an unwanted problem in


the operating system in which the processes are
loaded and unloaded from memory, and free
memory space is fragmented. Processes can't be
assigned to memory blocks due to their small size,
and the memory blocks stay unused. It is also
necessary to understand that as programs are loaded
and deleted from memory, they generate free space
or a hole in the memory. These small blocks cannot
be allotted to new arriving processes, resulting in
inefficient memory use.
Systems Programming 64 Systems Programming 68
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
• Let's suppose a process P1 with a size • Let's take the example of external
of 3MB arrives and is given a memory block fragmentation. In the above diagram, you can
of 4MB. As a result, the 1MB of free space in see that there is sufficient space (50 KB) to
this block is unused and cannot be used to run a process (05) (need 45KB), but the
allocate memory to another process. It is memory is not contiguous. You can use
known as internal fragmentation. compaction, paging, and segmentation to use
the free space to execute a process.

Systems Programming 69 Systems Programming 73


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

How to avoid internal fragmentation?

• The problem of internal fragmentation may


arise due to the fixed sizes of the memory
blocks. It may be solved by assigning space to
the process via dynamic partitioning. Dynamic
partitioning allocates only the amount of
space requested by the process. As a result,
there is no internal fragmentation.

Systems Programming 70 Systems Programming 74


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

External Fragmentation

• External fragmentation happens when a


dynamic memory allocation method allocates
some memory but leaves a small amount of
memory unusable. The quantity of available
memory is substantially reduced if there is too
much external fragmentation. There is enough
memory space to complete a request, but it is
not contiguous. It's known
as external fragmentation.
Systems Programming 71 Systems Programming 75
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

IMP Note to Self

Systems Programming 72 76
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
C as a System Programming Language

• A system programming language is used to create system


software.
Course Name : • C language is a system programming language because it can be
used to do low-level programming (for example driver and
Systems Programming kernel).
• It is generally used to create hardware devices, OS, drivers,
kernels, etc. For example, Linux kernel is written in C.
• It can't be used for internet programming like Java, .Net, PHP,
etc.
14th Sep 2024

Systems Programming 5
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

IMP Note to Self Features of c programming


Structure Oriented
Fully Case Sensitive
Modular Approach
Portable
Self define Functions

2
Systems Programming 6
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

General Overview of a Simple C Program's


IMP Note to Students Structure
• The general architecture of a simple C program typically consists of
 It is important to know that just login to the session does not several vital components
guarantee the attendance. • Header Files:
– The #include directives at the beginning of the program are used to
 Once you join the session, continue till the end to consider you
include header files. Header files provide function prototypes and
as present in the class. definitions that allow the C compiler to understand the functions
used in the program.
 IMPORTANTLY, you need to make the class more interactive by • Main Function:
responding to Professors queries in the session. – Every C program starts with the main function. It is the program's
entry point, and execution starts from here. The main function
 Whenever Professor calls your number / name ,you need to has a return type of int, indicating that it should return an integer
respond, otherwise it will be considered as ABSENT value to the operating system upon completion

3 Systems Programming 7
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

General Overview of a Simple C Program's


Agenda Structure
• Variable Declarations:
 Basic of C language
– Before using any variables, you should declare them with their
 Pre-defined Functions and User-defined function data types. This section is typically placed after the main
function's curly opening brace.
 Need for protection
• Statements and Expressions:
 System calls – This section contains the actual instructions and logic of the
 Communication between user mode and Kernel program. C programs are composed of statements that perform
actions and expressions that compute values.
mode
• Comments:
 Execution of a system calls – Comments are used to provide human-readable explanations
 Difference between system call & function call within the code. They are not executed and do not affect the
program's functionality. In C, comments are denoted by // for
 List of Different System Calls single-line comments and /* */ for multi-line comments.
4 Systems Programming 8
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
C Functions Example: Library Function

• Functions are an essential component of the C programming // C program to illustrate inbuilt function
language. They help you divide bigger problems into smaller, more // Header file for printf function
manageable chunks of code, making it simpler to create and run #include <stdio.h>
programs.
• A function is a block of code that executes a particular task in // Driver Code
programing. It is a standalone piece of code that can be called from int main()
anywhere in the program. {
• The function contains the set of programming statements enclosed // Library function printf
by {}. printf(“Hello Everyone!");
• A function can be called multiple times to provide reusability and return 0;
modularity to the C program. }

Systems Programming 9 Systems Programming 13


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Syntax of Function in C langauge How to Install gcc Compiler on Linux?


• GCC stands for GNU Compiler Collections which is used to compile mainly C and C++
language.
return_type function_name(parameter list) { Follow the commands step by step to install the GCC.
// function body • Command 1: The very first step is to update the packages. This command is used to
download package information from all configured sources and to get the info of the
}
updated versions of the packages.
• The return_type specifies the type of value that the function will – sudo apt-get update Note: In the first command it will ask for your
return. If the function does not return anything, the return_type will password, make sure to enter the password correctly.
be void. • Command 2: Now we have to install the build-essential packages, which is also known as
• The function_name is the name of the function, and the parameter a meta-package, it contains the GCC compiler all the other essentials used to compile
the software written in C and C++ language.
list specifies the parameters that the function will take in.
– sudo apt install build-essential
• Command 3: After the second command it will install GCC on your Linux, to verify it is
installed correctly, check the version of the GCC.
– gcc –version
– Now, we have successfully installed the GCC on Linux. Note: Versions may vary from
Systems Programming 10 time to time. 14
Systems Programming
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

How to Compile C Program in Linux using the


Types of Functions
gcc compiler
• Step 1: Open your terminal, navigate to the directory containing your C
There are two types of functions in C programming: file using the cd command.
cd Documents
• Step 2: Type gcc myfile.c -o hello to compile your C file.
• Library Functions or Inbuilt Function
– The -o hello part of the command names the output file "hello".
• User-defined functions: gcc myfile.c -o hello
• Step 3: Run your program by typing ./myfile.
– You should see the output Hello Everyone! in your terminal,
indicating that your C program has successfully compiled and run.

Systems Programming 11 Systems Programming 15


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Library Functions or Inbuilt Function User-defined Functions

• These functions are the built-in functions i.e., they are


• A user-defined function is a function written by the user to
predefined in the library of the C.
write any program code and execute specific actions.
• These are used to perform the most common operations like
• These user-defined functions can be modified and execute
calculations, updation, etc.
according to the requirement of the programmer.
• Some of the library functions are printf, scanf, sqrt, etc. To use
• A programmer can change the user-defined function, but
these functions in the program the user has to use a header file
these functions are not defined in the C header files.
associated with the corresponding function in the program.
• A user-defined function is made up using the function
• For Example:
declaration, function definition, and the function call.
– If the user has to use print the data or scan the data using an input stream
then we have to use functions printf() and scanf() in C program. To use
these functions the user has to include #include<stdio.h> preprocessor
directive in C program.

Systems Programming 12 Systems Programming 16


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
User- defined Function: Cont’d Example 2: User Defined Function

Function Definition • Program to sum two numbers using user


defined function in C
• The function definition defines the actual body of the function that int sum_numbers(int x, int y) // pass x and
#include <stdio.h> y as the parameters
perform some specific tasks in a program.
int sum_numbers(int x, int y); // function {
Syntax: prototype int result;
return_type function_name ( data_type arg1, data_type arg2, ... ) { int main() result = x + y; //add two numbers
{ return result;
// define the variables }
int sum; // store the result
statement to be executed; int num1, num2; // declare variables
return (expr); printf(" Enter two numbers \n");
} scanf("%d %d", &num1, &num2);
• Here return type represents the function that can return any defined sum = sum_numbers(num1, num2); //
function call
data type values. The return type value can be int, float, char, double,
printf( " The addition of two numbers is %d",
etc., where the function_name represents the name of a function that sum);
contains more than one argument in it. return 0;
Systems Programming 17 Systems Programming 21
BITS Pilani, Pilani Campus } BITS Pilani, Pilani Campus

User- defined Function: Cont’d Explanation of Example 2:

Function Calling
• In this example, the sum_Numbers function is declared with a
After defining the function definition, we need to call the defined function in a
program to execute its tasks. However, a function can be called multiple times
function statement at the top of the file, which specifies its
by writing the function name followed by arguments lists. name, return type (int), and parameters (x and y, both are of
Syntax: int type).
function_name(arg1, arg2, ...) • The actual code for the sum_Numbers function is defined in
Here, arg1, arg2 are the actual arguments passed to the function_name. the function definition. Here, the function simply adds its two
Function Declaration parameters and returns the result.
The function declaration defines the function name, return type and the passed • The main function calls the sum_numbers function with
arguments in it. A function definition is always defined outside of the main() arguments , and stores the result in the result variable. Finally,
function in any C program.
it prints the result using the printf function.
Syntax:
return_type function_name ( data_type arg1, data_type arg2, ..) ;

Systems Programming 18 Systems Programming 22


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Difference between the user-defined and the


Example1: User Defined Function
library function in C
Program to calculate the area of circle User defined Function Library function
int func(int radius)
using user-defined function in C A programmer creates a user-defined function A function whose prototypes are already defined in
{
#include <stdio.h> according to the requirement of a program. the C library is called the library function.
int area;
int func(int radius); float pi = 3.14; A user-defined function is required to write the We don't require writing a complete code to use the
complete code before using the function in a library function in a program.
int main() area = pi * radius * radius; program.
{ return area;
The name of any user-defined function can We can't change or modify the name of the library
} change easily. function because the functionality of these functions
int rad, result;
is already defined in the compiler.
printf(" Enter the radius of a circle ");
A user-defined function is part of a program. Library functions are part of the C header file.
scanf("%d", &rad);
A user-defined function does not require writing All the library functions are predefined inside the
result = func(rad); any code inside the header files. header files.
printf (" The area of a circle is: %d", For example: swap() function does not require any For example: printf(), and scanf() function are defined
result); header file. in the stdio.h header file.

return 0; Example: multiply(), sum() divide(), etc. are the Exemple: printf(), sqrt(), strcpy(), etc.
user defined or user created function in a
} Systems Programming program. Systems Programming
19 23
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Explanation of Example 1 Protection

• In this example, the func function is declared with a function • Protection refers to a mechanism which controls the access of
statement at the top of the file, which specifies its name, programs, processes, or users to the resources defined by a
return type (int), and parameters (radius of int type). computer system.

• The actual code for the func function is defined in the • We can take protection as a helper to multi programming
function definition. Here, the function calculate the area of operating system, so that many users might safely share a
the circle using basic formula and returns the result. common logical name space such as directory or files.

• The main function calls the func function by passing argument


value entered by the user , and stores the result in the result
variable. Finally, it prints the result using the printf function.
Systems Programming 20 24
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Protection Issues

• CPU protection
– Prevent a user from using the CPU for too long
• Throughput of jobs, and response time to events (incl.
user interactive response time) Privileged Instruction Examples
• Memory protection • Memory address mapping
• Data cache flush and invalidation
– Prevent users from modifying kernel code and data • Invalidating TLB entries
structures • Loading and reading system registers
This is important to know that a process in • Changing processor mode from kernel to
– …and each others code and data Kernel mode get power to access any user
• I/O protection device and memory, and same time any
crash in kernel mode brings down the
• Changing the voltage and frequency of the
processor
– Prevent users from performing illegal I/O’s whole system. But any crash in user mode • Halting a processor
brings down the faulty process only. • Reset a processor
Systems Programming
• I/O operations
25
Systems Programming 29
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Why do we need protection? How to communicate between two modes?

 To prevent the access of unauthorized users • System calls have always been the means through which
user space programs can access kernel services.
 To ensure that each active programs or processes in the system
uses resources only as the stated policy
• System call is the only legal entry point to the kernel.
• System call provides an abstract hardware interface for
 To improve reliability by detecting latent errors
user space
 In single task / programming environment • Application need not worry about type of disk, media
– To protect OS from incorrect program and file system in use.
 In multiprogramming / multitasking environment
– To protect OS and other programs from incorrect
program

26 30
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

How to Protect the system What Are System Calls?

• Hardware Protection or Software protection? How?


• It is a programmatic approach that enables a computer program
– Preferably Hardware ( fast and effective) to request a service from the operating system's kernel.
– Dual- mode operation
• System call provides the operating system's services to user
• 1. User mode- execution done on behalf of user. programs via an API (Application Programming Interface).
• Monitor mode (also supervisor mode or system, mode) –
• A system call is an interface between a user-space application
execution done on behalf of operating system.
and a service that the kernel provides.
– Mode bit(one bit) added to computer hardware to indicate the
current mode: monitor (0) or user (1). • The kernel system's single-entry points are system calls.
– When an interrupt or fault occurs hardware switches to monitor • System calls are needed for all the services that need resources.
mode. • User level processes do not have the same permissions as the
– Privileged instructions can be issued only in monitor mode. processes directly interacting with the operating system.

Systems Programming 27 Systems Programming 31


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

The User Mode and The Kernel Mode How System Call Works?

• There are two modes available in a computer system: the kernel mode
and the user mode.
• In the computer system, various types of programs or apps are executed
on a computer system. Programs have direct access to hardware
resources and memory in kernel mode, which is more privileged than
user mode.
• In user mode, programs are unable to access memory and hardware
resources directly. Most programs run in the user mode.
• A program sends a system call request to the kernel when it needs a
hardware resource and memory. Following that, the mode changes
from user mode to kernel mode.
• When the work is finished, the mode switches back to user mode from
kernel mode. As a result, this mode transition is also known as context
switching. Systems Programming 28
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
System calls / library calls Examples
How System Call Works?

• Steps Each I/O system call has corresponding procedure calls from
– Step 1) The processes executed in the user mode till the time the standard I/O library.
a system call interrupts it.
System calls Library calls
– Step 2) After that, the system call is executed in the kernel-
mode on a priority basis. open Fopen
– Step 3) Once system call execution is over, control returns to close Fclose
the user mode., read fread, getchar, scanf, fscanf,
– Step 4) The execution of user processes resumed in Kernel getc, fgetc, gets, fgets
mode. write fwrite, putchar, printf, fprintf
putc, fputc, puts, fputs
lseek fseek

37
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Why do you need system calls in Operating System Calls versus Function Calls
System?
Function Call System Call
There are various situations where you must require system calls in
the operating system.
Process Process
 It is must require when a file system wants to create or delete a
file. fnCall() sysCall()

 Network connections require the system calls to sending and


receiving data packets.
 If you want to read or write a file, you need to system calls.
OS
 If you want to access hardware devices, including a printer,
scanner, you need a system call. Caller and callee are in the same
 System calls are used to create and manage new processes. Process
- Same user - OS is trusted; user is not.
- Same “domain of trust” - OS has super-privileges; user does not
- Must take measures to prevent abuse
34 38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Differences between the System Call and Library Examples of System Calls
Call
1. A system call is a kernel function that allows you to enter kernel mode
and access a hardware resource. On the other hand, a library call is a • Example:
function provided by a programming library to complete a task. – getuid() //get the user ID
2. The mode switches from user to kernel mode in the system call function. – fork() //create a child process
On the other hand, there is no mode switching in the library call
function. – exec() //executing a program => execve (2)
3. The system call takes longer to execute than the library call. On the other in Linux
hand, the library call is faster to execute than the system call.
• Don’t confuse system calls with libc calls
4. All functions are part of the kernel in the system call. On the other hand,
all library functions in a library call are part of the programming – Differences?
language's standard library file. – Is printf() a system call?
5. It is a program's request to access a process in kernel mode. On the – Is scanf() a system call?
other hand, a library call is a program's request to access a library
function provided in a programming library.

Systems Programming 35 39
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System call Versus Library call System Call Example

#include <syscall.h>
• A system call is executed in kernel space, whereas a library call #include <unistd.h>
is executed in user space. #include <stdio.h>
• A library call is a function given by programming libraries. In #include <sys/types.h>
contrast, a system call is a kernel function that allows you to int main(void)
enter kernel mode and access a hardware resource. {

• Library calls may rely on the system calls to finish the task. long ID1, ID2;
ID1 = syscall(SYS_getpid); /* direct system call SYS_getpid (func no. is 20) */
• Some instances of system calls are cd(), fork(). Some examples printf ("syscall(SYS_getpid)=%ld\n“,ID1); /* "libc" wrapped system call
of library calls are fopen(), fprintf (). SYS_getpid (func no. is 20) */
ID2 = getpid();
printf ("getpid()=%ld\n", ID2);
return(0);
}
Systems Programming 36 40
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Types of System Calls Communication System Call

• These types of system calls are specially used for inter-process


communications.
• Two models are used for inter-process communication
– Message Passing(processes exchange messages with one
another)
– Shared memory(processes share memory region to
communicate)
• The system calls under this are pipe() , shmget() ,mmap().

41 Systems Programming 45
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Process Control System Call Communication System Call


Process Control system calls perform the task of process creation, process pipe():
termination, etc. – The pipe() system call is used to communicate between different Linux
The Linux System calls under this are fork() , exit() , exec(). processes.
fork()
• A new process is created by the fork() system call. – It is mainly used for inter-process communication. The pipe() system
• A new process may be created with fork() without a new program being run-the function is used to open file descriptors.
new sub-process simply continues to execute exactly the same program that shmget():
the first (parent) process was running. – shmget stands for shared memory segment.
exit()
– It is mainly used for Shared memory communication.
• The exit() system call is used by a program to terminate its execution.
• The operating system reclaims resources that were used by the process after the – This system call is used to access the shared memory and access the
exit() system call. messages in order to communicate with the process.
exec() mmap():
• A new program will start executing after a call to exec() – This function call is used to map or unmap files or devices into memory.
• Running a new program does not require that a new process be created first: any
process may call exec() at any time. The currently running program is immediately
– The mmap() system call is responsible for mapping the content of the file
terminated, and the new program starts executing in the context of the existing to the virtual memory space of the process.
42 Systems Programming 46
process.
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File Management System Call Device Management System Call


• File management system calls handle file manipulation jobs like creating a file,
reading, and writing, etc. The Linux System calls under this are open(), read(), Device management does the job of device manipulation like reading
write(), close(). from device buffers, writing into device buffers, etc. The Linux System
open(): calls under this is ioctl().
– This system call just opens the file, to perform operations such as read and write, ioctl():
we need to execute different system call to perform the operations.
– ioctl() is referred to as Input and Output Control.
read():
– This system call opens the file in reading mode. We can not edit the files with – ioctl is a system call for device-specific input/output operations
this system call.Multiple processes can execute the read() system call on the and other operations which cannot be expressed by regular
same file simultaneously. system calls.
write():
– This system call opens the file in writing mode. We can edit the files with this
system call. Multiple processes can not execute the write() system call on the
same file simultaneously.
close():
– This system call closes the opened file. 43 Systems Programming 47
Systems Programming BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Information Maintenance System Call An Example of a System Call


Information maintenance is a system call that is used to maintain information. There are • Let us trace the sequence when a system call to open a file
some examples of information maintenance, including getting system data, set time or occurs.
date, get time or date, set system data, etc. The System calls under this are getpid(),
– User process executes a system call to open a file.
alarm(), sleep().
getpid(): – User process links to a C runtime library for open and sets up the
• getpid stands for Get the Process ID. needed parameters in registers.
• The getpid() function shall return the process ID of the calling process. – A SW trap is executed now and the operation switches to the
• The getpid() function shall always be successful and no return value is reserved kernel mode.
to indicate an error. • The kernel looks up the system call vector to call "open“
alarm():
• This system call sets an alarm clock for the delivery of a signal that when it has • The kernel tables are modified to open the file.
to be reached. • Return to the point of call with exit status.
• It arranges for a signal to be delivered to the calling process. • Return to the user process with value and status.
sleep():
• The user process may resume now with modified status on file or
• This System call suspends the execution of the currently running process for
abort an error with exit status.
some interval of time
• Meanwhile, during this interval, another process is given chance to execute
44
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
File Descriptors ... review File Descriptors ... continued

– File offset: The byte position in the file where the next I/O
operation through that open file description begins
• Each open file description associated with a regular file, block special
file, or directory has a file offset
• There is no file offset specified for a pipe or FIFO (described later)
• Important points
– A file descriptor does not describe a file
• It is just a number that is ephemerally associated with a particular
open file description
– The description of the file is in the inode
• There may be several different open file descriptors (or none)
referring to a file at any given time.

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File Descriptors File Descriptors

• Including Inode table, the kernel also contain two other data Implication of the file descriptor/open file
structures, the file table and the user file descriptor table. descriptions/inode table organization in UNIX
• The file table is a global kernel structure, but the user file – open and creat
descriptor is allocated per process. • search for the first empty slot in the process file descriptor table
• When a process opens or creates a file, the kernel allocates an • allocate an open file description in the file table, which has a
entry from each table, corresponding to the file’s inode. pointer to the inode table

• Entries in the three structures- user file descriptor table, file – dup and dup2
table, and inode table- maintain the state of the file and the • Duplicate the file descriptor in the process file descriptor table
user’s access to it • Where is the current file position stored?

BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File Descriptors

• The file table keeps track of the byte offset in the file where the user’s
next read or write will start, and the access rights allowed to the
opening process.
• The user file descriptor table identifies all open file for a process and
their relationship to each other.
• The kernel returns a file descriptor for the open and create system
calls, which is an index into the user file descriptor table.
• When executing read and write system calls, the kernel uses the file
descriptor to access the user file descriptor tables, follows pointers to
the file table and inode table entries, and from the inode, finds the
data in the file.

BITS Pilani, Pilani Campus

File Descriptors ... continued


• The POSIX standard defines the following
– File descriptor: A per-process, unique, nonnegative integer
used to identify an open file for the purposes of file access
– Open file description: A record of how a process or group of
processes are currently accessing a file
• A file offset, file status, and file access modes are attributes of an
open file description
– File access modes: Specification of whether the file for read
or write.
– File status: Includes the following information
• append mode or not
• blocking/nonblocking

BITS Pilani, Pilani Campus


System Call IMP Note to Self

57 2
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

58 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda

System calls
File related system calls
Process related system calls
Execution of system calls

59 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System Call
• System calls are interfaces provisioned by the
operating system to allow user-level applications to
interact with low-level hardware components & make
Course Name : use of all the services provided by the kernel.
Systems Programming
• Types of System calls:
 File System :(open, create, close, read, write, mkdir, …)
 Process Control :(fork, wait, exec, exit, signal, kill, …)
 Memory Management :(brk, sbrk, mmap, munmap, mlock, …)
 Inter-process Communication : (pipe, socket, shmget, ...)
28th SEP 2024  Device Management :(setConsolMode, readConsol, writeConsol, …)

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related System calls - open Ex-1: To read first 10 characters from a file.

int open(const char *filepath, int flags, ,... /* mode_t mod */):

• Used to open or create a file


• Returns the file descriptor or -1 in case of an error.
• Flag can be O_RDONLY, O_WRONLY, O_RDWR, …
• Number of arguments can be 2 or 3.
• The third argument is used only when creating a new file.
• To open an existing file only two arguments are used.
• This can be used in read, write, lseek and close system calls.

O/P: The file descriptor of the file is: 3


6 abcdefghi
Systems Programming 10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File related system calls – creat, close Ex-1: To read first 10 characters from a file.

int creat(const char *path, mode_t mod): • The operating system assigns internally to each opened file a
• Used to create a new file. descriptor or an identifier (usually this is a positive integer). The
• Returns the file descriptor or -1 in case of an error. value of the file descriptor will always be a positive number
• This call is equivalent with: greater than 2.
open(path, O_WRONLY | O_CREAT | O_TRUNC, mod); • fd=open("test.txt",O_RDONLY): Opens test.txt in read mode
• The argument path specifies the name of the file, while and the file descriptor is saved in integer fd.
mod defines the access rights. • The printf() statement prints the value of the file descriptor.
• If the created file doesn’t exist, a new i-node is allocated • n=read(fd,buff,10): Reads 10 characters from the file pointed
and a link is made to this file from the directory it was by the file descriptor fd and save them in buffer buff.
created in. • write(1,buff,n): Writes the content of the buffer on the screen.
int close(int fd): • <sys/types.h>, <sys/stat.h>, and <fcntl.h> header files are
• Used to close the file. required for open().
• It returns 0 in case of success and -1 in case of an error. • <unistd.h> is required for read write and close.
Systems Programming 7 Systems Programming 11
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File related system calls – read Ex-2: To copy the contents of an existing file into another file.

ssize_t read(int fd, void* buf, size_t cnt):

• Used to read a certain number of bytes starting from the


current position in a file.
• Returns the number of bytes read, 0 for end of file (EOF)
and -1 in case of an error.
• It reads cnt bytes from the open file referred by the file
descriptor fd and it puts it into a buffer buf.
• If cnt is greater than number ssize_MAX the result is
unspecified.
• The pointer (current position) is incremented automatically
after a reading that certain amount of bytes.

Systems Programming 8 Systems Programming 12


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File related system calls – write Ex-2: To copy the contents of an existing file into another file.

ssize_t write(int fd, const void* buf, size_t cnt): • This example copies the contents of the file “test.txt” to a new file
named “dup1.txt” using system calls.
• Used for writing a certain number of bytes into a file • Line 10 opens the file “test.txt” in read only mode.
starting from the current position . • Line 11, checks if the file is not opened successfully then prints a
• Returns the number of bytes written, and -1 in case of an message “Error in opening file” and terminates.
error. • Line 16, opens a file “dup1.txt” for writing purpose.
• It writes cnt bytes from the buffer buf into the file that has • O_WRONLY : Opened for writing
as its descriptor fd. • O_CREAT : Creates the file if it doesn't exist, otherwise open for writing.
• O_TRUNC: Truncates the file to zero length if it already exists.
• Line 17, repeats a loop if number of bytes read is more than zero.
The read system call reads specific number of bytes into buffer
from the file referred by the file descriptor.
9
Systems Programming 13
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Ex-2: To copy the contents of an existing file into another file. File related system calls – mkdir, rmdir
• Line 19, writes n byte from buffer into file referred by fd1.
• Line 21, if there is problem in reading from file, otherwise prints
successful message.
• Finally, line 25 and 26 close the files.

• After execution for the copied file “dup1.txt” permission may need
to be changed.

18
Systems Programming 14
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-3
File related system calls – rename

15 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – lseek Ex-4: To create, rename and remove a directory

off_t lseek(int fd, off_t offset, int ref):


• Used to position a pointer (that points to the current
position) in an absolute or relative way. Read and write
operations are done relative to the current position in the
file.
• The function returns the displacement of the new current
position from the beginning of the file or -1 in case of an
error.
• The reference ref is set to 3 type of values:
SEEK_SET: The positioning is relative to the beginning of the file.
SEEK_CUR: The positioning is relative to the current position.
SEEK_END: The positioning is relative to the end of the file.

16
Systems Programming 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

File related system calls – lseek File related system calls – link
test1.txt::
abcdefghijklmnop

abcdefghijklmnop
^
O/P: klmno

abcdefghijklmnop
^
O/P: fghij

21
Systems Programming 17
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
File related system calls – symlink Process related system calls – fork

22 26
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

File related system calls – unlink Process related system calls – fork

23 27
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-4: To demonstrate link, symlink and unlink system calls Process related system calls – fork

28
Systems Programming 24
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls related to process Calling fork()


fork :- For creating a duplicate process from the parent process. Calling fork()
Consider the following example in which we have used the fork() system call to create
wait :- Processes are supposed to wait for other processes to a new child process:
complete their work.
exec :- Loads the selected program into the memory. #include <stdio.h> OUTPUT:
#include <sys/types.h> sysads@linuxhint $ gcc fork.c -o fork
exit :- Terminates the process #include <unistd.h> sysads@linuxhint $ ./fork
signal :- signal() system call installs a new signal handler for the int main()
signal with number signum. {
fork(); Using fork() system call
kill :- Takes two arguments. The first, pid, is the process ID you Using fork() system call
printf("Using fork() system call\n");
want to send a signal to, and the second, sig, is the signal you return 0;
want to send. Therefore, you have to find some way to know } In this program, we have used fork(), this will create a new child
process. When the child process is created, both the parent process
the process ID of the other party. and the child process will point to the next instruction (same
raise :- The raise() function sends the signal sig to the running Program Counter).
That is 2n times, where n is the number of fork() system calls.
program. 25
29
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Calling fork() Process related system calls – wait
#include <stdio.h>
#include <sys/types.h> Output:
sysads@linuxhint $ gcc fork.c -o fork
#include <unistd.h>
sysads@linuxhint $ ./fork
int main()
{
fork();
fork(); Now the total number of process
created are 24 = 16 and we have our
fork();
print statement executed 16 times.
fork();
printf("Using fork() system call\n");
return 0;
}

30 34
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork()
Process related system calls – wait
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t p;
p = fork();
if(p==-1) {
printf("There is an error while calling fork()\n");
}
if(p==0){
printf("We are in the child process\n"); OUTPUT:
} sysads@linuxhint $ gcc fork.c -o fork
else { sysads@linuxhint $ ./fork
printf("We are in the parent process\n"); We are in the parent process
} We are in the child process
return 0;
} 31 35
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Calling fork() Process related system calls – wait


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h> Output:
//main function begins This is the parent process
int main()
{ This is the child process
pid_t p= fork(); //calling of fork system call
if(p==-1)
printf("Error occured while calling fork()");
else if(p==0)
printf("This is the child process");
else
printf("This is the parent process");
return 0;
}
32 36
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-5: To demonstrate fork system calls Process related system calls – wait
#include<stdio.h> [vasudevarao@localhost Systemcalls]$ gcc
#include<sys/wait.h> Wait1.c -o Wait1
#include<unistd.h> [vasudevarao@localhost Systemcalls]$ ./Wait1
Hello from parent
int main(){ Hello from child
if (fork()== 0) Bye
printf("HC: hello from child\n"); Child has terminated
Bye
else {
printf("HP: hello from parent\n");
wait(NULL);
printf("CT: child has terminated\n");
o/p:
}
printf("Bye\n");
return 0;
} 37
Systems Programming 33
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
We have two .c files here
Ex-6: To demonstrate wait system call
exec.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("PID of exec.c = %d\n", getpid());
char *args[] = {"Helloexec", "C", "Programming", NULL};
execv("./helloexec", args);
printf("Back to exec.c");
return 0;
o/p: }

38 42
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-6: To demonstrate wait system call helloexec.c

• A pid_t variable named pid is declared to store the process ID #include <stdio.h>
returned by fork(). #include <unistd.h>
• The fork() system call is called, creating a new child process. #include <stdlib.h>
• The return value of fork() is checked to determine whether the int main(int argc, char *argv[])
current process is the child or parent. {
• If the current process is the child, it prints a message and then printf("We are in helloexec.c\n");
exits using return 0. printf("PID of helloexec.c = %d\n", getpid());
• If the current process is the parent, it prints a message and then return 0;
calls wait(&status). } Output :
• The wait() system call suspends the parent process until the child PID of exec.c = 4733
We are in helloexec.c
process terminates. The status argument is used to retrieve PID of helloexec.c = 4733
information about the child process's termination.
39 43
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – exec Ex-7: To demonstrate exec system call

• The exec system call family in C is used to replace the current o/p
process with a new process image. This means that the entire
memory space, program counter, and other process attributes
are replaced with those of the new program.
• Working of exec:
• Current process image is overwritten with a new process
image. New process image is the one you passed as exec
argument. The currently running process is ended.
• New process image has same process ID, same environment, • This program replaces the current process with the ls command,
and same file descriptor (because process is not replaced which lists the contents of the root directory.
process image is replaced) • The args array contains the arguments to the ls command, and
• Virtual memory mapping of the current process image is • the envp array is empty, indicating that no environment variables
replaced by virtual memory of new process image. are passed to the new process.
Systems Programming 40 Systems Programming 44
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Exec family Ex-8: To demonstrate exec system call

• execl(const char *path, const char *arg, ...): Takes a path to the
executable file and a list of arguments, including the program name.
• execlp(const char *file, const char *arg, ...): This function is
similar to execl, but it searches the environment variable PATH to find
the executable file.
• execle(const char *path, const char *arg, ..., char *envp[]):
Takes an additional argument envp, which is an array of environment
variables to be passed to the new process.
• execvp(const char *file, const char *argv[]): This function is The program uses execlp to search the PATH for an executable file
similar to execlp, but it takes an array of arguments instead of individual named ls and passing -l as argument to the new program. The new
strings. program is the same program used by the shell command ls to list
• execve(const char *path, char *argv[], char *envp[]): This files in a directory.
function is the most flexible exec system call, allowing you to specify the
path to the executable, an array of arguments, and an array of
environment variables.
Systems Programming 41 Systems Programming 45
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Ex-9: To demonstrate exec system call
IMP Note to Self
o/p

The program uses execvp to search the PATH for an executable file
named ls and passing -l as argument to the new program. The new
program is the same program used by the shell command ls to list
files in a directory. In comparison to using execv we don’t have to
provide the full path to ls when using execvp, only the name of the
executable. 2
Systems Programming 46
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Students


 It is important to know that just login to the session does not
guarantee the attendance.
 Once you join the session, continue till the end to consider you
as present in the class.
 IMPORTANTLY, you need to make the class more interactive by
responding to Professors queries in the session.
 Whenever Professor calls your number / name ,you need to
respond, otherwise it will be considered as ABSENT

47 3
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self Agenda

System calls
System calls for Process management
System calls for memory management

48 4
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

CS-14 Overview
• What is system call? Types of system calls.
• File Related System calls
• Open, Create, Close, Read, write, lseek, mkdir, rmdir, rename,
Course Name : link, simlink, unlink covered.
• Process Related System calls
Systems Programming
• Fork, wait, exec covered
• We will continue with other process related system calls such
as: exit, signal, kill, raise

5
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Process related system calls – exit Ex-1: exit
• execl("/bin/ls", "ls", "-l", 0): Replaces the current process with the ls
command. The four arguments represent the following: path to the executable
(/bin/ls), executable name, then the arguments for ls command and the last
argument 0 indicate the end of the argument list. If execl() fails (e.g., due to
incorrect path or permissions), it prints an error message and exits.

• wait(&status); or waitpid(pid, &status, 0): Causes the parent process to wait


for the child process to terminate. The status variable receives the exit status
of the child process.

• printf("Child exited already now I am exiting\n");: Prints a message indicating


that the child process has terminated. return 0;: Returns 0 to indicate
successful execution.

6
Systems Programming 10
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Ex-1: exit Process related system calls – signal


#include <stdio.h> else if (pid == 0)
#include <unistd.h> {
#include<sys/types.h> execl("/bin/ls","ls","-l",0);
#include<sys/wait.h> printf("Exec failed\n");
int main() exit(2);
{
}
pid_t pid;
else
int status;
const char *ls_argv[] = {"ls", "-a",0}; {
const char *ls_envp[] = printf("parent process\n");
{"PATH=/bin:/usr/bin",0}; }
printf("fork program starting \n"); wait(&status); // waitpid(pid,
pid = fork(); &status,0);
if (pid < 0 ) printf("Child exited already now I am
{ exiting\n");
printf("fork failed\n");
return 0;
exit(1);
} 7 11
}
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-1: exit Process related system calls – signal


Output:

vasudevarao@localhost Systemcalls]$ ./exit


fork program starting
parent process
total 156
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:56 exec
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:49 exex
-rwxrwxr-x. 1 vasudevarao vasudevarao 21984 Feb 23 19:59 exit
-rw-r--r--. 1 vasudevarao vasudevarao 624 Feb 23 19:56 exit.c
Child exited already now I am exiting

8 12
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-1: exit Process related system calls – signal


• The program demonstrates the use of the fork() and execl() system calls to create
a child process and execute the ls command with specific arguments and
environment variables. It also illustrates the use of wait() or waitpid() to
synchronize the parent process with the child process.

• pid_t pid: Stores the process ID of the child process.


int status: Stores the exit status of the child process.
const char *ls_argv[]: An array of strings passed as arguments to ls command.
const char *ls_envp[]: An array of strings representing environment variables.

• pid = fork();: Creates a child process. If pid < 0: Forking failed, print an error
message and exit. If pid == 0: In the child process, execute the ls command using
execl(). If pid > 0: In the parent process, proceed to the next step.

13
Systems Programming 9
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Process related system calls – signal Ex-2: signal
Given C code
demonstrates how to
handle the SIGINT signal
(typically generated by
pressing Ctrl+C) and
write a message to a file
before exiting.
o/p

14 18
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – signal Ex-2: signal


• (void)signal(SIGINT, leave): Registers the leave function to handle the SIGINT
signal.
• temp_file = fopen("tmp", "w"): Opens the file "tmp" in write mode.
• for(;;): This is an infinite loop that will continue until the SIGINT signal is
received or the program is terminated manually.
• printf("Ready...\n"): Prints a message indicating that the program is ready.
• (void)getchar(): Waits for the user to press any key.
• exit(EXIT_SUCCESS): Exits the program with a success status. This statement is
unreachable because the infinite loop will continue until the SIGINT signal is
received.
• fprintf(temp_file, "\nInterrupted...\n"): Writes the message "Interrupted...\n"
to the temporary file.
• fclose(temp_file): Closes the temporary file.
• exit(sig): Exits the program with the signal number as the exit status. This
indicates that the program was terminated due to the SIGINT signal.

15
Systems Programming 19
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Process related system calls – signal Process related system calls – kill

16 20
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Process related system calls – signal Process related system calls – kill & raise

17 21
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus
Ex-3: kill System calls for IPC: pipe()
• The program demonstrates
the use of the fork system • The header file of pipe() unistd.h
call to create a child process. • The program creates a pipe using the pipe function.
• The child process performs a
simple task (sleeping for 10
• A pipe is a special type of file that allows communication
seconds) and then exits. between processes. It consists of two file descriptors: one for
• The parent process sends an reading and one for writing.
interrupt signal to the child • The pipe function returns an array of two integers, pipefds[0] and
process using kill. pipefds[1], which represent the read and write file descriptors,
• The parent process waits for
the child to terminate using
respectively.
wait and prints its exit status. • The program then prints the values of these file descriptors to
the console.

26
Systems Programming 22
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

Ex-4: raise System calls for IPC: dup()


• The program demonstrates the use of the
raise system call to send a signal to the • dup() and dup2() create a copy of the file
current process.
• The signal function is used to register a descriptor oldfd.
signal handler to handle the received
signal.
• After a successful return from dup() or dup2(),the old
• The pause function is used to suspend the and new file descriptors may be used interchangeably.
process's execution until a signal is
received. They refer to the same open file description
• The while loop with the c variable is used (see open(2)) and thus share file offset and file status
to count the number of times the signal
handler is called. flags; for example, if the file offset is modified by
• The loop terminates after three iterations, using lseek(2) on one of the descriptors, the offset is
even though the process continues to
receive SIGINT signals. also changed for the other.

27
Systems Programming 23
BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls for IPC: pipe() System calls for IPC: dup()
#include<unistd.h>
The pipe() system function is used to open file #include<stdio.h>
descriptors, which are used to communicate #include<fcntl.h>
between different Linux processes. In short, int main()
the pipe() function is used for inter-process {
communication in Linux. int old_fd, new_fd;
old_fd=open("test.txt",O_RDWR);
printf("File descriptor is %d\n",old_fd);
new_fd=dup(old_fd);
printf("New file descriptor is %d\n",new_fd);
}
24 28
Systems Programming - V 3.0 BITS Pilani, Pilani Campus Systems Programming - V 3.0 BITS Pilani, Pilani Campus

System calls for IPC: pipe() System calls for IPC: dup()
#include <stdio.h>
#include <stdlib.h> • The program demonstrates the use of the open and dup
#include <unistd.h> functions to open a file and create a duplicate file descriptor.
int main(void) { • The open function creates a new file descriptor and returns
int pipefds[2]; its value.
if(pipe(pipefds) == -1) { • The dup function creates a duplicate file descriptor for an
perror("pipe"); existing open file.
exit(EXIT_FAILURE); • The duplicate file descriptor shares the same file descriptor
} table entry as the original file descriptor, which means they
printf("Read File Descriptor Value: %d\n", pipefds[0]); refer to the same open file.
printf("Write File Descriptor Value: %d\n", pipefds[1]);
return EXIT_SUCCESS;
}
25
Systems Programming 29
Systems Programming - V 3.0 BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Memory Management System Calls Memory Management System Calls: mmap

• There are two main system calls to manipulate memory, namely • void *mmap(void *addr, size_t length, int prot, int flags, int fildes,
brk and sbrk. off_t off):
• Both brk and sbrk change the amount of space allocated for the • The mmap system call in Linux provides a powerful and flexible
calling process's data segment. way to map files or devices into the address space of a process.
• They change the process's break value and the operating system • This allows for efficient memory-mapped I/O operations and
adjusts their allocated amount of space accordingly, which may shared memory communication between processes.
be an increase or a decrease or no change. • Parameters are discussed below.
• The break value is the address of the first memory location after
the end of the data segment for the process.
• brk and sbrk are very rarely used because the malloc, calloc, and
free library functions are adequate for allocating and freeing
memory space; brk and sbrk should not be used in a program
that also uses mallocs/calloc and free.
Systems Programming 30 Systems Programming 34
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Memory Management System Calls Memory Management System Calls: mmap

• *brk(void addr): Parameters


• Used to adjust the program's data segment (heap) size. • addr: starting address of the mapped region. If NULL, the kernel chooses an address.
• addr: A pointer to the desired new address for the break. If • length: The size of the mapped region in bytes.
• prot: Protection flags for the mapped region:
NULL, the current break address is returned. • PROT_READ: Read-only access.
• On success, the new break address is returned. On failure, -1 is • PROT_WRITE: Read-write access.
returned and errno is set to indicate the error. • PROT_EXEC: Execute access.
• flags: Flags to control the mapping:
• sbrk(intptr_t increment): • MAP_PRIVATE: Creates a private copy of the mapped region.
• Similar to brk, but adjusts the break by a specified increment. • MAP_SHARED: Creates a shared mapping, where changes made by one process
• increment: The amount by which to adjust the break (positive are visible to other processes.
• MAP_ANONYMOUS: Creates an anonymous mapping (not associated with a file).
for increasing, negative for decreasing). • MAP_FIXED: Attempts to fix the mapping at the specified address.
• On success, the new break address is returned. On failure, -1 is • fildes: The file descriptor of the file or device to map.
returned and errno is set to indicate the error. • off: The offset within the file or device from which to start mapping.

Systems Programming 31 Systems Programming 35


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Ex-5: brk & sbrk Ex-6: mmap

o/p

o/p

Systems Programming 32 Systems Programming 36


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus

Ex-5: brk & sbrk Ex-6: mmap


• Allocate memory using sbrk: The sbrk function is used to increase the • The open function creates a new file named "my_file.txt" in read-
program's data segment (heap) by size bytes. The returned address addr
points to the start of the newly allocated memory. write mode.
• The mmap function maps the file into memory.
• Release memory using brk: The brk function is used to set the program's break • The MAP_SHARED flag indicates that changes made to the
address back to its original value, effectively releasing the previously allocated memory will be visible to other processes that also have the file
memory. mapped.
• This program illustrates how brk and sbrk can be used to dynamically adjust • The memcpy function copies the string "Hello, world!" into the
the program's memory usage. However, it's important to note that brk and mapped memory.
sbrk are generally not recommended for modern applications due to their • The munmap function unmaps the memory region from the
limitations and potential fragmentation issues. mmap is often preferred for process's address space.
more flexible and efficient memory management.

Systems Programming 33 Systems Programming 37


BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
38
Systems Programming - V 3.0 BITS Pilani, Pilani Campus

IMP Note to Self

39
Systems Programming - V 3.0 BITS Pilani, Pilani Campus

You might also like