Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

System Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

System Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 674

Contact Session 1

System Programming
Dr. Parag Tamhankar
What will we learn today?
• Session 1 - Directories and Files in UNIX / Linux system. Basic UNIX commands

• 1.1 Getting Introduction to UNIX / Linux

• 1.2 Logging in to Linux system

• 1.3 UNIX files and directories

• 1.4 Basic UNIX commands


Unix & Linux
Unix History
Linux History
Linux Kernel Diagram
What is Unix ?
• The Unix operating system is a set of programs that act as a link between the computer and the user.

• The computer programs that allocate the system resources and coordinate all the details of the computer's internals is
called the operating system or the kernel.

• Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it
translates commands entered by the user and converts them into a language that is understood by the kernel.

• Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson, Dennis Ritchie, Douglas McIlroy,
and Joe Ossanna at Bell Labs.

• There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are a few examples. Linux is
also a flavor of Unix which is freely available.

• Several people can use a Unix computer at the same time; hence Unix is called a multiuser system.

• A user can also run multiple programs at the same time; hence Unix is a multitasking environment.
Unix Architecture
• 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 tasks like memory management, task scheduling and file management.

• Shell − The shell is the utility that processes your requests.


• When you type in a command at your terminal, the shell interprets the command and calls the program that you want.
• 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.
• 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.
• All files are then organized into directories. These directories are further organized into a tree-like structure called the
filesystem.
Unix Architecture
Unix Architecture
• The block diagram of the UNIX OS kernel shows
various modules and their relationships with each
other. It shows the file subsystem on the left side and
the process control subsystem on the right.
• The figure also shows three levels i.e user, kernel, and
hardware.
• The system call and library interface made the border
between user programs and the kernel.
• System calls look like ordinary function calls in C
programs.
• 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 provide a more sophisticated
use of the system calls.
What is Unix ?
• 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.
What is Unix ?
• The process control subsystem

• The process control subsystem is responsible for process synchronization, interprocess communication, memory
management, and process scheduling.
• The file subsystem and the process control subsystem interact when loading a file into memory for execution.

• Some of the system calls for controlling processes are fork (create a new process),
• exec (overlay the image of a program onto the running process),
• exit(finish executing a process), wait (synchronize process execution with the exit of a previously forked process),
• brk (control the size of memory allocated to a process), and
• signal (control process response to extraordinary events).

• The memory management module controls the allocation of memory.


• The kernel moves them between main memory and secondary memory so that all processes get a fair chance to
execute.
What is Unix ?
• Memory swapping and demand paging

• The swapper process is sometimes called the scheduler because it "schedules" the allocation of
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 while awaiting a resource
or until the kernel preempts them when their recent run time exceeds a time quantum.

• The scheduler then chooses the highest priority eligible process to run; the original process will run
again when it is the highest priority eligible process available
Shell Commands of UNIX

• Unix Commands
• When you first log into a unix system, you are presented with
something that looks like the following:

/home/jaya#

• That “something” is called a prompt. As its name would suggest, it is


prompting you to enter a command.
• Every unix command is a sequence of letters, numbers and
characters. But there are no spaces.

14

BITS Pilani
Unix Commands

• Unix is also case-sensitive. This means that cat and Cat are different
commands.

• The prompt is displayed by a special program called the shell.

• Shells accept commands, and run those commands.

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


are called “shell scripts”.

15

BITS Pilani
Unix Commands

 There are two major types of shells in unix:


 Bourne shells
 C shells.

 Steven Bourne wrote the original unix shell sh,and most shells since
then end in the letters sh to indicate they are extentions on the original
idea.

 Linux comes with a Bourne shell called bash written by the Free
Software Foundation.
 Bash stands for Bourne Again Shell and is the default shell to use
running linux 16

BITS Pilani
Unix Commands

The man command displays reference pages for the command you specify.

The UNIX man pages (man is short for manual ) cover every command available.

To search for a man page, enter man followed by the name of the command to find .

For example:

$ man ls

17

BITS Pilani
Unix - Basics
• 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 - Basics
• Change Password

• All Unix systems require passwords to help ensure that your files and data remain your own and that the system
itself is secure from hackers and crackers.
• Following are the steps to change your password −
• Step 1 − To start, type password at the command prompt as shown below.
• Step 2 − Enter your old password, the one you're currently using.
• Step 3 − Type in your new password.
• Step 4 − You must verify the password by typing it again.

• $ passwd
• Changing password for krishna
• (current) Unix password:******
• New UNIX password:*******
• Retype new UNIX password:*******
• passwd: all authentication tokens updated successfully
Unix - Basics
• Who Are You?
• While you're logged into the system, you might be willing to know : Who am I?
• $ who am i
• krishna

• Who is Logged in?


• Sometime you might be interested to know who is logged in to the computer at the same time.
• There are three commands available to get you this information, based on how much you wish to know about the
other users:
• users, who, and w.
• $ users
• krishna bablu qadir
• $ who
• krishna ttyp0 Oct 8 14:10 (limbo)
• bablu ttyp2 Oct 4 09:08 (calliope)
• qadir ttyp4 Oct 8 12:09 (dent)
UNIX files and directories
• All data in Unix is organized into files.
• All files are organized into directories.
• These directories are organized into a tree-like structure called the filesystem.
• In Unix, there are three basic types of files −

• Ordinary Files − An ordinary file is a file on the system that contains data, text, or program
instructions. In this tutorial, you look at working with ordinary files.

• Directories − Directories store both special and ordinary files.


• For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.

• Special Files − Some special files provide access to hardware such as hard drives, CD-ROM drives,
modems, and Ethernet adapters.
UNIX files and directories
UNIX files and directories
• Listing Files
• To list the files and directories stored in the current directory, use the following command −
• $ ls
• bin hosts lib res.03
• ch07 hw1 pub test_results
• ch07.bak hw2 res.01 users
• docs hw3 res.02 work
• The command ls supports the -l option which would help you to get more information about the
listed files −
• $ ls -l
• drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml
• -rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
• drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
• drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia
• -rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar
UNIX files and directories
• Listing Files
• $ ls –

• Prefix & Description


• - Regular file, such as an ASCII text file, binary executable, or hard link.
• - b - 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.
• - d - Directory file that contains a listing of other files and directories.
• - l - Symbolic link file. Links on any regular file
• - p - Named pipe. A mechanism for interprocess communications.
• - s - Socket used for interprocess communication.
UNIX files and directories
• Metacharacters
• Metacharacters have a special meaning in Unix.
• For example, * and ? are metacharacters.
• We use * to match 0 or more characters, a question mark (?) matches with a single character.
• $ ls ch*.doc
• Displays all the files, the names of which start with ch and end with .doc −

• ch01-1.doc ch010.doc ch02.doc ch03-2.doc


• ch04-1.doc ch040.doc ch05.doc ch06-2.doc
• ch01-2.doc ch02-1.doc c
• Here, * works as meta character which matches with any character.
• If you want to display all the files ending with just .doc, then you can use the following command −
• $ls *.doc
UNIX files and directories
• Hidden Files
• An invisible file is one, the first character of which is the dot or the period character (.).
• Unix programs (including the shell) use most of these files to store configuration information.
• Some common examples of the hidden files include the files −
• .profile − The Bourne shell ( sh) initialization script
• .kshrc − The Korn shell ( ksh) initialization script
• .cshrc − The C shell ( csh) initialization script
• .rhosts − The remote shell configuration file
• To list the invisible files, specify the -a
• $ ls -a
• . .profile docs lib test_results
• .. .rhosts hosts pub users
• .emacs bin hw1 res.01 work
• .exrc ch07 hw2 res.02
• .kshrc ch07.bak hw3 res.03
• Single dot (.) − This represents the current directory.
• Double dot (..) − This represents the parent directory.
UNIX files and directories
• Creating Files:
• You can use the vi editor to create ordinary files on any Unix system.
• $ vi filename
• The above command will open a file with the given filename.
• Now, press the key i to come into the edit mode.
• Once you are in the edit mode, you can start writing your content in the file as in the following program

• This is unix file....I created it for the first time.....
• I'm going to save this content in this file.
• Once you are done with the program, follow these steps −
• Press the key esc to come out of the edit mode.
• Press two keys Shift + ZZ together / wq to come out of the file completely.

• Display Contents of a File


• $ cat filename
UNIX files and directories
• Counting Words in a File
• You can use the wc command to get a count of the total number of lines, words, and characters
contained in a file. Following is a simple example to see the information about the file created above

• $ wc filename
• 2 19 103 filename
• Here is the detail of all the four columns −
• First Column − Represents the total number of lines in the file.
• 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 of the file.
• Fourth Column − Represents the file name.

• You can give multiple files and get information about those files at a time. Following is simple syntax −

• $ wc filename1 filename2 filename3


UNIX files and directories
• Copying Files
• To make a copy of a file use the cp command. The basic syntax of the command is −
• $ cp source_file destination_file
• $ cp filename copyfile

• Renaming Files
• To change the name of a file, use the mv command. Following is the basic syntax −
• $ mv old_file new_file
• The mv command will move the existing file completely into the new file.

• Deleting Files
• To delete an existing file, use the rm command. Following is the basic syntax −
• $ rm filename
• Caution − A file may contain useful information. It is always recommended to be careful while using this Delete command.
• It is better to use the -i option along with rm command. It will ask for a confirmation before delete
• You can remove multiple files at a time with the command given below −
• $ rm filename1 filename2 filename3
What’s in the File?

There are two major commands used in unix for listing files, cat, and more.
cat
• cat shows the contents of the file.
cat [-nA] [file1 file2 . . . fileN]

• cat is not a user friendly command-it doesn’t wait for you to read the file, and is
mostly used in conjuction with pipes.

• However, cat does have some useful command-line options.


• For instance, n will number all the lines in the file, and A will show control characters.
30

BITS Pilani
• more
• more is much more useful, and is the command that you’ll want to use when browsing
ASCII text files
more [-l] [+linenumber}] [file1 file2 ... fileN]

• 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 specified
linenumber.
• head
head will display the first ten lines in the listed files.
head [- lines}] [l file1 file2 ... fileN]
• Any numeric option will be taken as the number of lines to print, so head -15 frog will
print the first fifteen lines of the file frog
31

BITS Pilani
• tail
• Like head, tail display only a fraction of the file.
• tail also accepts an option specifying the number of lines.
tail [-lines] [l file1 file2 ... fileN]

• file
• file command attempts to identify what format a particular file is written in.

file [file1 file2 ... fileN]


• Since not all files have extentions or other easy to identify marks, the file command
performs some rudimentary checks to try and figure out exactly what it contains.

32

BITS Pilani
• grep
• grep is the generalized regular expression parser.

• This is a fancy name for a utility which can only search a text file.

grep [-nvwx] [-number] { expression} [file1 file2 ... fileN]

33

BITS Pilani
UNIX files and directories
• Standard Unix Streams
• Under normal circumstances, every Unix program has three streams opened for it when it starts up
• stdin − This is referred to as the standard input and the associated file descriptor is 0.
• This is also represented as STDIN.
• The Unix program will read the default input from STDIN.

• stdout − This is referred to as the standard output and the associated file descriptor is 1.
• This is also represented as STDOUT.
• The Unix program will write the default output at STDOUT

• stderr − This is referred to as the standard error and the associated file descriptor is 2.
• This is also represented as STDERR.
• The Unix program will write all the error messages at STDERR.
Unix / Linux - Directory Management
• A directory is a file the solo job of which is to store the file names and the related information.
• All the files, whether ordinary, special, or directory, are contained in directories.
• Unix uses a hierarchical structure for organizing files and directories.
• This structure is often referred to as a directory tree.
• The tree has a single root node, the slash character (/), and all other directories are contained below it.

• Home Directory
• The directory in which you find yourself when you first login is called your home directory.
• You will be doing much of your work in your home directory and subdirectories that you'll be creating to
organize your files.
• You can go in your home directory anytime using the following command −

• $cd ~
• Here ~ indicates the home directory
Unix / Linux - Directory Management
• Absolute/Relative Pathnames
• Directories are arranged in a hierarchy with root (/) at the top.
• The position of any file within the hierarchy is described by its pathname.
• Elements of a pathname are separated by a /.
• A pathname is absolute, if it is described in relation to root, thus absolute pathnames always begin with a /.
• Following are some examples of absolute filenames.
• /etc/passwd
• /users/sjones/chem/notes
• /dev/rdsk/Os3
• A pathname can also be relative to your current working directory.
• Relative pathnames never begin with /.
• Relative to user krishna's home directory, some pathnames might look like this −
• chem/notes
• personal/res

• To determine where you are within the filesystem hierarchy at any time, enter the command pwd to print the current working
directory −
• $pwd
• /user0/home/krishna
Unix / Linux - Directory Management
• Listing Directories
• To list the files in a directory, you can use the following syntax −
• $ls dirname (e.g. $ls demo)

• Creating Directories
• We will now understand how to create directories. Directories are created by the following command −
• $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 command produces no output if
it successfully creates the requested directory.
• If you give more than one directory on the command line, mkdir creates each of the directories. For example, −
• $mkdir docs pub
• Creates the directories docs and pub under the current directory.
Unix / Linux - Directory Management
• Removing Directories
• Directories can be deleted using the rmdir command as follows −
• $rmdir dirname
• Note − To remove a directory, make sure it is empty which means there should not be any file or sub-directory inside this directory.
• You can remove multiple directories at a time as follows −
• $rmdir dirname1 dirname2 dirname3
• The above command removes the directories dirname1, dirname2, and dirname3, if they are empty. The rmdir command produces
no output if it is successful.

• Changing Directories
• You can use the cd command to do more than just change to a home directory. You can use it to change to any directory by
specifying a valid absolute or relative path. The syntax is as given below −
• $cd dirname
• Here, dirname is the name of the directory that you want to change to. For example, the command −
• $cd /usr/local/bin
• Changes to the directory /usr/local/bin.
• From this directory, you can cd to the directory /usr/home/amrood using the following relative path −
• $cd ../../home/amrood
Unix / Linux - Directory Management
• Renaming Directories
• The mv (move) command can also be used to rename a directory. The syntax is as follows −
• $mv olddir newdir
• You can rename a directory mydir to yourdir as follows −
• $mv mydir yourdir
• The directories . (dot) and .. (dot dot)
• 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 directory.
• If we enter the command to show a listing of the current working directories/files and use the -a option to
list all the files and the -l option to provide the long listing, we will receive the following result.
• $ls -la
• drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
• drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
• ---------- 1 teacher class 4210 May 1 08:27 .profile
• -rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo
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.
• The Permission Indicators
• While using ls -l command, it displays various information related to file permission as follows −
• $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
Unix / Linux - File Permission / Access Modes
• File Access Modes
• The permissions of a file are the first line of defense in the security of a Unix system. The basic building
blocks of Unix permissions are the read, write, and execute permissions, which have been described below −
• 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.
• Execute - User with execute permissions can run a file as a program.

• Directory Access Modes


• Directory access modes are listed and organized in the same manner as any other file. There are a few
differences that need to be mentioned −
• Read - Access to a directory means that the user can read the contents. The user can look at the filenames
inside the directory.
• Write - Access means that the user can add or delete files from the directory.
• Execute - Executing a directory doesn't really make sense, so think of this as a traverse permission.
• 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
• Changing Permissions
• To change the file or the directory permissions, you use the chmod (change mode) command.
• There are two ways to use chmod — the symbolic mode and the absolute mode.

• 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
following table.

• Chmod operator & Description


• + 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).

• Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions are as follows −
• $ls -l testfile
• -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Unix / Linux - File Permission / Access Modes
• Changing Permissions
• $chmod o+wx testfile
• $ls -l testfile
• -rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
• $chmod u-x testfile
• $ls -l testfile
• -rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
• $chmod g = rx testfile
• $ls -l testfile
• -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile

• Here's how you can combine these commands on a single line −

• $chmod o+wx,u-x,g = rx testfile


• $ls -l testfile
• -rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Unix / Linux - File Permission / Access Modes
• Using chmod with Absolute Permissions
• The second way to modify permissions with the chmod command is to use a number to specify each set of
permissions for the file.
• Each permission is assigned a value, as the following table shows, and the total of each set of permissions
provides a number for that set.

• Number Octal Permission Representation Ref


• 0 No permission ---
• 1 Execute permission --x
• 2 Write permission -w-
• 3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx
• 4 Read permission r--
• 5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
• 6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
• 7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx
Unix / Linux - File Permission / Access Modes
• Using chmod with Absolute Permissions
• Here's an example using the testfile.
• Running ls -l on the testfile shows that the file's permissions are as follows −
• $ls -l testfile
• -rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile

• Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission
changes −
• $ chmod 755 testfile
• $ls -l testfile
• -rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile

• $chmod 743 testfile


• $ls -l testfile
• -rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile

• $chmod 043 testfile


• $ls -l testfile
• ----r---wx 1 amrood users 1024 Nov 2 00:10 testfile
Unix / Linux - File Permission / Access Modes
• Changing Owners and Groups
• While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions
mentioned above are also assigned based on the Owner and the Groups.
• Two commands are available to change the owner and the group of files −
• chown − The chown command stands for "change owner" and is used to change the owner of a file.
• chgrp − The chgrp command stands for "change group" and is used to change the group of a file.

