Unix Course Ware Ver 1[1]

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Unix Course Ware Ver 1[1] as PDF for free.

More details

  • Words: 45,812
  • Pages: 154
UNIX

Table of Contents Preface..................................................................................................................................4 1. Operating Systems Basics ...............................................................................................5 1.1 Introduction................................................................................................................5 1.2 What is an Operating system?....................................................................................6 1.3 Functions of an Operating System ............................................................................7 1.4 Process Management.................................................................................................8 1.5 Memory Management................................................................................................9 1.6 Device (I/O) Management.......................................................................................10 1.7 File System Management.........................................................................................11 1.8 Flavors......................................................................................................................12 2. UNIX – Overview..........................................................................................................13 2.1 History .....................................................................................................................13 2.2 Flavors......................................................................................................................15 2.3 Logging In................................................................................................................16 2.4 File and Directory Structure.....................................................................................17 2.5 Process.....................................................................................................................19 3. Shell Basics....................................................................................................................22 3.1 Introduction..............................................................................................................22 Program Initiation .................................................................................................24 Input-output Redirection .......................................................................................24 Pipeline Connection ..............................................................................................25 Substitution of Filenames .....................................................................................25 Maintenance of Variables ......................................................................................25 Environment Control ............................................................................................26 3.2 Flavors......................................................................................................................27 3.3 Interaction with Shell...............................................................................................33 3.4 Features....................................................................................................................36 3.5 Program execution...................................................................................................41 3.6 Background Jobs......................................................................................................43 3.7 Batch jobs.................................................................................................................43 3.8 Input, output and pipes.............................................................................................47 4. The Command Line.......................................................................................................52 4.1 Creating and Manipulating Files and Directories....................................................52 4.2 Controlling Permissions to Files..............................................................................68 4.3 grep and find............................................................................................................79 4.4 Extracting data.........................................................................................................89 4.5 Redirection and Piping.............................................................................................93 4.6 Sorting and Comparing............................................................................................95 5. The vi editor...................................................................................................................99 5.1 Command mode.....................................................................................................101 5.2 Ex mode.................................................................................................................101 5.3 Edit mode...............................................................................................................101 6. Shell Programming......................................................................................................104 6.1 Variables.................................................................................................................104

2

6.2 Command-Line arguments.....................................................................................106 6.3 Decision-making constructs...................................................................................107 6.4 Looping Constructs................................................................................................114 6.5 Reading data ..........................................................................................................118 7. Basics of UNIX Administration...................................................................................121 7.1 Login Process and Run Levels...............................................................................121 7.2 Processes ...............................................................................................................125 7.3 Archiving and backup............................................................................................127 7.4 Security..................................................................................................................132 8. Communication............................................................................................................133 8.1 Basic UNIX mail....................................................................................................133 8.2 Communicating with other users...........................................................................137 8.3 Accessing the Internet............................................................................................139 8.4 e-mail on the Internet.............................................................................................141 9. Makefile concepts........................................................................................................143 9.1 Introduction............................................................................................................143 9.2 Using Variables in Makefile ..................................................................................146 9.3 Writing Makefiles..................................................................................................148 9.4 Sample Makefile....................................................................................................153

3

Preface This courseware is meant for beginners of UNIX Operating System. The information presented provides an overview of the UNIX operating system and adequate stuff to gain considerable working knowledge. The content is divided into nine sections to enable easy understanding. The first section provides the basics of Operating System that would help beginners who come from a non-computer science background. The structure and the functions of any operating system, in general, are discussed here. The different modules of the operating system, namely, process management, memory management, file system management, Input-Output management and the different flavors of Operating are discussed in brief. The second section gives an overview of UNIX Operating System, briefly discussing the history, flavors and the structure of UNIX. The logging in process and other basic concepts underlying the UNIX operating System are also discussed here. The concepts and features relating to the UNIX shell are discussed in the third section. This covers the variety of shells available, interacting with the shell, the features associated with the shell, executing ordinary programs, batch jobs, background jobs and the concept of input, output and pipes are also covered in this section of the courseware. The various operations carried out on the command line are discussed in the fourth section. Creating and manipulating files and directories, controlling permissions, extracting data, redirection, piping, sorting, comparing and so on forms the essence of this section. The famous vi editor is discussed along with the various modes of operation in the fifth section. The Shell Programming concepts and constructs are discussed in the sixth section. The seventh section covers the basics of UNIX administration and security aspects of UNIX Operating System. The eighth section covers connecting to other systems, basic concepts of mailing in UNIX, communicating with other users, accessing and e-mailing on the Internet. The last section discusses creating an application on UNIX platform.

