0% found this document useful (0 votes)
40 views31 pages

Unit 1: Unix System Overview

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

Unit 1

Unix System Overview

Unix History
Originally developed in 1969 at Bell Labs by Ken Thompson and Dennis Ritchie. 1973, Rewritten in C. This made it portable and changed the history of OS 1974: Thompson, Joy, Haley and students at Berkeley develop the Berkeley Software Distribution (BSD) of UNIX two main directions emerge: BSD and what was to become System V

Unix History
Visit following links to know more about history: http://www.unix.org/what_is_unix/history_timeline.html http://www.levenez.com/unix/

Home work
Identify different UNIX versions (some generic, some trademark, some just Unix-like)

Traditional & Modern Unix Architecture

General Unix Architecture

Unix Architecture
The architecture of UNIX is divided into three levels. On the outer crust reside the Application Programs and other utilities. At the heart of UNIX, on the other hand, is the Kernel, which interacts with actual hardware in machine language. The stream lining of these two modes of communication is done by the middle layer called Shell.

Note: Refer to : William Stallings, Operating System- Internals & Design Principles, Chapter 2: Operating system Overview.

Features Of Unix
Portability: The system is written in high-level language making it easier to read, understand, change and, therefore move to other machines. The code can be changed and complied on a new machine. Customers can then choose from a wide variety of hardware vendors without being locked in with a particular vendor. Machine-independence: The System hides the machine architecture from the user, making it easier to write applications that can run on micros, mins and mainframes. Multi-User Operations: UNIX is a multi-user system designed to support a group of users simultaneously. The system allows for the sharing of processing power and peripheral resources, white at the same time providing excellent security features.

Features Of Unix
Hierarchical File System: UNIX uses a hierarchal file structure to store information. This structure has the maximum flexibility in grouping information in a way that reflects its natural state. It allows for easy maintenance and efficient implementation. UNIX shell: UNIX has a simple user interface called the shell that has the power to provide the services that the user wants. It protects the user from having to know the intricate hardware details. Pipes and Filters: UNIX has facilities called Pipes and Filters which permit the user to create complex programs from simple programs.

Features of Unix
Utilities: UNIX has over 200 utility programs for various functions. New utilities can be built effortlessly by combining existing utilities. Software Development Tools: UNIX offers an excellent variety of tools for software development for all phases, from program editing to maintenance of software.

Basic Unix Commands


ls ls -a mkdir cd directory cd cd ~ cd .. pwd list files and directories list all files and directories make a directory change to named directory change to home-directory change to home-directory change to parent directory display current dir path

Basic Unix Commands


cp file1 file2 mv file1 file2 rm file rmdir directory cat file more file who lpr -Pprinter psfile * ? man copy file1 and call it file2 move or rename file1 to file2 remove a file remove a directory display a file display a file a page at a time list users currently logged in print postscript file to named printer match any number of characters match one character command read the online manual page for a command

Basic Unix Commands


command > file command >> file command < file grep 'keyword' file
% grep science science.txt

redirect standard output to a file append standard output to a file redirect standard input from a file search a file for keywords

wc file
% wc -w science.txt

count number of lines/words/characters in file


sort data (numerically or alphabetically)

sort

Ex: to sort the list of object, type % sort < biglist and the sorted list will be output to the screen.

General Purpose Utilities


General purpose utilities are commands with varied functions, but can be broadly divided into two categories: Some commands tell the state of the system like the current user, the date, your machine & terminal names, etc. Other can aid you directly in your work like logging your session & providing calculator service ,etc. Examples of such commands: passwd, who, w, tty, lock, clear, uname, date, cal etc.

Note: Refer to : Sumitabha Das, Your Unix, chapter -3 General Purpose Utilities.

System Calls
System calls :
It is the means by which a process requests a specific kernel service. These are entry points into kernel code where their functions are implemented. (e.g. write(2)). There are several hundred system calls, divided into six categories as: file system, process scheduling, Interprocess communication, socket(networking) & miscellaneous.

Library Functions
Library calls : These are transfers to user code which performs the desired functions. (e.g. printf(3)). The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others are very specialized in their application.

Standardization
ANSI C (X3.159-1989) C89, C9X/C99 (ISO/IEC 9899) IEEE POSIX (1003.1-2001) and SUSv3 (ISO/IEC 9945-2003) X/Open XPG3 and FIPS

Note:

