Medium 9789381159057

Unix and Shell Programming

Views: 326
Ratings: (0)
This book, designed to meet the course on UNIX and SHELL Programming for graduate and undergraduate engineering students, presents an illuminative and objective exploration of algorithms and programming skills on UNIX and SHELL. The authors discuss all the concepts such as operating system services, file system, buffer cache, reading and writing disk blocks, inode assignment to a new file, system calls, structure and control of a process, inter-process communication, and sockets with the help of illustrations, tables, block diagrams, and industry based practical examples.

List price: $24.95

Your Price: $19.96

You Save: 20%

Remix
Remove
 

14 Slices

Format Buy Remix

Ch-1

PDF

2

U NIX

AND

S HELL P ROGRAMMING

Kernel → This is the name of UNIX Operating System.

Shell → This is the command interpreter used in unix.

Application programs and utilities —Utilities are UNIX commands. Application programs such as word processor, spreadsheets and Database Management Systems may be installed along side the linux (or UNIX) commands. A user may run a utility or Application Programs through shell.

User

(i)

Ordinary user → Work only their own working directory.

(ii)

Super user → Super user have the command on entire system. They can add user.

Remove user etc.

1.2 TYPES OF SHELL

(i)

(ii)

Bourne Shell—It was created by Steve Bourne probably that’s why its bounded with every UNIX system. Because it was bounded with every system it became popular.

Whatever the cause and the effect, the fact remains that this is the shell used by many

UNIX users.

C Shell—It was created by “Bill Joy” then pursuing his graduation at the University of California at Berkeley. This shell is a hit with those who are seriously into UNIX programming. It has two advantage over Bourne Shell.

 

Ch-2

PDF

Chapter

2

OPERATING SYSTEM SERVICES

The Kernel performs various primitive operations on behalf of user process to support the user interface. Among the services provided by the Kernel are:

(i)

Controlling the execution of process by allowing their creation, termination or suspension and communication.

(ii)

Scheduling process fairly for execution on the CPU. Processes share the CPU in a time shared manner. The CPU executes a process, the Kernel suspend it when its time quantum elapses and the Kernel schedules another process to execute. The Kernel later reschedules the suspended process.

(iii)

Allocating main memory for an executing process.

(iv)

Allocating secondary memory for efficient storage and retrieval of user data. This service constitutes the file system.

(v)

Allowing processes controlled access to peripheral devices such as terminals, tape driver, disk driver and N/W devices.

2.1

ASSUMPTION ABOUT H/W—LEVEL OF UNIX SYSTEM

The execution of user processes on UNIX systems is divided into two levels:

 

Ch-3

PDF

12

U NIX

AND

S HELL P ROGRAMMING

Chapter

3

FILE SYSTEM

After the disk has been partitioned its still not ready for use. A file system has to be created in each partition. There usually are multiple file systems in one m/c each one having its own directory tree headed by root.

Every file system is organized in a sequence of blocks of 512 bytes each. (1024 in Linux) and will have there four components:

Boot Block

Super Block

Inode Blocks

Data Block

3.1 FILE SYSTEM LAYOUT

1.

Boot Block—This block contains a small boot strap program. This is the Master Boot

Record (MBR) that DOS users would like to call it. This is loaded into memory when the system is booted. It may in turn load another program from the disk but eventually it will load the Kernel (the file/stand/unix or/vm linuz) into memory.

2.

Super Block—It describe the state of file system (balance sheet of every UNIX file system). The Kernel first reads this area before allocating disk blocks and inodes for new files. The super block contains global file information about disk usage and availability of data blocks and inodes. It contains:

 

Ch-4

PDF

Chapter

4

BUFFER CACHE

When a process want to access data from a file, the Kernel brings the data into main memory where the process can examine it alter it and request that the data be saved in the file system again. The super block of a file system describes the free space available on the file system. The

Kernel reads the super block into memory to access its data and writes it back to the file system when it wishes to save its data. Similarly the inode describes the layout of file. The Kernel reads an inode into memory when it want to update the file layout.

The Kernel could read and write directly to and from the disk for all file system access, but system response time and throught put would be poor because of the slow disk transfer rate. The

Kernel therefore minimize the frequency of disk access by keeping a pool of internal data buffers, called the buffer cache which contains the data inrecently used disk blocks.

The position of buffer cache module in Kernel architecture between the file subsystem and

 

Ch-5

PDF

READING

AND

W RITING DISK B LOCKS

Chapter

READING

31

5

WRITING

DISK BLOCKS

AND

To read a disk block a process uses an algorithm to get a Block search for it in the buffer cache. If its in the cache, the Kernel can return it immediately without physically reading the block from disk. If its not in cache, the Kernel calls the disk driver to schedule a request and goes to sleep awaiting the event that the I/O completes. The disk drivers notifies the disk controller h/w that it want to read data and the disk controller later transmits the data to buffer. Finally the disk controller interrupts the processor when the I/O is complete and the disk interrupt handler awakens the sleeping process: the contents of the disk block now in the buffer.