4

Chapter 1 1. Operating Systems Basics 1.1 Introduction The structure of a computer system can be depicted as in the figure below. The figure shows how the operating system fits in a computer system.

Application Programs | System Programs | Operating System | Assembly Language | Micro program | Physical devices Figure 1 – Computer System Physical devices consist of IC chips, wires, power supplies, CRT and so on. Micro program is located in the ROM. The software directly controls these physical devices and provides a cleaner interface to the next layer. This could be implemented in hardware also. The micro program interprets the machine language or assembly language instructions to the devices. Assembly language is specific to the processor and it typically contains 50 to 300 instructions for doing simple operations like moving data around, doing arithmetic and comparisons. Operating system hides all the complexity and provides a clean interface to the system and application programs that reside on it. System programs are those that are loaded according to user’s convenience on top of the operating system like compilers, editors and command interpreters. Application programs are those that are written by the users to solve their problems.

5

1.2 What is an Operating system? The term Operating System denotes those system program modules within a computer system that governs the control of equipment resources such as processes, main storage, secondary storage, I/O Devices, and files; and the system modules that provide a base for all application programs such as word processors, database management systems, and so on. The Operating System can be viewed as an extended machine and also as a resource manager; the former being the top-down view and the latter being the bottom-up view.

Operating system as an extended machine (Top-down view) • • •

Hides the truth about the hardware from the programmer Provides a nice, simple view of named files that can be read and written Abstraction

Hence, the top-down view looks upon the operating system as providing an easy interface to the user hiding all the hardware complexities.

Operating system as a resource manager (Bottom-up view) • • •

Manages all pieces of a complex system Provide an orderly and controlled allocation of processes, memory, and I/O devices among the various programs competing for them Keeps track who is using which resource, grants resource requests, accounts usage, mediate conflicting requests from different programs and users

Hence, the bottom-up view looks upon the system that governs the process management, memory management, file system management and I/O management.

6

1.3 Functions of an Operating System The functions of an Operating System can be listed as follows:     

Provides easy interface by hiding the hardware complexities Process Management Memory Management File System Management I/O Management

These functions will be discussed in brief in the subsequent sections.

7

1.4 Process Management Process is a program in execution. Processor management is concerned with the management of the physical processors, specifically, the assignment of processors to processes. As a process executes, it changes state. The state of a process is defined in part by the current activity of that process. The process state diagram is as follows:

Scheduler Dispatch

New

Admitted

Ready Interrupt

I/O Completion

Blocked

Exit

Running Termination

I/O or Event Wait

Figure 2 – Process States

The various functions of the process management functions are as follows: •

• • • • •

Keep track of the status of processes using process tables. The Operating System maintains a process table with one entry per process. When the switching takes place, the values in the process table are saved and the values corresponding to the next process is taken. Scheduling of processes is an important function in deciding which process will use the processor. The job scheduler chooses from all the jobs submitted to the system and decides which one will be allowed to take the processor. Allocate the resources to a process by setting up necessary hardware registers and this is often called the dispatcher. Reclaim resources when process relinquishes processor usage, terminates, or exceeds allowed amount of usage. Inter-process Communication using system calls. Process synchronization using monitors, event counters and semaphores.

8

1.5 Memory Management Memory management of an Operating System is concerned with the management of primary memory. The primary memory or the core memory is the part of memory that the processors directly access for instructions and data. The various functions of the memory management functions are as follows: • • • • •

Keep track of the status of each location of primary memory, whether allocated to any process or free Determining allocation policy for memory Allocation technique – Once it is decided to allocate memory, the specific location must be selected and allocation information updated De-allocation technique and policy – handling the de-allocation of memory Paging – Swapping pages from the secondary memory to primary memory when a page fault occurs