• Changing Ownership
• The chown command changes the ownership of a file. The basic syntax is as follows −
• $ chown user filelist
• 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 system.
• The following example will help you understand the concept −
• $ chown amrood testfile
• Changes the owner of the given file to the user amrood.
• 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.
Unix / Linux - File Permission / Access Modes
• Changing Group Ownership
• The chgrp command changes the group ownership of a file.
• The basic syntax is as follows −

• $ chgrp group filelist


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

• Following example helps you understand the concept −

• $ chgrp special testfile


• Changes the group of the given file to special group.
Unix / Linux - File Permission / Access Modes
• Unix / Linux - Environment

• An important Unix concept is the environment, which is defined by environment variables.


• Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

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

• For example, first we set a variable TEST and then we access its value using the echo command −

• $TEST="Unix Programming"
• $echo $TEST
• It produces the following result.
• Unix Programming

• 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.
Unix / Linux - File Permission / Access Modes
• Unix / Linux - Environment
• When you log in to the system, the shell undergoes a phase called initialization to set up the environment.
• This is usually a two-step process that involves the shell reading the following files −
• /etc/profile
• profile
• The process is as follows −
• 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.
• Your home directory is the directory that you start out in after you log in.
• 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.
Unix / Linux - File Permission / Access Modes
• The .profile File
• 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.

• The file .profile is under your control.


• You can add as much shell customization information as you want to this file.
• The minimum set of information that you need to configure includes −
• The type of terminal you are using.
• 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.
Unix / Linux - File Permission / Access Modes
• Setting the Terminal Type
• Usually, the type of terminal you are using is automatically configured by either the login or getty programs.
• 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 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 following way −
• $TERM=vt100

• Setting the PATH


• 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.
• Usually the Path variable is set as follows −
• $PATH=/bin:/usr/bin
• 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, a message
similar to the following appears −
• $hello
• hello: not found
wc
• wc (word count) simply counts the number of words, lines, and characters in the file(s).
$ wc [-clw] [file1 file2 ... fileN]
• The three parameters, clw, stand for character, line, and word respectively, and tell
wc which of the three to count.

spell
spell is very simple unix spelling program, usually for American English.
spell is a filter, like most of the other programs we’ve talked about.

$ spell [file1 file2 ... fileN]

52

BITS Pilani
cmp
• cmp compares two files.

• The first must be listed on command line, while the second is either listed
as the second parameter or is read in form standard input.

• cmp is very simple, and merely tells you where the two files first differ.
• $ cmp file1 [ file2]

53

BITS Pilani
diff

• One of the most complicated standard unix commands is called diff.

• The GNU version of diff has over twenty command line options.

• It is a much more powerful version of cmp and shows you what the differences are
instead of merely telling you where the first one is.

• $diff file1 file2

54

BITS Pilani
tr

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


filename as a parameter.

• Instead, it’s two parameters are arbitrary strings.

• It replaces all occurences of string1 in the input string2.

• In addition to relatively simple commands such as tr frog toad,


• tr can accept more complicated commands.

$tr string1 string2

55

BITS Pilani
ed command

• ed is a line-oriented text editor.

• It is used to create, display, modify and otherwise manipulate text files.

• red is a restricted ed: it can only edit files in the current directory and cannot execute
shell commands.

• If invoked with a file argument, then a copy of file is read into the editor’s buffer.

• Changes are made to this copy and not directly to file itself.

• Upon quitting ed, any changes not explicitly saved with a ‘w’ command are lost.
56

BITS Pilani
ed command

 Editing is done in two distinct modes: command and input.


 When first invoked, ed is in command mode.
 In this mode commands are read from the standard input and executed to manipulate the
contents of the editor buffer.
 A typical command might look like:
 ,s/old/new/g

 which replaces all occurrences of the string old with new.


 When an input command, such as ‘a’ (append), ‘i’ (insert) or ‘c’ (change), is given, ed enters
input mode.
 This is the primary means of adding text to a file.
 In this mode, no commands are available; instead, the standard input is written directly to the
editor buffer.
57

BITS Pilani
pr command

• Lines consist of text up to and including a newline character.


• Input mode is terminated by entering a single period (.) on a line.

• pr command:

• The pr command is famous for printing a file neatly on a page -- with margins at top
and bottom, filename, date, and page numbers.

• It can also print text in columns: one file per column or many columns for each file.

• The -t option takes away the heading and margins at the top and bottom of each page.
That's useful when "pasting" data into columns with no interruptions.

58

BITS Pilani
pr command

• The -m option reads all files on the command line simultaneously and prints each in its
own column, like this:

• % pr -m -t file1 file2 file3

• The lines The lines The lines


• of file1 of file2 of file3
• are here are here are here
• ... ... ...

59

BITS Pilani
pr command

• 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 56 lines).
• Next, the second column is filled.
• Then, the third column is filled.
• If there's more of the file, the first column of page 2 is filled -- and the cycle repeats:

• % pr -3 file1

• Oct 1 19:44 2017 file1 Page 1

• 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
• ... ... ...
60

BITS Pilani
pr command
 +n starts displaying with the nth page of each file. The default for n is 1.
 -n displays n columns of output. This is the obsolete equivalent of -c n.
 -a orders input lines across the page on output, instead of down. You should only use this
option with -n.

 -c n displays n columns of output. When you specify this option, pr behaves as though
you had also specified the
 -e and -i options. When you specify both this option and

 -t, pr uses the minimum number of lines possible to display the output. You should not
specify this option with -m.

 -d produces double-spaced output.


61

BITS Pilani
pr command
 -e[char][gap] expands each occurrence of the input tab character to a string of spaces
such that the following character has the next column position which is a positive
multiple of gap, plus 1.

 -F uses form feeds to separate pages.


 pr normally separates pages by sending a series of newline characters to fill the
length of a page.

 -f uses form feeds to separate pages.


 When output is to a terminal, pr sounds the bell and waits for you to type a carriage
return before displaying the text.
 pr normally separates pages by sending a series of newline characters to fill the
length of a page.
62

BITS Pilani
which
• Displays a path name of a command.
• Searches a path environmental variable for the command and displays
the absolute path.
• To find which tcsh and bash are actually in use, type:
% which tcsh
% which bash
• % man which for more details
chsh
• Change Login Shell
• Login shell is the shell that interprets commands after you logged in
by default.
• You can change it with chsh (provided that your system admin
allowed you to do so).
• To list all possible shells, depending on implementation:
% chsh -l
% cat /etc/shells
• % chsh with no arguments will prompt you for the shell.
whereis
• Display all locations of a command (or some other binary, man page, or a
source file).
• Searchers all directories to find commands that match whereis’
argument
• % whereis tcsh
date
• Guess what :-)
• Displays dates in various formats
• % date
• % date -u
• in GMT
• % man date
cal
• Calendar • % cal current month
• for month • % cal 2 2000 Feb 2000, leap
year
• entire year
• % cal 2 2100 not a leap year
• Years range: 1 - 9999 • % cal 2 2400 leap year
• No year 0 • % cal 9 1752 11 days skipped

• Calendar was corrected in • % cal 0 error


1752 - removed 11 days • % cal 2002 whole year
clear
• Clears the screen
• There’s an alias for it: Ctrl+L
• Example sequence:
• % cal
• % clear
• % cal
• Ctrl+L
sleep
• “Sleeping” is doing nothing for some time.
• Usually used for delays in shell scripts.
• % sleep 2 2 seconds pause
Command Grouping
• Semicolon: “;”
• Often grouping acts as if it were a single command, so an output of
different commands can be redirected to a file:
• % (date; cal; date) > out.txt
alias
• Defined a new name for a command
• % alias
• with no arguments lists currently active aliases
• % alias newcommand oldcommand
• defines a newcommand
• % alias cl cal 2003
• % cl
unalias
• Removes alias
• Requires an argument.
• % unalias cl
history
• % !n a history of recently used commands
Display
• repeat command n in the history
% • history
• % • !-1
all commands in the history
• repeat last command
% • history 10 = !!
• % • !-2
last 10
• repeat second-r
% • history last 10
command
• % • !ca
reverse order
• repeat last command that begins with ‘ca’
% • !!
• repeat last command
apropos
• Search man pages for a substring. • % apropos date
• % apropos word • % man -k date
• Equivalent: • % apropos password
• % man -k word
exit / logout
• Exit from your login session.
• % exit
• % logout
shutdown
• Causes system to shutdown or reboot cleanly.
• May require superuser privileges
• % shutdown -h now - stop
• % shutdown -r now - reboot
ls
• List
% ls directory
-F contents
• • append
Has whole “/” to dirsofand
bunch “*” to executables
options, see man ls for details.
•• %
% lsls -l
•• long format
all files except those starting with a “.”
•• %
% lsls -al
-a
• % • ls
all -lt
• sort -A
% • ls by modification time (latest - earliest)
• % • ls -ltr“.” and “..”
all without
• reverse
cat
• Display and concatenate files.
• % cat
• Will read from STDIN and print to STDOT every line you enter.
• % cat file1 [file2] ...
• Will concatenate all files in one and print them to STDOUT
• % cat > filename
• Will take whatever you type from STDIN and will put it into the file filename
• To exit cat or cat > filename type Ctrl+D to indicate EOF (End of File).
less
• less ("less is more") a bit more smart than the more command
• to display contents of a file:
• % less filename
• To display line numbers:
• % less -N filename
• To display a prompt:
• % less -P"Press 'q' to quit" filename
• Combine the two:
• % less -NP"Blah-blah-blah" filename
• For more information:
• % man less
touch
• By touching a file you either create it if it did not exists (with 0 length).
• Or you update it’s last modification and access times.
• There are options to override the default behavior.
• % touch file
• % man touch
cp
• Copies files / directories.
• % cp [options] <source> <destination>
• % cp file1 file2
• % cp file1 [file2] … /directory
• Useful option: -i to prevent overwriting existing files
and prompt the user to confirm.
mv
• Moves or renames files/directories.
• % mv <source> <destination>
• The <source> gets removed
• % mv file1 dir/
• % mv file1 file2
• rename
• % mv file1 file2 dir/
• % mv dir1 dir2
rm
• Removes file(s) and/or directories.
• % rm file1 [file2] ...
• % rm -r dir1 [dir2] ...
• % rm -r file1 dir1 dir2 file4 ...
script
• Writes a log (a typescript) of whatever happened in the terminal to a
file.
• % script [file]
• % script
• all log is saved into a file named typescript
• % script file
• all log is saved into a file named file
• To exit logging, type:
• % exit
find
• Looks up a file in a directory tree.
• % find . -name name
• % find . \(-name ‘w*’ -or -name ‘W*’ \)
mkdir
• Creates a directory.
• % mkdir newdir
• Often people make an alias of md for it.
cd
• Changes your current directory to a new one.
• % cd /some/other/dir
• Absolute path
• % cd subdir
• Assuming subdir is in the current directory.
• % cd
• Returns you to your home directory.
pwd
• Displays personal working directory, i.e. your current directory.
• % pwd
rmdir
• Removes a directory.
• % rmdir dirname
• Equivalent:
• % rm -r dirname
ln
• Symbolic link or a “shortcut” in M$ terminology.
• % ln –s <real-name> <fake-name>
chmod
• Changes file permissions
• Possible invocations
• % chmod 600 filename
• -rw------- 1 user group 2785 Feb 8 14:18 filename
(a bit not intuitive where 600 comes from)
• % chmod u+rw filename
(the same thing, more readable)
• For the assignment:
• % chmod u+x myshellscript
(mysshellscript is now executable)
• -rwx------ 1 user group 2785 Feb 8 14:18 myshellscript
grep
• Searches its input for a pattern.
• The pattern can be a simple substring or a complex regular expression.
• If a line matches, it’s directed to STDOUT; otherwise, it’s discarded.
• % echo “blah-foo” | grep blah
• Will print the matching line
• % echo “blah-foo” | grep zee
• Will not.
• See a separate grep tutorial.
Pipes
• What's a pipe?
• is a method of interprocess communication (IPC)
• in shells a '|' symbol used
• it means that the output of one program (on one side of a pipe) serves as an
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 more complex
tasks
Contact Session - 2
Systems Programming

Dr. Parag Anil Tamhankar


Agenda
Absolute and Relative Path
Basic UNIX commands :
write - Opens a line of communication to send messages to
other users on the system in real time.
pwd - Prints the current working directory
Relative & Absolute path
cd - Changes directories
The difference between relative and absolute paths
mkdir - Creates a directory
rmdir - Removes a directory (assuming it is empty)
ls - Lists the contents of a directory
cat - Shows the contents of a file, all at once
Pr - Displays the specified files on the standard output
95
Systems Programming
Example of Linux
File System

SysPgm SysPgm1
Absolute and
Relative path
addressing
– All platforms have a method for describing the paths
for files and directories
– You usually specify a path as a series of names
separated by separator characters
– Typically, the first name is the top level of the
hierarchical specification of the path. The last name is
the file or directory the path identifies.
– A path can be Relative or Absolute
Absolute path

• A path is a unique location to a file or a folder in a file


system.

• An absolute path is defined as the specifying the location


of a file or directory from the root directory(/).

• An absolute path is defined as specifying the location of a


file or directory from the root directory(/). In other
words,we can say that an absolute path is a complete
path from start of actual file system from / directory.

Systems Programming
Absolute path
To write an absolute path-name:
• Start at the root directory ( / ) and work down.
• Write a slash ( / ) after every directory name
Ex: $cat data.lst
• will work only if the file “data.lst” exists in your current
directory. However, if this file is not present in your
working directory and is present somewhere else say in
/home/Wipro/WASE, then this command will work only if
you will use it like shown below:
• cat /home/Wipro/WASE/data.lst

Systems Programming
Basic Linux
commands
• Absolute path
– Describes the location of a file or directory
starting from the top level of the file system.
• Examples
– cd /home/SysPgm
– cd /home/SysPgm/dir0/dirX
Relative path

• Relative path is defined as the path related to the present working


directly(pwd). It starts at your current directory and never starts with a / .

• UNIX offers a shortcut in the relative pathname– that uses either the current or
parent directory as reference and specifies the path relative to it. A relative
path-name uses one of these cryptic symbols:

 .(a single dot) - this represents the current directory.


 ..(two dots) - this represents the parent directory.

Systems Programming
Basic Linux
commands
• Relative
– “ ../Public”
– Can travel only along the links. There is no direct link
between “Download” and “Public”
– First traverse through the node “SysPgm” (parent
directory accessed with the help of ..) and then reach
“Public”
– If you were in directory named home, then the path
would be “SysPgm/Public”
– depending on relative location in the hierarchy tree,
path to the destination address changes. Hence the
name, Relative Path Addressing
Basic Linux
commands
• Relative path
– Describes the location of a file or directory relative
to an arbitrary location in the file system
• Examples
– cd ..
– cd ../dir0
Basic Linux
commands
• A path does not necessarily go from the top of
the hierarchy down to the target.
– Can use a platform‐specific tag in place of a name that
indicates the path should go up a level from the current
location
• Some of the platform‐specific tags are
– ~ Home Directory (of the user logged in)
– / Root Directory (it is different from the directory
named “root”)
– . Current Directory
– .. Parent Directory
Basic Linux
commands
• Observe behavior of the following commands
• [assuming for each execution pwd gives
/home/SysPgm/exercises]
– cd .
– cd ..
– cd /home/SysPgm
– cd /home/SysPgm/dir0
– cd ../dir0
Basic Linux
commands
• Examples of platform specific tags
– [assuming for each execution pwd gives
/home/SysPgm/exercises]
– cd ~/dir0
– cd /home/SysPgm/dir0
– cd ./../dir0
write Command
 The write command enables message sending over the system in
real time. It provides conversation-like communication with
another logged-in user. Each user alternately sends and receives
short messages from the other workstation. Long messages can be
sent by putting the complete message in a file and then redirecting
that file as input to the write command.
Ex: To write a message to a user who is logged in,
enter : write username Welcome to Systems Programming
press the Ctrl-D key sequence to terminate the write command mode.

Systems Programming
pwd Command

• pwd - print working directory


• At any time you can determine where you
are in the file system hierarchy with the
pwd, print working directory,
E.g.,:
$ pwd
/home/frank/src

108
Systems Programming
mkdir - make a directory

• You extend your home hierarchy by making sub-directories. This is done


with the mkdir, make directory, command. Again, you specify either the
full or relative path of the directory.
Examples
mkdir patch Creates a directory patch under current directory
mkdir patch dbs doc Creates three directories under current directory
mkdir wip wip/progs wip/data Creates a directory tree with wip as a
directory under the current directory and progs and data as subdirectories
under wip
• Note the order of specifying arguments in example 3. The parent directory
should be specified first, followed by the subdirectories to be created
under it.
• The system may refuse to create a directory due to the following reasons:
1. The directory already exists.
2. There may be an ordinary file by the same name in the current
directory.
3. The permissions set for the current directory don’t permit the creation
of files and directories by the user.
Systems Programming
rmdir - remove directory

A directory needs to be empty before you can remove it. If it’s


not, you need to remove the files first.

Also, you can’t remove a directory if it is your present working


