Search This Blog

Friday, March 19, 2010

Main Features of UNIX

Main Features of UNIX

  • multi-user
    more than one user can use the machine at a time
    supported via terminals (serial or network connection)
  • multi-tasking
    more than one program can be run at a time
  • hierarchical directory structure
    to support the organisation and maintenance of files
  • portability
    only the kernel ( <10%) written in assembler
    tools for program development
    a wide range of support tools (debuggers, compilers)

The UNIX Operating System Consists of

  • kernel
    schedules tasks
    manages data/file access and storage
    enforces security mechanisms
    performs all hardware access
  • shell
    presents each user with a prompt
    interprets commands types by a user
    executes user commands
    supports a custom environment for each user
  • utilities
    file management (rm, cat, ls, rmdir, mkdir)
    user management (passwd, chmod, chgrp)
    process management (kill, ps)
    printing (lpr) 
Multi-User Operating Systems

A multi-user operating system allows more than one user to share the same computer system at the same time. It does this by time-slicing the computer processor at regular intervals between the various users.
In the above example, there are five users which share the processor hardware and main memory on a time basis. 

We start off by giving the first user (which we will call Sophia) the processor hardware, and run Sophia's program for 1/5th of a second. When the time is up, we intervene, save Sophia's program state (program code and data) and then start running the second users program (for 1/5th of a second). 

This process continues till we eventually get back to user Sophia. To continue running Sophia's program, we restore the programs code and data and then run for 1/5th of a second. 

This switching between user programs is done by part of the kernel. To switch from one program to another requires,
  • a regular timed interrupt event (provided by a clock)
  • saving the interrupted programs state and data
  • restoring the next programs state and data
  • running that program till the next timed interrupt occurs
Main system memory is divided into portions for the operating system and user programs.
main memory
As you can see in the above diagram, the kernel space is kept separate from user programs.

In UNIX systems, each user is presented with a shell. This is a program which displays the users prompt, handles user input and displays output on the terminal. The shell program provides a mechanism for customizing each user's setup requirements, and storing this information for re-use (in a file called .profile).

When the UNIX system starts up, it also starts a system process which monitors the state of each terminal input line. Whenever a user logs in and their password is validated the UNIX system associates the shell program with that terminal.

Multi-Tasking OperatingSystems

Multi-tasking operating systems permit the use of more than one program to run at once. It does this in the same way as a multi-user system, by rapidly switching the processor between the various programs.

OS/2 and Windows 95 are examples of multi-tasking single-user operating system. UNIX is an example of a multi-tasking multi-user operating system. 

A multi-user system is also a multi-tasking system. This means that a user can run more than one program at once, using key selection to switch between them. 

Multi-tasking systems support foreground and background tasks. A foreground task is one that the user interacts directly with using the keyboard and screen. A background task is one that runs in the background (it does not have access to the keyboard). Background tasks are usually used for printing or backups. 

The role of the operating system is to keep track of all the programs, allocating resources like disks, memory and printer queues as required.

 UNIX Shells



The UNIX Shell is the users interface to the UNIX operating system.
  • It enables users to run application programs
  • It is run when a user logs into a UNIX host
Features of the UNIX shell

  • interactive

    The shell presents a prompt and waits for the user to enter a command. After the return key is pressed, the shell processes the command and when the command is finished, the shell re-displays the prompt. This process continues until the user exits the shell, by typing exit or by pessing ctrl-d, at which time the user is logged out of the UNIX host. 

  • runs programs in the background

    Non-interactive tasks which do not require keyboard input or display output can be run in the background as a separate task. The user continues working with other inter-active programs. Examples of this are printing or sorting files. 
  • input/output redirection

    Programs designed to use the standard input device (keyboard) and standard output device (display) can have their input and output devices redirected to other devices. For example, a program which generally reads from the keyboard can be redirected to read from a file instead. A program which writes its output to the display can be instructed to redirect its output to the printer or a file. 
  • programs can be chained or connected together via pipes

    The output of one program can be fed directly into another program by connecting the two programs via a pipe. This allows a user to create powerful new commands by chaining existing commands together. 
  • wild-card characters are supported in filenames

    The handling of files is simplified by using wild-card characters to match files which match particular patterns. Common operations can thus be performed on a group of common files using a single command. 
  • script files

    A number of commonly used commands can be stored in a file, which when executed, runs each command as though it has been typed from the command line. A sequence of commands can be executed by executing the file which contains the command. This simplifies repititious commands. 
  • environment variables

    The user can customize and control the behavior of the shell by using special variables that the shell supports. The variables can also be used by application programs and shell script files to control their behavior. An example of a shell variable is the prompt string used to display the shell prompt sign ($). 
  • macro language

    The shell supports a simple language definition for creating shell script files. These can be used to generate very complex command sequences.