9

1.6 Device (I/O) Management Management of I/O devices includes efficient management of actual I/O devices such as printers, card readers, tapes, disks, control units, control channels and so on. There are three basic techniques for implementing the device management. Dedicated – A technique whereby a device is assigned to a single process. Shared – A technique whereby a device is shared by many process. Virtual – A technique whereby a physical device is simulated on another. The various functions of the device management functions are as follows: • • • •

Keep track of the status of I/O devices using unit control blocks for devices I/O scheduling using scheduling algorithms Allocation – physically assigning a device to a process. The corresponding channels and control units must be assigned. De-allocation – may be done on either on a process level or job level. On a process level, a device may be assigned for as long as the process needs it. On a job level, a device is assigned for as long as the job exists in the system.

The method of deciding how devices are allocated depends on the flexibility of the device. Some devices cannot be shared e.g., card readers and must therefore be dedicated to a process. Others may be shared e.g., disks and hence more flexibility. Others may be made into virtual devices. For example, the operation of punching on a cardpunch could be transformed into a write onto a disk i.e., a virtual card punch and at some later time a routine would copy the information onto a card punch. Virtual card reader, card punch, and printer devices is performed by spooling routines. The virtual devices approach allows: • • •

Dedicated devices to be shared, hence, more flexibility in scheduling these devices More flexibility in job scheduling Better match of speed of device and speed of requests for that device

10

1.7 File System Management Information Management is concerned with the storage and retrieval of information entrusted to the system. The modules of information system are collectively referred to as the file system. The various functions of the file system management functions are as: • •





Keep track of all information in the system through various tables, the major one being the file directory – sometimes called the VTOC (Volume Table of Contents). These tables contain the name, location, and access rights of all information within the system Deciding policy for determining where and how information is stored and who gets access to the information. Factors influencing this policy are efficient utilization of secondary storage, efficient access, flexibility to users, and protection of access rights to the information requested. Allocate the information, e.g., open a file. Once the decision is made to let a process have access to information, the allocation modules must find the desired information, make the desired information accessible to the process, and establish the appropriate access rights. De-allocate the information, e.g., close a file. Once the information is no longer needed, temporary table entries and other such resources may be released. If the user has updated the information, the original copy of that information may be updated for possible use by other processes.

11

1.8 Flavors The different flavors of Operating systems are as follows. MS-DOS UNIX WINDOWS MVS/ESA ATLAS XDS-940 THE RC 400 CTSS MULTICS MACH The student is advised to explore more information about the operating systems mentioned above.

12

Chapter 2 2. UNIX – Overview 2.1 History UNIX An open discussion on the workings of an operating system is never complete without discussing what the operating system is and the history behind it. The purpose of this module is to explain how UNIX came to be what it is today. UNIX is a multi-user, multitasking, multithreading computer operating system that enables different people to access a computer at the same time and to run more than one program simultaneously. Since its humble beginning nearly 40 years ago, it has been redefined and refined time and time again. Networking capabilities enhance the suitability of UNIX for the workplace, and support for DOS and Windows is coming in the 32-bit workstation markets. UNIX was designed to be simple and flexible. The key components include a hierarchical directory tree that divides files into directories and real time processing.

Exploring History The UNIX Operating System came into life more or less by accident. In the late 1960s, an operating system called MULTICS was designed by the Massachusetts Institute of Technology to run on GE Mainframe computers. Built on banks of processors, MULTICS enabled information sharing among users, although it required huge amounts of memory and ran slowly. Ken Thompson working for Bell Labs wrote a crude computer game to run on the mainframe. As the performance the mainframe gave or the cost of running it is very low, Ken with the help of Dennis Ritchie rewrote the game to run on a DEC computer and in the process, wrote an entire operating system as well. Several variations have circulated about the system, but the common is that it is a derivative of MULTICS. In 1970, Thompson and Ritchie’s operating system came to be called UNIX and Bell Labs kicked in financial support to refine the product. By 1972, around 10 computers were running UNIX, and in 1973, Thompson and Ritchie rewrote the kernel from Assembly language to C language – the brainchild of Ritchie. Since then, UNIX and C have been intertwined, and growth of UNIX is partially due to the ease of transporting the C language to other platforms. AT&T, the parent company of Bell offered UNIX in source-code form to government institutions and to universities for a fraction of its worth. In 1979, it was ported to popular VAX computers from Digital, further cementing its way to many universities. Thompson, in 1975 moved to University of California and he recruited a graduate student named Bill Joy to help enhance the system. In 1977, Joy mailed out free copies of his system modifications. When AT&T began releasing it as a commercial product, system numbers were used (System III, System V and so on). The refinements done at the University were released as Berkeley Software Distribution, or BSD (2BSD, 3BSD, and so on). These included the vi editor and C shell. AT&T versions accepted 14 characters for file names and Berkeley expanded it to