directory; you must first change out of that directory.

You cannot remove a subdirectory unless you are placed in a


directory which is hierarchically above the one you have chosen to
remove.
E.g.
rmdir patch Directory must be empty
rmdir wip wip/progs wip/data Shows error as wip is not
empty.
However rmdir silently deletes the lower level subdirectories
progs and data.
Systems Programming
Types of files

A simple description of the UNIX system is this:

• “On a UNIX system, everything is a file; if


something is not a file, it is a process.”

• A UNIX system makes no difference between a file


and a directory, since a directory is just a file
containing names of other files. Programs,
services, texts, images, and so forth, are all files.

Systems Programming
ls command

•Listing Files
•To list the files and directories stored in the current
directory, use the following command
$ ls

ls – l to display file attributes (properties)


Listing of a specific directory
Ownership and group ownership
Different file permissions

Systems Programming
LISTING FILE ATTRIBUTES

ls –l provides attributes like


• Permissions
• Links
• Owner
• Group owner
• Size
• Date
• File name

Systems Programming
• $ ls –l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 dept.lst
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 genie.sh
drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

Systems Programming
ls – l provides attributes like

1. The file type and permissions associated with each


file
2. The number of file names maintained by the
system. This does not mean that there are two
copies of the file
3. File created by the owner
4. Every user is attached to a group owner

Systems Programming
5. File size in bytes
6. Last modification time. If you change only the
permissions or ownership of the file, the
modification time remains unchanged
7. Displays file name

Systems Programming
LISTING DIRECTORY ATTRIBUTES

• ls –ld helpdir progs


drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

Note: ls -d will not list all subdirectories in the current


directory

Systems Programming
UNIX files and
directories
•Listing Files: $ ls –
•Prefix & Description
- Regular file, such as an ASCII text file, binary executable, or
hard link.
- b - 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.
- d - Directory file that contains a listing of other files and
directories.
- l - Symbolic link file. Links on any regular file
- p - Named pipe. A mechanism for interprocess communications.
- s - Socket used for interprocess communication.

Systems Programming
UNIX files and
directories
•Metacharacters
•Metacharacters have a special meaning in Unix.
•For example, * and ? are metacharacters.
•We use * to match 0 or more characters, a question mark (?) matches with
a single character.
•$ ls ch*.doc
–Displays all the files, the names of which start with ch and end with .doc −

–ch01-1.doc ch010.doc ch02.doc ch03-2.doc


–ch04-1.doc ch040.doc ch05.doc ch06-2.doc
–ch01-2.doc ch02-1.doc c
•Here, * works as meta character which matches with any character.
•If you want to display all the files ending with just .doc, then you can use
the following command −
•$ls *.doc

Systems Programming
UNIX files and
directories
•Hidden Files
•An invisible file is one, the first character of which is the dot or the period
character (.).
•Unix programs (including the shell) use most of these files to store
configuration information.
•Some common examples of the hidden files include the files −
–.profile − The Bourne shell ( sh) initialization script
–.kshrc − The Korn shell ( ksh) initialization script
–.cshrc − The C shell ( csh) initialization script
•To list the invisible files, specify the -a
•$ ls -a
–. .profile docs lib test_results
–.. .rhosts hosts pub users
–.emacs bin hw1 res.01 work
–.exrc ch07 hw2 res.02

•Single dot (.) − This represents the current directory.


•Double dot (..) − Systems
This represents
Programmingthe parent directory.
UNIX files and
directories
•Creating Files:
•You can use the vi editor to create ordinary files on any Unix system.
•$ vi filename
•The above command will open a file with the given filename.
•Now, press the key i to come into the edit mode.
•Once you are in the edit mode, you can start writing your content in the file
as in the following program −
–This is unix file....I created it for the first time.....
–I'm going to save this content in this file.
•Once you are done with the program, follow these steps −
–Press the key esc to come out of the edit mode.
–Press two keys Shift + ZZ together / wq to come out of the file completely.

•Display Contents of a File


•$ cat filename

Systems Programming
ed command

• ed command in Linux is used for launching the ed text editor which is a line-
based text editor with a minimal interface which makes it less complex for
working on text files i.e creating, editing, displaying and manipulating files. It is
the oldest editor built in the Unix. It is succeeded by vi and emacs text editor.
Syntax: ed [options] [file]
• ed command without any option: It will launch the ed editor with an empty
buffer for us to write, this is similar to the other visual or command line based
editor when invoked without a filename. The simplest way to start the ed text
editor is by typing “ed” into the terminal.

Systems Programming
ed command

• Ex 1: Here we have not specified the filename so this will create an empty buffer
for us to write.
• Ex 2: To Enter into insert mode press “a” and when you are done writing stop it
by “.” (dot).
• Ex 3: To view, the last line enter “p” into the ed command prompt.
• Ex 4: To print all the lines that we inserted in the buffer by using “, p”.
• Ex 5: To save these lines into a file write “f [filename]”.
• Ex 6: Type “w” in ed command prompt to write the data into the file and see how
many bytes are written.
cat : displaying and creating files
cat(concatenate) command is very frequently used in Linux. It
reads data from the file and gives their content as output. It
helps us to create, view, concatenate files.

To display the contents of the small files


$ cat dept.lst -- dept.lst should be present in the current directory

cat also accepts more than one argument.


$ cat file1 file2
The contents of file2 is shown immediately after file1. cat concatenates the
2 files, hence its name.
Options with cat (-v and –n) (not POSIX rec.)
-v --- displays non printable characters
-n --- displays the numbering lines. (else use pr)
Systems Programming
frequently used cat
commands.

1) To view a single file


$cat filename
Output : It will show content of given filename
2) To view multiple files
$cat file1 file2
Output : This will show the content of file1 and file2
3) To view contents of a file preceding with line numbers.
$cat -n filename
Output : It will show content with line number

Systems Programming
frequently used cat
commands.
4) Create a file
$ cat > newfile
Output : Will create a file named newfile
5) Copy the contents of one file to another file.
$cat [source filename] > [destination-filename]
Output : The content will be copied in destination file
6) Cat can append the contents of one file to the end of another file.
$cat file1 >> file2
Output : Will append the contents of one file to the end of another file
7) Cat command can display content in reverse order using tac command.
$tac filenameOutput
Will display content in reverse order
Systems Programming
pr command
• pr displays the specified files on the standard output in
a paginated form, pr formats the given files into single-
column 66-line pages.
• Each page has a 5-line header. A 5-line trailer consists
of blank lines.
• pr command adds suitable headers, footers and
formatted text
• pr adds five lines of margin at the top and bottom
• The header shows the date and time of last
modification of the file along with the filename and page
number
Systems Programming
pr command
pr : paginating files
We know that,
cat dept.lst
01|accounts|6213
02|progs|5423
03|marketing|6521
04|personnel|2365
05|production|9876
06|sales|1006

Systems Programming
pr options

-k prints k (integer) columns


-t to suppress the header and footer
-h to have a header of user’s choice
-d double spaces input
-n will number each line and helps in debugging
-on offsets the lines by n spaces and increases left
margin of page

Systems Programming
Basic Linux
commands
• pr can also produce multi‐ column output
– pr ‐3 FirstFile
– pr –J
• merge full lines, no column alignment
– pr –m
• print all files in parallel, one in each column, truncate
lines, but join lines of full length with ‐J
Basic Linux commands
– Exercise #1
• Task #1
– Create two directories SysPgm1 and SysPgm2 under
exercises
• Task #2
– Change directory to SysPgm1 directory
• Task #3
– Create test1 and test2 directories under SysPgm1
directory
• Task #4
– Change directory to SysPgm2 directory using absolute
path
Basic Linux commands
– Exercise #1
• Task #5
– Create test1 directory under SysPgm2 directory
• Task # 6
– From SysPgm2 directory create “check” directory under
SysPgm1 directory
• Task #7
– List the contents of SysPgm1 directory from the current
directory
• Task #8
– Using cd command (only once) to change directory to
dirX directory
Basic Linux commands –
Exercise #1
• Task #9
– Try the following commands and observe the
output obtained.
• ls ~
• ls ../.
• ls ./..
Basic Linux commands –
Exercise #2
• Task #1
– Create directories dir1, dir2, dir3 under exercises.
• Task #2
– Change directory to dir3 directory.
• Task #3
– Display path of current working directory.
• Task #4
– Create test1 directory under dir1 directory from
current working directory.
Basic Linux
commands – Exercise
#2
• Task #5
– Create test2 directory under dir2 directory from current
working directory
• Task #6
– Change directory to test1 directory using relative path
• Task #7
– From test1 directory display contents of dir2 directory
• Task #8
– Change directory to test2 directory by using absolute
path
Basic Linux commands
– Exercise #2
• Task #9
– Try the following commands and observe the
output obtained.
• ls ‐lt
• ls ‐lS
• ls ‐lh
• ls ‐al
The UNIX Pipe (|)

• The pipe (|) creates a channel from one command to another.


Think of the pipe as a way of connecting the output from one
command to the input of another command.
• The pipe can be used to link commands together to perform more
complex tasks that would otherwise take multiple steps (and
possibly writing information to disk).
• Examples:
• Count the number of users logged onto the current system.
• The who command will give us line by line output of all the current
users.
• We could then use the wc -l to count the number of lines...
• who | wc –l

Systems Programming
wc
• wc (word count) simply counts the number of words, lines, and characters in the file(s).
$ wc [-clw] [file1 file2 ... fileN]
• The three parameters, clw, stand for character, line, and word respectively, and tell
wc which of the three to count.

spell
spell is very simple unix spelling program, usually for American English.
spell is a filter, like most of the other programs we’ve talked about.

$ spell [file1 file2 ... fileN]

138

BITS Pilani
cmp

• cmp compares two files.

• The first must be listed on command line, while the second is either
listed as the second parameter or is read in form standard input.

• cmp is very simple, and merely tells you where the two files first differ.
• $ cmp file1 [ file2]

139

BITS Pilani
diff

• One of the most complicated standard unix commands is called diff.

• The GNU version of diff has over twenty command line options.

• It is a much more powerful version of cmp and shows you what the differences are
instead of merely telling you where the first one is.

• $diff file1 file2

140

BITS Pilani
tr
• The “translate characters” command operates on standard input-it doesn’t accept a
filename as a parameter.

• Instead, it’s two parameters are arbitrary strings.

• It replaces all occurences of string1 in the input string2.

• In addition to relatively simple commands such as tr frog toad,


• tr can accept more complicated commands.

$tr string1 string2

141

BITS Pilani
ed command

• ed is a line-oriented text editor.

• It is used to create, display, modify and otherwise manipulate text files.

• red is a restricted ed: it can only edit files in the current directory and cannot execute
shell commands.

• If invoked with a file argument, then a copy of file is read into the editor’s buffer.

• Changes are made to this copy and not directly to file itself.

• Upon quitting ed, any changes not explicitly saved with a ‘w’ command are lost.
14
2

BITS Pilani
ed command

 Editing is done in two distinct modes: command and input.


 When first invoked, ed is in command mode.
 In this mode commands are read from the standard input and executed to manipulate the
contents of the editor buffer.
 A typical command might look like:
 ,s/old/new/g

 which replaces all occurrences of the string old with new.


 When an input command, such as ‘a’ (append), ‘i’ (insert) or ‘c’ (change), is given, ed enters
input mode.
 This is the primary means of adding text to a file.
 In this mode, no commands are available; instead, the standard input is written directly to the
editor buffer.
14
3

BITS Pilani
pr command
• Lines consist of text up to and including a newline character.
• Input mode is terminated by entering a single period (.) on a line.

• pr command:

• The pr command is famous for printing a file neatly on a page -- with margins at top
and bottom, filename, date, and page numbers.

• It can also print text in columns: one file per column or many columns for each file.

• The -t option takes away the heading and margins at the top and bottom of each
page. That's useful when "pasting" data into columns with no interruptions.
1
4
4

BITS Pilani
pr command

• The -m option reads all files on the command line simultaneously and prints each in
its own column, like this:

• % pr -m -t file1 file2 file3

• The lines The lines The lines


• of file1 of file2 of file3
• are here are here are here
• ... ... ...

145

BITS Pilani
pr command

• 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 56 lines).
• Next, the second column is filled.
• Then, the third column is filled.
• If there's more of the file, the first column of page 2 is filled -- and the cycle repeats:

• % pr -3 file1

• Oct 1 19:44 2017 file1 Page 1

• 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
• ... ... ...
14
6

BITS Pilani
pr command

 +n starts displaying with the nth page of each file. The default for n is 1.
 -n displays n columns of output. This is the obsolete equivalent of -c n.
 -a orders input lines across the page on output, instead of down. You should only use this
option with -n.

 -c n displays n columns of output. When you specify this option, pr behaves as though
you had also specified the
 -e and -i options. When you specify both this option and

 -t, pr uses the minimum number of lines possible to display the output. You should not
specify this option with -m.

 -d produces double-spaced output.


1
4
7

BITS Pilani
pr command

 -e[char][gap] expands each occurrence of the input tab character to a string of spaces
such that the following character has the next column position which is a positive
multiple of gap, plus 1.

 -F uses form feeds to separate pages.


 pr normally separates pages by sending a series of newline characters to fill the
length of a page.

 -f uses form feeds to separate pages.


 When output is to a terminal, pr sounds the bell and waits for you to type a carriage
return before displaying the text.
 pr normally separates pages by sending a series of newline characters to fill the
length of a page.
1
4
8

BITS Pilani
whi
ch
• Displays a path name of a command.
• Searches a path environmental variable for the command and displays
the absolute path.
• To find which tcsh and bash are actually in use, type:
% which tcsh
% which bash
• % man which for more details
chsh

• Change Login Shell


• Login shell is the shell that interprets commands after you logged
in by default.
• You can change it with chsh (provided that your system admin
allowed you to do so).
• To list all possible shells, depending on implementation:
% chsh -l
% cat /etc/shells
• % chsh with no arguments will prompt you for the shell.
whereis

• Display all locations of a command (or some other


binary, man page, or a source file).
• Searchers all directories to find commands that match
whereis’ argument
• % whereis tcsh
date
• Guess what :-)
• Displays dates in various formats
• % date
• % date -u
• in GMT
• % man date
cal

• Calendar • % cal current month


• % cal 2 2000 Feb 2000, leap year
• for month
• % cal 2 2100 not a leap year
• entire year
• % cal 2 2400 leap year
• Years range: 1 - 9999 • % cal 9 1752 11 days skipped

• No year 0 • % cal 0 error


• % cal 2002 whole year
• Calendar was corrected
in 1752 - removed 11
days
clear
• Clears the screen
• There’s an alias for it: Ctrl+L
• Example sequence:
• % cal
• % clear
• % cal
• Ctrl+L
sleep
• “Sleeping” is doing nothing for some time.
• Usually used for delays in shell scripts.
• % sleep 2 2 seconds pause
Command Grouping
• Semicolon: “;”
• Often grouping acts as if it were a single command, so an output of
different commands can be redirected to a file:
• % (date; cal; date) > out.txt
alias
• Defined a new name for a command
• % alias
• with no arguments lists currently active aliases
• % alias newcommand oldcommand
• defines a newcommand
• % alias cl cal 2003
• % cl
unalias
• Removes alias
• Requires an argument.
• % unalias cl
history
Display
• % !n a history of recently used commands
repeat command n in the history
• % •history
• % •!-1
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
• Search man pages for a substring.
• % apropos word • % apropos
• Equivalent: date
• % man -k word • % man -k date
• % apropos
password
exit / logout
• Exit from your login session.
• % exit
• % logout
Contact Session - 3

Course Name :
Systems Programming

Dr. Parag Tamhankar


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

163
Systems Programming - V 3.0
What is a file system?

• Speaking broadly, a file system is the logical


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

164
Systems Programming - V 3.0
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
165
Systems Programming - V 3.0
What is a file system?

Overhead sector track


view

cylinder

Disk blocks are composed of The same track on each platter


one or more contiguous in a disk makes a cylinder;
sectors partitions are groups of
contiguous cylinders
166
Systems Programming - V 3.0
What is a file system?

• File system instances reside on partitions


• Partitioning is a means to divide a single hard
drive into many logical drives
• 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

167
Systems Programming - V 3.0
The UNIX File System
• The root directory has many subdirectories. The following table describes some of the
subdirectories contained under root

Directory Content

/bin Common programs, shared by the system, the system administrator and the users.

Contains references to all the CPU peripheral hardware, which are represented as
/dev
files with special properties.
Most important system configuration files are in /etc, this directory contains data
/etc
similar to those in the Control Panel in Windows
/home Home directories of the common users.
Library files, includes files for all kinds of programs needed by the system and the
/lib
users.
/sbin Programs for use by the system and the system administrator.
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.
168
Systems Programming - V 3.0
UNIX and POSIX file attributes

• File type : Type of file


• Access permission : The file access permission (owner, group, others)
• Hard link count : Number of hard links of a file
• UID : The file owner user ID
• GID : The file group ID
• File size : The file size in bytes
• Last access time : The time the file was last accessed
• Last modify time : The time the file was last modified
• Last change time : The time the file access permission
UID ,GID or hard link count was last changed
• Inode number : The system inode number of the file
• File system ID : The file system ID where the file is stored
169
Systems Programming - V 3.0
INTERNAL REPRESENTATION OF FILES

