You are on page 1of 55

System Programming

Lecture 1
Operating System
• The operating system is the most important program that runs
on a computer.
• Operating systems provide a software platform on top of which
other programs, called application programs, can run.
• The operating system (OS) is the first thing loaded onto the
computer -- without the operating system, a computer is useless.
• Operating systems can now be found on many of the devices
we use every day, from cell phones to wireless access points.
• We can call it as Resource Manager for Computer hardware.
• Allocation of resources to Users.
• Easy interaction with Computer Resources like Input output
device, File System etc.
Purpose of Operating System
• The purpose of an operating system is to organize and control
hardware and software so that the device it lives in behaves in a
flexible but predictable way.
• Three main purposes of an operating system are:
–To provide an environment for a computer user to execute programs on
computer hardware in a convenient and efficient manner.
– To allocate the separate resources of the computer as needed to solve
the problem given. The allocation process should be as fair and efficient
as possible.
– As a control program it serves two major functions: (1) supervision of
the execution of user programs to prevent errors and improper use of
the computer, and (2) management of the operation and control of I/O
devices.
Function of Operating Systems
• Operating System Services
– Controlling the execution of processes by allowing their
creation, termination or suspension.
– Scheduling processes fairly for execution on the CPU.
Processes share the CPU in a time-shared manner.
– Allocating main memory for an executing process.
– Allocating secondary memory for efficient storage and
retrieval of user data.
– Allowing processes controlled access to peripheral devices
as terminals, tape drives, disk drives, and network devices.
Definition
What is an Operating System?
• An operating system (OS) is a resource
manager. It takes the form of a set of software
routines that allow users and application
programs to access system resources (e.g. the
CPU, memory, disks, modems, printers
network cards etc.) in a safe, efficient and
abstract way.
Classification of Operating System
Operating systems can be classified as follows:
• Multi-User Operating System:  Multi-User OS allows two or more users to
run programs at the same time.
• Multi-tasking Operating Systems: Multi-tasking OS allows more than one
program to run concurrently.
• Distributed Operating Systems: In a distributed system, software and data
may be distributed around the system, programs and files maybe stored
on different storage devices which are located in different geographical
locations and maybe accessed from different computer terminals.
• Real Time Operating System: A real-time operating system is a
multitasking operating system which executes only real-time applications.
The main objective of real-time OS is their quick to events.
• Embedded Operating System: Embedded operating systems are designed
to be used in embedded computer systems. They operate on small
machines like PDAs with less autonomy. They are able to operate with a
limited number of resources.
What is Unix?
• The UNIX operating system is a set of programs that
act as a link between the computer and the user.
• Unix is a powerful, multi-user environment that has
been implemented on a variety of platforms.
• The UNIX operating system comprises three parts:
The kernel, the standard utility programs, and the
system configuration files.
• The UNIX system is full duplex: the character you
type on the keyboard are sent to the system, which
sends them back to the terminal to be printed on
screen.
Unix System
• The computer programs that allocate the system resources
and coordinate all the details of the computer's internals is
called the operating system or 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.
• There are various Unix variants available in the market. Solaris
Unix, AIX, HP Unix and BSD are few examples. Linux is also a
flavor of Unix which is freely available.
• UNIX is a multiuser system and multitasking operating
system.
Unix History
• Unix was created in 1969 by Dennis Ritchie (also the
creator of C programming language) & Ken Thompson.
• Slowly, an open operating system standardization was
thought of that led to the birth of POSIX (Portable
Operating System Interface). The first POSIX standard
was published in 1988.
• In 1991,LinusTorvalds developed the 1st version of this
new OS ( LINUX ) that was powered by Intel's 80386
microprocessor (a 32 bit microprocessor launched in
1985)
Unix and Linux
• Linux is a “Unix-like" operating system.  Unix (POSIX)
actually is a standard by which a Unix-like operating
system should conform to.  
• Linux mostly conforms to the POSIX standard.
• Almost all companies run Linux, BSD, Solaris or some
other Unix-like variant. 
• You can configure them using different mechanisms
but the basic commands like ls, cd, chmod, chown,
find, etc work  pretty much the same on all Unix
flavour.
Linux Features
• Open Source Free Operating System
• Multiuser OS
• Multitasking
• Users and Groups of defined
• User and Group mapped to IDs
• Special User Root has unrestricted privileges
Pros And Cons of Linux
• Pros
– Linux is free license.
– Linux is portable to any hardware platform.
– Linux is secure and versatile.
– Linux OS and most Linux application have very short
debug times.
• Cons
– There are far too many different distributions.
– Linux is not very user friendly and confusing for beginners.
– Is an open source product trustworthy.
Unix Architecture
All Components of typical OS
• Kernels
– Core of OS. Manages and controls hardware and File Systems.
– First part of OS to load into memory during booting.
– It provides basic services for all other parts of the OS like Application
Programs, Utilities through system calls
– Programs such as the shell and editors (ed and vi) interact with kernel by
invoking a well defined set of system calls.
• Shells
– Outermost part of OS and which interacts with User Commands.
– A program that provides an interface to access the services that are provided
by the OS.
– Examples are:
• COMMAND.COM of MS-DOS.
• Bourne shell of UNIX
• Bash (Unix shell) and Z shell
• Application Programs
– Useful programs like editors, office, browser etc.
Block Diagram
Hardware