13

25. Towards the end of 1970s, ARPA (Advanced Research Projects Agency), of DoD (Department of Defense) used Berkeley version of UNIX. Bill Joy, in the mean time left the campus setting and became one of the founding members of Sun Microsystems. Sun workstations used a derivative of BSD known as the Sun Operating System, or SunOS. As there was lack of uniformity among UNIX versions, several steps were taken to correct the problem. In 1988, Sun Microsystems and AT&T joined forces to rewrite UNIX into System V, release 4.0. Other companies like IBM and Digital Equipment, countered by forming their own standards group to come up with a guideline for UNIX. Both groups incorporated BSD in their guidelines but still managed to come up with different versions of System V. In 1992, a joint product, UNIXWARE as announced by UNIX System Laboratories, a spin company of AT&T. UNIXWARE combines UNIX with features of Netware was marketed. In the early 1990s, Berkeley announced that no more editions of BSD would be forthcoming. As the enhancements in PC Operating Systems were interesting, Microsoft created XENIX – a form of UNIX designed for desktop PC and later sold it to Santa Cruz Operations (SCO). As the personal computer has matured, UNIX has come into favor as an Operating system for it. SCO UNIX, as well as Sun’s and Novell’s entries provide excellent operating systems for multi-user environments.

14

2.2 Flavors Since it began to escape from AT&T's Bell Laboratories in the early 1970's, the success of the UNIX operating system has led to many different versions. Universities, research institutes, government bodies and computer companies all began using the powerful UNIX system to develop many of the technologies which today are part of the IT environment. Computer Aided Design, Manufacturing Control Systems, Laboratory Simulations and the Internet began life with and because of UNIX systems. Soon all the large vendors, and many smaller ones, were marketing their own, versions of the UNIX system optimized for their own computer architectures and boasting many different strengths and features. Find below the chronological picture of UNIX. Click on this image to view a larger picture.

Figure 3 – UNIX Chronology The different flavors of UNIX are shown in the picture. Various giants like SCO, IBM, AT&T, Siemens, Berkeley, SUN Microsystems, DEC, and HP redefined the preliminary versions adding many features and thus leading to many flavors of UNIX. The different flavors include Multics, System III, BSD, Sun OS, Solaris, Sinix, Ultrix, HP UX, AIX, XENIX, SCO-UNIX and so on.

15

2.3 Logging In A UNIX session begins with the login prompt. This prompt appears on the screen. The system administrators must again a login name and password to you before you can gain admittance to the system. After you have attained both, enter the login name at the prompt, as in the following: login : user1 <Enter> Next a password : prompt appears. Enter your password here. After you correctly enter the login and password, the shell prompt appears. The shell prompt can be dollar sign ($) or percentage sign (%). UNIX contains various shells or command interpreters. The % identifies that the C shell is in use, and the $ is used by most of the shells. A # sign on the other hand indicates that you have logged in with administrative permissions. To end your session with the operating system, you need to leave the shell and return to the login. This task is known as logging out or signing off. Several methods are available depending on your software. The preferred method is the exit command. An alternative is pressing Ctrl+D, which signifies the end of data input. Other commands that might exist on your system and perform the same function are logout, logoff, or log.

16

2.4 File and Directory Structure The UNIX operating system uses an inverted tree structure, much the same as the many other operating systems. In an inverted tree, one main directory branches into several subdirectories. Each subdirectory can then further branch into more subdirectories. This structure, although novel at the time UNIX first became available, is familiar now and commonplace with most operating systems. The purpose of each of the standard subdirectories and the method by which UNIX maintains files and directories and the method by which UNIX maintains files and directories, however, is vitally important.