Refer to - Stevens & Rago, Advanced Programming in Unix Enviornment.

Files
Unix files are organized by a hierarchy of labels, commonly known as hierarchy structure. There are three types of files. Regular Files: This contains a sequence of bytes that generally corresponds to code or data. Directory Files: Directory file contains an entry for every file and subdirectory that it is placed. Device Files: These files correspond to the printers or other devices connected to the system.

File I/O

Note:

Refer to - Stevens & Rago, Advanced Programming in Unix Enviornment.

File I/O

Introduction
functions available for file I/O
open a file, read a file, write a file, and so on. Most file I/O on a UNIX system can be performed using only five functions: open, read, write, lseek, and close.

File Descriptors
A file descriptor (or file handle) is a small, nonnegative integer which identify a file to kernel. When we open an existing file or create a new file, the kernel returns a file descriptor to the process. Traditionally, stdin, stdout and stderr are 0, 1 and 2 respectively, called magic numbers This convention is used by the shells and many applications; it is not a feature of the UNIX kernel.

File Descriptors

File Descriptors
The magic numbers 0, 1, and 2 should be replaced in POSIX-compliant applications with the symbolic constants STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. These constants are defined in the <unistd.h> header.

Standard I/O
Basic File I/O: almost all UNIX file I/O can be performed using these five functions: open(2) close(2) lseek(2) read(2) write(2) Processes may want to share resources. This requires us to look at: atomicity of these operations file sharing manipulation of file descriptors

Open Function
A file is opened or created by calling the open function. #include <fcntl.h> int open(const char *pathname, int oflag, .... /* mode_t mode */ ); Returns: file descriptor if OK, 1 on error

We show the third argument as ..., which is the ISO C way to specify that the number and types of the remaining arguments may vary. The pathname is the name of the file to open or create. This function has a multitude of options, which are specified by the oflag argument. This argument is formed by ORing together one or more of the following constants from the <fcntl.h> header:

oflag must be one (and only one) of:


O_RDONLY Open for reading only O_WRONLY Open for writing only O_RDWR Open for reading and writing

and may be ORd with any of these:


O_APPEND Append to end of file for each write O_CREAT Create the file if it doesnt exist. Requires mode argument O_EXCL Generate error if O_CREAT and file already exists. (atomic) O_TRUNC If file exists and successfully open in O_WRONLY or O_RDWR, make length = 0 O_NOCTTY If pathname refers to a terminal device, do not allocate the device as a controlling terminal O_NONBLOCK If pathname refers to a FIFO, block special, or char special, set nonblocking mode (open and I/O) O_SYNC Each write waits for physical I/O to complete

Close() Function
An open file is closed by calling the close function. #include <unistd.h> int close(int fd); Returns: 0 if OK, -1 on error closing a file descriptor releases any record locks on that file (more on that in future lectures) file descriptors not explicitly closed are closed by the kernel when the process terminates.

lseek()
Every open file has an associated "current file offset," normally a non-negative integer that measures the number of bytes from the beginning of the file. #include <sys/types.h> #include <fcntl.h> off _t lseek (int filedes, off _t offset, int whence ); Returns: new file offset if OK, -1 on error The value of whence determines how offset is used:
SEEK SET SEEK CUR SEEK END bytes from the beginning of the file bytes from the current file position bytes from the end of the file

Weird things you can do using lseek(2):


seek to a negative offset seek 0 bytes from the current position seek past the end of the file

Read()
Data is read from an open file with the read function. #include <unistd.h> ssize t read( int filedes, void *buff, size_t nbytes ); Returns: number of bytes read, 0 if end of file, -1 on error There can be several cases where read returns less than the number of bytes requested:
EOF reached before requested number of bytes have been read Reading from a terminal device, one line read at a time Reading from a network, buffering can cause delays in arrival of data Record-oriented devices (magtape) may return data one record at a time

read begins reading at the current offset, and increments the offset by the number of bytes actually read.

Write()
Data is written to an open file with the write function. #include <unistd.h> ssize t write(int filedes, void *buff, size_t nbytes ); Returns: number of bytes written if OK, -1 on error write returns nbytes or an error has occurred (disk full, file size limit exceeded). For regular files, write begins writing at the current offset (unless O_APPEND has been specified, in which case the offset is first set to the end of the file.) After the write, the offset is adjusted by the number of bytes actually written.

You might also like