Unix Command Syntax

  • all commands have a similar format
  • commands are generally two to five characters long
  • commands are case sensitive
  • options always preceed filenames
  • options are prefixed using a - symbol
  • the man command can be used to display the correct syntax


UNIX Standard Devices

  • there are THREE standard devices supported by the UNIX shell
  • these devices may be redirected to another device or file
  • standard output (stdout) is associated with the users terminal display
  • standard input (stdin) is associated with the users terminal keyboard
  • standard error (stderr) is associated with the users terminal display
When a program is executed, it has associated with it each of three standard UNIX devices.


DeviceRedirection
  • the standard devices may be redirected to a device or file
  • other devices can be printers, terminals
  • devices appear as files in the /dev subdirectory
  • the symbol > changes the standard output device
  • the symbol < changes the standard input device

PIPES
  • allow commands to be combined in a sequential order
  • connects stdout of one program to the stdin of the next program
  • the symbol | (vertical bar) represents a pipe
  • any number of commands can be connected in sequence, forming a pipeline
  • all programs in a pipeline execute at the same time
  • complex operations are easily perfomed by piping commands
WildCard Characters

  • used to match characters in filenames
  • the asterisk * matches any sequence of zero or more characters
  • the question mark ? matches exactly one character
Running Programs In The Background
  • lets the user carry on with more important tasks
  • examples are printing and formatting documents
  • the ampersand symbol (&) is appended to the command
  • the shell assigns a process number (pid) to the command
  • background jobs can be deleted using the kill command
netscape &
[1] 456

Choosing your shell
There are many shells out there, a subset of those are available on your machine. The most common ones are sh, csh, tcsh, ksh, bash, and zsh.

Sh is the bourne shell. This is one of the first shells ever written and hasn't changed much since it was written. Sh is very simple and doesn't have the features that a moderm day shell has. Bash or the Bourne again shell is a modern day implementation of sh. It has job control, it keeps a history and you cna rerun the previous command but hitting the up arrow. Bash by default mimics the emacs keys. Searching as well as moving around the line use the same keystrokes. This is why most programmers use bash as their shell.

Ksh is very similar in syntax and features as bash however ksh is not free while bash is free. Bash is licensed under the Free Software Foundation. Ksh by default uses the vi keystrokes however modern implementations allow either. 

csh and tcsh are both based on the c language and makes writing shell scripts easier if you know the language. Both have similar features with tcsh having a few more.
Zsh is the Z shell. It most closely resembles ksh but includes many enhancements. Zsh has comand line editing, builtin spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and a host of other features.

Which shell is right for you. Well that depends on your system. If you don't have the option of the shell, then you are out of luck. Otherwise I suggest starting with bash. You can try out any shell by just typing the shell name. That will switch you to that shell until you type exit, which will bring you back to your previous shell. To change your shell permantly use the chsh (shell) where shell is the path to the shell you want to use like chsh /bin/zsh.



The UNIX File System


The file system refers to the way in which UNIX implements files and directories. In UNIX, a file system has the following features,
  • hierarchical structure (support for directories)
  • files are expandable (may grow as required)
  • files are treated as byte streams (can contain any characters)
  • security rights are associated with files and directories
    (read/write/execute privledge for owner/group/others)
  • files may be shared (concurrent access)
  • hardware devices are treated just like files
What is a file?
A file is a collection of information, which can be data, an application, documents; in fact, under UNIX, a file can contain anything. When a file is created, UNIX assigns the file a unique internal number (called an inode). 
What is a file Link?
A file link is a directory entry which points to an original file somewhere else. A link is made to an existing file using the ln command. 

This creates a directory entry which points to the existing file (it does not make a copy of the existing file). This allows more than one reference to an exisiting file.

What are the security rights associated with files and directories?
UNIX provides three sets of security rights for each file and directory entry. These sets are for the owner, the group to the owner belongs, and any other
users on the system.

Each user belongs to a group (only one at a time). Group membership facilitates the sharing of common files. A user can change their membership to another group by using the newgrp command.
The security rights are
  • read (read, display, copy the file contents)
  • write (modify and append to the file contents)
  • execute (run the file as a program)
The security bits are grouped as a series of three bits for each of the owner, group and other access rights. 

Files are listed using the ls command.

Security rights for a file or directory are modified by using the chmod utility. The owner of the file is changed using the chown command. The group to which the file belongs is changed using the chgrp command. 