Kernel

Shell

Application
programs/Users
Shell Scripting
• The shell works as an interpreter. Shell scripting is the process
of writing a "script" file for the shell.
• A script file typically contains the code that shall be interpreted
by the shell. The code should be written in the language that the
shell can understand.
For example:
– Bash is a shell.
– Bash understands a scripting language that can be called "bash
scripting language"
– You can write a bash script, which is a file that contains code written in
that bash scripting language. This process is called "shell scripting".
– You can execute the bash script by running bash and telling it to
interpret the script.
System Bootup
• If you have a computer which has UNIX
operating system installed on it, then you
simply need to turn on its power to make it
live.
• As soon as you turn on the power, system
starts booting up and finally it prompts you to
log into the system, which is an activity to log
into the system and use it for your day to day
activities.
Login Unix
• The login program is used to establish a new session with
the system.
• It is normally invoked automatically by responding to the
"login:" prompt on the user's terminal.
• 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 instructed.
• Type your password at the password prompt, then press
ENTER. Your password is also case-sensitive.
• If you provided correct userid and password then you
would be allowed to enter into the system.
Example
• Read the information and messages that come up on the
screen something as below.
login : amrood
amrood's password:
Last login: Sun Jun 14 09:32:32 2009 from 62.61.164.73
$
• You would be provided with a command prompt ( sometime
called $ prompt ) where you would type your all the
commands.
• With correct credentials you would be allowed to enter the
system
• Logout = Ctrl-d ( proper way). Or turn off the terminal
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.
• Here are the steps to change your password −
– To Change the password type “ passwd” at terminal
$ passwd press enter
Old password … “type your old password”. Press en
New password..” type your new password”. Press enter
Retype password..’ retype your new password” press enter
Password is not visible and case sensitive
Example
$ passwd
Changing password for amrood
(current) Unix password:******
New UNIX password:*******
Retype new UNIX password:*******
passwd: all authentication tokens updated successfully
$
• Note − I have put stars (*) just to show you the location
where you would need to enter the current and new
passwords otherwise at your system, it would not show
you any character when you would type.
Mistake in Typing
• If you make a typing mistake and seen before pressing
RETURN.
• There are 2 ways to recover:
– Erase characters one at a time (by default @sign)
– Or kill the whole line and re-type it. (The sharp character #)
• Example:
– $ ddate@
Date
Sun jul 28 12:23:39 EDT 1998
$
– $dd#atte##e
Sun jul 28 12:34:39 EDT 1998
Unix Files and Directories
• All data is organized into files
• Files are organized into directories
• These directories are organized into tree like
structure called the file system.
• You can use ls command to list out all the files
or directories available in a directory.
Example
• Following is the example of using ls command with -l option.
$ ls -l
total 7
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
drwxr-xr-x 8 root root 4096 Nov 25 2007 usr
-rw-rw-r-- 1 amrood amrood 20480 Nov 25 2007 webthumb.tar

• Here enteries starting with d..... represent directories. For example


uml, univ and urlspedia are directories and rest of the entries are files.
Unix Commands – In General
• A Unix command start with command name
like below.
$ command
• It can have multiple option detailing for
particular need.
• Options start with “-”.
$ command –option
• Ex: $ ls –l
Unix Commands - ls
• It shows all the files and directories available in a
present working directory.
• $ ls
Output as below if directory has 2 files abc and tmp.
abc
tmp
• ls like most commands has some options like
-l long listing: output like below.
-rw-r--r– 1 you 19 Sep 26 16:25 abc
-rw-r--r– 1 you 19 Sep 19 16:25 tmp
ls command with different options
ls -a option will list hidden files.
ls-dl detailed information about the directory and not the files
present in current directory.
ls-t option will list the sort by time modified instead of name.
ls-r option will provide reverse order.
ls- ul option will display detail info with usage time of the files
in current directory.
ls- ct sort the files according to time inode of the files
changed.
ls –x will print files horizontally.
ls -1 will print 1 option per row.
Unix Commands - date
• $ date
• Print or set the system date and time
• For Ex:
$ date
Mon Sep 26 12:20:33 EDT 1983
• To set we use option -s
Unix Commands - Cal
• Cal command is used to display calendar
• Synopsis: $ cal [ [month] year]
• Examples
$ cal : will display current month’s calendar
$ cal 2 2017 : will display feb 2017 calendar
$ cal 2015: will display complete 2015 yr calendar
$ cal -1 : will display current month calendar
$ cal -3 : will display Pervious, current and next months calendar
$ cal –h : you can turn off the highlighting of current date
$ncal : will display the weeks name horizontally
Unix Commands - who
• There are two commands available to get you this information, based on
how much you'd like to learn about the other users: users and who.
• This command tells you everyone who is currently logged in
• For Ex:
$ users
amrood bablu qadir
$ who
Abc tty0 Sep 26 11:17
Sam tty1 Sep 26 11:27
Tom tty4 Sep 26 12:17

First col: User Name


Second: System name of connection being used
Last tells the user logged on time
Unix Commands – who am i
• Display the name of the current user id
• For example:
– $ who am i
you tty01 Sep 26 12:20
Logging Out
• When you finish your session, you need to log
out of the system to ensure that nobody else
accesses your files while masquerading as you.
• To log out
– Just type logout command at command prompt,
and the system will clean up everything and break
the connection
Unix Commands - mail
• Mail is an intelligent mail processing system which sends and receives mail. It allows to read
mail. If users has a value, then it allows you to send mail to those users.
$ mail [options] [users]
•If you give $ mail, it will checks your mail in post office and give your mail one by one (recent
one will be first).
• Some other options are as follows:
–After examining a message you can delete message by typing “d”
–You can reply message by typing “ r”
–The message can be undeleted “u” by giving its number, or the
–mail session can be aborted by giving the exit x command.
• Compose mail
• Use following format:
mail -s <subject> <mailaddress>
• For example write mail to boss@yahoo.com:
$ mail - "Hello" boss@yahoo.com
You are then expected to type in your message, followed by an ‘ctrl-d’
Unix Commands - write
• Write allows you to communicate with other users, by
copying lines from your terminal to theirs.
• When you run the write command, the user you are writing
to gets a message of the form:
– Message from yourname@yourhost on yourtty at hh:mm ...
• Any further lines you enter will be copied to the specified
user's terminal. If the other user wants to reply, they must
run write as well.
• When you are done, type an end-of-file or interrupt
character. The other user will see the message EOF
indicating that the conversation is over.
Unix Commands - man
• Format and displays online manual pages.
• For examples:
• To read about who command type
$man who
print all information about who command
• And, of course
$man man
Tells about the man command
Unix Commands - ed
• Line oriented text editor.
• Used to create, display, modify and otherwise manipulate text files.
• When first invoked ed , it is in command mode.
• When input a ( append), i (insert) or c(change) is given, ed enters input mode.
• Input mode is terminated by entering a single period(.) on a line.
• The editor command store the information that you typed
• “w filename”: stores it in file.
• For example:
$ ed
a
Now type text
.
w tempfile
39 (Nos of characters)
q ( quits the ed)
Unix Commands - wc
• Prints the number of lines, words and bytes
• For ex: $wc tmpfile
8 46 263 poem
8 is line count, 46 is words count and 263 character.
– Some option in wc command
wc-l : print the line counts.
wc -c : print the byte counts.
wc -m : print the character counts.
wc –w : print the word counts.
Unix Commands - grep
• grep searches the named input files for lines
containing a match to given pattern.
• For ex:
$grep ‘ram’ tempFiles
hello ram how are you
ram u got good marks.
It will give all lines matching that pattern.
grep-v : this option will look for all those lines
which don’t match the pattern.
Unix Commands - sort
• Which sorts its input into alphabetical order line by
line
• But the default sorting order puts blanks first ,
then upper case letter, then lower case and so on.
• Some of the options with sort command
sort - r reverse normal order
sort - n numerical order
sort - nr reverse numerical order
sort- f fold upper and lower case together
Unix Commands - tail
• Print the last 10 line of each file to standard out put.
• Tail has an option to specify the number of lines.
• For ex:
$ tail filename
• Prints last 10 lines of the file.
$ tail -1 filename
• Prints last line of the file
$ tail +3 filename
• Starts printing from the 3rd lines of the file.
Unix Command- head
• Head prints the first N number of data of the given
input. By default, it prints first 10 lines of each given file.
• To view the first N number of lines, pass the file name as
an argument with -n option as shown below.
• For ex:
$ head -n filename
• Prints first n lines of the file.
$ head filename
• Prints first 10 lines of the file.
$ head +3 filename
• Prints last 3 lines of the file.
Unix file Commands – cat
• cat : Concatenate files and print on standard output
$ cat file1
This is simple file.
$ cat file2
This is second file.
$ cat file1 file2
This is simple file.
This is second file.
• If more than 1 file name is mentioned, it will display the concatenated
output in the file in a order mentioned
• With cat command, when file is small in size there is no problem, but if
size of file is more than a page than it will flows off the screen.
• You need to press quickly ctrl-s to view page wise.
Unix file Commands – pr
• Prints the contents of all the files named in the list but in a form
suitable for line printer.
• Pr prints the contents of all the files named in a list, but in a form
suitable for line printers; every page is 66 lines(11 inches) long, with
the date and time that the file was changed, the page number, and
the filename at the top of each page, and extra lines to skip over
the fold in the paper.
– $pr file1,file2
Aug 28 16:29 1998 file1 page 1
This is simple file.
(60 more blank lines)
Aug 29 18:32:45 1998 file 2 page 1
This is second file.
(60 more blank lines)
Pr Command contd..
• $pr -3 filename : prints file in 3- column format.
Dec 3 18:40 2015 file1 page 1
Line1 here line57 here line115 here
Line2 here line58 here line 116 here
……. ….. ……
• $pr –m –t file1 file2 file3 : the –m option reads all files on the command
line simultaneously and print each in its own column, like this
Dec 3 18:40 2015 file1 page 1
The lines the lines the lines
of file1 of file2 of file3
here here here
– It option take away the heading and margins at the top and bottom of
each page
Unix Commands - pwd
• pwd is present working directory.
• It prints the name of the directory you are currently in.
• Gives full pathname of the current directory.
• For ex:
$pwd
/usr/you
(‘/’ is root directory and /usr is sub directory of root
which generally contains all directories of normal user of
the system.
Unix Commands - which
• It locate the executable file associated with a given
command.
• Which returns the pathnames of the files which would
be executed in the current environment.
• It does this by searching the paths in the PATH
environment variable for executable files matching the
names of the arguments.
• Syntax:
– which filename : prints all matching pathnames of each
– which –a filename: prints only matching filename
Absolute Path
• What is pathname?
– A path is a unique location to a file or a folder in a file system of anOperating System.
It represent the full name of the path from the root through the tree of directories to
a particular file.
• It ia a universal rule in the UNIX system that wherever you can use an
ordinary filename, you can use a pathname.
• What is an absolute path?
– An absolute path is defined as the specifying the location of a file or directory from
the root directory(/). In other words we can say absolute path is a complete path from
start of actual file system from / directory.
Some examples of absolute path:
/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf  
If you see all these paths started from / directory which is a root directory for
every Linux/Unix machines.
Relative Path
• What is the relative path?
Relative path is defined as path related to the present working directory(pwd). Suppose
I am located in /var/log and I want to change directory to /var/log/kernel. I can use
relative path concept to change directory to kernel
• Changing directory to /var/log/kernel by using relative path concept.
pwd
/var/log
cd kernel
• Note: If you observe there is no / before kernel which indicates it's a relative directory
to present working directory.
• Changing directory to /var/log/kernel using absolute path concept.
cd /var/log/kernel

• Note: We can use an absolute path from any location where as if you want to use
relative path we should be present in a directory where we are going to specify relative
to that present working directory.
Processing Environment
• On a Unix system everything is file, if something is not a file ,
it is a process.
• A program is an executable file, and a process is an instance
of a running program.
• Many processes can execute simultaneously on UNIX system
with no logical limit to their number, and many instances of a
program can exist simultaneously in the system.
• As soon as you typed command and press enter, the
program gets ready in the memory and executed by kernel.
• Some programs create single process, such as ‘ls command’
while some creates more than one, such as open office.
Process ID
• Every process executing in the UNIX system has an
execution environment that includes a current
directory.
• In Unix based operating system, every process has a
process ID called PID.
• At any one time, no two processes with the same PID
exist in the system because it is the PID that UNIX uses
to track each process.
• PID must be unique integer value , no negative
interger
Process
• When you start a process (run a command), there are two ways you can run it:
– Foreground Processes
– Background Processes
Foreground Processes
• By default, every process that you start runs in the foreground. It gets its input
from the keyboard and sends its output to the screen.
• For Example
– You can see this happen with the ls command. If I want to list all the files in my current
directory, I can use the following command:
$ls ch*.doc
– This would display all the files whose name start with ch and ends with .doc:
– The process runs in the foreground, the output is directed to my screen, and if the ls
command wants any input (which it does not), it waits for it from the keyboard.
• While a program is running in foreground and taking much time, we cannot run
any other commands (start any other processes) because prompt would not be
available until program finishes its processing and comes out.
Background Processes:
• A background process runs without being connected to
your keyboard. If the background process requires any
keyboard input, it waits.
• The advantage of running a process in the background is
that you can run other commands; you do not have to
wait until it completes to start another!
• The simplest way to start a background process is to add
an ampersand ( &) at the end of the command.
• For Example:
$ls ch*.doc &
– This would also display all the files whose name start with ch
and ends with .doc:
Ps Command
• The ps (process status) command produces a list of the currently running processes on your
computer.

$ ps
• ps
• The output will show rows of data containing the following information:
• PID, TTY, Time, Command
• The PID is the process ID which identifies the running process. The TTY is the terminal type. 
TIME is the amount of CPU (central processing unit) time in minutes and seconds that the
process has been running. CMD is the name of the command that launched the process.
• One of the most commonly used flags for ps is the -f ( f for full) option.
$ ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 -ksh
• Here is the description of all the fileds displayed by ps -f command:

Column Description
UID User ID that this process belongs to (the person
running it).
PID Process ID.
PPID Parent process ID (the ID of the process that started
it).
C CPU utilization of process.
STIME Process start time.
TTY Terminal type associated with the process
TIME CPU time taken by the process.
CMD The command that started this process.
Stopping Processes
• Ending a process can be done in several different ways. Often,
from a console-based command, sending a CTRL + C keystroke
(the default interrupt character) will exit the command. This
works when process is running in foreground mode.
• If a process is running in background mode then first you would
need to get its Job ID using ps command and after that you can
use kill command to kill the process as follows:
$ps -f
UID PID PPID C STIME TTY TIME CMD
amrood 6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood 6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood 3662 3657 0 08:10:53 pts/6 0:00 –ksh
$ kill 6738
Here kill command would terminate first_one process

You might also like