42
UNIX INTERNALS Disk and Terminal Drivers A. Sarang

Ui disk & terminal drivers

Embed Size (px)

Citation preview

UNIX INTERNALS

Disk and Terminal Drivers

A. Sarang

DEVICE DRIVERS :-

The I/O Subsystem allows the process to

communicate with the peripheral devices

such as :-

disks, terminal, tape drives, printers,

networks and kernel modules.

A disk can be partitioned into a no. of file

systems.

An administrator can leave a partition

mounted or unmounted and can make it

read only / read write.

Disk driver

A disk driver translates the file system address to the particular sector of the disk.

The file system address consists of a logical device number and a block no.

Eg : /dev/dsk0 of a DEC RP07 disk

Which means accessing section 0 of the DEC RP07 disk.

Accessing block 940 of dev/dsk3

Section 3 starts at block 336000

336000+940

336940

Accessing disk data

Using raw or block interface

Eg : lseek , ls

Using commands like mkfs & fsck.

They access the file systems directly.

Lseek sets the file offset for the open file

Mkfs : formats disk section for UNIX file system & creates the super block , inode , list , linked list of free disk blocks and root directory.

Fsck : checks the consistency of the file system and corrects the errors.

Block and raw interface

Ls –l /dev/dsk15 /dev/rdsk15

Br ----- 2 root 0,21 Feb12 15:00 /dev/dsk15

0 = Major n0.

21= Minor no.

2= physical drive

1= section no.

Block interface = read , write

Raw interface = lseek

# include “fcntl.h”

Main()

{

Char buf1[4096],buf2[4096];

Int fd1,fd2,I;

If(((fd1=open(“/dev/dk5”,O_RDONLY))==-

1)||((fd2=open(“/dev/rdsk5”,O_RDONLY))==-1))

{

Printf(“failure on open\n”);

Exit();

}

Lseek(fd1,8192L,0);

Lseek(fd2,8192L,0);

If((read(fd1,buf1,sizeof(buf1))==-1)|| (read(fd2,buf3,sizeof(buf1))==-1))

{

Printf(“failure on read\n”);

Exit();

}

For(i=0;i<sizeof(buf1);i++)

If(buf1[i]!=buf2[i])

{

Printf(“different at offset %d\n”,i);

Exit();

}

Printf(“reads match\n”);

}

From the program we infer :-

Using read & write to the disk directly is dangerous .

Jeopardizes system security.

Administrators need to put appropriate permissions for the disk device.

For eg : /dev/dsk15 should be owned by root to read but should not allow other users to read or write.

Read or write routine converts byte offset to block offset .

Use of block interface needs an extra copy of data between user address space & kernel buffers

Use of raw interface on the other hand is faster.

TERMINAL DRIVERS

Terminal Drivers are used to control the transmission of data to & from the terminals.

But terminals are also user interface to the sytem unlike disks.

Line Disciplines

Set of manipulations that input editing does.

For eg : for moving one step backward , you have BACKSPACE in some terminals and DEL in some others.

Modes of a terminal

Raw mode

Canonical mode

Raw mode sends the entire sequence typed sequence including the erase characters.

In Canonical mode , line disciple buffers the data into lines and processes erase characters before sending the revised sequence to the reading process.

Canonical mode processes edit, delete.

Example

Hello data DEL DEL DEL DEL world

RAW mode output :-

Hello data DEL DEL DEL DEL world

Canonical mode output :-

Hello world

Functions of line discipline:-

Parse the input string into lines.

Process the erase characters.

Process “kill” character that invalidates all characters typed so far on the current line.

To echo(write) received characters to the terminal.

To expand o/p such as tab character to a sequence of blank spaces

Generate signals to process terminal hangups, line breaks or response to user hitting the delete key.

To allow raw mode that does not interpret special characters such as erase,kill or carriage return.

Data structures used

Cblocks

Clists

Buffer is made of several physical entities or character blocks – Cblocks.

Clist is a variable length linked list of Cblocks.

Cblock has a pointer to the next Cblock on the linked list.

Data structures used :-

Three Clists :-

O/p Clist : Clist to store o/p of terminal

Raw i/p Clist : Clist to store raw i/p data (provided by terminal interrupt handler as the user types in).

Canonical i/p Clist : Clist to store “cooked” i/p data(after line discipline converts splcharacters )

Terminal driver in Canonical mode :-

Write :-

Terminal driver invokes the line discipline.

Line discipline loops reading o/p characters from user address space & places them on o/p clist.

If no. of characters in o/p clist is greater than the “high-water mark” , it puts the writing process to sleep.

If the amount of data is below the “low water mark” , the interrupt handler awakens all processes asleep.

Line discipline processes the spl characters .

Write operation is started with data on o/p clist.

Algorithm for write

Algorithm terminal_write

{

while (more data to be copied from user space)

{

if (tty flooded with o/p data)

{

start write operation on hardware with data on o/p clist;

Sleep;

Continue;

}

copy cblock size of data from user space to o/p clist ;

line discipline converts tab characters etc;

}

start write operation on h/w with data on output clist;

}

Read :-

If no data is available in the i/p clist the reading process sleeps until the arrival of a line of data.

Whenever data is entered , terminal interrupt handler places the data in raw clist for i/p & o/p clist for echoing back to the terminal.

If terminal is in raw mode , copy all data from raw clist to canonical clist.

If terminal is in canonical mode , copy one character at a time from raw clist to canonical clist , process the data.

Finally copy cblocks from canonical clist to user address space.

Algorithm for read

Algorithm terminal_read{

while (no data on raw clist ){

if (tty opened with no delay option)return;if (tty in raw mode based on timer & timer not

active)arrange for timer wakeup(callout table);

Sleep (event : data arrives from terminal)}

/*there is data on raw clist*/

If(tty in raw mode)

copy all data from raw clist to canonical clist

Else /*tty in canonical mode*/

{

while(characters on raw clist)

{

copy one character at a time from raw clist to canonical clist;

do erase , kill processing ;

if (char is carriage return or e-o-f )

Break;

}}}

While (characters on canonical clist and read count not satisfied)

Copy from cblocks on canonical list to user address space;

}

Terminal driver in Raw mode

In raw mode , line disclipline transmits characters exactly as the user types them: no input processing is done.

Kernel must know when to satisfy user read calls since carriage return is treated as ordinary character

Satisfying read system calls

Read system calls are satisfied after minimum no. of characters are input at the terminal.

After waiting a fixed time from the receipt of any characters from the terminal.

The kernel times the entry of characters from the terminal by placing entries in the callout table.

Algorithm is same as canonical case.

Terminal Polling

Terminal polling can be done by opening a terminal with “no delay” option & polling all of the devices.

However this leads to wasting processing power.

Terminal Polling in BSD

BSD system has a select system call for device polling.

Select (nfds,rfds,wfds,efds,timeout)

Nfds = no.of file descriptors

Rfds,wfds & efds – bit masks

Timeout = how long select should sleep.

Control Terminal

It is the terminal on which a user logs into the system .

It controls processes that the user initiates from the terminal.

Plays an important role in handling signals.

When user presses delete , break or quit keys, it sends appropriate signals to all the processes.

Indirect Terminal Driver

Unix systems provide indirect terminal access via the device file /dev/tty .

Kernel can define a special device no. for the indirect terminal file with a special entry in the character device switch table.

Allocating inode for the control terminal.

Logging in

First the get ty (get terminal process executes) and the process is set.

Open terminal line.

If open successful , exec login program & prompt for username and password.

If login successful , put the terminal in canonical mode else try again.