5.1

DISK CONTROLLER

Several physical disk units are managed by a single disk controller device. The controller in turn is connected to an I/O channel that transmits information from disk unit to the central computer one channel might support several disk. Controller each of which might, in turn, support several disk drives.

 

Ch-6

PDF

42

U NIX

AND

S HELL P ROGRAMMING

Chapter

INODE A SSIGNMENT

TO A

6

NEW F ILE

The file system contains a linear list of inodes. An inode is free if its type field is zero. When a process needs a new inode, the Kernel could theoretically search the inode list for a free node.

However, such a search would be expensive, requiring atleast one read operation for every inode.

To improve performance, the file system super block contains an array to cache the numbers of free inodes in the file system.

For assigning new inodes. The Kernel first verifies that no other processes have locked access to the super block free inode list. If the list of inode numbers in the super block is not empty the

Kernel assigns the next inode number, allocates a free incore inode for the newly assigned disk inode copies the disk inode to the incore copy initilizes the fields in the inode and returns the locked inode. It updates the disk inode to indicate that the inode is now in use. A non zero file type field indicates that the disk inode is assigned.

 

Ch-7

PDF

50

U NIX

AND

unlock

S HELL P ROGRAMMING

(inode);

return (user file descriptor);

}

The Kernel searches the file system for the file name parameter. It checks permission for opening the file after it finds the incore inode and allocates an entry in the file table for the open file. The file table entry contains a pointer to the inode of the open file and a field that indicates the byte offset in the file where the Kernel expects the next read or write to begin. The Kernel initializes the offset to O during the open call, meaning that the initial read or write starts at the begining of a file by default. The entry in the user file table points to the entry in the global file table.

User file

Descriptor table

Inode table

File table

0

1

2

3

4

5

6

7

Count

2 (etc/passed) count Read

Count

1

Rd-wrt

Count

1 (local)

count 1 write

Fig. 7.1 Data structures after open

fd1 = Open (“/etc/passwd”, O – RDONLY); fd2 = Open (“local”, O – RDWR); fd3 = Open (“/etc/passwd”, O – WRONLY);

The first three user file description (0, 1, 2) are called the standard i/p standard o/p and standard error file descriptors.

 

Ch-8

PDF

70

U NIX

AND

S HELL P ROGRAMMING

Chapter

STRUCTURE

OF A

8

P ROCESS

The Kernel contains a process table with an entry that describes the state of every active process in the system. The u-area contains additional information that controls the operation of a process. The process table entry and the u-area are part of the context of a process. The aspect of the process context that most visibly distinguishes it from the context of another process is of course, the contents of its address space.

8.1 PROCESS STATES AND TRANSITIONS

The following list contains the complete set of process states.

(i)

The process is executing in user mode.

(ii)

The process is executing in Kernel mode.

(iii)

The process is not executing but is ready to run as soon as the Kernel schedules it.

(iv)

The process is sleeping and resides in main memory.

(v)

The process is ready to run, but the swapper (schedule process O) must swap the process into main memory before the Kernel can schedule it to execute.

(vi)

The process is sleeping and the swapper has swapped the process to secondary storage to make room for other processes in main memory.

 

Ch-9

PDF

P ROCESS C ONTROL

Chapter

83

9

PROCESS CONTROL

The fork system call creates a new process, the exit call terminates process, execution and the wait call allows a parent process to synchronize its execution with the exit of a child process.

Signals inform processes of a synchronous events. Because the Kernel synchronizes execution of exit and wait via signals.

9.1

PROCESS CREATION

To create a new process in the UNIX operating system is to invoke the fork system call. The process that invokes fork is called the parent process, and the newly created process is called the child process. The syntax for the fork system call is: pid = fork ();

On return from the fork system call the two processes have identical copies of their user level context except for the return value pid. In the parent process pid is the child process ID; in the child process, pid is O. Process O created internally by the Kernel when the system is boated, is the only process not created via fork.

The Kernel does the following sequence of operations for fork:

 

Ch-10

PDF

90

U NIX

AND

S HELL P ROGRAMMING

Chapter

10

INTER -PROCESS COMMUNICATION

IPC mechanism allow arbitrary process to exchange data and synchronize execution.

We have already considered several forms of interprocess communication, such as pipes, named pipes and signals. Pipes suffer from the drawback that they are known only to processes which are descendants of the process that invoke the pipe system call: unrelated process cannot communicate via pipes. Although named pipe allow unrelated process to communicate, they cannot generally be used across a network nor do they readily lend themselves to setting up multiple communications paths for different sets of communicating process: its impossible to multiplex a named pipe to provide private channels for pairs of communicating process. Arbitrary process can also communicate by sending signals via the kill system call, but the message consist only of the signal number.

10.1 PROCESS TRACING

One process traces and controls the execution of another process. Tracing processes, useful for debugging. A debugger process such as sdb, spawns a process to be traced and controls its execution with the ptrace system call, setting and clearing break points, and reading and writing data in its virtual address space.

 

Ch-11