Associated with each file,UNIX keeps the following information.
  • location of the file (location of first 10 block numbers)
  • size in bytes
  • link count
  • ownership
  • security bits
  • type (directory or file)
  • creation date and time
  • modification date and time
  • last accessed date and time
Below is an applet that demonstrates the use of files and directories. The applet is not a shell so it can not do everything that a shell can. It is meant as a tool to visualize how directories are structured. A directory tree is like a normal tree except it is upside down. The root of the tree is "/". As shown below "/" is the only directory currently available. You can think of it as an expandable folder. The folder can hold tons of files and it can also expand and hold other folders. So you can have a folder with files and folders in it.
The following commands are available:

  • mkdir - makes a director

  • mkdir directory_name

    A directory is an entry in the parent directory telling it that this child node will hold other files/directories. All directories are created with two files in them. The files are "." and ".." pronounced dot and dot-dot. These files are hidden files as well as any other file begining with a ".". The dot file is the actual directory entry. It is like a place holder. It points to a record on disk which holds information about the directory. The information that it holds includes the ownership, permissions and date and timestamps of the directory. The dotdot file points to the parents record. 
  • rmdir - remove an empty directory
    An empty directory has no files or directories in it.
    rmdir directory_nam
  • cd
    Allows you to change which folder(directory) you are currently in.
    cd new_directory
    The point of cd is convenience. You can reference every file by its full path for instance the file if you wanted to make the following hierarchy: root folder contains a folder called classes which contains a folder for cs1007. The mkdir command would be mkdir /classes, followed by mkdir /classes/cs1007. However you could have typed in mkdir classes, cd classes, mkdir cs1007. You can access all files either with the absolute path i.e a path beginning with a / or a relative path.
  • pwd (print working directory)

    When a user logs in to a UNIX system, they are located in their own directory space. The pwd (print working directory) command displays the pathname of the current directory you are in. This is helpful when you want to know exactly where you are.

    pwd
  • touch
    Creates an empty file
    touch filename

    There are many ways to create a file. You can use a text editor, you can use shell redirection, copy a file. For this applet I only implemeted the touch command. The command create an empty file. The point of it is to illustrate files and how what role they play in the directory structure.
  • mv

    Move a file/directory to a new location or alternatively rename a file/directory
    mv old_directory_name new_directory_name

    A file and a directory are very similar in their attributes. Both have a name attribute. The mv command takes the file or directory and places it in its new location. Sometimes the location is the same, the only difference is the name of the file/directory. So this command acts both like a rename and a move command.
  • cp (copy)

    This command stands for copy, and is used for copying one file to another.
    cp .profile temp2
    The above makes a copy of the .profile file and names it temp2
  • rm (remove)

    The rm utility is used for erasing files and directories. rm temp2

    This removes the file. Once a file is removed, it cannot be restored. To cover situations where mistakes might occur, a switch -i appended to this command will request a Yes or No response before deleting the file.
  • ls (list files)

    This command is similar to DIR in DOS; it displays a list of all the files in the directory. A new user does not have many files in their home directory.
    Just like DOS, UNIX systems support hidden files. A hidden file in unix is any file beginning with a "."

    UNIX systems extend the power of commands by using special flags or switches. These switches are one of the most powerful features of UNIX systems. Switches are preceded with a "-" symbol.
    The two most common switches that you will use is the -a and -l switch. The switch l stands for long listing, and the a switch is for all files, including directories and hidden files.
  • chmod
    Changes the permissions on the file
    chmod permission filename
  • chgrp
    Changes the group associated with the filename
    chgrp group filename
  • exit
    Exits the applet
    exit
    Try typing in the following commands to see how the directory tree is formed. Play around with other commands as well:
    mkdir /usr
    mkdir /usr/bin
    cd /usr
    mkdir local
    cd local
    pwd
    mkdir bin
    mkdir /usr/local/gnu
    touch /usr/bin/bash
    ls -l /usr/bin/bash
    chmod 124 /usr/bin/bash
    chgrp foo /usr/bin/bash
    ls -l /usr/bin/bash
    rmdir /usr/local/gnu
    cd /usr/bin
    mv bash ksh
    mv ksh /usr
    cp /usr/ksh /bin/bash
    rm /usr/ksh


    You'll notice that the current directory that you are in is highlighted in red while the other directories are black. If you browser is java 1.2 capable then the following applet will work Otherwise either download the tar file or cd into ~sauce/html/tutorial/java.

     

    Editors and Viewers

    There are various editors available on UNIX systems. An editor is a program that allows you to modify text files. There are three editors that we will go over in this tutorial. Pico, vi, and emacs. There are several ways to view files without editing them. Less, more, cat, head, and tail are the most common. 

    Viewers
    less and more are almost identical except that less comes from the Free Software Foundation and really has more capabilities than more. So remember less is more. Both of these view the file one screen at a time. To use them type either less or more at the command prompt. 
     
    Cat prints out the entire file without any consideration for how many lines it can actually display on your screen. Head and tail show the first (head) n lines or the last(tail) n lines of your file. n is 10 by default but you can cahnge that with the -n # option. To use these type tail or head with the optional -n # filename at the command prompt.

    Pico


    Pico, the default UNIX text editor on the CUNIX Cluster, provides a user-friendly interface easily learned during a first encounter. Pico's editing features include block cut and paste, inserting and exporting files, paragraph justification, case-insensitive searching, and a "file browser," which provides a convenient format for directory maintenance, as well as switching back and forth between editing a file and viewing or importing other directories and files. 
     
    To start Pico, type pico at the UNIX prompt. This will open an editing window, or "buffer," with a menu of commands at the bottom. This menu appears in every window, obviating the need to memorize commands or consult a manual. Pico's "buffer" functions similar to a word-processing file; you can enter new text into it, insert other files, save it as a new file, or export it. To create a new text file in Pico, simply type the new text in the open buffer window, just as you would for a word-processing program.

    Pico commands are composed of the control key (^) followed by a character. For example, to display a help file with the ^G (Get Help) command, you would press the control key while typing lowercase letter g.
    Following is a list of Pico editing commands:


    Help
    • ^G -- Get Help: display help text from any screen. 
    •  
    Moving Between Pages
     
    • ^Y -- Prev pg: move backward a page, or screen.
    • ^V -- Next pg: move forward a page, or screen. 
    •  
    Moving the Cursor
    (The "arrow keys" can also be used to move the cursor on some keyboards.)
    • ^C -- Cur Pos: show current cursor position.
    • ^F -- Move cursor Forward a character.
    • ^B -- Move cursor Backward a character.
    • ^P -- Move cursor to the Previous line.
    • ^N -- Move cursor to the Next line.
    • ^A -- Move cursor to the beginning of text line.
    • ^E -- Move cursor to the End of text line. 
    Saving and Exiting
    • ^O -- WriteOut: save buffer/file.
    • ^X -- EXit Pico and save buffer or file. 

    Editing Text
    • ^W -- Where is: search for text.
    • ^R -- Read File: open and insert an existing file at the current cursor position.
    • ^D -- Del Char: delete character at cursor position.
    • ^K -- Del Line: delete/Kill the entire line of text at the cursor position, or cut selected text.
    • ^U -- UnDel Line: undelete last deleted line[s] at cursor position; paste or insert cut text at cursor position.
    • ^^ -- Select/unselect text: use to indicate beginning and end of selected text.
    • ^T -- Justify the current paragraph, delimited by blank lines or indentation.
    • ^I -- Insert a tab at the current cursor position.

    Using Pico's "File Browser" to Access UNIX Directories and Files

    Pico's "file browser" provides an easy way to search for, view, copy, edit, move, or rename directories and files in your CUNIX account and other locations. Typing the command ^R (Read file) and then ^T (To files) brings you into the file browser window and opens a directory of the files in your CUNIX account. Using the file browser G (Goto Dir) command allows you to access directories and files in other locations. If you know the name and location of the file you would like to open, you can type it following the "insert file" prompt at the bottom of the window opened by the ^R
    command. File browser commands are executed as individual letter keystrokes:
    • ? -- Help: display file browser help
    • space-bar -- Fwd Pg: page forward
    • - -- Back Pg: page backward
    • W -- Where is: find a file or directory
    • M -- Make Copy: copy highlighted file
    • G -- Goto Dir: to different directory
    • S -- Select: select highlighted file for insertion into open buffer or file
    • D -- Del File: delete highlighted file
    • E -- Exit Brwsr: exit the file browser

    Emacs


    Emacs is a very powerful text editor which you can use to create or edit text files. To start Emacs, type emacs at the UNIX $ prompt. Editing commands are issued by first pressing the Escape key (shown below as M) followed by a command character or by holding down the Control key (shown below as C) and typing a command character.
    Glossary

    C-A control character. C-f means hold down the key marked Control or CTRL and then type f
    M-A two-character command sequence where the first is the key marked ESC. M-f means press ESC, then f.
    M-x stringA command obtained by typing in its name. M-x revert-buffer means type ESC then x then revert-buffer then press the Enter or Return key.
    pointEmacs term for cursor position in current buffer
    markAn invisible text marker in the file

    Help and Information Functions
    C-h tDisplay the Emacs tutorial (for novices)
    C-h iEnter Info, the internal documentation browser
    C-gAbort the current command (quit)
    C-h mDescribe special Emacs commands that are currently available
    C-h kDescribe a particular command (for example, C-h k C-a describes the C-a command)
    C-h lShow the last 60 characters and commands typed
    C-_Undo effect of the last change/command (can be repeated)
    C-h a stringShow all commands containing string in their name
    M-nUsed before a command, repeats it n times (same as C-u n)
    C-qInsert special characters (for example, typing C-q C-l inserts C-l)
    C-x dDisplay and edit directory lists (C-h m shows commands)

    Getting out
    C-x C-sWrite this buffer to disk (overwrites old file contents)
    C-x C-w stringWrite this buffer to a disk file with specified name
    C-x C-zExit to the shell (can be continued later)
    C-x C-cExit and kill this Emacs (cannot be continued later)
    M-x shellStart up a UNIX shell in its own buffer

    Buffer Operations
    C-x C-f fileGet or create file named file in buffer of same name
    C-x b fileSelect existing buffer named file for editing
    C-x k fileDelete buffer named file
    C-x C-bDisplay the list of available buffers
    M-<Move to the top of the current buffer/file
    M->Move to the end of the current buffer/file

    Character Operations
    C-bMove point one character to the left (backward)
    C-fMove point one character to the right (forward)
    C-pMove point up one line (previous line)
    C-nMove point down one line (next line)
    DELETEDelete character to the left of point (also marked RUBOUT)
    C-dDelete character to the right of (or under) point
    C-tTranspose two characters around point (for example, ht becomes th)

    Word Operations
    M-bMove point one word to the left (backward word)
    M-fMove point one word to the right (forward word)
    M-DELETEDelete one word to the left (C-y yanks it back at point)
    M-dDelete one word to the right (C-y yanks it back at point)
    M-tReverse words around point (for example, if only becomes only if)

    Line Operations
    C-aMove point to the beginning of this line
    C-eMove point to the end of this line
    C-oOpen up blank line at point
    C-x C-oClose up all blank lines above and below point
    C-kDelete from point to end of line (C-y yanks it back)
    M-0 C-kDelete from beginning of line to point (C-y yanks it back )
    Sentence Operations
    M-aMove point to the beginning of this sentence
    M-eMove point to the end of this sentence
    C-x DELETEDelete beginning of sentence to point (C-y yanks it back)
    M-kDelete from point to end of sentence (C-y yanks it back)
    Paragraph Operations

    M-{Move point to beginning of this paragraph
    M-}Move point to end of this paragraph
    M-qFill this paragraph
    M-1 M-qJustify this paragraph
    M-n C-x f Set fill column to n (for example, M-60 C-x f sets it to 60)
    Screen Operations

    C-vMove to next screen of text (screens overlap by one line)
    M-vMove to previous screen (overlaps by one line)
    C-lRedisplay current screen and center point
    Page Operations

    C-x [Move to beginning of this page (previous C-l character)
    C-x ]Move to end of current page (next C-l character)
    Search and Replace

    C-s stringSearch forward for string, one character at a time
    C-r stringSearch backward for string, one character at a time
    M-x replace-string string1 string2Replace string1 with string2 (prompts for strings)
    M-x query-replace string1 string2 Optionally replace string1 with string2, but confirms each
    Region Operations
    A region is the area of buffer between the point and mark.
    Some commands set the mark, so check them before using.

    C-@Set the mark
    C-x C-xExchange point and mark (move to other end of region)
    C-wDelete region (C-y yanks it back)
    C-yYank back the last deleted text at point
    Window Operations

    C-x 2Split screen into two windows (same buffer in each)
    C-x 1Make this window the only one (delete all others)
    C-x oMove to other window on screen
    ESC C-vDisplay the next screen in the other window
    Although it may seem that emacs is a lot more difficult to learn, you will be amazed at how easy and powerful the editor is. It has special modes for writing different types of files such as Java, C, HTML, text, and many other types of files. It is a lot more useful and customizable.

    vi


    No discussion of editors would be complete without the mention of vi. Vi is one of the first editors available on UNIX. It is very compact and quick to load. The editor has two modes, a command mode and a text entry mode. The default mode is the command mode. 
     
    Enter text mode insert i (use ESC to return to command mode)
    Delete character x
    Delete worddw
    Delete linedd
    Save and exit:x
    Quit and abandon:q!
    The vi editor is invoked by typing vi filename at the UNIX prompt.

No comments:

Post a Comment