You are on page 1of 111

Chanderprabhu Jain College of Higher Studies & School of Law

Plot No. OCF, Sector A-8, Narela, New Delhi – 110040


(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)

Semester: First Semester

Name of the Subject:

Introduction to Operating System


DEFINITION

• An operating system is an intermediary


between a computer user and the hardware.
• Make the hardware convenient to use.
• Manages system resources.
• Use the hardware in an efficient manner

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FUNCTION
• The main functions of operating systems are:
• • Manages and Interacts with Computer Hardware
• • Process the tasks
• • Provides the User/ Computer Interface (CLI / GUI)
• • Provides the Interface for Application Software
• • Input / Output Operations
• • Error Detection, Resource Allocation, Security and
• Protection

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
TYPES OF OS
• Batch
– submit large number of jobs at one time
– system decides what to run and when
• Time Sharing
– multiple users connected to single machine
– few processors, many terminals
• Single User Interactive
– one user, one machine

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Parrallel
– traditional multiprocessor system
– higher throughput and better fault tolerance
• Distributed
– networked computers
• Real Time
– very strict response time requirements
– hardware or software

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FUNCTION
• Only one program can perform at a time
• Simple to implement
– only one process attempting to use resources
• Few security risks
• Poor utilization of the CPU and other
resources
• Example: MS-DOS

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Multi tasking
• Very complex
• Serious security issues
– how to protect one program from another sharing
the same memory
• Much higher utilization of system resources
• Example: Unix, Windows NT

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Hardware basics
• OS and hardware closely tied together
• Many useful hardware features have been
invented to compliment the OS
• Basic hardware resources
– CPU
– Memory
– Disk
– I/O
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
CPU
• CPU controls everything in the system
– if work needs to be done, CPU gets involved
• Most precious resource
– this is what your paying for
– want to get high utilization (from useful work)
• Only one process on a CPU at a time
• Hundreds of millions of instructions / sec
– and getting faster all the time
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
MEMORY
• Limitted in capacity
– never enough memory
• Temporary (volatile) storage
• Electronic storage
– fast, random access
• Any program to run on the CPU must be in
memory

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
DISK
• Virtually infinite capacity
• Permanent storage
• Orders of magnitude slower than memory
– mechanical device
– millions of CPU instructions can execute in the
time it takes to access a single piece of data on
disk
• All data is accessed in blocks
– usually 512 bytes
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
I/O
• Disk is actually part of the I/O subsystem
– they are of special interest to the OS
• Many other I/O devices
– printers, monitor, keyboard, etc.
• Most I/O devices are painfully slow
• Need to find ways to hide I/O latency
– like multiprogramming

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
PROTECTION AND SECURITY
• OS must protect itself from users
– reserved memory only accessible by OS
– hardware enforced
• OS may protect users from one another
– not all systems do this
– hardware enforced again

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
PROTECTION AND SECURITY
• Dual -Mode Operation
– user mode
• limited set of hardware instr and memory available
• mode all user programs run in
– supervisory mode
• all hardware instr and memory are available
• mode the OS runs in
• Never let user run in supervisory mode
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
INTERRUPPTS
• Modern OS’s are event driven
• Event is signaled by special hardware signal sent to
the CPU
• Two types of events
– interrupts
• caused by external devices or timers
• can occur at any moment in time
– exceptions (traps)
• caused by software
– the generic term for both is Interrupt
• sorry for the confusion

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
INTERRUPT PHILOSOPHY
• One way to handle interrupts is with one
standard program
– big case-switch statement that gets executed on
any interrupt
– inefficient
• Second alternative is to use an interrupt table
and special hardware
– this is the way modern systems operate

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
INTERRUPT TABLE
• Large array indicating what code to run for a
given interrupt
• Each interrupt has a corresponding number
associated with it
– on Intel processors this is from 0 to 255
– this gives fixed size interrupt table
• Use the interrupt number to index into the
array to find out what code to run

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
INTERRUPT HARDWARE
• Programmable Interrupt Controller (PIC)
– connected to I/O devices via interrupt request lines (IRQ)
• ideally, one IRQ for each I/O device - doesn’t work this way in
reality
• PIC connected to CPU by a special signal
• PIC also connected to CPU via I/O bus
• Besides the PIC, interrupts can also be generated by
software instructions or errors
– again, these are usually referred to as exceptions

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
HARDWARE HANDLING OF INTERRUPT
• After each instruction executes, CPU checks to see if interrupt
pin has been raised
• If so, the following occurs:
1) sets the system into kernel mode (if not already there)
2) determine interrupt number (from PIC or instruction)
3) read appropriate interrupt table entry
- special register contains base address of interrupt table
- each entry in table is fixed size so easy to calculate where
to look in memory ( memLoc = idtr + 8 * intNum )
4) saves the program counter to the stack (with a couple of others)
5) saves error code to stack (if it exists)
6) loads the program counter with the value stored in the TABLE.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Software handaling interrupt
• The following 6 steps are common to all interrupt handlers:

1) save IRQ to kernel mode stack


2) save registers to kernel mode stack
3) send acknowledgement to PIC
- this allows PIC to then handle other interrupts on IRQ line
4) execute the appropriate handler code
5) restore registers
6) issue an iret instruction

• The steps for exception handlers are almost identical


– simply remove steps 1 & 3 above

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
More of exception
• As indicated earlier, exceptions are caused by software
– divide by zero error
– page fault
– int instruction
– etc
• Some of these cause the program to stop executing
• Some of them invoke special operating system code that is
invisible to the user
• Some of them invoke operating system code at the user’s
request
– system calls

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Ingle IRQ for multiple device
• The number of IRQ lines is usually limited
• May have more I/O devices than IRQ’s
• Solution: let multiple devices share an IRQ
• Interrupt table contains a pointer to a linked list of
interrupt handlers
– instead of the address of the interrupt handler
• On interrupt, execute all of the handlers associated
with an IRQ
– requires handlers to recognize
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
EXAMPLE
Interrupt Table
0 floppy disk
1
2
3 printer scanner camera
4
5
6
7
hard
8 disk
9
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
EXAMPLE
• Assume an interrupt from the scanner arrives at the PIC
1) PIC raises INTR signal on CPU
2) CPU reads PIC over I/O bus to determine IRQ
3) CPU then accesses array in memory to get the first interrupt handler
- printer
4) CPU executes printer handler code
5) printer handler queries printer and notices there is no interrupt
pending
- handler returns immediately
6) next the scanner handler gets executed
7) scanner handler queries scanner and notices there is an interrupt
pending

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
System calls
• An OS’s system calls are called the Application
Programmers Interface (API)
• System calls are routines run by the OS on behalf of
the user
• They are run in supervisory mode
• Allow user to access I/O, create processes, get
system information, etc.
• How many system calls an OS has varies
– Unix: around a hundred

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
System startups
• On power up
– everything in system is in random, unpredictable state
– special hardware circuit raises RESET pin of CPU
• sets the program counter to 0xfffffff0
– this address is mapped to ROM (Read-Only Memory)
• BIOS (Basic Input/Output Stream)
– set of programs stored in ROM
– some OS’s use only these programs
• MS DOS
– many modern systems use these programs to load other system
programs
• Windows, Unix, Linux

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
BIOS
• General operations performed by BIOS
1) find and test hardware devices
- POST (Power-On Self-Test)
2) initialize hardware devices
- creates a table of installed devices
3) find boot sector
- may be on floppy, hard drive, or CD-ROM
4) load boot sector into memory location 0x00007c00
5) sets the program counter to 0x00007c00
- starts executing code at that address

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
BOOT LEADER
• Small program stored in boot sector
• Loaded by BIOS at location 0x00007c0
• Configure a basic file system to allow system
to read from disk
• Loads kernel into memory
• Also loads another program that will begin
kernel initialization

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
INITIAL KERNEL PROGRAM
• Determines amount of RAM in system
– uses a BIOS function to do this
• Configures hardware devices
– video card, mouse, disks, etc.
– BIOS may have done this but usually redo it
• portability