PDF

SOCKETS

97

11

Chapter

SOCKETS

Furthermore, the methods may not allow processes to communicate with other processes on the same m/c because they assume existence of a server process that sleeps in a driver open or read system call. To provide common methods for IPC and to allow use of sophisticated n/w protocols, the BSD system provides a mechanism known as sockets.

Client Process

Server Process

Socket layer

Protocol layer

Socket layer

TCP

Protocol layer

IP

Device layer

TCP

IP

Device layer

Ethernet

Driver

Ethernet

Driver

N/W

Fig. 11.1 Sockets model

The Kernel structure consists of three parts:

(i)

Socket Layer—Provide the interface b/w the system calls and the lower layers.

(ii)

Protocol Layer—Contains the protocol modules used for communication (e.g., TCP/IP).

(iii)

Device Layer—Contains the device driver that control the n/w devices.

Layer combinations of protocols and drivers are specified when configuring the system.

Process communicate using the client server model; a server process listens to a socket one end point of a two way communication path, and client process communicate to the server process cover another socket, the other end point of communications path which may be on another m/c. The Kernel maintains internal connections and routes data from client to server.

 

Ch-12

PDF

U NIX C OMMAND 103

Chapter

12

UNIX COMMAND

12.1 INTRODUCTION TO SHELL

The Shell is a program that provides an interpreter and interface between the user and the

UNIX Operating System. It executes commands that are read either from a terminal or from a file.

Files containing commands may be created, allowing users to build their own commands. In this manner, users may tailor UNIX to their individual requirements and style.

There are a number of different Shells. Each provides a slightly different interface between the user and the UNIX Operating System. There are three important types of shell in UNIX these are:

(i)

Bourne Shell

(ii)

C-Shell

(iii)

Korn Shell

There are other shells that are less widely used and not available on many machines. A command issued by a user may be run in the present shell, or the shell itself may start up another copy of itself in which to run that command. In this way, a user may run several commands at the same time. A secondary shell is called a sub-shell.

When a user logs onto the system, a shell is automatically started. This will monitor the user’s terminal, waiting for the issue of any commands. The type of shell used is stored in a file called passwd in the subdirectory etc. Any other shell may be run as a sub-shell by issuing it as a command. For example, /usr/bin/ksh will run a Korn shell. The original shell will still be running—in background mode—until the Korn shell is terminated.

 

Ch-13

PDF

128

U NIX

AND

S HELL P ROGRAMMING

13

Chapter

AWK

AND

P ERL P ROGRAMMING

13.1 INTR

ODUCTION TO A

WK

INTRODUCTION

AWK awk is a simple and elegant pattern scanning and processing language. I would call it the first and last simple scripting language. awk is a little programming language, with a syntax close to C in many aspects. It is an interpreted language and the awk interpreter processes the instructions.

About the syntax of the awk command interpreter itself: awk is also the most portable scripting language in existence. It was created in late 70th of the last century almost simultaneously with Borne shell. The name was composed from the initial letters of three original authors Alfred V. Aho, Brian W. Kernighan, and Peter J. Weinberger.

It is commonly used as a command-line filter in pipes to reformat the output of other commands.

It’s the precursor and the main inspiration of Perl. Although originated in Unix it is available and widely used in Windows environment too. awk takes two inputs: data file and command file. The command file can be absent and necessary commands can be passed as augments. As Ronald P. Loui aptly noted awk is very underappreciated language.

 

Exercise

PDF

E X E RC IS E S 183

E X E R C ISE S

1. E xp lain the system stru ctu re of U N IX op erating system .

2. D escribe the file and d irectory stru ctu re of U N IX .

3. D efine op erating system services.

4. H ow m any level u sed by a p rocess w hen p rocess is ru nning on U N IX system ? D escribe in d etail.

5. D raw the block d iagram of the system kernel. A nd exp lain its w orking.

6. W hat is the p rocess and context of a p rocess? D efine p rocess states and transition.

7. W hat shou ld hap p en? If the kernel attem p ts to aw aken all p rocess sleep ing on an event, bu t no p rocess are asleep on the event at the tim e of the w akeu p .

8. W hat is the bu ffer head er and d u ring system initialization w hy kernel allocates sp ace for a nu m ber of bu ffers. Su p p ose the kernel d oes a d elayed w rite of a block. W hat hap p ens w hen another p rocess takes that block from its hash qu eu e? From the free list?

9. In the algorithm getblk (A lgorithm for buffer allocation),if the kernel rem oves the bu ffer from the free list,it m u st raise the p rocessor p riority level to block ou t interru p ts before checking the free list. W hy?

 

Details

Print Book
E-Books
Slices

Format name
PDF
Encrypted
No
Sku
B000000054724
Isbn
9781944534370
File size
7.2 MB
Printing
Allowed
Copying
Allowed
Read aloud
Allowed
Format name
PDF
Encrypted
No
Printing
Allowed
Copying
Allowed
Read aloud
Allowed
Sku
In metadata
Isbn
In metadata
File size
In metadata