Download Linux+ Guide to Linux Certification

Document related concepts

Commodore DOS wikipedia , lookup

Linux adoption wikipedia , lookup

Mobile operating system wikipedia , lookup

Copland (operating system) wikipedia , lookup

Unix time wikipedia , lookup

Smallfoot wikipedia , lookup

Library (computing) wikipedia , lookup

Batch file wikipedia , lookup

Distributed operating system wikipedia , lookup

OS 2200 wikipedia , lookup

RSTS/E wikipedia , lookup

Unix wikipedia , lookup

Process management (computing) wikipedia , lookup

Berkeley Software Distribution wikipedia , lookup

Security-focused operating system wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

DNIX wikipedia , lookup

Paging wikipedia , lookup

Burroughs MCP wikipedia , lookup

VS/9 wikipedia , lookup

Spring (operating system) wikipedia , lookup

CP/M wikipedia , lookup

Unix security wikipedia , lookup

Transcript
Chapter 15
UNIX and Linux Operating
Systems
Understanding Operating Systems,
Fourth Edition
Objectives
You should be able to describe:
• The similarities between UNIX and Linux
• The design goals for both operating systems
• The significance of using files to manipulate
devices
• The differences between command-driven and
menu-driven interfaces
• The roles of the Memory, Device, and Processor
Managers
Understanding Operating Systems, Fourth Edition
2
Overview
• UNIX and Linux share three major advantages:
– Portable from large systems to small systems
• Written in a high-level language, C
– Powerful utilities
• Brief, single-operation commands can be combined in
a single command line to achieve any desired result
– Device independent
• Device drivers are included as part of the operating
system, and not as part of devices
Understanding Operating Systems, Fourth Edition
3
Overview
• Disadvanatges:
– No single standardized version of operating system
in UNIX
– Both have very brief, cryptic commands
• Novice users find unfriendly
• Both UNIX and Linux are case-sensitive and
strongly oriented toward lower-case characters
Understanding Operating Systems, Fourth Edition
4
The Evolution of UNIX
Table 15.1: Evolution of UNIX
Understanding Operating Systems, Fourth Edition
5
The Evolution of UNIX (continued)
Table 15.1 (continued): Evolution of UNIX
Understanding Operating Systems, Fourth Edition
6
The Evolution of UNIX (continued)
Table 15.1 (continued): Evolution of UNIX
Understanding Operating Systems, Fourth Edition
7
The Evolution of UNIX (continued)
• New releases of UNIX include following features:
– Full support for local area networks
– Complies with international operating system
standards
– Greatly improved system security
• Meets many security requirements
– Most feature Common Desktop Environment (CDE)
which is a uniform GUI
• Challenge: To standardize to improve portability of
programs from one system to another
– Release of ISO/IEC 9945:2003 is big step
Understanding Operating Systems, Fourth Edition
8
The Evolution of Linux
• Linux is a contraction of Linus and UNIX
• Based on a powerful multiplatform operating
system, UNIX
• Brought speed, efficiency, and flexibility of UNIX
to PC environment
• Linux is an open-source program
• Has Windows-like graphical user interfaces
Understanding Operating Systems, Fourth Edition
9
The Evolution of Linux (continued)
Table 15.2: Major releases of Linux by Red Hat, Inc.
Understanding Operating Systems, Fourth Edition
10
The Evolution of Linux (continued)
Table 15.2 (continued): Major releases of Linux by Red Hat, Inc.
Understanding Operating Systems, Fourth Edition
11
The Evolution of Linux (continued)
Table 15.2 (continued): Major releases of Linux by Red Hat, Inc.
Understanding Operating Systems, Fourth Edition
12
Design Goals
• Linux and UNIX share similar design goals:
– Develop an operating system that would support
software development
• Included utilities in OS for which programmers
typically need to write code
• Each utility designed for simplicity
• Each utility designed to be used in combination with
each other
– Keep its algorithms as simple as possible
– Make it portable
Understanding Operating Systems, Fourth Edition
13
Design Goals (continued)
Table 15.3: System functions supported in Linux and UNIX
Understanding Operating Systems, Fourth Edition
14
Design Goals (continued)
Table 15.3 (continued): System functions supported in Linux and UNIX
Understanding Operating Systems, Fourth Edition
15
UNIX Memory Management
• Uses following memory management techniques:
– Swapping (for small jobs)
– Demand paging (for large jobs)
• Typical internal memory layout consists of:
– Program code
– Data segment
– Stack
Understanding Operating Systems, Fourth Edition
16
UNIX Memory Management
(continued)
Figure 15.1: Typical internal memory layout for
single user-memory part UNIX
image
Understanding Operating Systems, Fourth Edition
17
UNIX Memory Management
(continued)
• Program code:
– Sharable portion of program
– Written in reentrant code as physically shared by
several processes
– Protected so that its instructions aren’t modified in
any way during normal execution
– Space allocated to program code can’t be released
until all processes using it have completed execution
– UNIX uses text table to keep track of which
processes are using which program code
Understanding Operating Systems, Fourth Edition
18
UNIX Memory Management
(continued)
• Data Segment:
– Starts after program code and grows toward higher
memory locations
– Nonsharable section of memory
• Stack:
– Starts at highest memory address and grows
downward as subroutine calls and interrupts add
information to it
– Section of main memory where process information
is saved when process is interrupted
– Nonsharable section of memory
Understanding Operating Systems, Fourth Edition
19
UNIX Memory Management
(continued)
• Unix Kernel:
– Part of OS that implements “system calls” to set up
memory boundaries
– Set of programs that implements most primitive of
that system’s functions
– Permanently reside in memory
– UNIX uses LRU page replacement algorithm
• Same memory management concepts are used for
network PCs, single-user and multi-user systems
Understanding Operating Systems, Fourth Edition
20
Linux Memory Management
• Allocates memory space to each process
– e.g., In Intel X86, Linux allocates 1 GB of high order
memory to kernel and 3 GB of memory to executing
processes
• Address space is divided among:
–
–
–
–
Process code
Process data
Code and shared library data used by process
Stack used by process
Understanding Operating Systems, Fourth Edition
21
Linux Memory Management
(continued)
• Linux has system calls that change size of process
data segment as required
• Offers memory protection based on type of
information stored
• When kernel needs memory space, pages are
released using LRU algorithm
• Maintains a dynamically managed area in memory,
a page cache
Understanding Operating Systems, Fourth Edition
22
Linux Memory Management
(continued)
• If any pages marked for deletion have been
modified, they’re rewritten to disk
– Page corresponding to file mapped into memory is
rewritten to file
– Page corresponding to data is saved on swap device
• Linux uses system of page table to keep track of
free and busy pages
• Uses virtual memory mechanism
Understanding Operating Systems, Fourth Edition
23
Processor Management
• Processor Manager of UNIX system kernel
handles:
– Allocation of CPU
– Process scheduling
– Satisfaction of process requests
• Process scheduling algorithm picks process with
highest priority to be run first
– Any processes that have used a lot of CPU time will
get lower priority than those that have not
• System updates compute-to-total-time ratio for
each job every second
Understanding Operating Systems, Fourth Edition
24
Processor Management (continued)
• If several processes have same computed priority,
they’re handled round-robin
• Process with longest time spent on secondary
storage will be loaded first from READY queue
• Process either waiting for disk I/O or currently idle
is temporarily moved out to make room for new
arrival
• UNIX dynamically recalculates all process priorities
to determine which inactive but ready process will
begin execution when processor becomes
available
Understanding Operating Systems, Fourth Edition
25
Process Table Versus User Table
• UNIX uses several tables to keep system running
smoothly
• Information on simple processes, those with
nonsharable code, is stored in two sets of tables:
– Process table:
• Always resides in memory
– User table:
• Resides in memory only while process is active
• User table, together with process data segment and
code segment, can be swapped into or out of main
memory as needed
Understanding Operating Systems, Fourth Edition
26
Process Table Versus User Table
(continued)
Figure 15.2: Process
control structure showing
how process table and
text table interact for
processes with sharable
code, as well as for those
without sharable code
Understanding Operating Systems, Fourth Edition
27
Process Table Versus User Table
(continued)
Process Table:
• Each entry contains following information:
– Process identification number
– User identification number
– Process memory address or secondary storage
address
– Size of process and scheduling information
• Process table is set up when process is created
and is deleted when process terminates
Understanding Operating Systems, Fourth Edition
28
Process Table Versus User Table
(continued)
Text Table:
• For processes with sharable code, process table
maintains a text table, which contains:
– Memory address or secondary storage address of
the text segment (sharable code)
– A count to keep track of number of processes using
this code
• Increased by one when process starts using code
• Decreased by one when process stops using code
• Count = 0 implies code is no longer needed
Understanding Operating Systems, Fourth Edition
29
Process Table Versus User Table
(continued)
User Table:
• Allocated to each active process
• Kept in transient area of memory
• Contains following information that must be
accessible when process is running:
– User and group identification numbers to determine
file access privileges
– Pointers to system’s file table for every file being
used by the process
– A pointer to current directory
– A list of responses for various interrupts
Understanding Operating Systems, Fourth Edition
30
Synchronization
• UNIX is true multitasking operating system
• Achieves process synchronization by requiring that
processes wait for certain events
• Each event is represented by integers equal to
address of table associated with event
• A race may occur if event happens during
process’s transition between deciding to wait for
event and entering WAIT state
Understanding Operating Systems, Fourth Edition
31
fork
• fork: Capability of executing one program from
another program
– Gives second program all attributes of first program,
such as any open files, and saves first program in its
original form
– Splits program into two copies, which are both
running from statement after fork command
– When fork is executed “process id” (pid) generated
• Ensures each process has own unique ID number
Understanding Operating Systems, Fourth Edition
32
fork (continued)
Figure 15.3: When fork command is received, parent process
shown in (a) begets child process shown in (b)
and Statement 2 is executed twice
Understanding Operating Systems, Fourth Edition
33
wait
• wait: Allows programmer to synchronize process
execution by suspending parent until child is
finished
– In a program, the IF-THEN-ELSE structure is
controlled by value assigned to pid:
• pid > 0: parent process,
• pid = 0: child process
• pid < 0: error in fork call
Understanding Operating Systems, Fourth Edition
34
wait (continued)
Figure 15.4: wait command used in conjunction with fork
command synchronizes parent and child processes. (a) shows
parent process, (b) shows parent and child after fork, and (c)
shows parent and child during wait
Understanding Operating Systems, Fourth Edition
35
exec
• exec: Used to start execution of new program from
another program, e.g., execl, execv, execls, execlp
and execvp
– Successful exec call will overlay second program
over first, leaving only second program in memory
– No return from successful exec call
• Concept of parent-child doesn’t hold here
– Each exec call is followed by test to ensure
successful completion
Understanding Operating Systems, Fourth Edition
36
exec (continued)
Figure 15.5: exec command is used after fork and wait
combination. (a) shows parent before fork, (b) shows parent and
child after fork, and (c) shows how the child process (Process 2)
is overlaid by the ls program after the exec command
Understanding Operating Systems, Fourth Edition
37
Linux Process Management
• Linux supports the concept of “personality” to
allow processes coming from other operating
systems to be executed
– Each process is assigned to an execution domain
specifying the way in which
• System calls are carried out
• Messages are sent to processes
• Supports pipes to allow executing processes to
exchange data
• Has an extension, which permits process “clones”
to be created
Understanding Operating Systems, Fourth Edition
38
Linux Process Management
(continued)
• Clone process is created using primitive “clone,”
by duplicating its parent process
– Allows both processes to share same segment of
code and data
– Modification of one is visible to other, which is unlike
classical processes
• Ability to clone processes brings possibility of
implementing servers in which several threads may
be executing
Understanding Operating Systems, Fourth Edition
39
Organization of Table of Processes
• Each process is referenced by descriptor
– Describes process attributes together with
information needed to manage process
• Kernel dynamically allocates these descriptors
when processes begin execution
• All process descriptors are organized in doubly
linked list
• Scheduler used Macro instructions to manage
and update process descriptor lists as needed
Understanding Operating Systems, Fourth Edition
40
Process Synchronization
• To allow two processes to synchronize with each
other, Linux provides:
– Wait queue: Linked circular list of process
descriptors
– Semaphores: Used to solve problems of mutual
exclusion and problems of producers and
consumers
• In Linux they contain three fields:
– Semaphore counter
– Number of waiting processes
– List of processes waiting for semaphore
Understanding Operating Systems, Fourth Edition
41
Device Management
• UNIX and Linux are truly device independent
• Both treat each I/O device as special type of file
• Device files are given “descriptors”
– Identify devices, contain information about them, and
are stored in device directory
• Device drivers:
– Written in C
– Part of the kernel
• Both come with device drivers to operate the most
common peripheral devices
Understanding Operating Systems, Fourth Edition
42
Device Management (continued)
• Actual incorporation of device driver into kernel is
done during system configuration
• Recent versions of UNIX have program called
config that automatically creates conf.c file for any
given hardware configuration
– Contains parameters that control resources such as
number of internal buffer for kernel and size of swap
space
– Contains two tables:
• bdevsw (short for “block I/O devices”)
• cdevsw (short for “character I/O devices
Understanding Operating Systems, Fourth Edition
43
Device Classifications
• Both UNIX and Linux divide I/O system into:
– “Block I/O” system (“structured I/O” system)
– “Character I/O” system (“unstructured I/O” system)
• Each physical device is identified by:
– A minor device number
– A major device number
– A class—either block or character
• Each has a Configuration Table that contains an
array of entry points into device drivers
Understanding Operating Systems, Fourth Edition
44
Device Classifications (continued)
Figure 15.6: The hierarchy of I/O devices in UNIX and Linux
Understanding Operating Systems, Fourth Edition
45
Device Classifications (continued)
• Major device number: Used as index to array to
access appropriate code for specific device driver
• Minor device number: Passed to device driver as
argument and is used to access one of several
identical physical devices
• Block I/O system: Used for devices that can be
addressed as sequence of 512-byte blocks
– Allows Device Manager to use buffering to reduce
I/O traffic
Understanding Operating Systems, Fourth Edition
46
Device Classifications (continued)
• Devices in character class are handled by device
drivers that implement character lists
– Example: A terminal is typical character device that
has two input queues and one output queue
• I/O procedure is synchronized through hardware
completion interrupts
• Some devices can actually belong to both classes:
block and character
– Example: Disk drives and tape drives
Understanding Operating Systems, Fourth Edition
47
Device Drivers
• Special section in kernel, which includes all the
instructions necessary for OS to communicate with
device
• Device drivers for disk drives use a seek strategy
to minimize the arm movement
• Kept in a set of files:
– Can be loaded as needed, in case of seldom used
devices
– Can be kept in memory all the time when operating
system boots
– Kept in /dev directory by default and convention
Understanding Operating Systems, Fourth Edition
48
File Management
• UNIX has three types of files and each file enjoys
certain privileges:
– Directories
– Ordinary files
– Special files
• Directories:
– Used by system to maintain hierarchical structure of
file system
– Users are allowed to read information in directory
files
– Only system is allowed to modify directory files
Understanding Operating Systems, Fourth Edition
49
File Management (continued)
• Ordinary files: Files in which users store information
– Protection is based on user’s requests
– Related to read, write, execute, and delete functions
that can be performed on a file
• Special files: Device drivers that provide interface to
I/O hardware
– Appear as entries in directories
– Part of file system, and most of them reside in /dev
directory
– Name of each special file indicates type of device
with which it’s associated
Understanding Operating Systems, Fourth Edition
50
File Management (continued)
• UNIX stores files as sequences of bytes and
doesn’t impose any structure on them
– Text files are strings of characters with lines
delimited by line feed, or new line, character
– Binary files are sequences of binary digits grouped
into words as they will appear in memory during
program execution
• Structure of files is controlled by programs that use
them, not by system
Understanding Operating Systems, Fourth Edition
51
File Management (continued)
• UNIX file management system organizes disk
into blocks of 512 bytes each
• Divides disk into four basic regions:
– First region (address 0) reserved for booting
– Second region contains size of disk and boundaries
of other regions
– Third region includes list of file definitions, “i-list,”
– Remaining region holds free blocks available for file
storage
• Whenever possible files are stored in contiguous
empty blocks
Understanding Operating Systems, Fourth Edition
52
File Management (continued)
i-node:
• Each entry in the i-list is called an “i-node” (or
inode) and contains 13 disk addresses
• Each contains the following information on a
specific file:
–
–
–
–
–
Owner’s identification
Protection bits, physical address, file size
Time of creation, last use and last update
Number of links
If file is directory, ordinary file, or special file
Understanding Operating Systems, Fourth Edition
53
Filenames in UNIX and Linux
• Filenames are case sensitive
• Linux and most versions of UNIX allow filenames to
be of unlimited length
– Older versions of UNIX have maximum of 14
characters including any suffixes and period
• OSs don’t impose any naming conventions on files
– Some compilers expect files to have specific suffixes
• Linux and UNIX support hierarchical tree file
structure
– Root directory is identified by slash (/)
Understanding Operating Systems, Fourth Edition
54
Filenames in UNIX and Linux
(continued)
Figure 15.7: File hierarchy with ( / ) as root, directories
as branches, and files as leaves
Understanding Operating Systems, Fourth Edition
55
Filenames in UNIX and Linux
(continued)
• Rules that apply to all path names:
– If path name starts with slash, path starts at root
directory
– Path name can be either one name or list of names
separated by slashes
• Last name on list is name of file requested
– Using two periods (..) in path name will move you
upward in hierarchy (closer to root)
• Only way to go up hierarchy; all other path names go
down tree
– Spaces are not allowed within path names
Understanding Operating Systems, Fourth Edition
56
Directories in UNIX and Linux
Table 15.4: List of files stored in the directory journal from the
system illustrated in Figure 15.7. The command ls -l (short for
“listing-long”) was used to generate this list
Understanding Operating Systems, Fourth Edition
57
Directories in UNIX and Linux
(continued)
• “long listing” of files in a directory shows eight
pieces of information for each file
• First column shows the type of file and the access
privileges for each file:
– First character describes nature of file or directory
– Next three characters show access privileges
granted to owner of file
– Next three characters describe access privileges
granted to other members of user’s group
– Last three characters describe access privileges
granted to users at large, those system-wide
Understanding Operating Systems, Fourth Edition
58
Directories in UNIX and Linux
(continued)
• Second column in the directory listing indicates
the number of links (number of aliases) that refer to
the same physical file
• Aliases are an important feature of UNIX
– Support file sharing when several users work
together on the same project
– Make it convenient for the shared files to appear in
different directories belonging to different users
– Filename may be different from directory to directory
– Eventually this number will indicate when the file is
no longer needed and can be deleted
Understanding Operating Systems, Fourth Edition
59
Data Structures in UNIX
• UNIX divides the file descriptors into parts
– Hierarchical directories containing only the name of
the file and the “i-number,” which is a pointer to
another location, the “i-node,”
– i-node contains the rest of the information
• All i-nodes are stored in a reserved part of the
device where the directory resides
• Each i-node has room for 13 pointers (0–12)
Understanding Operating Systems, Fourth Edition
60
Data Structures in UNIX (continued)
Figure 15.8: Hierarchy for directories, i-nodes, and file blocks
Understanding Operating Systems, Fourth Edition
61
Data Structures in UNIX (continued)
• When a file is opened
– Its device, i-number, and read/write pointer are
stored in the system file table and indexed by the inode
• When a file is created
– An i-node is allocated to it
– A directory entry with filename and its i-node number
is created
Understanding Operating Systems, Fourth Edition
62
Data Structures in UNIX (continued)
• When a file is linked
– A directory entry is created with the new name
– Original i-node number, and the link-count field in the
i-node is incremented by one
• When a shared file is deleted
– Link-count field in the i-node is decremented by one
– When the count reaches zero, the directory entry is
erased and all disk blocks allocated to the file, along
with its i-node block, are deallocated
Understanding Operating Systems, Fourth Edition
63
User Interface
• UNIX and Linux are command-driven systems and
many user commands are virtually identical
• User commands:
– Are very short, either one character or a group of
characters (acronym of words making command)
– Can’t be abbreviated or spelled out
– Must be in the correct case
• System prompt is very economical
– Only one character, e.g., ($) or (%)
• Error messages are also quite brief
Understanding Operating Systems, Fourth Edition
64
User Interface (continued)
Table 15.5A: User commands
Understanding Operating Systems, Fourth Edition
65
User Interface (continued)
Table 15.5B: User commands
Understanding Operating Systems, Fourth Edition
66
User Interface (continued)
• General syntax of commands:
command arguments file_name
– “command” is any legal operating system command
• interpreted and executed by the shell
– “arguments” are required for some commands and
optional for others
– “file_name” can be a relative or absolute path name
Understanding Operating Systems, Fourth Edition
67
Script Files
• Command files, often called shell files or script
files, can be used to automate repetitious tasks
• Each line of the file is a valid instruction and can be
executed by typing sh and name of script file
• Can also be executed be defining the file as an
executable command and typing filename at the
system prompt
• The default shell for Linux is called bash (for
Bourne Again Shell)
– Other common shells: csh, and ksh
Understanding Operating Systems, Fourth Edition
68
Script Files (continued)
• Example of a script file:
setenv DBPATH /u/lumber:.:/zdlf/product/central/db
setenv TERMCAP $INFODIR/etc/termcap
stty erase `^H’
set savehistory
set history=20
alias h history
alias 4gen infogen -f
setenv PATH /usr/info/bin:/etc
Understanding Operating Systems, Fourth Edition
69
Redirection
• Used to send output to a file or to another device
– Symbol: > (between command and destination)
– Examples: ls > myfiles
cat chapt1 chapt2 > sectiona
cat chapt* > sectiona
• Symbol >> appends new file to an existing file
– Examples: cat chapt1 chapt2 >> sectiona
cat chapt* >> sectiona
• Reverse redirection (<) takes input for a program
from an existing file instead of from keyboard
– Example: mail ann roger < memo
Understanding Operating Systems, Fourth Edition
70
Redirection (continued)
• Redirection is combined with system commands to
achieve any desired results:
– Example: who > temporary (will store in file
named “temporary” the names of all users logged on
to system)
• Interpretation of < and > is done by the shell and
not by the individual program
• Input and output redirection can be used with any
program
Understanding Operating Systems, Fourth Edition
71
Pipes
• Pipes and filters make it possible to redirect output
or input to selected files or devices
• Pipe can connect output from one program to input
of another without the need for temporary or
intermediate files
– Example: who | sort
• A pipeline is several programs simultaneously
processing the same I/O stream
– Example: who | sort | lpr
Understanding Operating Systems, Fourth Edition
72
Filters
• Programs that read some input, manipulate it in
some way, and generate output
– wc (word count): e.g., wc journal
• System would respond with: 10 140 700, meaning that
the file journal has 10 lines, 140 words, and 700
characters
– sort: Contents of the file are sorted and displayed
on the screen
• Example: sort sortednames
Understanding Operating Systems, Fourth Edition
73
Filters (continued)
• To sort the list in alphabetical order but ignore the
case of letters:
sort -f sortednames
• To obtain a numerical sort in ascending order:
sort -n sortednums
• To obtain a numerical sort in descending order:
sort -nr sortednums
Understanding Operating Systems, Fourth Edition
74
Additional Commands
•
man: displays online manual supplied with the
operating system
– man cmp (displays the page for the compare
(cmp) command)
•
grep: stands for “global regular expression and
print”, looks for specific patterns of characters
–
Examples: grep Pittsburgh maillist
grep -v Pittsburgh maillist
grep -c Pittsburgh maillist
Understanding Operating Systems, Fourth Edition
75
Additional Commands (continued)
• grep can be combined with who command.
– e.g., who | grep sam (displays Sam’s name, device,
and the date and time he logged in)
– ls -l / | grep '^d‘ (displays list of all subdirectories
(but not the files) found in the root directory)
• nohup: Allows logging off the system without
having to wait for program to finish
– e.g., nohup cp oldlargefile newlargefile &
• nice: Allows lowering the priority of a program
– e.g., nice cp oldlargefile newlargefile &
Understanding Operating Systems, Fourth Edition
76
Summary
• UNIX and Linux were written by programmers for
programmers
• Both are quite popular among those fluent in the
ways of programming
• Their advantages include spare user interface,
device independence, portability, lack of verbosity,
and powerful combinations of commands
• Versions of UNIX can operate very large multiuser
systems, single-user systems, and every size in
between
Understanding Operating Systems, Fourth Edition
77
Summary (continued)
• Linux is being adopted widely for single-user and
enterprise-wide computing systems
• Linux is characterized by its power, flexibility, and
constant maintenance
• Linux applies all the features of UNIX, including
multitasking and multiuser capabilities, to desktop
computers
• Both operating systems are expected to increase in
use and popularity for many years to come
Understanding Operating Systems, Fourth Edition
78