• Switches the CPU from real to protected mode


– real mode: fixed segment sizes, 1 MB memory addressing, and no
segment protection
– protected mode: variable segment sizes, 4 GB memory addressing,
and provides segment protection
• Initializes paging (virtual memory)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FINAL KERNEL PROGRAM
• Sets up page tables and segment descriptor tables
– these are used by virtual memory and segmentation
hardware (more on this later)
• Sets up interrupt vector and enables interrupts
• Initializes all other kernel data structures
• Creates initial process and starts it running
– init in Linux
– smss (Session Manager SubSystem) in NT

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
SYSTEM PROGRAM
• Application programs included with the OS
• Highly trusted programs
• Perform useful work that most users need
– listing and deleting files, configuring system
– ls, rm, Windows Explorer and Control Panel
– may include compilers and text editors
• Not part of the OS
– run in user space
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Processes: The Process Model

 Multiprogramming of four programs


 Conceptual model of 4 independent, sequential processes
 Only one program active at any instant

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Processes and programs
The difference between a process and a program:
 Baking analogy:
o Recipe = Program
o Baker = Processor
o Ingredients = data
o Baking the cake = Process
 Interrupt analogy
o The baker’s son runs in with a wounded hand
o First aid guide = interrupt code

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Main OS Process-related
Goals
 Interleave the execution of existing processes to
maximize processor utilization
 Provide reasonable response times
 Allocate resources to processes
 Support inter-process communication (and
synchronization) and user creation of processes

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
How are these goals achieved?
 Schedule and dispatch processes for execution by
the processor
 Implement a safe and fair policy for resource
allocation to processes
 Respond to requests by user programs
 Construct and maintain tables for each process
managed by the operating system

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Creation
When is a new process created?

1. System initialization (Daemons)


2. Execution of a process creation system call by a
running process
3. A user request to create a process
4. Initiation of a batch job

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process States

 Running - actually using the CPU


 Ready – runnable, temporarily stopped to let
another process run
 Blocked - unable to run until some external event
happens

A process can block itself, but not “run” itself

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process State Transitions
When do these
transitions occur?
1. Process blocks for input or waits for an
event
2. End of time-slice, or preemption
3. Scheduler switches back to this process
4. Input becomes available, event arrives

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Suspended Processes
 Processor is much faster than I/O so many processes
could be waiting for I/O
 Swap some of these processes to disk to free up
more memory
 Blocked state becomes blocked-suspended state
when swapped to disk, ready becomes
ready-suspended
 Two new states
o Blocked-suspended
o Ready-suspended

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Management
Operations
Process creation and termination
 Process scheduling and dispatching
 Process switching
 Process synchronization and support for inter-
process communication

The OS maintains process data in the


Process Control Blocks (PCB)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Table
 Process image consists of program (code/text), data,
stack, and attributes
 Control Attributes form the Process Control Block -
PCB
o Unique ID (may be an index into the PT)
o User ID; User group ID, Parent process ID
o process control information
o Processor state information

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Control Information
Additional information needed by the operating
system to control and coordinate the various active
processes
o Execution state: see next slide…
o Scheduling-related information - state; priority;
scheduling info
o inter-process communication - signals; pipes
o Time of next alarm
o memory management - pointers to text/data/stack
segments
o resource ownership and utilization - open files
o Process relationships: Parent, process group…
o Environment variables
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Creation
 Assign a unique process identifier
 Allocate space for the process
 Initialize process control block
 Set up appropriate linkage to the scheduling
queue:
o In the former example: add the PCB to the ready queue

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Stop a running process

 Clock event: process has executed a full


