1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The...

35
1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    378
  • download

    6

Transcript of 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The...

Page 1: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

1

UNIX

1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX

Page 2: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

2

History of UNIX

• MULTICS and UNICS (Thompson)

• PDP-11 UNIX (B & C, Richie & Tompson)

• Portable UNIX

• Berkeley UNIX

• Standard UNIX

• Linux (Linus Torvald)

Page 3: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

3

UNIX Goals

• An interactive system designed to handle multiple pro-cesses and multiple (sophisticated) users at the same time

• Group of experienced users != personal computer model• Experienced users want:

– simplicity, elegancy, consistency

– power and flexibility

– avoid useless redundancy

Page 4: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

4

Interfaces to UNIX

The layers of a UNIX system.

UserInterface

Page 5: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

5

UNIX Shell (Φλοιός)• Shell: a command line interface != X Windows

• Faster to use, more powerful, easily extensible

• When the shell starts up: initializes it self, types a prompt character. User: types a command line. Shell: extract first word -> program to execute. Shell is just another program

• Commands may take arguments, e.g. cp a1.txt b1.txt

• Other aspects: flags, wildcards e.g. –2 , */?

• Standard input, output, error. Redirection ( <, >).– e.g. sort < in > tmp; head –30 < temp; rm temp

• Output of a program is input to another. Pipes (|)– e.g. sort < in | head –30

• Multiple commands: e.g. sort < in > tmp &. Shell scripts.

Page 6: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

6

UNIX Utility Programs

A few of the more common UNIX utility programs required by POSIX

Page 7: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

7

Processes in UNIX• Processes are created using the fork system call• Parent and child processes have their own, private memory

images. Open files are shared between the two processes• How do the processes know which one should run the

parent code and which one should run the child code?

Page 8: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

8

POSIX

The signals required by POSIX.

• Processes communicate with each other:• using a form of message passing (pipes) and with signals

Page 9: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

9

System Calls for Process Management

s is an error codepid is a process IDresidual is the remaining time from the previous alarm

Page 10: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

10

POSIX Shell

A highly simplified shell

Page 11: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

11

UNIX Processes: Implementation• Two data structures: process table and user structure• Process table (keeps information for all processes):

– Scheduling parameters (process priority, amount of CPU time consumed recently, sleeping time recently)

– Memory image (pointers to text, data and stack segments)– Signals (masks showing treatment of signals)– Miscellaneous (process state, event being waited for, PID, etc.)

• User structure (only if process in memory and runnable)– Machine registers– System call state– File descriptor table (used to locate the i-nodes)– Accounting (CPU used so far)

Page 12: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

12

The ls Command

Steps in executing the command ls type to the shell

Page 13: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

13

UNIX Scheduler• Designed to provide good response to interactive processes• Two-level algorithm

– Low-level: picks the process to run next from the runnable ones

– High level: moves processes between memory and disk

• Low level algorithm:– Uses multiple queues, each with a range of priorities values

– Processes in user mode have positive values, kernel mode negative

– A process run for a quantum (usually 100msec) or until it blocks and then it is put on the end of the queue

– Every second: priority = CPU_usage + nice + base

Page 14: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

14

UNIX Scheduler

The UNIX scheduler is based on a multilevel queue structure

Page 15: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

15

Booting UNIX

The sequences of processes used to boot some systems

cp

The boot program located in the MBR runs first and loads the O.S.

Page 16: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

16

Handling Memory

• Process A's virtual address space (text,data and stack)• Physical memory• Process B's virtual address space (text, data and stack)

Process A Process B

Page 17: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

17

System Calls for Memory Management

• s is an error code• b and addr are memory addresses• len is a length• prot controls protection• flags are miscellaneous bits• fd is a file descriptor• offset is a file offset

Page 18: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

18

Implementation of Paging in UNIX

• On-demand• No prepaging• Page daemon• OS, core map

and page frames

The core map has an entry for each page

• Earlier UNIX systems used swapping (low-level scheduling)

Page 19: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

19

Page Replacement in UNIX• When a page fault occurs and the there is no available page,

process is suspended until page daemon has freed some.• Page replacement algorithm is executed by page daemon;

every 250 msec it checks if free pages >= lotsfree parameter• If not, it transfers pages to disk so free pages >= lotsfree• Modified version of the clock algorithm; global replacement• Two-hand clock algorithm: the page daemon maintains two

pointers into the core map. When it runs, it first clears the usage bit at the front end and then checks the usage bit at the back end, after which it advances both hands.

Page 20: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

20

The UNIX File System

Some important directories found in most UNIX systems

Page 21: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

21

The UNIX File System

• Before linking.• After linking.

(a) Before linking. (b) After linking

Page 22: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

22

The UNIX File System

• Separate file systems• After mounting

(a) (b)

(a) Before mounting. (b) After mounting

Page 23: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

23

Locking Files

(a) File with one lock

(b) Addition of a second lock

(c) A third lock

Exclusive and Shared Locks: similar to read and write locks

Page 24: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

24

System Calls for File Management

• s is an error code

• fd is a file descriptor

• position is a file offset

Page 25: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

25

The lstat System Call

Fields returned by the lstat system call.

Page 26: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

26

System Calls for Directory Management

• s is an error code• dir identifies a directory stream• dirent is a directory entry

Page 27: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

27

UNIX File System

Disk layout in classical UNIX systems

Page 28: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

28

UNIX File System

Directory entry fields.

Structure of the i-node

Page 29: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

29

UNIX File System• Example: n = read (fd, buffer, nbytes)• When kernel gets control, all it has is just 3 parameters. It

has to locate the i-node. Also store the file position.• One of the internal tables: file descriptor array• One possibility: have a pointer to the i-node at the file

descriptor and keep file position in the i-node. FAILS (?)• Another possibility: keep file position in the file descriptor

array. What happens if a child process modifies the file?• Introduce a new table, the open file description between the

file descriptor table and the i-node table.

Page 30: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

30

UNIX File System

The relation between the file descriptor table, the open file description

Page 31: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

31

UNIX I/O System• All I/O devices are made to look like files and are accessed

with the same read/write system calls.• UNIX integrates I/O devices to the file system as special

files. E.g. /dev/hd1 (hard disk), /dev/lp1 (printer), /dev/net.• Programs can read/write special files the same way with

regular files. E.g. cp account.txt /dev/lp1• Special files:

– block: sequence of numbered blocks (e.g. disks)

– character: input or output character streams (e.g. keyboard, printer)

Page 32: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

32

Networking

• Sockets are analogous to mailboxes or wall sockets and are used for networking• Sockets are created (returns a file descriptor) and destroyed dynamically• Each socket supports:

– reliable connection-oriented byte stream (equivalent of a pipe between 2 processes)– reliable connection-oriented packet stream (preserves packet boundaries)– unreliable packet transmission (access to the raw network – no guarantees)

• Before a socket can be used, it must have an address

Page 33: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

33

Terminal Management

The main POSIX calls for managing the terminal

Page 34: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

34

UNIX I/O

Some of the fields of a typical cdevsw table

Page 35: 1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.

35

UNIX I/O

The UNIX I/O system in BSD