• Inodes
• Structure of a regular file
• Directories
• Conversion of a path name to an inode
• Super block
• Inode assignment to a new file
• Allocation of disk blocks
• Other file types
170
Systems Programming - V 3.0
File System Implementation
(Disk Layout)

Entire Disk

Master Boot Partition Table Disk Partitions


Record

Boot Block Super Block Inode List Data Blocks

A Possible File System Instance Layout


171
Systems Programming - V 3.0
File system layout
• The central structural concepts of a file system type
are:
- Boot Block
- Super Block
- Inode List
- Data Block

Boot Block Super Block Inode List Data Blocks


172
Systems Programming - V 3.0
Boot Block
Boot Block:
- Occupies the beginning of a file system
- Typically residing at the first sector, it may also
contain the bootstrap code that is read into the
machine at boot time
- Although only one boot block is required to boot the
system, every file system may contain a boot block

Boot Block
173
Systems Programming - V 3.0
Super Block
Super Block:
- Describes the state of a file system
- How large it is
- How many files it can store
- Where to find free space in the file system

Boot Block Super Block


174
Systems Programming - V 3.0
Super Block

Boot Block Super Block


175
Systems Programming - V 3.0
Inode List

Inode List:
- An inode is the internal representation of a file
contains the description of the disk layout of the file
data
- file owner
- permissions
- The inode list contains all of the inodes present in an
instance of a file system

Boot Block Super Block Inode List


176
Systems Programming - V 3.0
Data Blocks

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


177
Systems Programming - V 3.0
What is a file system?

So what is a file system?


- A file system is a set of abstract data types that are
implemented for the storage, hierarchical
organization, manipulation, navigation, access, and
retrieval of data

178
Systems Programming - V 3.0
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

179
Systems Programming - V 3.0
What is a file system?

• The kernel deals on a logical level with file systems


rather than with disks
• The separate file systems that the system may use are
not accessed by device identifiers
• Instead they are combined into a single hierarchical
tree structure that represents the file systems as one
whole single entity

180
Systems Programming - V 3.0
File system consistency check

181
Systems Programming - V 3.0
Kernel I/O structure

182
Systems Programming - V 3.0
I/O in Unix ‐ Devices

183
Systems Programming - V 3.0
I/O in Unix ‐ Devices

184
Systems Programming - V 3.0
I/O in Unix ‐ Devices

185
Systems Programming - V 3.0
File Systems and inodes

186
Systems Programming - V 3.0
File Systems and inodes

187
Systems Programming - V 3.0
Hard Links

188
Systems Programming - V 3.0
ln: Creating Hard Links

189
Systems Programming - V 3.0
WHERE TO USE HARD LINKS

1. ln data/ foo.txt input_files


2. Links provide some protection against accidental
deletion
3. Because of links, we don’t need to maintain two
programs as two separate disk files.

190
Systems Programming - V 3.0
LIMITATIONS OF HARD LINKS

1. We cant have two linked filenames in two file


systems.
2. We cant link a directory even within the same file
system

This can be solved by using symbolic links (soft links)

191
Systems Programming - V 3.0
Symbolic Links

192
Systems Programming - V 3.0
Symbolic Links

193
Systems Programming - V 3.0
Contact Session - 4

Course Name :
Systems Programming

Dr. Parag Tamhankar


Agenda

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

195
Systems Programming - V 3.0
Stat command
stat is a command which gives information about
the file and filesystem. Stat command gives
information such as the size of the file, access
permissions and the user ID and group ID, birth
time access time of the file.

syntax of using stat command:


stat --options filenames

196
Systems Programming - V 3.0
Stat command
-L, --dereference follow links
-f, --file-system display file system status instead of
file status
-c --format=FORMAT use the specified FORMAT
instead of the default; output a newline after each
use of FORMAT

197
Systems Programming - V 3.0
Information we get from stat
• File: The file name, If the provided file is a symlink, then the name will be
different.
• Size: The size of a given file in Bytes.
• Blocks: Total number of allocated blocks to the file on hard disk.
• File type: Regular files, special files, directories, or symbolic links.
• Device: Device number in hexadecimal format.
• Inode: Inode number of the file.
• Links: Number of hard links of the file.
• Access: The last time at which the file was accessed.
• 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.
198
Systems Programming - V 3.0
du Command
du command displays the number of blocks used for files. If
the File parameter specified is actually a directory, all files
within the directory are reported on. If no File parameter
is provided, the du command uses the files in the current
directory.
Notes:
• Files with multiple links are counted and written for only
one entry.
• Block counts are based only on file size

199
Systems Programming - V 3.0
du Command
Flags
-a Displays disk usage for each file specified, or displays the individual
disk usage for each file in a directory.
-k Calculates the block count in 1024-byte units rather than the
default 512-byte units.
-l Allocates blocks evenly among the links for files with multiple links.
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 du command
du [OPTION]... [FILE]...

200
Systems Programming - V 3.0
df Command
• df command (short for disk filesystem) is used to
show disk utilization for a Linux system.
• 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 201
Systems Programming - V 3.0
df Command
To display information about /boot file system,
df /boot
We can display information in various block format. For instance see
1M-byte blocks, run:
df -m
df -m /boot
Show statistics about the number of free and used inodes
Pass the -i as follows to display index node (inodes) info:
df -i
df -i -H
df -i -H -T
202
Systems Programming - V 3.0
Inodes
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.

203
Systems Programming - V 3.0
Directories
• 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.
204
Systems Programming - V 3.0
Links
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.
205
Systems Programming - V 3.0
Device special files
• Devices can be accessed via special files. A device special file
does not use any space on the filesystem. It is only an access
point to the device driver.
• Two types of special files exist: character and block special
files. The former allows I/O operations in character mode
while the later requires data to be written in block mode via
the buffer cache functions.
• When an I/O request is made on a special file, it is forwarded
to a device driver. A special file is referenced by a major
number, which identifies the device type, and a minor
number, which identifies the unit.
206
Systems Programming - V 3.0
I/O Queuing and Interrupt Handling

207
Systems Programming - V 3.0
I/O in Unix – Block Devices and I/O

208
Systems Programming - V 3.0
I/O in Unix – Block Devices and I/O

209
Systems Programming - V 3.0
File System Algorithms

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
corresponds to the path.

210
Systems Programming - V 3.0
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

211
Systems Programming - V 3.0
inodes

• Disk inodes consists of the following fields


- file owner identifier
- file type
- file access permissions
- file access times
- number of links to the file
- table of contents for the disk address of data in a file
- file size

212
inodes

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


to the fields of the disk inode:
- status of the in-core inode (The inode is locked, a process is
waiting for the inode to become unlocked)
- logical device number of file system that contains the file
- 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.
- pointers to other in-core inodes
- reference count, indicating the number of instances of the file
that are active (such as when opened).

213
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:

block number = ((inode number - 1) / number of inodes per block) +


start block of inode list
214
Systems Programming - V 3.0
Algorithm iput
- The kernel locks the inode if it has not been already locked
- The kernel decrements inode reference count
- The kernel checks if reference count is 0 or not
- If the reference count is 0 and the number of links to the file is
0, then the kernel releases disk blocks for file(algorithm free),
free the inode(algorithm ifree)
• If the file was accessed or the inode was changed or the
file was changed , then the kernel updates the disk inode
• The kernel puts the inode on free list
- If the reference count is not 0, the kernel releases the inode lock

215
Systems Programming - V 3.0
iput (inode_no) //releaseIncoreInode
– lock inode if not locked
– decrement inode refernece count
– if (refernce count==0)
• if (inode link==0)
– free disk block
– set file type to 0
– free inode
• if (file accessed or inode changed or file changed)
– update disk inode
• put inode on free list
– Release inode lock
216
Systems Programming - V 3.0
Structure of a Regular File

In UNIX, the data in files is not stored sequentially on disk.


If it was to be stored sequentially, the file size would not be
flexible without large fragmentation.
In case of sequential storage, the inode would only need to
store the starting address and size. Instead, the inode
stores the disk block numbers on which the data is
present. But for such strategy, if a file had data across
1000 blocks, the inode would need to store the numbers
of 1000 blocks and the size of the inode would differ
according to the size of the file.
217
Systems Programming - V 3.0
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
storage strategy.
The first 10 members of the array are "direct addresses", meaning
that they store the block numbers of actual data.
The 11th member is "single indirect", it stores the block number of
the block which has "direct addresses".
The 12th member is "double indirect", it stores block number of a
"single indirect" block.
And the 13th member is "triple indirect", it stores block number of a
"double indirect" block.
This strategy can be extended to "quadruple" or "quintuple" indirect
addressing. 218
Systems Programming - V 3.0
Structure of a regular file
Inode Data Blocks
direct0
direct1
direct2
direct3
direct4
direct5
direct6
direct7
direct8
direct9
single indirect
double indirect
triple indirect

219
Systems Programming - V 3.0
Structure of a regular file

Suppose System V UNIX


Assume that a logical block on the file system holds 1K bytes and that a
block number is addressable by a 32 bit integer, then a block can hold up to
256 block numbers

10 direct blocks with 1K bytes each=10K bytes


1 indirect block with 256 direct blocks= 1K*256=256K bytes
1 double indirect block with 256 indirect blocks=
256K*256=64M bytes
1 triple indirect block with 256 double indirect blocks=
64M*256=16G bytes

220
Systems Programming - V 3.0
Structure of a regular file
• Processes access data in a file by byte offset and view a file as a
stream of bytes
• The kernel accesses the inode and converts the logical file block
into the appropriate disk block
• algorithm bmap
- The kernel calculates logical block number in file from byte
offset
- The kernel calculates start byte in block for I/O
- The kernel calculates number of bytes to copy to user
- The kernel checks if read-ahead is applicable, then marks inode
- The kernel determines level of indirection

221
Systems Programming - V 3.0
Structure of a regular file

- While it’s not at necessary level of indirection,


the kernel calculates index into inode or indirect block from
logical block number in file, gets disk block number from inode
or indirect block and release buffer from previous disk read
• If there is no more levels of indirection , the kernel stops
conversing
• Otherwise the kernel reads indirect disk block(bread) and
adjusts logical block number in file according to level of
indirection

222
Systems Programming - V 3.0
Conversion of Path Name to an Inode

The kernel parses the path by accessing each inode in the


path and finally returning the inode of the required file.
Every process has a current directory. The current directory
of process 0 is the root directory.
For every other process, it is the current directory of its
parent process.
Later the process can change the current directory with the
system call chdir. The inode of the current directory is
stored in the u-area.

223
Systems Programming - V 3.0
Path conversion to an inode
• if (path name starts with root)
– working inode= root inode
• else
– working inode= current directory inode
• while (there is more path name)
– read next component from input
– read directory content
– if (component matches an entry in directory)
• get inode number for matched component
• release working inode
• working inode=inode of matched component
– else
– return no inode
• return (working inode)
224
Systems Programming - V 3.0
Algorithm namei
- If path name starts from root, then the kernel assigns root
inode(iget) to working inode
- Otherwise, the kernel assigns current directory inode to working
inode
- While there is more path name, the kernel reads next path name
component from input, and verifies that working inode is of
directory, access permissions OK
• If working inode is of root and component is ‘..’, then the
kernel checks whether there is more path name or not
• Otherwise the kernel reads directory by repeated use of
bmap,bread,brelse

225
Systems Programming - V 3.0
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

226
Systems Programming - V 3.0
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
227
Systems Programming - V 3.0
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)

228
Systems Programming - V 3.0
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

229
Systems Programming - V 3.0
Contact Session - 5

Course Name :
Systems Programming
Dr. Parag Anil Tamhankar
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
⮚ cp, cut, paste commands.
231
Systems Programming - V 3.0
vi basics
• To write and edit some programs and scripts, we require
editors
• UNIX provides vi editor for BSD system – created by Bill
Joy
• Bram Moolenaar improved vi editor and called it as vim
(vi improved) on Linux OS

232
Systems Programming - V 3.0
The vi Editor
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.

233
Systems Programming - V 3.0
The vi Editor

234
Systems Programming - V 3.0
INPUT MODE – ENTERING AND
REPLACING TEXT

235
Systems Programming - V 3.0
INPUT MODE – ENTERING AND
REPLACING TEXT
Insertion of Text (i and a)

236
Systems Programming - V 3.0
INPUT MODE – ENTERING AND REPLACING TEXT
Insertion of Text at Line Extremes (I and A)

237
Systems Programming - V 3.0
INPUT MODE – ENTERING AND REPLACING TEXT

Opening a New Line (o and O)

238
Systems Programming - V 3.0
INPUT MODE – ENTERING AND REPLACING
TEXT
Replacing Text (r, s, R and S)

239
Systems Programming - V 3.0
INPUT MODE – ENTERING AND REPLACING
TEXT
Input Mode Commands

240
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Saving the Work (:w)

241
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Saving and Quitting (:x and :wq)

242
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Aborting Editing (:q)

243
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING

Writing Selected Lines

244
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Writing Selected Lines

245
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Escape to the UNIX Shell (:sh and [Ctrl-z])

246
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Recovering from a Crash (:recover and –r)

247
Systems Programming - V 3.0
ex MODE – SAVING TEXT AND QUITTING
Save and Exit Commands of the ex Mode

248
Systems Programming - V 3.0
Command MODE – NAVIGATION
Movement in the Four Directions (h, j, k and l)

249
Systems Programming - V 3.0
Command MODE – NAVIGATION
Movement in the Four Directions (h, j, k and l)

250
Systems Programming - V 3.0
Command MODE – NAVIGATION

Word Navigation (b, e and w)

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

251
Systems Programming - V 3.0
Command MODE – NAVIGATION
Moving to Line Extremes (0, | and $)

252
Systems Programming - V 3.0
Command MODE – NAVIGATION
Moving to Line Extremes (0, | and $)

253
Systems Programming - V 3.0
Command MODE – NAVIGATION
Scrolling ([Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u])

254
Systems Programming - V 3.0
Command MODE – NAVIGATION
Absolute Movement (G)

255
Systems Programming - V 3.0
Command MODE – EDITING TEXT
Editing Text – Deleting characters (x, X)

256
Systems Programming - V 3.0
Command MODE – EDITING TEXT
Editing Text – Deleting lines (dd)

257
Systems Programming - V 3.0
Command MODE – EDITING TEXT
Editing Text – Moving Text (p, P)

258
Systems Programming - V 3.0
Command MODE – EDITING TEXT

Editing Text)

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

259
Systems Programming - V 3.0
Command MODE – EDITING TEXT

Editing Text – Copying Text (y and p)

260
Systems Programming - V 3.0
Command MODE – EDITING TEXT
Editing Text – Joining Lines (J)

261
Systems Programming - V 3.0
Command MODE – EDITING TEXT

Undoing Last Editing Instructions (u and U)

262
Systems Programming - V 3.0
Command MODE – EDITING TEXT

Repeating the Last Pattern Search (n and N)

263
Systems Programming - V 3.0
SEARCHING FOR A PATTERN

• / search forward
• ? search backward

/printf The search begins forward to position the


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

264
Systems Programming - V 3.0
Command MODE – EDITING TEXT

Search and Repeat Commands

265
Systems Programming - V 3.0
ex MODE
Substitution – Search and Replace ( :s )

266
Systems Programming - V 3.0
ex MODE

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
267
Systems Programming - V 3.0
ex MODE
Substitution – Search and Replace ( :s )

268
Systems Programming - V 3.0
ex MODE
Command Function
:nd Deletes nth line
:m,n d Deletes line from m to n
:n mo p Moves line n after line p
:m,n mo p Moves line m to n after line p
:m co p Copies line m after line p
:m,n co p copies line m to n after line p
:m,n w file1 writes line m to n to a file file1
:m,n w >>file1 Appends line m to n to file file1
:r file1 Reads the contents of the file file1 at
current cursor position
:r !command Executes shell command and the
output of the command is read at the
current cursor position.
269
Systems Programming - V 3.0
cp – COPYING A FILE

270
Systems Programming - V 3.0
cp – COPYING A FILE

271
Systems Programming - V 3.0
cp – COPYING A FILE

272
Systems Programming - V 3.0
File Management Commands

Activity Single Source Multiple Source


Copy file cp file1 file2 cp file1 file2 file3 destdir
Move file mv file1 file2 mv file1 file2 file3 destdir
Remove file rm file1 rm –f file1 file2 file3
create directory mkdir dir mkdir –p dir1/dir2/dir3

copy directory cp –r dir1 dir2 cp –r dir1 dir2 dir3 destdir

move directory mv dir1 dir2 mv dir1 dir2 dir3 destdir

remove directory rm –rf dir1 rm –rf dir1 dir2 dir3

Systems Programming - V 3.0


•2
cut Command

• 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

274
Systems Programming - V 3.0
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

275
Systems Programming - V 3.0
cut Command

• To print the remaining fields, we have


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

276
Systems Programming - V 3.0
paste Command

• 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

277
Systems Programming - V 3.0
paste: pasting file (contd.,)

• We can specify one or more delimiters with -d

paste -d “|” cutlist1 cutlist2