time-slice (a.k.a. time-quantum)
 Process becomes blocked
 Another process is ready
 Error occurred
 Signal received

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process Context Switch
 Save processor context, including program counter
and other registers
 Update the process control block with the new state
and any accounting information
 Move process control block to appropriate queue -
ready, blocked
 Select another process for execution
 Update the process control block of the process
selected
 Update memory-management data structures
 Restore context of selected process
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Inter-Process
Communication
 Shared memory – the fastest way
o Need to avoid race conditions
 Non-shared Memory:
o File/Pipes
o Unbuffered messages - Rendezvous
o Buffered messages – Mailboxes and Sockets
o Sockets: Address – Domain+Port
o Sockets: Types – Stream or Datagrams
o Sockets: API: Socket, Bind, Connect, Read/Write

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Scheduling
• Assign each process time to use CPU
– Determine sequence (order), timing (when)
– Conflicting objectives  need compromise
• Scheduling levels
– High: whether to admit a new process
– Medium: suspend/resume a process
– Low: dispatch (run) a ready process

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Scheduling Objectives
• Maximise throughput
• Give all users a “fair” (not equal) chance
• Provide tolerable performance
– Response time for on-line user
– Turnaround time for batch users
• Degrade performance gracefully
– OK to be slow but avoid complete collapse
• Be consistent, predictable over time
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Scheduling Criteria
• Priority which may be either/both:
– Assigned to job by user
– Determined by properties of the job
• Class of job (real-time > on-line > batch)
• Resources needed (CPU time, memory)
• I/O or CPU bound (the aim is a balance)
• Resources already used
• Time already waited
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Types of Scheduling Policies
• Preemptive (Used by most OS today)
– OS stops one process to run another
• Non-preemptive
– process runs until termination or I/O wait
• Cooperative (Used by Windows 3.11)
– Programs must voluntarily give up CPU
– Not managed by OS; trusts programmers
– If a process hangs the whole PC hangs 

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Scheduling Policies

• First come first served (FCFS/FIFO)


• Shortest job first (SJF)
• Shortest remaining time (SRT)
• Highest response ratio next
• Round robin (RR)
• Multi-level feedback queues (MFQ)

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
First come first served
• Also called first in first out (FIFO)
• Process waiting longest is first in queue
• Favours long jobs
– high run-time/wait-time ratio
• Favours CPU-bound jobs
– I/O devices underused

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Shortest job first (next)

• Run job with shortest estimated run time


• Long jobs may be delayed indefinitely
• JCL commands can specify run time

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Shortest remaining time

• Run job w/ shortest est. remaining time


• Highly favours short jobs
• Long jobs will be delayed indefinitely
• Requires estimating total run time
• Requires measuring elapsed run time

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Highest response ratio next

• P = (time waiting + run time) / run time


• Priority based on two factors
• Favours shorter jobs
• Guarantees a job cannot be starved

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Round Robin
• Each process given a set time slice
• Pre-emption at end of time quantum
– Hardware timer generates interrupts
• After running, go to back of queue
• Used in most interactive operating systems
• How large should each time slice be?
– Small  high context switch overhead
– Large  user response time is reduced
– In practice, about 10-20 ms

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Multi-level feedback queues
• Separate queues for different priorities
– New process  level 1 FIFO (highest)
– After timeout  level 2 FIFO, etc.
– Lowest level is Round Robin
• Adapts to past process behaviours
– high CPU usage  reduced access level
• Long processes may starve
– if wait time is long, promote/increase slice

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Deadlock

• Each thread is waiting on a resource


held by another thread
– So, there is no way to make progress

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Deadlock
• Examples: Traffic Jam :

• Dining Philosophers