Root directory and its branches The root directory is the beginning, or first layer, of the file system. Symbolized as a forward slash (/), the root directory is the point from which all other subdirectories branch. A root user also called the super user has the ability to change anything related to the file system without question. This user can bring the system up, shut it down, and do everything in between. By no small coincidence, the home directory of this user is the root directory – from here, all information filters down. The figure below illustrates this by presenting the classic diagram of the UNIX subdirectory file system.

/

bin

dev

etc

lib

l&f

crdm

bin

shlb

include

tmp

tcb

lib

usr

man

var

spool

l&f – lost & Found

Figure 4 – Inverted tree of UNIX – Classic Diagram Only one file – UNIX should be within the normal system’s root directory. This is the actual, bootable, operating system file. In its absence, the operating system cannot come up after a restart. This file is also known as the kernel. In addition to the UNIX file, there are a number of subdirectories. Every system has unique ones created by an administrator for specific purposes, but the default ones created when a new operating system is installed are as follows.

17

bin dev etc lib lost+found shlib tcb tmp usr var

18

2.5 Process UNIX is a multi-user, multi-tasking operating system, which means, more than one user can use the system and each user can run more than one program at the same time. In UNIX, each task that the system that the system is executing is a process. Normally, the UNIX has to handle several processes at the same time. However, the CPU can handle only one job at a time. So, UNIX uses the time-sharing concept to solve the problem. Time-sharing means the kernel maintains a list of current tasks or processes and allocates a small time slice for each process. The kernel switches to other process in the queue once the current process completed or the allocated time has elapsed. The kernel also assigns a priority number to each of the processes. Based on the priority, the processes are scheduled to run on the CPU. Typically, the kernel switches from process to process very rapidly. This gives the user the impression that all the processes are running simultaneously. Suppose the system is executing many processes that do not fit simultaneously into the main memory, the kernel then swaps the blocked and sleeping processes to the disk and makes room for the running process. All the processes are assigned a unique identification number by the kernel called the process id (pid). This pid is used by the kernel to identify each process. The information about the processes, their states, pid, memory area, the terminal from which the program was executed, the owner of the process and some other system information are maintained in the process table by the kernel. After the login process, the kernel invokes a shell for that user. The shell is the parent process that runs till the user logs out of the system. All the commands, jobs, or processes that run under the shell or get invoked from the shell are child processes of the parent shell. Each process gets killed after it is completed. All the child processes should be killed before the parent dies. Another shell can also be invoked from an existing shell. The child shell should be killed before exiting from the parent shell.

Manipulation of Processes Process Status The ps command displays the contents of the process table. This command without any option displays a four-column output containing the status of the processes of the particular user terminal only. The first column is the pid of the process, the second column is the terminal number, the third column is the time taken by the process at the time of issuing the command and last column displays the command or the program name itself. The ps command with the option –e, displays all the active processes. With the –l option displays among other details, the priority associated with each process, the state of the process and number of CPU resources used by the process. The –l option along with the –e option displays the information about all the processes that are active in the system.

Process Scheduling There are also some process scheduling commands namely nice, sleep, at, and nohup etc.

nice

19

It is also possible for individual user to run a process with a reduced priority. Only the super user can increase the priority value for a process. This is achieved by a command nice. Example:

$ nice <enter> $ nice -15 vi first <enter>

The priority gets reduced by 15 units. If the number is not specified, the priority gets reduced by 10 units.

sleep To suspend the execution of any process started by the user for a short interval, the command sleep is used. The time period for sleep is given in terms of seconds and up to a maximum of 65535 seconds. Example:

$ sleep <seconds> <enter>

at The at command is used for scheduling one or more commands to be executed at a specified time. This command can be used to execute a command or program at a later time, even after logged out from the system. Example:

$ at

Related Documents

Unix Course Ware Ver 1[1]
October 2019 23
Lancamento Course Ware Sere
December 2019 14
Course Ware Iks
July 2020 27
Unix
November 2019 40
Unix
May 2020 36
Unix
June 2020 24