Where each field will be separated by the delimiter |

278
Systems Programming - V 3.0
paste Command

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

279
Systems Programming - V 3.0
Contact Session - 6

Course Name :
Systems Programming
Dr. Parag Tamhankar
Agenda
⮚ Commands :
• cat - to create, view, concatenate files.
• mv - mv is used to move one or more files or directories
• rm - removes the entries for a specified file, group of files.
• cmp, comm, diff - to display the differences by comparing the files
• chmod- used to change the access permissions
• top - Displays list of running processes
• head- Pick lines from the beginning
• tail - Pick lines from the end
• sort - Sort, merge and remove
• 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
281
Systems Programming - V 3.0
cat – DISPLAYING AND CREATING FILES

282
Systems Programming - V 3.0
cat – DISPLAYING AND CREATING FILES

cat options ( -v and –n)

283
Systems Programming - V 3.0
cat – DISPLAYING AND CREATING FILES

cat is also useful for creating files

284
Systems Programming - V 3.0
rm – DELETING FILES

285
Systems Programming - V 3.0
rm – DELETING FILES

286
Systems Programming - V 3.0
rm – DELETING FILES

287
Systems Programming - V 3.0
rm – DELETING FILES

288
Systems Programming - V 3.0
mv – RENAMING FILES

289
Systems Programming - V 3.0
cmp – COMPARING TWO FILES

290
Systems Programming - V 3.0
comm – WHAT IS COMMON?

291
Systems Programming - V 3.0
comm – WHAT IS COMMON?

292
Systems Programming - V 3.0
comm – WHAT IS COMMON?

293
Systems Programming - V 3.0
diff – CONVERTING ONE FILE TO OTHER

294
Systems Programming - V 3.0
umask: DEFAULT FILE AND DIRECTORY
PERMISSIONS

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

295
Systems Programming - V 3.0
FILE OWNERSHIP

• When you create a file, you become its owner (third


column)
• Group owner of the file (fourth column)
• Several users may belong to a single group, but the
privileges of the group are set by the owner of the file
and not by the group members

296
Systems Programming - V 3.0
FILE OWNERSHIP

• UNIX follows a three-tiered file protection system that


determines a file’s access rights
• Filetype owner (rwx) groupowner (rwx) others (rwx)
• Example:
-rwxr-xr-- 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x
r--
owner/user group owner others
297
Systems Programming - V 3.0
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

298
Systems Programming - V 3.0
RELATIVE AND ABSOLUTE
PERMISSIONS

• In a relative manner, specify the changes to the


current permissions

• In an absolute manner, specify the final permissions

299
Systems Programming - V 3.0
RELATIVE PERMISSIONS

• chmod only changes the permissions specified in the


command line and leaves the other permissions
unchanged
• Syntax
chmod category operation permission filename(s)

300
Systems Programming - V 3.0
RELATIVE PERMISSIONS

• chmod takes an expression as its argument which


contains:
1. user category (user, group, others)
2. operation to be performed (assign or remove a
permission)
3. type of permission (read, write, execute)

301
Systems Programming - V 3.0
RELATIVE PERMISSIONS

Category operation permission


u - user + assign r - read
g - group - remove w - write
o - others = absolute x - execute
a - all (ugo)

302
Systems Programming - V 3.0
RELATIVE PERMISSIONS

• Examples
-rw-r--r-- 1 kumar metal 1906 sep 23:38
xstart

chmod u+x xstart

-rwxr--r-- 1 kumar metal 1906 sep 23:38


xstart

The command assigns (+) execute (x) permission to the


303
user (u), other permissions remain
Systems Programming - V 3.0 unchanged
RELATIVE PERMISSIONS

• chmod ugo+x xstart


• chmod a+x xstart
• chmod +x xstart

-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

chmod accepts multiple file names in command line

• chmod u+x note note1 note3


304
Systems Programming - V 3.0
RELATIVE PERMISSIONS

Let initially

-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

chmod go-r xstart

Then, it becomes
-rwx--x--x 1 kumar metal 1906 sep 23:38 xstart

305
Systems Programming - V 3.0
ABSOLUTE PERMISSIONS

• Need not to know the current file permissions


• Set all nine permissions explicitly
• A string of three octal digits is used as an
expression
• The permission can be represented by one octal
digit for each category
• For each category, we add octal digits

306
Systems Programming - V 3.0
ABSOLUTE PERMISSIONS

Octal Permissions Significance


0 --- no
permissions
1 --x execute only
2 -w- write only
3 -wx write and
execute
4 r-- read only
5 r-x read and
execute
6 rw- read and write
7 rwx read, write
and execute
307
Systems Programming - V 3.0
ABSOLUTE PERMISSIONS

• Using relative permission


chmod a+rw xstart

• Using absolute permission


chmod 666 xstart
chmod 644 xstart
chmod 761 xstart

will assign all permissions to the owner, read and write


permissions for the group and only execute permission to the
others
308
Systems Programming - V 3.0
ABSOLUTE PERMISSIONS

• 777 signifies all permissions for all category, but


still we can prevent a file from being deleted

• 000 signifies absence of all permissions for all


category, but still we can delete a file

• It is the directory permissions that determines


whether a file can be deleted or not

309
Systems Programming - V 3.0
PERMISSIONS

• Only owner can change the file permissions


• User can not change other user’s file’s permissions
• But the system administrator can do anything

310
Systems Programming - V 3.0
THE SECURITY IMPLICATIONS

• Let the default permission for the file xstart is


-rw-r--r--
• chmod u-rw, go-r xstart or
• chmod 000 xstart
----------
• This is simply useless but still the user can
delete this file

311
Systems Programming - V 3.0
chmod RECURSIVELY

chmod -R a+x shell_scripts

This makes all the files and subdirectories found in


the shell_scripts directory, executable by all
users

312
Systems Programming - V 3.0
DIRECTORY PERMISSIONS

• It is possible that a file cannot be accessed even though


it has read permission, and can be removed even when
it is write protected
• The default permissions of a directory are
rwxr-xr-x (755)
• A directory must never be writable by group and others

313
Systems Programming - V 3.0
DIRECTORY PERMISSIONS

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


755 (777-022) for directories
umask 000
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

314
Systems Programming - V 3.0
CHANGING FILE OWNERSHIP

• On BSD and AT&T systems


• Kumar – owner (user)
• Metal – group owner
• If sharma copies a file of kumar, then sharma will
become its owner and he can manipulate the
attributes
• chown and chgrp
• On BSD, only system administrator can use chown
• On other systems, only the owner can change both

315
Systems Programming - V 3.0
chown Command

• Changing ownership requires superuser


permission, so use su command
ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note
• chown sharma note; ls -l note
-rwxr----x 1 sharma metal 347 may 10 20:30 note

316
Systems Programming - V 3.0
chgrp Command

• This command changes the file’s group owner


• No superuser permission is required

ls –l dept.lst

-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
317
Systems Programming - V 3.0
sort : ordering a file
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 (fields) 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
318
Systems Programming - V 3.0
sort : ordering a file

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).
319
Systems Programming - V 3.0
head - display the start of a file

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

320
Systems Programming - V 3.0
tail - display the end of a file
tail displays the tail, or end, of the file.
Syntax
tail [options] file
Common Options
-number number of lines to display, counting from the bottom of
the file
Examples
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 file. To display the last 30 lines of a file use the -number
style:
tail -30 filename
321
Systems Programming - V 3.0
wc – COUNTING LINES, WORDS AND
CHARACTERS

322
Systems Programming - V 3.0
wc – COUNTING LINES, WORDS AND
CHARACTERS

323
Systems Programming - V 3.0
wc – COUNTING LINES, WORDS AND
CHARACTERS

324
Systems Programming - V 3.0
Tee Command
Tee command is used to store and view (both at the same time)
the output of any other command.
Tee command writes to the STDOUT, and to a file at a time as
shown in the examples below
The following command writes the output only to the file and not
to the screen.
$ ls > file

The following command (with the help of tee command) writes


the output both to the screen (stdout) and to the file.
$ ls | tee file
325
Systems Programming - V 3.0
top - display top CPU processes

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.

326
Systems Programming - V 3.0
Contact Session - 7

Course Name :
Systems Programming

Dr. Parag Tamhankar


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.
328
grep Command

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


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

329
grep Command

grep : searching for a pattern


grep options
Basic regular expressions (BRE)
An introduction
The character class
The *
The dot
Specifying pattern locations Metacharacters

330
grep Command

• It scans the file / input for a pattern and displays


lines containing the pattern, the line numbers or
filenames where the pattern occurs

• It’s a command from a special family in UNIX for


handling search requirements

grep options pattern filename(s)

331
emp.lst (Data used for grep)
• 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
• 5423 | n.k.gupta | chairman | admin | 30/08/56 | 5400
• 1006 | chanchal singhvi | director | sales | 03/09/38 | 6700
• 6213 | karuna ganguly | g.m. | accounts | 05/06/62 | 6300
• 1265 | s.n. dasgupta | manager | sales | 12/09/63 | 5600
• 4290 | jayant choudhury | executive | production | 07/09/50 | 6000
• 2476 | anil aggarwal | manager | sales | 01/05/59 | 5000
• 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
332
grep Command

grep “sales” emp.lst

• Patterns with and without quotes is possible


• Its generally safe to quote the pattern
• Quote is mandatory when pattren involves more
than one word
• It returns the prompt in case the pattren can’t be
located
grep president emp.lst
333
grep Command

• When grep is used with multiple filenames, it


displays the filenames along with the output

grep “director” emp1.lst emp2.lst

Where it shows filename followed by the contents

334
grep frequently used options

-i ignores case for matching


-v doesn’t display lines matching expression
-n displays line numbers along with lines
-c displays count of number of occurrences
-l displays list of filenames only

335
grep Command

-e exp specifies expression with this option


-x matches pattern with entire line
-f file takes pattrens from file, one per line
-E treats pattren as an extended RE
-F matches multiple fixed strings

336
grep Command

1. grep -i ‘agarwal’ emp.lst


2. grep -v ‘director’ emp.lst > otherlist
wc -l otherlist will display 11 otherlist
1. grep –n ‘marketing’ emp.lst
2. grep –c ‘director’ emp.lst
3. grep –c ‘director’ emp*.lst
will print filenames prefixed to the line
count

337
grep Command

1. grep -i ‘manager’ *.lst


will display filenames only
2. grep -e 'Agarwal' -e 'aggarwal' -e 'agrawal'
emp.lst
will print matching multiple patterns

338
BASIC REGULAR EXPRESSIONS

• It is tedious to specify each pattern separately


with the -e option
• grep uses an expression of a different type to
match a group of similar patterns
• if an expression uses meta characters, it is
termed a regular expression
• Some of the characters used by regular
expression are also meaningful to the shell

339
BRE character subset

* Zero or more occurrences


g* nothing or g, gg, ggg, etc.
. A single character
.* nothing or any number of characters
[pqr] a single character p, q or r
[c1-c2] a single character within the ASCII
range represented by c1 and c2

340
The character class

• grep supports basic regular expressions (BRE)


by default and extended regular expressions
(ERE) with the –E option

• A regular expression allows a group of


characters enclosed within a pair of [ ], in which
the match is performed for a single character in
the group

341
grep Command

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

• A single pattern has matched two similar strings


• The pattern [a-zA-Z0-9] matches a single
alphanumeric character. When we use range,
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)

342
THE *

* Zero or more occurrences of the previous character

g* nothing or g, gg, ggg, etc.

grep “[aA]gg*[ar][ar]wal” emp

Notice that we don’t require to use –e option three


times to get the same output!!!!!

343
THE DOT

A dot matches a single character

.* signifies any number of characters or none

grep “j.*saxena” emp.lst

344
^ and $

Most of the regular expression characters


are used for matching patterns, but there are two
that can match a pattern at the beginning or end
of a line

^ for matching at the beginning of a line


$ for matching at the end of a line

345
grep Command

grep “^2” emp.lst


Selects lines where emp_id starting with 2

grep “7…$” emp.lst


Selects lines where emp_salary ranges between
7000 to 7999

grep “^[^2]” emp.lst


Selects lines where emp_id doesn’t start with 2
346
When meta characters lose their meaning

• It is possible that some of these special characters


actually exist as part of the text
• Sometimes, we need to escape these characters
Eg: when looking for a pattern g*, we have to use \
To look for [, we use \[
To look for .*, we use \.\*

347
EXTENDED RE (ERE)

• 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

348
grep ERE

b+ matches b, bb, bbb, etc.

b? matches either a single instance of b or nothing

These characters restrict the scope of match as


compared to the *

grep –E “[aA]gg?arwal” emp.lst

349
The ERE set

ch+ matches one or more occurrences of


character ch
ch? Matches zero or one occurrence of
character ch

exp1|exp2 matches exp1 or exp2


(x1|x2)x3 matches x1x3 or x2x3

350
Matching multiple patterns

grep -E 'sengupta|dasgupta' emp.lst

We can locate both without using –e option twice,


or

grep -E '(sen|das)gupta' emp.lst

351
Essential Shell programming

352
What is Shell?
• A Shell Is a program that interprets commands
• It allows a user to execute commands
• Either manually at a terminal or
automatically in programs called shell scripts
• A shell is not an operating system
• It is a way to interface with the operating
system and execute commands
• Login shell is BASH = Bourne Again Shell
353
Advantage of using Shell
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
354
Bash
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

355
Wild cards
Wild cards – Special characters interpreted by shell

356
Wild card – “*”

357
Shell Meta characters

358
Shell Meta characters

359
Shell Scripts
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.
360
Wild card – “?”

361
Examples

362
System Variables in Shell

363
What is Shell Programming

• Grouping a set commands


• Programming constructs used

364
Need for Shell Programming

• To execute a set of commands regularly


• Typing every time every command is laborious & time
consuming
• To have control on the sequence of
commands to be executed based previous results

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

366
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”

367
Shell Scripts

To run the script we need to first make it


executable. This is achieved by using the chmod
command as shown below:
$ chmod +x script.sh
Then invoke the script name as:
$ script.sh

368
Shell Scripts

Explicitly spawn a child with script name as


argument:
sh script.sh

Note: Here the script neither requires a executable


permission nor an interpreter line.

369
Read: Making Scripts Interactive

• Shell’s internal tool for making scripts interactive


• Used with one or more variables.
• Inputs supplied with the standard input are read
into these variables.
Ex: read name
causes the script to pause at that point to take
input from the keyboard.

370
Read: Making Scripts Interactive

371
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;

372
Using Command Line Arguments

373
Using Command Line Arguments

#! /bin/sh
echo “Program Name : $0”
echo “No of Arguments : $#”
echo “Arguments are : $*”

$ chmod +x 2.sh
$ 2.sh A B C
o/p Program Name : 2.sh
No of Arguments : 3
Arguments are : A B C
374
Contact Session - 9

Course Name :
Systems Programming

Dr. Parag Tamhankar

Shell Scripting
Agenda

Current Session CS09


▪ Decision making constructs
▪ Shell scripts with more examples
▪ Conditional structure

376
Systems Programming - V 3.0
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.

377
Systems Programming - V 3.0
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;

378
Systems Programming - V 3.0
Using Command Line Arguments

379
Systems Programming - V 3.0
Shell Scripts

#!/bin/bash
echo "Hello, Student”

# This script displays the date, time, username and


# current directory.
echo "Date and time is "
date
echo
echo -e "Your username is: `whoami` \n"
echo -e "Your current directory is: \c"
pwd
380
Systems Programming - V 3.0
Shell Scripts
#Reading user input
#To read standard input into a shell script use the read command.
echo "Please enter your name:"
read name
echo "Welcome to Testing Department Mr. $name"

# This prompts the user for input, assigns this to the variable name and
then displays the value of this variable to standard output.

# If there is more than one word in the input, each word can be assigned
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“
381
Systems Programming - V 3.0
Shell Scripts
# Script to print user information who currently login , current date & time
#
clear
echo -e "\nHello $USER"
echo -e "\nToday is \c ";date
echo -e "\nNumber of user login : \c" ; who | wc -l
echo -e "\nCalendar\n"
cal

# declaration and printing of string variable


#!/bin/bash
x='Beautiful World'
echo $x
echo x # just string 'x'

382
Systems Programming - V 3.0
Shell Scripts

# printing of string constant


#!/bin/bash
echo 'hello'
echo "hello"
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 :- $* "
383
Systems Programming - V 3.0
Shell Scripts

# printing of string constant


#!/bin/bash
echo 'hello'
echo "hello"
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 :- $* "
384
Systems Programming - V 3.0
Starting

$ echo '#!/bin/sh' > my-script.sh


$ echo 'echo Hello World' >> my-script.sh
$ chmod 755 my-script.sh
$ ./my-script.sh
Hello World
$

• Note that to make a file executable, you must set the


eXecutable bit, and for a shell script, the Readable bit
must also be set:
• chmod a+rx first.sh

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


Commands

• grep "mystring" /tmp/myfile


• cat /tmp/myfile | grep "mystring“

Which is more efficient?

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


My first script

#!/bin/sh
# This is a comment!
echo Hello World # This is a comment, too!

• A new copy of the shell is invoked


• # is for command

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


Variables

#var.sh
#!/bin/sh
MY_MESSAGE="Hello World"
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, Deemed to be University under Section 3 of UGC Act, 1956


Input

# var1.sh
#!/bin/sh
echo What is your name?
read MY_NAME
echo "Hello $MY_NAME - hope you're well."

• MY_NAME is a variable
• read helps to read the value from console
• echo is used to echo the value on console
• $ sign is used to signify a variable

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


Scope of Variables

# myvar2.sh
#!/bin/sh
echo "MYVAR is: $MYVAR"
MYVAR="hi there"
echo "MYVAR is: $MYVAR"
Output:
$ ./myvar2.sh
MYVAR is:
MYVAR is: hi there
Note: MYVAR hasn't been set to any value, so it's blank.

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


Export

$ MYVAR=hello
$ ./myvar2.sh
MYVAR is:
MYVAR is: hi there
• Note: When you call myvar2.sh from your interactive shell, a new
shell is spawned to run the script.
$ export MYVAR
$ ./myvar2.sh
MYVAR is: hello
MYVAR is: hi there
• Note: We need to export the variable for it to be inherited by another
program - including a shell script.
• But changing the value does not export it back.

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


Run in the same shell

$ MYVAR=hello
$ echo $MYVAR
hello
$ . ./myvar2.sh
MYVAR is: hello
MYVAR is: hi there
$ echo $MYVAR
hi there

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


Handling Variables

# var3.sh
#!/bin/sh
echo "What is your name?"
read USER_NAME
echo "Hello $USER_NAME"
echo "I will create you a file called ${USER_NAME}_file"
touch "${USER_NAME}_file"

• The touch command changes attriobutes of a file and


creates a new file of 0 bytes if it does not exist
• Note the use of {} arounf the variable name to separate it
from text.

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


Escape Characters

$ echo Hello World


Hello World
$ echo "Hello World"
Hello World
$ echo "Hello \"World\"“
Hello "World"

• Note how SPACES are interpreted with and without


QUOTES.
• Note the use of \ to give character meaning to the
special character “

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


*

echo * shows all files in the current directory.


$ echo *
case.shtml escape.shtml first.shtml
functions.shtml hints.shtml index.shtml
ip-primer.txt raid1+0.txt
$ echo *txt
ip-primer.txt raid1+0.txt
$ echo "*"
*
$ echo "*txt"
*txt

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


Special characters

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


A quote is ", backslash is \, backtick is `.
$ echo "A few spaces are ; dollar is \$. \$X is ${X}."
A few spaces are ; dollar is $. $X is 5.

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


exit and Exit Status of Command

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.

397
Systems Programming - V 3.0
exit and Exit Status of Command

Parameter $? And its use


The variable stores the exit status of the last commend.
• 0 for success.
• Nonzero for failure
The value of the parameter is set by the argument of exit.
If no exit status is specified the value is 0 ( true or success)
Example:
$ grep director emp.lst > /dev/null: echo $?
0 Success
$ grep manager emp.lst > /dev/null: echo $?
1 Failure – in pattern search
$ grep manager emp3.lst > /dev/null: echo $?
2 Failure – in opening file

3 These returns conditions are used by programmers to control the flow of the
program.

398
Systems Programming - V 3.0
The Logical Operators && and ||- 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”

399
Systems Programming - V 3.0
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

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.

400
Systems Programming - V 3.0
The if Conditional

401
Systems Programming - V 3.0
Shell Scripts

#!/bin/bash
#Script to print file
if cat $1
then
echo -e "\n\nFile $1, found and successfully
echo ed“
fi

#!/bin/bash
# Script to see whether argument is positive
if test $1 -ge 0
then
echo "$1 number is positive“
fi 402
Systems Programming - V 3.0
The if Conditional

403
Systems Programming - V 3.0
Using test and [] to evaluate expressions

404
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

405
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

406
Systems Programming - V 3.0
Shell Scripts
# 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
407
Systems Programming - V 3.0
Shell Script

# 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 408
Systems Programming - V 3.0
Shell Scripts

# 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

409
Systems Programming - V 3.0
Shell Scripts

#!/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
410
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

411
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

412
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

413
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

414
Systems Programming - V 3.0
Using test and [] to evaluate expressions –
Numeric Comparison

415
Systems Programming - V 3.0
The case Conditional

416
Systems Programming - V 3.0
The case Conditional

417
Systems Programming - V 3.0
The case Conditional – Matching Multiple Patterns

418
Systems Programming - V 3.0
The case Conditional – Wild-Cards

419
Systems Programming - V 3.0
expr – Computation and String handling

420
Systems Programming - V 3.0
expr – Computation

421
Systems Programming - V 3.0
expr – Computation

422
Systems Programming - V 3.0
expr – String Handling

423
Systems Programming - V 3.0
expr – String Handling

424
Systems Programming - V 3.0
expr – String Handling

425
Systems Programming - V 3.0
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
Answer : a 426
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
427
3) Consider the following case statement. What will be the output if
the user inputs "y"?

read -p "Continue? (y/n): " answer


case $answer in
y|Y) echo "Continuing...";;
n|N) echo "Stopping...";;
*) echo "Invalid input";;
esac