• Device allocation
– process 1 requests tape drive 1 & gets it
– process 2 requests tape drive 2 & gets it

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Necessary Conditions for Deadlock
• Mutual exclusion
– Resources cannot be shared
• e.g., buffers, locks
• Hold and wait (subset property)
– A thread must hold a subset of its resource needs
• And, the thread is waiting for more resources
• No preemption
– Resources cannot be taken away
• Circular wait
– A needs a resource that B has
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Resource Allocation Graph with a Deadlock

P => R: request edge


R => P: assignment edge

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Cycle Without Deadlock

P => R: request edge


R => P: assignment edge

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Approaches to Deadlock

1. Avoid threads
2. Deadlock prevention
– Break up one of the four necessary conditions
3. Deadlock avoidance
– Stay live even in the presence of the four
conditions
4. Detect and recover

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Deadlock Detection

• Detection algorithm (sketch)


– Keep track of who is waiting for resources
– Keep track of who holds resources
– Assume that all runnable processes release all
their resources
• Does this unblock a waiting process?
– If yes, release that process’s resources
– If processes are still blocked, we are deadlocked

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Recovery

• Must “back out” of a resource allocation


decision
– By preemption or by killing the process
• This is particularly problematic for lock-based
deadlocks
– System can be in an inconsistent state

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Memory Management
• Linux’s physical memory-management
system deals with allocating and
freeing pages, groups of pages, and
small blocks of memory
• It has additional mechanisms for
handling virtual memory, memory
mapped into the address space of
running processes
• Splits memory into four different zones
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Managing Physical Memory
• The page allocator allocates and frees all
physical pages; it can allocate ranges of
physically-contiguous pages on request
• The allocator uses a buddy-heap algorithm to
keep track of available physical pages
– Each allocatable memory region is paired with an
adjacent partner
– Whenever two allocated partner regions are both
freed up they are combined to form a larger region
– If a small memory request cannot be satisfied
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Managing Physical Memory (Cont.)
• Memory allocations in the Linux kernel occur
either statically (drivers reserve a contiguous area
of memory during system boot time) or
dynamically (via the page allocator)
• Also uses slab allocator for kernel memory
• Page cache and virtual memory system also
manage physical memory
– Page cache is kernel’s main cache for files and main
mechanism for I/O to block devices
– Page cache stores entire pages of file contents
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Splitting of Memory in a Buddy Heap

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Slab Allocator in Linux

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Virtual Memory
• The VM system maintains the address space visible to each
process: It creates pages of virtual memory on demand, and
manages the loading of those pages from disk or their
swapping back out to disk as required.
• The VM manager maintains two separate views of a process’s
address space:
– A logical view describing instructions concerning the layout
of the address space
• The address space consists of a set of non-overlapping
regions, each representing a continuous, page-aligned
subset of the address space

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Virtual Memory (Cont.)
• Virtual memory regions are characterized by:
– The backing store, which describes from where the
pages for a region come; regions are usually backed by a
file or by nothing (demand-zero memory)
– The region’s reaction to writes (page sharing or copy-
on-write
• The kernel creates a new virtual address space
_ When a process runs a new program with the exec()
system call
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Swapping and Paging
• The VM paging system relocates pages of memory from physical
memory out to disk when the memory is needed for something
else
• The VM paging system can be divided into two sections:
– The pageout-policy algorithm decides which pages to write out
to disk, and when
– The paging mechanism actually carries out the transfer, and
pages data back into physical memory as needed
– Can page out to either swap device or normal files
– Bitmap used to track used blocks in swap space kept in physical
memory

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Static and Dynamic Linking
• A program whose necessary library functions are
embedded directly in the program’s executable binary
file is statically linked to its libraries
• The main disadvantage of static linkage is that every
program generated must contain copies of exactly the
same common system library functions
• Dynamic linking is more efficient in terms of both
physical memory and disk-space usage because it loads
the system libraries into memory only once

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Kernel Virtual Memory
• The Linux kernel reserves a constant, architecture-dependent region
of the virtual address space of every process for its own internal use
• This kernel virtual-memory area contains two regions:
– A static area that contains page table references to every available
physical page of memory in the system, so that there is a simple
translation from physical to virtual addresses when running kernel
code
– The reminder of the reserved section is not reserved for any
specific purpose; its page-table entries can be modified to
point to any other areas of memory

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Virtual Memory That is Larger Than Physical Memory

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Virtual-address Space
 Usually design logical address space for
stack to start at Max logical address and
grow “down” while heap grows “up”
 Maximizes address space use
 Unused address space between
the two is hole
 No physical memory needed
until heap or stack grows to a
given new page
 Enables sparse address spaces with
holes left for growth, dynamically linked
libraries, etc
 System libraries shared via mapping into
virtual address space
 Shared memory by mapping pages read-
write into virtual address space

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Shared Library Using Virtual
Memory

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Demand Paging
• Could bring entire process into memory at
load time
• Or bring a page into memory only when it is
needed
– Less I/O needed, no unnecessary I/O
– Less memory needed
– Faster response
– More users
• Similar to paging system with swapping
(diagram on right)
• Page is needed  reference to it
– invalid reference  abort
– not-in-memory  bring to memory
• Lazy swapper – never swaps a page into
memory unless page will be needed
– Swapper that deals with pages is a
pager

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Systems
• To the user, Linux’s file system appears as a hierarchical directory tree
obeying UNIX semantics
• Internally, the kernel hides implementation details and manages the
multiple different file systems via an abstraction layer, that is, the virtual
file system (VFS)
• The Linux VFS is designed around object-oriented principles and is
composed of four components:
– A set of definitions that define what a file object is allowed to look
like
• The inode object structure represent an individual file
• The file object represents an open file
• The superblock object represents an entire file system
• A dentry object represents an individual directory entry

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Systems (Cont.)
• To the user, Linux’s file system appears as a hierarchical directory tree
obeying UNIX semantics
• Internally, the kernel hides implementation details and manages the
multiple different file systems via an abstraction layer, that is, the virtual
file system (VFS)
• The Linux VFS is designed around object-oriented principles and layer of
software to manipulate those objects with a set of operations on the
objects
– For example for the file object operations include (from struct
file_operations in /usr/include/linux/fs.h
int open(. . .) — Open a file
ssize t read(. . .) — Read from a file
ssize t write(. . .) — Write to a file
int mmap(. . .) — Memory-map a file

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
OVERVIEW
• The file system permits users to create data
collections, called files,with desirable
properties.

• Long-term existence: Files are stored on disk


or other secondary storage and do not
disappear when a user logs off.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Sharable between processes: Files have names
and can have associated access permissions
that permit controlled sharing.
• Structure: Depending on the file system, a file
can have an internal structure that is
convenient for particular applications. In
addition, files can be organized into hierarchical
or more complex structure to reflect the
relationships among files.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Typical operations include
the following:
• Create: A new file is defined and positioned within the
structure of files.
• Delete: A file is removed from the file structure and
destroyed.
• Open: An existing file is declared to be “opened” by a
process, allowing the process to perform functions on
the file.
• Close: The file is closed with respect to a process, so
that the process no longer may perform functions on
the file, until the process opens the file again.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FILE ORGANIZATION AND ACCESS
• In choosing a file organization,several criteria
are important:
– Short access time
– Ease of update
– Economy of storage
– Simple maintenance
– Reliability

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FILE DIRECTORIES
• Associated with any file management system and collection
of files is a file directory. The directory contains information
about the files, including attributes, location, and ownership.
Much of this information, especially that concerned with
M12_STAL6329_06_SE_C12.QXD 2/21/08 9:40 PM Page
56212.3 / FILE DIRECTORIES 563 storage, is managed by the
operating system. The directory is itself a file, accessible by
various file management routines. Although some of the
information in directories is available to users and
applications, this is generally provided indirectly by system
routines.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
FILE SHARING
• The following list is representative of access rights that
can be assigned to a particular user for a particular file:
- None: The user may not even learn of the existence of
the file, much less access it. To enforce this restriction,
the user would not be allowed to read the user
directory that includes this file.
- Knowledge: The user can determine that the file
exists and who its owner is. The user is then able to
petition the owner for additional access rights.
- Execution: The user can load and execute a program
but cannot copy it.Proprietary programs are often
made accessible with this restriction.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Access can be provided to different
classes of users:
• Specific user: Individual users who are
designated by user ID.
• User groups: A set of users who are not
individually defined. The system must have
some way of keeping track of the membership
of user groups.
• All: All users who have access to this system.
These are public files.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Given the size of a block,there are three
methods of blocking that can be used:

• Fixed blocking: Fixed-length records are used, and an


integral number of records are stored in a block. There
may be unused space at the end of each block. This is
referred to as internal fragmentation.
• Variable-length spanned blocking: Variable-length
records are used and are packed into blocks with no
unused space. Thus, some records must span two
blocks, with the continuation indicated by a pointer to
the successor block.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Variable-length unspanned blocking: Variable-
length records are used, but spanning is not
employed. There is wasted space in most
blocks because of the inability to use the
remainder of a block if the next record is
larger than the remaining unused space.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
What is Unix?
• A multi-task and multi-user Operating System
• Developed in 1969 at AT&T’s Bell Labs by
– Ken Thompson (Unix)
– Dennis Ritchie (C)
– Douglas Mcllroy (Pipes - Do one thing, do it well)
• Some other variants: System V, Solaris, SCO
Unix, SunOS, 4.4BSD, FreeBSD, NetBSD,
OpenBSD, BSDI

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
What is Linux?
• A clone of Unix
• Developed in 1991 by Linus Torvalds, a Finnish graduate
student
• Inspired by and replacement of Minix
• Linus' Minix became Linux
• Consist of
– Linux Kernel
– GNU (GNU is Not Unix) Software
– Software Package management
– Others

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
What is Linux?
• Originally developed for
32-bit x86-based PC
• Ported to other
architectures, eg.
– Alpha, VAX, PowerPC, IBM
S/390, MIPS, IA-64
– PS2, TiVo, cellphones,
watches, Nokia N810, NDS,
routers, NAS, GPS, …

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Which Linux Distribution is better?
• > 300 Linux Distributions
– Slackware (one of the oldest, simple and stable distro.)
– Redhat
• RHEL (commercially support)
• Fedora (free)
– CentOS (free RHEL, based in England)
– SuSe ( based in German)
– Gentoo (Source code based)
– Debian (one of the few called GNU/Linux)
– Ubuntu (based in South Africa)
– Knoppix (first LiveCD distro.)

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Basic Commands
• ls
• which
– $ ls -l
– $ which ls
– $ ls -a
– $ ls -la • whereis
– $ ls -l --sort=time – $ whereis ls
– $ ls -l --sort=size -r • locate
• cd – $ locate stdio.h
– $ cd /usr/bin – $ locate iostream
• pwd • rpm
– $ pwd – $ rpm -q bash
– $ rpm -qa
• ~
– $ rpm -qa | sort | less
– $ cd ~
• ~user • find
– $ find / | grep stdio.h
– $ cd ~weesan
– $ find /usr/include | grep stdio.h
• What will “cd ~/weesan” do?

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Basic Commands (cont)
• echo
• rm
– $ echo “Hello World”
– $ rm foo
– $ echo -n “Hello World”
– $ rm -rf foo
• cat – $ rm -i foo
– $ cat /etc/motd – $ rm -- -foo
– $ cat /proc/cpuinfo
• chgrp
• cp – $ chgrp bar /home/foo
– $ cp foo bar • chsh
– $ cp -a foo bar – $ chsh foo
• mv • chfn
– $ mv foo bar – $ chfn foo
• mkdir • chown
– $ mkdir foo – $ chown -R foo:bar /home/foo

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Basic Commands (cont)
• Pipe
• tar – $ cal > foo
– $ tar cvfp lab1.tar lab1
– $ cat /dev/zero > foo
• gzip – $ cat < /etc/passwd
– $ gzip -9 lab1.tar
– $ who | cut -d’ ‘ -f1 | sort |
• untar & ungzip uniq | wc –l
– $ gzip -cd lab1.tar.gz | tar xvf – • backtick
– $ tar xvfz lab1.tar.gz – $ echo “The date is `date`”
• touch – $ echo `seq 1 10`
– $ touch foo • Hard, soft (symbolic) link
– $ cat /dev/null > foo – ln vmlinuz-2.6.24.4 vmlinuz
– ln -s firefox-2.0.0.3 firefox

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Unix Commands:
When you first log into a unix system, you are
presented with something that looks like the
following:
/home/larry#
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.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Unix is also case-sensitive. This means 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 are called “shell scripts”.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Storing Information:
Unix provides files and directories.
A directory is like a folder: it contains pieces of
paper, or files. A large folder can even hold other
folders-directories can be inside directories.
In unix, the collection of directories and files is
called the file system. Initially, the file system
consists of one directory, called the “root”
directory.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Inside “root” directory, there are more directories,
and inside those directories are files and yet more
directories.
• Each file and each directory has a name. **
• A short name for a file could be joe, while it’s “full
name” would be /home/larry/joe. The full name
is usually called the path.
• The path can be decote into a sequence of
directories.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• The second lesson is more general. Many unix
commands are like ls. They have options, which
are generally one character after a dash, and
they have parameters. Unlike ls, some
commands require certain parameters and/or
options.
• pwd (present working directory) , cd
Unix shells have a feature called the “current” or
“present” or “working” directory.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• If you omit the optional parameter directory,
you’re returned to your home, or original
directory. Otherwise, cd will change you to
the specified directory. **
• Creating and Removing Directories Creating
your own directories is extremely simple
under unix, and can be a useful organizational
tool.
To create a new directory, use the command
mkdir.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• mkdir stands for make directory.
mkdir can take more than one parameter,
interpreting each parameter as another directory
to create.
• The opposite of mkdir is rmdir (remove directory).
rmdir works exactly like mkdir. rmdir will refuse
to remove a non-existant directory, as well as a
directory that has anything in it.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Moving Information: The primary commands for
manipulating files under unix are cp, mv, and rm. They
stand for copy, move, and remove, respectively.
• cp cp is a very useful utility under unix, and extremely
powerful. It enables one person to copy more information
in a second than a fourteenth century monk could do in a
year.
cp file1 file2
(one of file1 copied to file2 in the same directory)
cp folder1/file1 folder2
(one of file1 copied to file2 in the inside of folder2 directory)

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Some Other UNIX Commands
• The Power of Unix: The power of unix is hidden in
small commands that don’t seem too useful when
used alone, but when combined with other
commands produce a system that’s much more
powerful, and flexible than most other operating
systems. The commands I’m going to talk about in
this chapter include sort, grep, more, cat, wc,
spell, diff, head,and tail.

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• Operating on Files: In addition to the commands
like cd, mv, and rm you learned in shell section,
there are other commands that just operate on
files, but not the data in them. These include
touch, chmod, du, and df. All of these files don’t
care what is in the file-the merely change some
of the things unix remembers about the file.
Some of the things these commands manipulate:

Chanderprabhu Jain College of Higher Studies & School of Law


Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Working with Unix

• Unix is a powerfull system for those who know how


to harness its power. In this chapter, we’ll try to
describe various ways to use Unix’s shell, bash, more
efficently.
• Wildcards
You might want to copy all the files beginning with
data into a directory called ˜backup. You could do
this by either running many cp commands, or you
could list every file on one command line. Both of
these methods would take a long time.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)

You might also like