Introduction To OS-Day2
Introduction To OS-Day2
05/03/2021 Introduction to OS 3
What is Linux?
• Unix is a multitasking, multi-user computer operating system originally
developed in 1969 by a group of AT&T employees at Bell Labs with the
Goals:
• Simplicity
• Recyclable Code
• Written in C as opposed to assembly
• Linux is a Unix clone written from scratch by Linus Torvalds in 1991 with
assistance from a loosely knit team of hackers across the Net.
• Linux and Unix strive to be POSIX compliant.
• 64% of the world’s servers run some variant of Unix or Linux.
• The Android phone and the Kindle run Linux.
05/03/2021 Introduction to OS 4
What is Linux?
• Linux is not an operating system
• Linux is a Kernel
• Kernel is the core of the OS
• Responsible to convert user commands
to machine level language commands
• A kernel is a program that allocates and
controls hardware resources in a system
• Linux distributions use the Linux Kernel
a set of small programs
together with the GNU operating system Linux core written by Richard
• Kernel + GNU Utilities = Linux “Kernel” written Stallman and others. They
by Linus Torvalds are the GNU utilities.
and others
The Linux kernel is currently maintained by Linus Torvalds and a few hundred other
developers
05/03/2021 Introduction to OS 5
Types of Kernel
• Classified in to two categories:
• Monolithic Kernel
• Micro Kernel
05/03/2021 Introduction to OS 6
Monolithic vs Micro Kernel
Basis for Micro Kernel Monolithic Kernel
Comparison
Basic Separate Address space for user services and kernel Same address space
services
05/03/2021 Introduction to OS 7
Kernel vs Operating System?
Key Operating System Kernel
Type Operating system is a system software Kernel is a part of operating system
Work OS acts as an interface between user and Kernel acts as an interface between applications and
hardware hardware
Main Tasks Ease of doing system operations, security Memory management, space management, process
etc., management and task management.
Basis A computer need operating system to run An operating system needs kernel to run.
Types Operating systems types are multiuser, Kernel types are monolithic kernel and micro kernel
multitasking, multiprocessor, realtime,
distributed etc.,
Boot Operating system is the first program to load Kernel is the first program to load when operating
when computer boots up. system loads.
05/03/2021 Introduction to OS 8
Linux Distributions
05/03/2021 Introduction to OS 9
Advantages of Linux
• As a server platform
• Only very few operating system can match Linux in:
• Performance
• Price
• Stability
• Largest server platform in the world
• For Developers
• Resources:
• Linux has a tremendous number of tools available for developers.
• They are all free
• For the desktop
• It’s fun
• Community
• Very active Linux community and helpful
05/03/2021 Introduction to OS 10
• Makes support very rapid
Dis-Advantages of Linux
• Disadvantages of Open Source Operating System mentioned below
• Difficulty of use
• Compatibility Issues
• Liabilities and warranties
• Hidden costs
05/03/2021 Introduction to OS 11
Quiz
• Which of the following introduced the flavour of UNIX named
solaris?
• HP
• IBM
• Digital Equipment Corp
• Sun Microsystems
05/03/2021 Introduction to OS 12
Quiz
• Which animal is used as the logo for Linux?
• Cat
• Elephant
• Penguin
• Chameleon
05/03/2021 Introduction to OS 13
Quiz
• Which of the following is not a variant of UNIX?
• Solaris
• AIX
• IRIX
• AS400
05/03/2021 Introduction to OS 14
Quiz
• GRUB Stands for _________?
• Grand Unified BootLoader
• Gentoo’s Regular Booter
• Great Unix Boot Loader
• GNU Released Unix Booter
05/03/2021 Introduction to OS 15
Quiz
• Ubuntu is based on _________?
• Fedora
• Slackware
• Debian
• None of these
05/03/2021 Introduction to OS 16
Quiz
• What is GNOME _________?
• A computer software system and network protocol that provides a basis
for graphical user interfaces and rich input device capability for
networked computers.
• A desktop environment and graphical user interface that runs on top of
a computer operating system.
• A Linux distribution
05/03/2021 Introduction to OS 17
Quiz
• Who invented Linux?
• Richard Stallman
• Linus Torvalds
• Steve Jobs
• Bill Gates
05/03/2021 Introduction to OS 18
Quiz
• Fedora Linux uses ______ packages?
• deb
• ebuild
• deb_src
• rpm
05/03/2021 Introduction to OS 19
Quiz
• What is the core of the Linux OS?
• Kernel
• GNU
05/03/2021 Introduction to OS 20
Quiz
• What is LILO?
• A boot loader that can boot Linux
• Stands for “Lannister-In-Lannister-Out”
• Used by Slackware
• Stands for “Light Loader”
• Type of Linux Distro
05/03/2021 Introduction to OS 21
Essential commands in Linux?
05/03/2021 Introduction to OS 23
Sed Examples
• Assignment: Test_file.txt (test, , test1, , test2, , test3)
• How to print only blank line of file?
• sed -n '/^$/p' Test_file.txt
• Write a command to print First and last line using Sed
command?
• sed -n ‘1p;$p’ Test_file.txt
• Write A Command To Print All Line Except First Line?
• sed –n ‘1!p’ Test_file.txt
• Delete all line except first line?
• sed ‘1!d’ Test_file.txt
05/03/2021 Introduction to OS 24
Sed Assignment
• Change every occurrence of the word
‘easy' with ‘hard‘
• Insert one blank line after each line
• Delete the specific line of the file
• Delete the last line of the file
• Delete the pattern matching line “Easy”
05/03/2021 Introduction to OS 25
AWK?
05/03/2021 Introduction to OS 27
AWK Assignment
• Print the hostname of the system from uname –a command
05/03/2021 Introduction to OS 28
Difference b/w AWK & SED
• Both awk and sed are command-line utilities that are used
to transform text.
AWK SED
It works with delimited fields on a per- It works with characters on a per-line
line basis. basis.
It is mainly used to search and print data It is primarily used to filter and modify
from text files. text files.
It has advanced programming constructs It has fairly limited programming
like for, while, do-while, and multi- constructs like pattern matching and
dimensional arrays simple conditionals
It is more robust than sed. It is less robust than awk.
Writing mathematical expressions in awk Writing mathematical expressions in sed
is easy is hard
05/03/2021 Introduction to OS 29
Quiz
• In awk, what does FS stands for?
• Input Field Seperator
• Output field separator
• Record separator
• Subscript separator
05/03/2021 Introduction to OS 30
Quiz
• What is the correct syntax for using sed?
• sed options file(s)
• sed options ‘action’
• sed options ‘address action’ file(s)
• sed ‘action’ file(s)
05/03/2021 Introduction to OS 31
Quiz
• Consider the following commands?
$ sed -n ‘1,2p’ emp.lst
$ sed -n ‘3,$!p’ emp.lst
• True
• False
05/03/2021 Introduction to OS 32
Quiz
• The command $ sed -n ‘$p’ emp.lst will display the last line?
• True
• False
05/03/2021 Introduction to OS 33
Quiz
• Which command on the command line provides the same output as this
executable awk script?
#! /usr/bin/awk –f
BEGIN
{
print “Understanding AWK”
}
05/03/2021 Introduction to OS 34
Quiz
• An Awk program can be run by:
• Including the program in the command that runs awk
• Running an executable awk script
• Putting it into a file and run with a command
• All of the Above
05/03/2021 Introduction to OS 35
Quiz
• Command to display the file ‘sample.txt’ one page at a time:
• man sample.txt > more
• cat sample.txt
• cat sample.txt | more
• None of the Above
05/03/2021 Introduction to OS 36
Quiz
• Which command is used to display the operating system name:
• OS
• Unix
• Kernel
• uname
05/03/2021 Introduction to OS 37
Quiz
• Find / -name ‘*’ will:
• List all files and directories recursively starting from /
• List a file named * in /
• List all files in / directory
• List all files and directories in / directory
05/03/2021 Introduction to OS 38
Quiz
• Expand AWK?
05/03/2021 Introduction to OS 39
What you’ll be studying
Operating System Overview
Evolution of Operating System
Components of Operating System
Operating System vs Application Software
Functionality and Services of Operating System
Types of Operating System
Introduction to Linux
File Management wrt Linux
Shell Programming in Linux
Process
Process Scheduling algorithms with examples
Threads
Concurrency Control
05/03/2021 Introduction to OS 40
File System
• The *Nix (Unix or Linux) file system is a hierarchical directory
structure
• The structure resembles an upside down tree
• Directories are collections of files and other directories. The
structure is recursive with many levels.
• Every directory has a parent except for the root directory.
• Many directories have children directories.
• Unlike Windows, with multiple drives and multiple file systems,
a *Nix system only has ONE file system.
05/03/2021 Introduction to OS 41
05/03/2021 Introduction to OS 42
File Systems
05/03/2021 Introduction to OS 43
Linux File Systems
• A standard Linux Distribution provides the choice of partitioning disk with the file
formats listed below:
• ext2
• ext3
• ext4
These are the progressive version of Extended File System(ext), which primarily
was developed for MINIX.
The first one created in 1992 for Linux.
05/03/2021 Introduction to OS 44
EXT2
• The second extended version (ext2) was an improved version
with better longevity and flexibility.
• EXT2 expanded the maximum file system size from 2GB to
32TB.
05/03/2021 Introduction to OS 45
EXT3
• In 2001 EXT3 was created to enable journaling within the
filesystem.
• The maximum file system size is 2TB – 32TB.
• Journaling: writing all filesystem changes to a temporary
location, or journal, before writing permanentely to the
filesystem.
• Allows better recovery.
05/03/2021 Introduction to OS 46
EXT4
• Not an entirely new filesystem, but rather a fork of ext3.
• Main improvements: Journal checksum and delayed allocation
of memory.
• This meant the system waits until right before it writes the file
permanentely to allocate memory.This allows better decision
making.
• EXT4 is backwards compatible with all other versions of EXT.
• The maximum file system size is 2TB – 1EB.
05/03/2021 Introduction to OS 47
Difference between ext versions
05/03/2021 Introduction to OS 48
File Management
05/03/2021 Introduction to OS 49
Agenda
• Attributes and Operations on File Management
• File Access Methods
• Directory Structure
05/03/2021 Introduction to OS 50
What is a File
• A large amount of information or data that lives a very long time!
• Often much larger than the memory of the computer
• Often much longer than any computation
• Sometimes longer than life of machine itself
05/03/2021 Introduction to OS 51
Attributes of Files
• Size: • Identifier:
• Name:
• Length in number of bytes; • The file is identified by a
• It is the only information which
is in human-readable form occasionally rounded up unique tag( number) within
• Size of the file file system.
• Type:
• It is needed for systems that • Protection:
support different types of files. • Owner, group, etc.
• May be encoded in the name • Authority to read, update,
(e.g., .cpp, .txt) extend, etc.
• Dates: • Controls and assigns the
• Creation, updated, last power of r, w, x
accessed, etc.
• (Usually) associated with • Locks:
container • For managing concurrent
• Better if associated with access
content
05/03/2021 Introduction to OS 52
Definition – File Metadata
• Information about a file
• Maintained by the file system
• Separate from file itself
• Some information visible to user/application
• Dates, permissions, type, name etc.,
• Some information primarily for OS
• Location on disk, locks, cached attributes
• Location is stored in metadata
• Location can change, even if file does not
• Location is not visible to user or program
05/03/2021 Introduction to OS 53
Example - Location
• Example 1:
mv ~lauer/project1.doc ~cs4513/public_html/d08
• Example 2:
• System moves file from disk block 10,000 to disk block 20,000
• System restores a file from backup
05/03/2021 Introduction to OS 54
File Types
05/03/2021 Introduction to OS 55
Operations on Files
• Open, Close
• Gain or relinquish access to a file
• OS returns a file handle – an internal data structure letting it cache internal information
needed for efficient file access
• Read, Write, Truncate
• Read: return a sequence of n bytes from file
• Write: replace n bytes in file, and/or append to end
• Truncate: throw away all but the first n bytes of file
• Seek, Tell
• Seek: reposition file pointer for subsequent reads and writes
• Tell: get current file pointer
• Create, Delete:
• Conjure up a new file; or blow away an existing one
05/03/2021 Introduction to OS 56
Methods for Accessing Files
• Sequential access
• Random access
05/03/2021 Introduction to OS 57
Sequential Access Method
• Read all bytes or records in order from the beginning
• Writing implicitly truncates
• Cannot jump around
• Could possibly rewind or back up
• Appropriate for certain media or systems
• Magnetic tape or punched cards
• Video tape (VHS, etc.)
• Unix-Linux-Windows pipes
• Network streams
05/03/2021 Introduction to OS 58
Random Access Method
• Bytes/records can be read in any order
• Writing can
• Replace existing bytes or records
• Append to end of file
• Cannot insert data between existing bytes!
• Seek operation moves current file pointer
• Maintained as part of “open” file information
• Discarded on close
• Typical of most modern information storage
• Data base systems
• Randomly accessible multi-media (CD, DVD, etc)
• …
05/03/2021 Introduction to OS 59
Keyed (or indexed) Access Methods
• Access items in file based on the contents of (part of) an item in
the file
• Provided in older commercial operating systems (IBM ISAM)
05/03/2021 Introduction to OS 60
Directory
• A special kind of file
• For users & applications to organize and find files
• User-friendly names
• Names that are meaningful
05/03/2021 Introduction to OS 62
Directory Structure
• Two-level
• Single “master” directory per system
• Each entry points to one single-level directory per user
• Uncommon in modern operating systems
• Advantages:
• We can give full path like /User-name/directory-name/.
• Different users can have same directory as well as file name.
• Searching of files become more easy due to path name and user-grouping.
• Disadvantages:
• A user is not allowed to share files with other users.
• Still it not very scalable, two files of the same type cannot be grouped together in
the same user.
05/03/2021 Introduction to OS 63
Directory Structure
• Hierarchical
• Any directory entry may point to
• Individual file
• Another directory
• Common in most modern operating systems
• Advantages:
• Very generalize, since full path name can be given.
• Very scalable, the probability of name collision is less.
• Searching becomes very easy, we can use both absolute path as well as relative.
• Disadvantages:
• Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
• We can not share files.
• It is inefficient, because accessing a file may go under multiple directories.
05/03/2021 Introduction to OS 64
Directory Considerations
• Efficiency – locating a file quickly
• Naming – convenient to users.
• Separate users can use same name for separate files.
• The same file can have different names for different users.
• Names need only be unique within a directory.
• Grouping – logical grouping of files by properties
• Eg., all Java programs, all games…
05/03/2021 Introduction to OS 65
Directory organization - Heirarchical
• Most systems support idea of current (working) directory
• Absolute names – fully qualified from root of file system
• /usr/local/bin/foo, ~/kernelSrc/config.h
• Relative names – specified with respect to working directory
• foo.c, bar/bar2.h
• A special name – the working directory itself
• “.”
05/03/2021 Introduction to OS 66
Path Name Translation
• Assume that I want to open “/home/lauer/foo.c”
fd = open(“/home/lauer/foo.c”, O_RDWR);
05/03/2021 Introduction to OS 67
Path Name Translation (cont…)
• File Systems spend a lot of time walking down directory paths
• File System attempts to cache prefix lookups to speed up common
searches –
• “~” for user’s home directory
• “.” for current working directory
• Once open, file system caches the metadata of the file
05/03/2021 Introduction to OS 68
Directory Operations
• Create:
• Make a new directory
• Add, Delete entry:
• Invoked by file create & destroy, directory create & destroy
• Find, List:
• Search or enumerate directory entries
• Rename:
• Change name of an entry without changing anything else about it
• Link, Unlink:
• Add or remove entry pointing to another entry elsewhere
• Introduces possibility of loops in directory graph
• Destroy:
• Removes directory; must be empty
05/03/2021 Introduction to OS 69
Links
• Symbolic (soft) links:
• An indirect pointer to a file
• You can create a symbolic link to a directory
• A symbolic link can point to a file on a different file system
• If the source file is deleted, the link exists but pointer is invalid
• Hard links:
• Hard link is a reference to the physical data on a file system.
• All named files are hard links
• More than one name can be associated with the same physical data.
• Hard links can only refer to data that exists on the same file system.
• You can not create hard link to a directory.
05/03/2021 Introduction to OS 70
Symbolic Links
• To create a symbolic link to the file “myfile”, use
• #ln –s myfile symlink
• #ln --symbolic destination_file symbolic_filename
05/03/2021 Introduction to OS 71
Properties of Symbolic Links
• The i-node number is different from the pointed to file
• The link counter of the new symbolic link file is “1”
• Symbolic link file does not affect the link counter of the pointed
to file.
• The type field of symbolic file contains the letter “l”
• The symbolic link file and the pointed to file have different status
information (e.g., file size, last modification time etc.,)
05/03/2021 Introduction to OS 72
Properties of Symbolic links
• The symbolic link to a directory has a file type of “l”
• The permissions on the link are set to “rwx” for all.
• Chmod on the link applies to the actual directory (or file), the
permissions on the link stay the same.
• Can point to a non-existent directory.
05/03/2021 Introduction to OS 73
Hard Links example
• Assume you used vi to create a new file, you create the first
hard link.
• To create the 2nd, 3rd and etc., hard links, use the command:
• #ln filename link-name
05/03/2021 Introduction to OS 74
Hard Links
• When a file has more than one link, you can remove any one link and still be
able to access the file through the remaining links.
• Hard links are a good way to backup files without having to use the copy
command!
• Hard links are not used very often in modern Unix practice
• Exception: Linked copies of large directory trees!
• (Usually) safe to regard last element of path as name of file
05/03/2021 Introduction to OS 75
Popular commands in Linux
• #1: cd • #11: more • #21: ln • #31: finger
• #2: man • #12: head • #22: zip • #32: whoami
• #3: ls • #13: tail • #23: unzip
• #33: alias
• #4: cp • #14: man • #24: tar
• #5: mv • #15: whereis • #25: cal
• #34: ps
• #6: mkdir • #16: find • #26: bc • #35: wc
• #7: rmdir • #17: grep • #27: date • #36: locate
• #8: touch • #18: file • #28: touch
• #9: rm • #19: diff • #29: echo
• #10: cat • #20: pwd • #30: w
05/03/2021 Introduction to OS 76
File Listing
• #ls
05/03/2021 Introduction to OS 77
Creating files
• #touch filename #vim filename
05/03/2021 Introduction to OS 78
Copying a file
• #cp source/filename destination/filename1
• Ex: Source File: test/filename
Destination File: test1/filename
#cp test/filename test1/filename
Moving a File
• #mv source/filename destination/filename
05/03/2021 Introduction to OS 79
Renaming a File
• #mv filename new_filename
Deleting a file
• #rm filename
05/03/2021 Introduction to OS 80
Commands associated with Directory
• #mkdir directory #mv directory new_directory
05/03/2021 Introduction to OS 81
Quiz
• Which filesystem type is used in Linux OS?
• NTFS
• FAT32
• EXT
• PFS
05/03/2021 Introduction to OS 82
Quiz
• Which is the default file system of Linux?
• etx
• ext2
• ext3
• mimix
05/03/2021 Introduction to OS 83
Quiz
• Which filesystem has journaling checksum capability?
• EXT
• EXT2
• EXT3
• EXT4
05/03/2021 Introduction to OS 84
Quiz
• Which command is used to display the Linux version?
• uname
• kernel
• Unix
• Linux
05/03/2021 Introduction to OS 85
Quiz
• Which command is used to remove files?
• rm
• dm
• erase
• delete
05/03/2021 Introduction to OS 86
Quiz
• mv command can be used for?
• Renaming a file
• Move the file to different directory
• Both 1 & 2
• None of these
05/03/2021 Introduction to OS 87
Quiz
• How do you list hidden files in Linux?
• ls -a
• ls -l
• ls –h
• ls -k
05/03/2021 Introduction to OS 88
Quiz
• How can you determine the total memory used by LINUX?
• free
• /proc/meminfo
• vmstat
• top
05/03/2021 Introduction to OS 89
Quiz
• How can you determine the total memory used by LINUX?
• free
• /proc/meminfo
• vmstat
• top
05/03/2021 Introduction to OS 90
File Permissions
05/03/2021 Introduction to OS 91
Why File Permissions?
• Multi-tasking system
• Multi-user system
• Permissions can be set to files and Directories.
05/03/2021 Introduction to OS 92
A Program runs…
•A program may be run by a user, when the system starts or by another
process.
•Before the program can execute the kernel inspects several things:
• Is the file containing the program accessible to the user or group of the process
that wants to run it?
• Does the file containing the program permit execution by that user or group (or
anybody)?
•In most cases, while executing, a program inherits the privileges of the
user/process who started it.
05/03/2021 Introduction to OS 93
A Program in detail
• When we type:
• ls -l /usr/bin/top
• We'll see:
• -rwxr-xr-x 1 root root 68524 2011-12-19 07:18
/usr/bin/top
05/03/2021 Introduction to OS 94
-r-xr-xr-x 1 root root 68524 2011-12-19 07:18 /usr/bin/top
05/03/2021 Introduction to OS 96
File Permissions
• You can set permissions using chmod command:
• There are two ways to set permissions when using the chmod command:
• Symbolic mode:
• testfile has permissions of -r--r--r--
• U G O*
• $ chmod g+x testfile ==> -r--r-xr--
• $ chmod u+wx testfile ==> -rwxr-xr--
• $ chmod ug-x testfile ==> -rw--r--r--
• U=user, G=group, O=other (world)
05/03/2021 Introduction to OS 97
File permissions cont…
• Absolute mode:
• We use octal (base eight) values represented like this:
• Letter Permission Value
• R read 4
• W write 2
• X execute 1
• - none 0
• For each column, User, Group or Other you can set values from 0 to 7. Here is what each
means:
• 0= --- 1= --x 2= -w- 3= -wx
• 4= r-- 5= r-x 6= rw- 7= rwx
05/03/2021 Introduction to OS 98
File permission cont…
• Numeric mode cont:
• Example index.html file with typical permission values:
• $ chmod 755 index.html
• $ ls -l index.html
-rwxr-xr-x 1 root wheel 0 May 24 06:20 index.html
05/03/2021 Introduction to OS 99
Inherited Permissions
• Two critical points:
1.The permissions of a directory affect whether someone can see its contents
or add or remove files in it.
2.The permissions on a file determine what a user can do to the data in the file.
• Example:
• If you don't have write permission for a directory, then you can't delete a file in
the directory. If you have write access to the file you can update the data in
the file.