a) Continuing...
b) Stopping...
c) Invalid input
d) Syntax error
Answer : a

428
Which of the following correctly tests if a number stored in var
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 )

429
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"
else
echo "Sum is less than 10"
fi
a) Sum is greater than 10
b) Sum is exactly 10
c) Sum is less than 10
d) Syntax error
Answer : a

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

431
ANSWER

#!/bin/bash
file="example.txt"

if [ ! -e "$file" ]; then
echo "File does not exist. Creating $file..."
touch "$file"
fi
if [ ! -s "$file" ]; then
echo "This is a new file" > "$file"
echo "Added text to new file."
else
echo "File is not empty. Appending date and time..."
echo "$(date)" >> "$file"
fi
echo "Displaying contents of $file:"
cat "$file"
432
Contact Session - 10

Course Name :
Systems Programming
Agenda

⮚ Loop constructs in Shell with examples


⮚ Shell scripting with file examples
⮚ set & shift Commands

434
Systems Programming - V 3.0
while: Looping

435
Systems Programming - V 3.0
while: Looping – Setting up an Infinite loop

436
Systems Programming - V 3.0
while: Looping

437
Systems Programming - V 3.0
while: Looping

438
Systems Programming - V 3.0
while: Looping

439
Systems Programming - V 3.0
while: Looping

440
Systems Programming - V 3.0
while: Looping

441
Systems Programming - V 3.0
Print numbers as 5,4,3,2,1
using while loop
#!/bin/bash
i=5
while test $i != 0
do O/P
5
echo "$i" 4
i=`expr $i - 1` 3
2
done 1

442
Systems Programming - V 3.0
Tables Printing using while loop

if [ $# -eq 0 ]
then
echo "Wrong number of arguments from command line"
exit 1 sh filename.sh 5
fi O/P
n=$1 5*1=5
5 * 2 = 10
i=1 5 * 3 = 15
while [ $i -le 10 ] 5 * 4 = 20
do 5 * 5 = 25
5 * 6 = 30
echo "$n * $i = `expr $i \* $n`" 5 * 7 = 35
i=`expr $i + 1` 5 * 8 = 40
done 5 * 9 = 45
5 * 10 = 50

443
Systems Programming - V 3.0
Pyramid printing using while

rows=5 # Print stars


i=1 j=1
while [ $i -le $rows ] while [ $j -le $((2 * i - 1)) ]
do
do
echo -n "*"
j=$i j=$((j + 1))
while [ $j -lt $rows ] done
do echo ""
echo -n " "
j=$((j + 1)) i=$((i + 1))
done done
Output:

*
***
*****
*******
*********
444
Systems Programming - V 3.0
Shell script to find sum of first N numbers
from number M (take N and M from the user)

445
Systems Programming - V 3.0
for: Looping with a list

446
Systems Programming - V 3.0
for: Looping with a list

447
Systems Programming - V 3.0
for: Looping with a list –
Possible sources of the List

448
Systems Programming - V 3.0
for: Looping with a list –
Possible sources of the List

449
Systems Programming - V 3.0
Program to break out of a for loop using
'break' command as shown below

clear
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
if [ $i -eq 3 ]; then
break;
fi
done
O/P:
Weekday 1 : Mon
Weekday 2 : Tue
450
Systems Programming - V 3.0
Program to add "(WEEKEND)" to Sat and
Sun, and "(weekday)" to rest of the days.

clear
i=1
for day in Mon Tue Wed Thu Fri Sat Sun
do
echo -n "Day $((i++)) : $day"
if [ $i -eq 7 -o $i -eq 8 ]; then O/P
Day 1 : Mon (weekday)
echo " (WEEKEND)" Day 2 : Tue (weekday)
Day 3 : Wed (weekday)
continue; Day 4 : Thu (weekday)
fi Day 5 : Fri (weekday)
Day 6 : Sat (WEEKEND)
echo " (weekday)" Day 7 : Sun (WEEKEND)
done
451
Systems Programming - V 3.0
Program to sum the numbers
passed to its argument on command line

clear
if [ $# -eq 0 ]
then
echo "No arguments passed"
exit
fi
sum=0
for i in $*
do
sum=`expr $sum + $i` O/P
sh filename.sh 20 30 40 10 50
done Sum of Numbers is : 150
echo "Sum of Numbers is : $sum"
452
Systems Programming - V 3.0
Pyramid printing using for loop

rows=5

for ((i=1; i<=rows; i++))


do
for ((j=i; j<rows; j++))
do
echo -n " "
Output:
done
*
# Print stars ***
for ((j=1; j<=(2*i-1); j++)) *****
do *******
echo -n "*" *********
done
echo ""
done
453
Systems Programming - V 3.0
Program to find factorial of a positive
integer N (take N from the user). fact(N)=1×2×…..×N

Using while loop

echo "Input N" while [ $i -le $N ]


read N do
fact=1 fact=`expr $fact \* $i`
if [ $N -lt 0 ] i=`expr $i + 1`
then done
Echo "Wrong arguments" echo "The factorial of $N is
exit 1 $fact"
fi
i=1

454
Systems Programming - V 3.0
Program to find factorial of a positive
integer N (take N from the user). fact(N)=1×2×…..×N

Using for loop

echo "Input N" for ((i=1; i<=N; i++))


read N do
fact=1 fact=`expr $fact \* $i`
if [ $N -lt 0 ] done
then echo "The factorial of $N is
echo "Wrong arguments" $fact"
exit 1
fi
sh filename.sh
Input N
5
The factorial of 5 is 120
455
Systems Programming - V 3.0
Script to print the first N numbers in Fibonacci series
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
O/P
echo "Input N" fi Input N
read N for ((i=0; i<N; i++)) 5
0
fib0=0 do 1
fib1=1 echo "$fib0 " 1
fib2=1 fib0=$fib1 2
3
if [ $N -lt 0 ] fib1=$fib2
then fib2=`expr $fib0 + $fib1`
echo "Wrong arg" done
exit 1
456
Systems Programming - V 3.0
Nesting of Loops [for and while]

457
Systems Programming - V 3.0
Nesting of Outer Loop and Inner Loop
1,1
1,2
i=1 1,3
while [ $i -le 4 ] # Outer for loop 1,4
do
j=1 2,1
while [ $j -le 4] # Inner for loop 2,2
do 2,3
echo "$i , $j " 2,4
j=`expr $j + 1`
3,1
done
3,2
echo " " #print the new line 3,3
i=`expr $i + 1` 3,4
done
4,1
4,2
4,3
4,4 458
Systems Programming - V 3.0
Nesting of Loops [for and while]

459
Systems Programming - V 3.0
Take two integer numbers N and M from user and
display all the Multiplication table between N and M

460
Systems Programming - V 3.0
Take start range as N and end range as M from user and
Find all prime numbers between N and M

461
Systems Programming - V 3.0
Files in Shell Scripting
In shell programming, files are fundamental for storing and
manipulating data. Below are some key file concepts in shell
programming:

1. File Types

Regular Files: These are standard files that contain data, such as
text files, binaries, etc.
Directories: These contain information about files and other
directories.
Special Files: These include devices, pipes, and sockets.

462
Systems Programming - V 3.0
Files in Shell Scripting
2. File Operations

Reading Files: Using commands like cat, less, more, head, tail, or by
opening them in a loop with read.
Writing to Files: Using redirection (> or >>) or commands like echo,
tee.
Appending to Files: Using >> to add content to the end of a file
without overwriting it.
Deleting Files: Using the rm command to remove files.
Copying Files: Using cp to duplicate files.
Moving/Renaming Files: Using mv to move or rename files.
Creating Files: Using touch to create an empty file or
echo "" > filename to create and write to a file.
463
Systems Programming - V 3.0
Files in Shell Scripting
3. File Redirection

Standard Output (stdout): Redirecting output to a file using >.


echo "Hello World" > output.txt
# Writes "Hello World" to output.txt

Standard Error (stderr): Redirecting error messages to a file using 2>.


ls non_existing_file 2> error.log
# Redirects the error message to error.log

Standard Input (stdin): Redirecting input from a file using <.


while read line; do echo $line; done < input.txt
# Reads from input.txt
464
Systems Programming - V 3.0
Files using Shell Scripting
Write a shell script program to display all the even
numbers from the file.
Create a text file called integernos.txt and store some random
numbers in it (one number in one line)
while read line
do
x=`expr $line % 2`
if [ $x ‐eq 0 ]
then
echo $line
fi
done < integernos.txt
465
Systems Programming - V 3.0
Files using Shell Scripting

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

466
Systems Programming - V 3.0
Files using Shell Scripting

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 >
datanew.txt

467
Systems Programming - V 3.0
Files using Shell Scripting

468
Systems Programming - V 3.0
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
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 469
Systems Programming - V 3.0
Write a shell script to find all
prime numbers from integernos.txt file and
store in another file called primes.txt

470
Systems Programming - V 3.0
Shell script that reads numbers from a file, checks which ones
are prime, and writes the prime numbers to another file

input_file="input.txt" # Input file with numbers


output_file="primes.txt" # Output file to store prime numbers

# Function to check if a number is prime


is_prime() {
local num=$1
if [ $num -le 1 ]; then
return 1
fi
for ((i=2; i*i<=$num; i++)); do
if [ $(($num % $i)) -eq 0 ]; then
return 1
fi
done
return 0
}
471
Systems Programming - V 3.0
Cont..

# Clear the output file


> "$output_file"

# Read numbers from the input file and check for primes
while read -r number; do
if is_prime "$number"; then
echo "$number" >> "$output_file"
fi
done < "$input_file"

echo "Prime numbers have been written to $output_file."

472
Systems Programming - V 3.0
set and shift: Manipulating the Positional
Parameters

473
Systems Programming - V 3.0
set and shift: Manipulating the Positional
Parameters

474
Systems Programming - V 3.0
shift: Shifting Arguments left

475
Systems Programming - V 3.0
Contact Session - 11

Course Name :
Systems Programming

Dr. Parag Tamhankar


Agenda

 awk
 getopts
 automation using shell
 crontab
 at command for scheduling

477
Systems Programming - V 3.0
awk
 awk is a powerful text-processing tool in
Unix/Linux. It is used for pattern scanning and
processing.
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
478
Systems Programming - V 3.0
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
Rajeev Designer 479
Systems Programming - V 3.0
awk
Example 2: Sum of column
If you have a file system.txt consists of following
numbers: 10,20,30,40
To calculate the sum of the numbers:
awk '{s+=$1} END {print s}' system.txt

O/P:
100
480
Systems Programming - V 3.0
getopts
 getopts is a built-in shell function used to parse
command-line options. Here is example f1.sh
#!/bin/bash
while getopts "a:b:c" opt; do
case $opt in
a) echo "Option a with value: $OPTARG" ;;
b) echo "Option b with value: $OPTARG" ;;
c) echo "Option c selected" ;;
\?) echo "Invalid option: -$OPTARG" ;;
esac
done
481
Systems Programming - V 3.0
getopts
 getopts: This built-in command is used to parse command-line
options. It reads options and their arguments, one by one, from
the command line.

 "a:b:c": The string "a:b:c" specifies the valid options:


a:: Option a requires an argument (indicated by the colon : after a).
b:: Option b also requires an argument.
c: Option c does not require an argument (no colon after c).

 opt: This variable stores the current option being processed.

 The while loop will continue to process options until all options
from the command line are parsed.
Systems Programming 482
getopts
 Execute the following command
./f1.sh -a 10 -b 20 –c

O/P:
Option a with value: 10
Option b with value: 20
Option c selected

483
Systems Programming - V 3.0
Automation Using Shell
 Automation often involves combining shell
scripts with other tools like awk, sed, etc.
 Example 1: Automated Backup Script
#!/bin/bash
# Backup Directory
BACKUP_DIR="/backup"
# Create Backup with Timestamp
tar -czf $BACKUP_DIR/backup_$(date +%F_%T).tar.gz /path/to/data
# Print Message
echo "Backup created at $BACKUP_DIR"
484
Systems Programming - V 3.0
Automation Using Shell
 BACKUP_DIR="/backup": This line defines the path to
the directory where the backup will be stored. The
variable BACKUP_DIR holds this path. You can change
/backup to any directory where you want to save the
backup.

 tar -czf:tar is a Unix/Linux utility used for creating


archive files.
-c creates a new archive.
-z compresses the archive using gzip.
-f specifies the filename of the archive to be created.
Systems Programming 485
Automation Using Shell
 $BACKUP_DIR/backup_$(date +%F_%T).tar.gz: The backup file is
saved in the directory specified by BACKUP_DIR.
The filename is backup_$(date +%F_%T).tar.gz, where:
$(date +%F_%T) generates a timestamp using the date
command.
%F produces the date in YYYY-MM-DD format.
%T produces the time in HH:MM:SS format.

The final backup file will have a name like backup_2024-08-


24_15:30:01.tar.gz.
 /path/to/data: This is the path to the directory or files you want
to back up. Replace /path/to/data with the actual directory or
files you need to back up.
Systems Programming 486
crontab
 crontab is used to schedule recurring tasks.
Example 1: Run a Script Every Day at 2 AM
 Edit crontab file
crontab –e
Add the following line:
0 2 * * * /path/to/script.sh

487
Systems Programming - V 3.0
crontab
 Example 2: Run a Command Every 5 Minutes

*/5 * * * * /path/to/command
• */5: This means "every 5 minutes." The */
syntax specifies that the command should run
at every interval of 5 minutes.
• *: The remaining asterisks represent "every
hour," "every day," "every month," and "every
day of the week," respectively.
488
Systems Programming - V 3.0
at
 at is used to schedule a command to run
once at a particular time.
Example 1: Run a Command at 3:30 PM
echo "tar -czf /backup/backup.tar.gz
/path/to/data" | at 3:30 PM
• echo "tar -czf /backup/backup.tar.gz /path/to/data":
This echoes the command that you want to execute at the
scheduled time.
• | at 3:30 PM: The at command schedules the echoed
command to run at 3:30 PM on the current day.
489
Systems Programming - V 3.0
Advanced Example
 You can combine all these tools to create an
automated system monitoring script that runs
periodically and alerts you if something goes
wrong.

 Monitor.sh script will check the disk usage every


30 minutes and send an email if the usage
exceeds 90%.

490
Systems Programming - V 3.0
Advanced Example: monitor.sh
#!/bin/bash
# Check Disk Usage
DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
# Threshold
THRESHOLD=90
# If Disk Usage Exceeds Threshold, Send an Email
if [ $DISK_USAGE -gt $THRESHOLD ]; then
echo "Disk usage is critically high: $DISK_USAGE%" | mail -s "Disk
Usage Alert" chandrashekhar.chavan@wilp.bits-pilani.ac.in
Fi
crontab Entry: */30 * * * * /path/to/monitor.sh
491
Systems Programming - V 3.0
Advanced Example: monitor.sh
 df /: The df command reports the disk space usage of
file systems. df / checks the root ("/") file system.
 grep /: Filters the output to include only the line
containing the root file system.
 awk '{print $5}': Extracts the fifth column, which
represents the percentage of disk space used.
 sed 's/%//g': Removes the percentage symbol (%) from
the value so that it can be compared as an integer.

492
Systems Programming - V 3.0
Advanced Example: monitor.sh
 echo "Disk usage is critically high: $DISK_USAGE%": Prepares the
message body, including the current disk usage percentage.

 mail -s "Disk Usage Alert" chandrashekhar.chavan@wilp.bits-


pilani.ac.in: Sends an email with the subject "Disk Usage Alert" to
the specified email address.

 */30 * * * *: This crontab entry means the script will run every 30
minutes.

 /path/to/monitor.sh: Replace this with the actual path to your


script. 493
Systems Programming - V 3.0
Shell Scripts : Demonstrations

Script 1
Script to read values from the user and perform arithmetic operations
#!/bin/bash
##Arithmetic operations using inputs read from the user
echo "Enter the values : "
read -p 'Enter Value 1: ' a
read -p 'Enter Value 2: ' b
ADD=$(($a+$b))
SUB=$(($a-$b))
MUL=$(($a*$b))
DIV=$(($a/$b))
echo -e "Addition=$ADD\nSubtraction=$SUB\nMultiplication=$MUL\nDivision=$DIV"
Script for arithmetic operations using case
statement

Script 2
#!/bin/bash
###Arithmetic Operations using case statement
read -p 'Enter Value1 : ' a
read -p 'Enter value2 : ' b
read -p 'Enter the operation[ADD|SUB|MUL|DIV]: ' op
case $op in
ADD) ADD=$(($a+$b))
echo "Addition = $ADD" ;;
SUB) SUB=$(($a-$b))
echo "Subtraction = $SUB" ;;
MUL) MUL=$(($a*$b))
echo "Multiplication = $MUL" ;;
DIV) DIV=$(($a/$b))
echo "Divide = $DIV" ;;
*) echo "Invalid Operation!!" ;;
esac
Script 3
Script to demonstrate the useage of Array
Variables

###Script to demonstrate the usage of Array Variables


###Defining the array variables individually
name[0]=Smith
name[1]=Steve
name[2]=Rohit
### Accessing the variables
echo 'echo ${name[0]}='${name[0]}
echo 'echo ${name[1]}='${name[1]}
echo 'echo ${name[2]}='${name[2]}
### Accessing all values at a time
echo 'echo ${name[*]}='${name[*]}
echo 'echo ${name[@]}='${name[@]}
### To get the array size
echo 'echo ${#name[*]}='${#name[*]}
echo 'echo ${#name[@]}='${#name[@]}
Script 4 :- Conditional Control Structures

if – statement
###Script to check the type of file
#!/bin/bash
###File test using if--else statement
FILE='/dev/sda'
if [ -c $FILE ]; then
echo "$FILE is a character special file"
else
echo "$FILE is not a character special file"
fi
if [ -b $FILE ]; then
echo "$FILE is a block special file"
else
echo "$FILE is not a block special file"
fi
if [ -d $FILE ]; then
echo "$FILE is a directory"
else
echo "$FILE is not a directory"
fi
Script 5

###Option parsing using GETOPTS


#!/bin/bash
###Option Parsing using GETOPTS
if [ "$#" -ne "6" ]; then
echo "Invalid Parameters"
exit 1
fi
while getopts a:b:c: var >/dev/null 2>&1
do
case $var in
a) a=$OPTARG;;
b) b=$OPTARG;;
c) c=$OPTARG;;
*) echo Invalid Suffix ; exit 1;;
esac
done

echo "Values given to suffix a = $a"


echo "Values given to suffix b = $b"
echo "Values given to suffix c = $c"
Script 6
Shell script for counting the total number of lines across a set of files.

#!/bin/bash
# Counting the number of lines in a list of files
# for loop over arguments
if [ $# -lt 1 ]
then
echo "Usage: $0 file ..."
exit 1
fi
echo "$0 counts the lines of code"
l=0
n=0
s=0
for f in $*
do
l=`wc -l $f | sed 's/^\([0-9]*\).*$/\1/'`
echo "$f: $l"
n=$[ $n + 1 ]
s=$[ $s + $l ]
Done
echo "$n files in total, with $s lines in total"
Contact Session - 12

Course Name :
Systems Programming

Dr. Parag Tamhankar


Agenda

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

501
Systems Programming - V 3.0
System Call

502
Systems Programming - V 3.0
System calls for low level I/O

503
Systems Programming - V 3.0
File related system calls ‐ open

504
Systems Programming - V 3.0
File related system calls – creat, close

505
Systems Programming - V 3.0
File related system calls – read

506
Systems Programming - V 3.0
File related system calls – write

507
Systems Programming - V 3.0
System call program example

508
Systems Programming - V 3.0
File related system calls – lseek

509
Systems Programming - V 3.0
File related system calls – lseek

510
Systems Programming - V 3.0
File related system calls – mkdir, rmdir

511
Systems Programming - V 3.0
File related system calls – rename

512
Systems Programming - V 3.0
File related system calls – link

513
Systems Programming - V 3.0
File related system calls – symlink

514
Systems Programming - V 3.0
File related system calls – unlink

515
Systems Programming - V 3.0
System calls related to process
fork :- For creating a duplicate process from the parent
process.
wait :- Processes are supposed to wait for other processes to
complete their work.
exec :- Loads the selected program into the memory.
exit :- Terminates the process
signal :- signal() system call installs a new signal handler for the
signal with number signum.
kill :- Takes two arguments. The first, pid, is the process ID you
want to send a signal to, and the second, sig, is the signal you
want to send. Therefore, you have to find some way to know the
process ID of the other party.
raise :- The raise() function sends the signal sig to the running
program.
516
Process related system calls – fork

517
Systems Programming - V 3.0
Process related system calls – fork

518
Systems Programming - V 3.0
Process related system calls – fork

519
Systems Programming - V 3.0
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.
520
Systems Programming - V 3.0
Calling fork()
#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;
}

521
Systems Programming - V 3.0
Calling fork()
#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;
} 522
Systems Programming - V 3.0
Calling fork()
#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;
}
523
Systems Programming - V 3.0
Process Creation Example

524
Systems Programming - V 3.0
Process related system calls – wait

525
Systems Programming - V 3.0
Process related system calls – wait

526
Systems Programming - V 3.0
Process related system calls – wait

527
Systems Programming - V 3.0
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;
} 528
Systems Programming - V 3.0
Process related system calls – wait

529
Systems Programming - V 3.0
Output of system calls – wait
[vasudevarao@localhost Systemcalls]$ gcc Wait.c -o Wait
[vasudevarao@localhost Systemcalls]$ ./Wait
fork program starting
This is from parent process
This is from child process I am exiting
Child exited already now with exit status 512

530
Systems Programming - V 3.0
Process related system calls – exec

531
Systems Programming - V 3.0
Process related system calls – exec

532
Systems Programming - V 3.0
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.

533
Systems Programming - V 3.0
We have two .c files here
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;
}

534
Systems Programming - V 3.0
helloexec.c

#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

535
Systems Programming - V 3.0
536
Systems Programming - V 3.0
Process related system calls – exit

537
Systems Programming - V 3.0
Process related system calls – exit
#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;
int status;
else
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);
}
} 538
Systems Programming - V 3.0
Process related system calls – exit
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

539
Systems Programming - V 3.0
Process related system calls – signal

540
Systems Programming - V 3.0
Process related system calls – signal

541
Systems Programming - V 3.0
Process related system calls – signal

542
Systems Programming - V 3.0
Process related system calls – signal

543
Systems Programming - V 3.0
Process related system calls – signal

544
Systems Programming - V 3.0
Process related system calls – signal

545
Systems Programming - V 3.0
Process related system calls – signal

546
Systems Programming - V 3.0
Process related system calls – signal

547
Systems Programming - V 3.0
Process related system calls – kill

548
Systems Programming - V 3.0
Process related system calls – kill & raise

549
Systems Programming - V 3.0
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.

550
Systems Programming - V 3.0
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;
}

551
Systems Programming - V 3.0
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.

552
Systems Programming - V 3.0
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
and new file descriptors may be used interchangeably.
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.

553
Systems Programming - V 3.0
System calls for IPC: dup()
#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);
}
554
Systems Programming - V 3.0
Contact Session – 13

Course Name :
Systems Programming

Dr. Parag
Tamhankar
Agenda
⮚ Assemblers
⮚ Assembler’s functions
⮚ Assembler directives
⮚ Object code structure
⮚ Object code
⮚ Two pass assembler
⮚ Data structures for two pass assembler
⮚ Operation code table
⮚ Symbol table and location counter

556
Systems Programming - V 3.0
Software

557
Systems Programming - V 3.0
System Software
• System software is computer software designed to
operate the computer hardware and to provide a
platform for running application software.
• Examples :
– Loaders
– Assembler
– Linkers
– Operating system
– Compilers
– Editors
558
Systems Programming - V 3.0
Assemblers

559
Systems Programming - V 3.0
Assemblers

560
Systems Programming - V 3.0
What is an Assembler & Assembly Language?

• An assembler is a type of computer program that takes in basic


instructions written in assembly language and converts them into a
pattern of bits (machine language) that the computer's processor
can use to perform basic operations.
• Assembly Language: Assembly language is a kind of low level
programming language, which uses symbolic codes or mnemonics
as instruction.
• Some examples of mnemonics include ADD, SUB, LDA and STA that
stands for addition, subtraction, load accumulator ans store
accumulator, respectively.

Systems Programming 561


Applications of Assembly Language

• Assembly language is used for direct hardware manipulation,


access to specialized processor instructions, or to address
critical performances issues.
• Typical uses are device drivers(CD, HDD). Low-level embedded
systems (Keyboard, water tank indicator) and real-time
systems (computer, notepad).

Systems Programming 562


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
the target machine code.
563
Systems Programming - V 3.0
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

Systems Programming 564


Assembly Process
• Convert .asm file into .obj file
1. Pass 1: Complete scan .asm file. Find all labels,
instructions and calculating corresponding address.
2. Pass2: Convert all the instructions into machine language
format.
3. Symbol Table: Stored all the information of assembly
language data, variables, instructions, address etc.

Systems Programming 565


Assembler Directives

566
Systems Programming - V 3.0
Two Pass Assembler
Pass 1 (Analysis)
•This part scans the program looking for symbols, labels,
variables, etc. and organizes them in labels.
– Passes through the instructions in sequence, looking for
symbol addresses.
– Create a symbol and literal table
– Keep track of the location counter
– Process Pseudo operation(Assembler directives)
– Error checking

567
Systems Programming - V 3.0
Two Pass Assembler

Pass 2 (synthesis)
•If no errors are found in pass one then the second pass
assembles the code into object code.
– Symbolic addresses are replaced with absolute addresses
– Symbolic opcodes are replace with binary opcodes
– Perform processing of assembler directives not done in
Pass 1
– Write the object program and the assembly listing

Systems Programming 568


Two Pass Assembler

569
Systems Programming - V 3.0
Data Structures

• Operation Code Table (OPTAB)


• Symbol Table (SYMTAB)
• Location Counter(LOCCTR)

570
Systems Programming - V 3.0
Data Structures

571
Systems Programming - V 3.0
Data Structures

572
Systems Programming - V 3.0
Data Structures

573
Systems Programming - V 3.0
Pseudo code

574
Systems Programming - V 3.0
Pseudo code

575
Systems Programming - V 3.0
Pseudo code

576
Systems Programming - V 3.0
Copy Program Before LOC is assigned by
Assembler
Line LOC Source Startement Object Code
5 COPY START 1000
10 FIRST STL RETADR
15 CLOOP JSUB RDREC
20 LDA LENGTH
25 COMP ZERO
30 JEQ ENDFILL
35 JSUB WRREC
40 J CLOOP
45 ENDFILL LDA EOF
50 STA BUFFER
55 LDA THREE
60 STA LENGTH
65 JSUB WRREC
Systems Programming 577
Location Address and Object Code for each
instruction
• Based on the starting address all the instructions are assigned
with memory.
– In our example, starting address is 1000
– All instruction occupies 3 bytes, so second instruction LOC
will be 1003 and goes on…
– All the LOC addresses are written in hexa-decimal value
• Opcode has its mnemonic value and after that address for its
corresponding label is specified, so for each line object code is
specified.
• Converted all the instruction into its object code.

Systems Programming 578


v=4cniAiTgQKU

Program – takes input from input device


F1 and copies them to output device 05

579
Systems Programming - V 3.0
Program – takes input from input device
F1 and copies them to output device 05

580
Systems Programming - V 3.0
Example: Source Program to Object Code

581
Systems Programming - V 3.0
Example: Source Program to Object Code

582
Systems Programming - V 3.0
Object Program
Simple object program format contains 3 types of records
– Header
• Starting address, program name and length
– Text
• Translated instructions and data of the program
together with an indication of the addresses where
these are to be loaded
– End
• Marks the end of the program and specifies the
address in the program where the execution is to
begin
583
Systems Programming - V 3.0
Object Program

584
Systems Programming - V 3.0
Object Code

585
Systems Programming - V 3.0
Object Code

586
Systems Programming - V 3.0
Difficulties: Forward Reference

587
Systems Programming - V 3.0
Example 2: Generate Object
Program
LABEL OPCODE Operand
• Generate the complete SUM START 4000(H)

object program for the FIRST LDX ZERO


LDA ZERO
following assembly LOOP ADD TABLE,X
language program. TIX COUNT

Assume standard SIC JLT LOOP


STA TOTAL
machine and the RSUB
following machine codes TABLE RESW 2000
in hexa and also indicate COUNT RESW 1
ZERO WORD 0
the content of symbol at TOTAL RESW 1
the end. Systems Programming
END FIRST
588
Example: How Assembler
Converts Assembly
Language code

Systems Programming 589


Example of Assembly Language with
Assembler Directives

Systems Programming 590


Example of Assembly Language with
Assembler Directives
• START: This instruction starts the execution of program from
location 200 and label with START provides name for the
program(John is name for program)
• MOVER: it moves the content of literal(=‘3’) into register operand
R1.
• MOVEM: it moves the content of register into memory
operand(X).
• MOVER: It again moves the content of literal(=‘2’) into register
operand R2 and it label is specified as L1.
• 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 591
Design of working Assembler
1. Analysis Phase (PASS 1 of Assembler):
1. To build symbol table for synthesis phase to
proceed.
2. Determines address of each symbols called as
memory allocation.
3. Location counter used to hold address of next
instruction.
4. Isolated label, mnemonic opcode, operands,
constants etc.
5. Validate meaning and address of each statements.
2. Synthesis Phase: (Pass 2 of Assembler):
1. Use data structures generated by analysis phase.
2. To build machine instructions for every assembly
statements as per mnemonic code and there
address allocation.
3. Synthesis machine instruction as per source code.

Systems Programming 592


Pass 1 of Assembler (Analysis Phase)
1. Symbol Table(St or SYMTAB): Store value or
address assign to the Label.
Label Address
John 200
L1 202
X 204

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 593


Pass 1 of Assembler (Analysis Phase)

• 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 594


Pass 2 of Assembler (Synthesis Phase)
• In the second pass the instruction are again read and are assembled using
the symbol table.
• Basically, the assembler goes through the block of program and generates
machine code for that instruction.
• Then the assembler proceeds to the next instruction. In this way, the
entire machine code program is created.
• Convert mnemonic operation code, symbolic table operands with there
equivalent machine code.
• Convert data constants to internal machine representations.
• Convert complete program into .obj file.

Systems Programming 595


Contact Session – 14

Course Name :
Systems Programming

Dr. Parag Tamhankar


Agenda
 Loaders
 Basic Loader Functions
 Absolute loader
 Bootstrap loader
 Relocating loader (relative loader)
 Direct linking loader

597
Systems Programming - V 3.0
Basic Definition
Loading - which allocates memory location and brings the
object program into memory for execution - (Loader)

Linking- which combines two or more separate object


programs and supplies the information needed to allow
references between them - (Linker)

Relocation - which modifies the object program so that it can


be loaded at an address different from the location originally
specified - (Linking Loader)

598
Systems Programming - V 3.0
Role of Loader

Source Object Object

Program Translator Loader program


ready for
Program execution

Translator – Assembler/Compiler

Memory

599
Systems Programming - V 3.0
Role Of Loader And Linker

Memory

Source Object
Program Assembler Linker
Program Object
program
ready for
Executable execution

Code

Loader

600
Systems Programming - V 3.0
• Source Program – Assembly Language
• Object Program - From assembler
• Contains translated instructions and data values from
the source program
• Executable Code - From Linker
• Loader - Loads the executable code to the specified
memory locations and code gets executed.

601
Systems Programming - V 3.0
Need For Linking And Loading

602
Systems Programming - V 3.0
We Need…three Processes
•Loading - which allocates memory location and brings the object
program into memory for execution
•Relocation - which modifies the object program so that it can be
loaded at an address different from the location originally
specified.
•Linking- which combines two or more separate object programs
and supplies the information needed to allow references between
them
•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.
603
Systems Programming - V 3.0
Basic Loader Functions

• A Loader is a system program that performs the


loading function
•It brings object program into memory and starts its
execution

604
Systems Programming - V 3.0
Type Of Loaders

• Absolute loader
• Bootstrap loader
• Relocating loader (relative loader)
• Direct linking loader

605
Systems Programming - V 3.0
Absolute Loader
• Operation is very simple
• The object code is loaded to specified locations
in the memory
• At the end the loader jumps to the specified
address to begin execution of the loaded
program

606
Systems Programming - V 3.0
Role Of Absolute Loader

1000
Object Absolute Object
Program Loader program
ready for
execution
2000

Memory

607
Systems Programming - V 3.0
Absolute Loader
For a simple absolute loader, all functions are
accomplished in a single pass as follows:

1) The Header record of object programs is checked to


verify that the correct program has been presented for
loading.
2) As each Text record is read, the object code it contains
is moved to the indicated address in memory.
3) When the End record is encountered, the loader jumps
to the specified address to begin execution of the
loaded program
608
Systems Programming - V 3.0
Advantages and disadvantages of
absolute loader

• The advantage of absolute loader is that it is simple


and efficient, but the need for programmer to specify
the actual address restricts the flexibility.

• As a result we cannot run several independent


programs together, sharing memory between them.

• Another disadvantage is that it is difficult to use


subroutine libraries while using an absolute loader.

609
Systems Programming - V 3.0
Absolute Loader Implementation

610
Systems Programming - V 3.0
Begin
read Header record
verify program name and length
read first Text record
while record type is <> ‘E’ do
begin
{if object code is in character form, convert into
internal representation}
move object code to specified location in memory
read next object program record
end
jump to address specified in End record
end
611
Systems Programming - V 3.0
Object Program
• An object program is loaded at the address specified
on the START directive.
• No relocation or linking is needed
• Thus is very simple

612
Systems Programming - V 3.0
Absolute Loader

No text record corresponds here. XXX indicates that the


previous contents of these locations remain unchanged.

BITS Pilani, Pilani Campus


Space for
2 inch x 2 inch
size Picture
Format-1(a) (in PowerPoint)

614
Systems Programming - V 3.0
A Simple Bootstrap Loader

• When a computer is first tuned on or restarted, a


special type of absolute loader, called bootstrap
loader is executed
• This bootstrap loads the first program to be run by
the computer -- usually an operating system

615
Systems Programming - V 3.0
Example (Sic Bootstrap Loader)

• The bootstrap itself begins at address 0


• It loads the OS starting address 0x80
• No header record or control information, the
object code is consecutive bytes of memory

616
Systems Programming - V 3.0
Bootstrap Loaders

How is the loader itself loaded into the memory ?

• When computer is started – with no program in memory, a


program present in ROM ( absolute address) can be made
executed – may be OS itself or A Bootstrap loader, which
in turn loads OS and prepares it for execution.
• 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
617
Systems Programming - V 3.0
Begin
X=0x80 (the address of the next memory location to be
loaded
Loop
AGETC (and convert it from the ASCII character code
to the value of the hexadecimal digit)
save the value in the high-order 4 bits of S
AGETC
combine the value to form one byte A (A+S)
store the value (in A) to the address in register X
XX+1
End
618
Systems Programming - V 3.0
Subroutine GETC

GETC Aread one character


if A=0x04 then jump to 0x80
if A<48 then GETC
A  A-48 (0x30)
if A<10 then return
A  A-7
return

619
Systems Programming - V 3.0
SIC uses the above mentioned second method.

• The bootstrap begins at address 0 in the memory of the


machine.
• It loads the operating system at address 80.
• Each byte of object code to be loaded is represented on
device
• F1 as two hexadecimal digits just as it is in a Text record of
a SIC object program.
• The object code from device F1 is always loaded into
consecutive bytes of memory, starting at address 80.

620
Systems Programming - V 3.0
SIC uses the above mentioned second method.

• The main loop of the bootstrap keeps the address of


the next memory location to be loaded in register X.

• After all of the object code from device F1 has been


loaded, the bootstrap jumps to address 80, which
begins the execution of the program that was loaded.

621
Systems Programming - V 3.0
Relocating Loader

622
Systems Programming - V 3.0
Program Written in SIC/XE

623
Systems Programming - V 3.0
Base-relative

BITS Pilani, Pilani Campus


Base-relative

This program is written in SIC/XE instructions. Program counter-relative


and base-relative addressing are extensively used to avoid the need for
many address modification records.

BITS Pilani, Pilani Campus


Machine-dependent Loader Features

The features of loader that depends on


machine architecture are called machine
dependent loader features. It includes:

1. Program Relocation
2. Program Linking

626
Systems Programming - V 3.0
Program Relocation (Relocating Loader)

• The absolute loader has several disadvantages. One of the most


obvious is the need for the programmer to specify the actual
address at which it will be loaded into memory.

• On a simple computer with a small memory the actual address at


which the program will be loaded can be specified easily.

• On a larger and more advanced machine, we often like to run


several independent programs together, sharing memory between
them. We do not know in advance where a program will be
loaded. Hence we write relocatable programs instead of absolute
ones.

627
Systems Programming - V 3.0
Program Relocation (Relocating Loader)

• Writing absolute programs also makes it difficult to use


subroutine libraries efficiently. This could not be done
effectively if all of the subroutines had preassigned absolute
addresses.

• The need for program relocation is an indirect consequence of


the change to larger and more powerful computers. The way
relocation is implemented in a loader is also dependent upon
machine characteristics.

628
Systems Programming - V 3.0
Relocation

• Execution of the object program using any part of the


available and sufficient memory
• The object program is loaded into memory wherever there is
room for it
• The actual starting address of the object program is not
known until load time

• Loaders that has the capability to perform relocation are


called relocating loaders or relative loaders.

629
Systems Programming - V 3.0
Relocating Loaders

There are two methods for specifying relocation in object program

1. Modification Record
2. Relocation Bit

630
Systems Programming - V 3.0
Modification Record

A Modification record is used to describe each part of


the object code that must be changed when the program
is relocated.

631
Systems Programming - V 3.0
The Modification has the following format:

Each Modification record specifies the starting address and


length of the field whose value is to be altered. It then describes
the modification to be performed.
632
Systems Programming - V 3.0
HCOPY 000000 001077
T000000 1D17202D69202D48101036…4B105D3F2FEC032010
T00001D130F20160100030F200D4B10105D3E2003454F46
T001035 1DB410B400B44075101000…33200857C003B850
T0010531D3B2FEA1340004F0000F1..53C003DF2008B850
T00070073B2FEF4F000005
M00000705+COPY
M00001405+COPY
M00002705+COPY
E000000
Object program with relocation by Modification records

633
Systems Programming - V 3.0
Relocation Bit

• To overcome the disadvantage of modification record,


relocation bit is used.
• The Text records are the same as before except that
there is a relocation bit associated with each word of
object code.
• Since all SIC instructions occupy one word, this means
that there is one relocation bit for each possible
instruction.
• The relocation bits are gathered together into a bit
mask following the length indicator in each Text record.
634
Systems Programming - V 3.0
Relocation Bit

•For simple machines


•Relocation bit
- 0: no modification is necessary
- 1: modification is needed

635
Systems Programming - V 3.0
Relocation Bit

• Twelve-bit mask is used in each Text record


– col:10-12 – relocation bits
- since each text record contains less than 12 words
- unused words are set to 0
- any value that is to be modified during relocation must
coincide with one of these 3-byte segments

636
Systems Programming - V 3.0
Relocation Bit

HCOPY 000000 00107A


T0000001EFFC140033481039000036280030300015…3C0003  …
T00001E15E000C00364810610800334C0000…000003000000
T0010391EFFC040030000030…30103FD8105D280030...
T0010570A 8001000364C0000F1001000
T00106119FE0040030E01079…508039DC10792C0036...
E000000

Object program with relocation by bit mask


- FFC - all ten words are to be modified
- E00 - first three records are to be modified
637
Systems Programming - V 3.0
Direct Linking Loaders

• A Direct linking loader is a general relocating loader and is the most


popular loading scheme presently used.
• This scheme has an advantage that it allows the programmer to use
multiple procedure and multiple data segments.
• In addition, the programmer is free to reference data or instructions
that are contained in other segments.
• The direct linking loaders provide flexible intersegment referencing
and accessing ability.

638
Systems Programming - V 3.0
Direct Linking Loaders

• The assembler should give the following information to the loader:


1) The length of the object code segment
2) The list of all the symbols which are not defined in the current segment but
can be used in the current segment.
3) The list of all the symbols which are defined in the current segment but can
be referred by the other segments.
Contact Session - 15

Course Name :
Systems Programming

Dr. Parag Anil Tamhankar


Agenda

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

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

642
Systems Programming - V 3.0
Process related system calls – exit

643
Systems Programming - V 3.0
Ex-1: exit
#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;
int status;
else
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);
}
} 644
Systems Programming - V 3.0
Ex-1: exit
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

645
Systems Programming - V 3.0
Ex-1: exit
• 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.

Systems Programming 646


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.

Systems Programming 647


Process related system calls – signal

648
Systems Programming - V 3.0
Process related system calls – signal

649
Systems Programming - V 3.0
Process related system calls – signal

650
Systems Programming - V 3.0
Process related system calls – signal

651
Systems Programming - V 3.0
Process related system calls – signal

652
Systems Programming - V 3.0
Process related system calls – signal

653
Systems Programming - V 3.0
Process related system calls – signal

654
Systems Programming - V 3.0
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

655
Systems Programming - V 3.0
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.

Systems Programming 656


Process related system calls – kill

657
Systems Programming - V 3.0
Process related system calls – kill & raise

658
Systems Programming - V 3.0
Ex-3: kill
• The program demonstrates
the use of the fork system
call to create a child process.
• The child process performs a
simple task (sleeping for 10
seconds) and then exits.
• The parent process sends an
interrupt signal to the child
process using kill.
• The parent process waits for
the child to terminate using
wait and prints its exit status.

Systems Programming 659


Ex-4: raise
• The program demonstrates the use of the
raise system call to send a signal to the
current process.
• The signal function is used to register a
signal handler to handle the received
signal.
• The pause function is used to suspend the
process's execution until a signal is
received.
• The while loop with the c variable is used
to count the number of times the signal
handler is called.
• The loop terminates after three iterations,
even though the process continues to
receive SIGINT signals.

Systems Programming 660


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.

661
Systems Programming - V 3.0
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;
}

662
Systems Programming - V 3.0
System calls for IPC: pipe()
• The header file of pipe() unistd.h
• The program creates a pipe using the pipe function.
• A pipe is a special type of file that allows communication
between processes. It consists of two file descriptors: one for
reading and one for writing.
• The pipe function returns an array of two integers, pipefds[0] and
pipefds[1], which represent the read and write file descriptors,
respectively.
• The program then prints the values of these file descriptors to
the console.

663
Systems Programming - V 3.0
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
and new file descriptors may be used interchangeably.
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.

664
Systems Programming - V 3.0
System calls for IPC: dup()
#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);
}
665
Systems Programming - V 3.0
System calls for IPC: dup()
• The program demonstrates the use of the open and dup
functions to open a file and create a duplicate file descriptor.
• The open function creates a new file descriptor and returns
its value.
• The dup function creates a duplicate file descriptor for an
existing open file.
• The duplicate file descriptor shares the same file descriptor
table entry as the original file descriptor, which means they
refer to the same open file.

Systems Programming 666


Memory Management System Calls

• There are two main system calls to manipulate memory, namely


brk and sbrk.
• Both brk and sbrk change the amount of space allocated for the
calling process's data segment.
• They change the process's break value and the operating system
adjusts their allocated amount of space accordingly, which may
be an increase or a decrease or no change.
• 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 667
Memory Management System Calls

• *brk(void addr):
• Used to adjust the program's data segment (heap) size.
• addr: A pointer to the desired new address for the break. If
NULL, the current break address is returned.
• On success, the new break address is returned. On failure, -1 is
returned and errno is set to indicate the error.
• sbrk(intptr_t increment):
• Similar to brk, but adjusts the break by a specified increment.
• increment: The amount by which to adjust the break (positive
for increasing, negative for decreasing).
• On success, the new break address is returned. On failure, -1 is
returned and errno is set to indicate the error.

Systems Programming 668


Ex-5: brk & sbrk

o/p

Systems Programming 669


Ex-5: brk & sbrk
• Allocate memory using sbrk: The sbrk function is used to increase the
program's data segment (heap) by size bytes. The returned address addr
points to the start of the newly allocated memory.

• Release memory using brk: The brk function is used to set the program's break
address back to its original value, effectively releasing the previously allocated
memory.

• This program illustrates how brk and sbrk can be used to dynamically adjust
the program's memory usage. However, it's important to note that brk and
sbrk are generally not recommended for modern applications due to their
limitations and potential fragmentation issues. mmap is often preferred for
more flexible and efficient memory management.

Systems Programming 670


Memory Management System Calls: mmap

• void *mmap(void *addr, size_t length, int prot, int flags, int fildes,
off_t off):
• The mmap system call in Linux provides a powerful and flexible
way to map files or devices into the address space of a process.
• This allows for efficient memory-mapped I/O operations and
shared memory communication between processes.
• Parameters are discussed below.

Systems Programming 671


Memory Management System Calls: mmap

Parameters
• addr: starting address of the mapped region. If NULL, the kernel chooses an address.
• length: The size of the mapped region in bytes.
• prot: Protection flags for the mapped region:
• PROT_READ: Read-only access.
• PROT_WRITE: Read-write access.
• PROT_EXEC: Execute access.
• flags: Flags to control the mapping:
• MAP_PRIVATE: Creates a private copy of the mapped region.
• MAP_SHARED: Creates a shared mapping, where changes made by one process
are visible to other processes.
• MAP_ANONYMOUS: Creates an anonymous mapping (not associated with a file).
• MAP_FIXED: Attempts to fix the mapping at the specified address.
• fildes: The file descriptor of the file or device to map.
• off: The offset within the file or device from which to start mapping.

Systems Programming 672


Ex-6: mmap

o/p

Systems Programming 673


Ex-6: mmap

• The open function creates a new file named "my_file.txt" in read-


write mode.
• The mmap function maps the file into memory.
• The MAP_SHARED flag indicates that changes made to the
memory will be visible to other processes that also have the file
mapped.
• The memcpy function copies the string "Hello, world!" into the
mapped memory.
• The munmap function unmaps the memory region from the
process's address space.

Systems Programming 674

You might also like