Sie sind auf Seite 1von 15

Unix, C & C++ UNIT - I Snapshot

Operating System An Introduction History of Unix Unix fundamentals Text Processing Communication The Unix Environment Introduction to the Shell Programming Shell Variables Shell Meta characters Text Processing Program Development Tools Debugging Tools Programming Maintenance Tools

1.0

Introduction

An operating system is an important part of a computer system. You can view a computer system as being built from three general components: the hardware, the operating system, and the applications. The hardware includes pieces such as a central processing unit (CPU), a keyboard, a hard drive, and a printer. Applications are why you use computers; they use the rest of the system to perform the desired task (for example, play a game, edit a memo, send electronic mail). The operating system is the component that on one side manages and controls the hardware and on the other manages the applications. UNIX is an increasingly popular operating system. Traditionally used on minicomputers and workstations in the academic community, UNIX is now available on personal computers, and the business community has started to choose UNIX for its openness. Previous PC and mainframe users are now looking to UNIX as their operating system solution.

1.1

Objective

The objective of this lesson makes the learner aware of the fundamentals of UNIX operating system, which consists a broad range of topics from operating system and UNIX fundamentals. The fundamental concepts include text processing, communication, UNIX environment, and introduction to shell programming and program development and maintenance tools. Towards end of this lesson the student can get the basic knowledge about UNIX operating system.

1.2
1.2.1

Content
Operating System An Introduction

Page 1

Unix, C & C++


An operating system (OS) is a collection of software programs that coordinates the operation of computer hardware and software. Functions of an OS An operating system provides various functions as depicted in the following diagram.

Command Interpretation

Process Management

Operatin g System

Peripheral Management

Memory Management

1. Command Interpretation The Command Interpreter reads the commands entered by a user at the command prompt and translates the commands into instructions which the computers CPU can understand. 2. Process Management Process Management is required if CPU time. 3. Peripheral Management The Main function of the OS is the Peripheral Management, the communication between the peripheral devices and the CPU is overseen by the OS. 4. Memory Management The OS handles the job of allocating the memory for the various process running on the system. 1.2.2 History of UNIX a number of programs have to run parallelly. The OS has to ensure that no program gets more than its share of

Page 2

Unix, C & C++


Unix is the most popular multi-user operating system. In 1969, the research department of Bell Laboratory used an operating system called MULTICS. The disadvantage of this was it retained the batch code of the previous operating system. In 1973, Thompson and Ritchie reworte the Unix operating system in C, breaking away from the tradition of writing operating systems in assembly language. Around 1974, Unix was licensed to universities for educational purposes and a few years later, was made commercially available. Some of the popular versions of Unix are: Version Unix System Unix BSD(4.xx) XENIX ULTRIX AIX 1.2.3 UNIX fundamentals Developed by AT&T Berkeley Software Berkeley University Microsoft for micros DEC for minis IBM

Distribution

of

The Unix Operating System can be broken down into three basic components, namely the Scheduler, the File System and the Shell. The Unix Scheduler The UNIX scheduler is a program that allows several users to access the system simultaneously. The scheduler shares computer resources among these users, allowing each a small slice of the computers processor. The UNIX Shell The shell is the UNIX systems command interpreter. It is a program that reads the lines typed in at the terminal and performs various operations. A shell performs two basic functions that is it acts as a command interpreter and as a programming language. As a command interpreter it is similar to DOS. As a programming interface it allows the user to process commands stored in shell scripts, this function is similar to batch files in DOS. The UNIX File System To organize data on to the disk, the UNIX operating system provides a File System. The File System allows grouping of similar files together, since if thousands of files are stored together it would be difficult to locate a single file. The Unix File System has a Hierarchical structure; therefore the files can be stored under directories. The directories are created to store files containing data of similar nature. The root directory will contain the bin, dev, var, etc, usr, home, and tmp directories. The following picture shows a sample file system:

Page 3

Unix, C & C++

Figure 1.1: File System Getting Started In order to use the UNIX system, first the user has to login. To login to the Unix system the user needs to type in the user id in the login: prompt and then pressing the Enter key prompts for a password which the user has to type to enter into the Unix system. Login: test <ret> Password: **** $ Once the user has successfully logged in, the Unix system will indicate that it is ready to accept the command by displaying a dollar sign ($). If the user enters a wrong password then, the Unix system will prompt a message saying login incorrect and will display the login: prompt again for the user to re-enter the user id and password. Login: test Password : ****** Login incorrect Login: UNIX allows user to create their own files by using a command called cat. The cat command can also be used for appending content to an already existing file and also to view the content of a file. The following format is used for creating a file using cat command cat > filename To append text to an already existing file, the format is: cat >> filename To view the content of an already existing file, the format is: cat filename

Page 4

Unix, C & C++


1.2.4 Text Processing

Text processing is one of the things which people spend most time doing on any computer. It is important to distinguish text editing from word processing. Normally word or WordPerfect are used for writing documents. UNIX has a word-like program called lyx, but most of the UNIX users do not use word processors. It is more common in the UNIX community to write all documents using a non formatting text editor. The user can edit a file by typing emacs myfile Emacs is the most powerful text editor that exists on any system. It is not a wordprocessor, it is not used for formatting printed documents, but it can be linked to almost any other program in order to format and print text. 1.2.5 Communication

Unix system allows a user at one terminal to interact with another user at some other terminal. Some of the commands used for interaction are discussed below. write command This command is used to send a message from one user to another user who is currently working on the system. This is a means of on-line communication because the recipient obtains the message immediately. write user [tty] The other users terminal beeps and displays a message. The other user can respond to the message, by invoking the write command and typing a message to the system. The conversation between the users can be terminated by pressing the ctrl+d command. news command Unix can communicate in off-line mode also by using the news command To send news to all users even if they are not logged in, the news command is useful. Only one file is created and all the users read that file. When the user logs on the contents of the news command are displayed on screen. news [-a][-n][-s][items] -a displays all items regardless of whether the message are already viewed or not. -n shows only the names of the current files. Page 5

Unix, C & C++


-s shows an account of the number of current items.

mesg command The mesg command controls the display of messages on the screen. The user can decide whether a message can be received by the system or not. The syntax to restrict messages is mesg [n] [y] wall command This command is used to send messages to all users. The command is placed in the /etc directory. To invoke the wall command the full path name has to be specified. Example $/etc/wall Good morning every one message from administrator 1.2.6 Ctrl +d The UNIX Environment

UNIX operating systems structure consists of Kernel, Shell and tools and applications

H W Kernel Shell

Figure 1.1 Structure of UNIX System Kernel Kernel forms the core of the UNIX operating system. The Kernel interacts with the hardware. It is loaded into the memory when a system is booted. The functions of Kernel are: Managing the system resources. Allocating time for different users and processes. Deciding process priorities and performing them. Page 6

Unix, C & C++


The Kernel does not interact with the user directly, instead it starts up a separate file called SHELL. Shell UNIXs shell interacts with the user and the computer. The features of shell are as follows: 1.2.7 Background processing - Simultaneously two processes can be run, one in the foreground and another in the background, which is called as multitasking. Input/Output Redirection - Programs can be instructed to take input from a file other than the standard input devices and redirect output other than the standard output device. Shell Scripts contain a sequence of commands to be performed with a single filename. Introduction to Shell Programming

The shell is a command programming language that provides an interface to the UNIX operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. Constructs such as while, if then else, case and for are available. Two-way communication is possible between the shell and commands. String valued parameters, typically file names or flags may be passed to a command. A return code is set by commands that may be used to determine control-flow and the standard output from a command may be used as shell input. The shell can modify the environment in which commands run. Input and output can be redirected to files and processes that communicate through pipes can be invoked. Commands are found by searching directories in the file system in a sequence that can be defined by the user. Commands can be read either from the terminal or from a file, which allows command created to be sorted for later use. 1.2.8 Shell Variables

Variables in shell scripts do not have associated data types. All variables in shell scripts are treated as character strings. Creating variables Variables can be created either in shell scripts or at the shell prompt. Any variable created within a shell script is lost when the script stops executing. A variable created at the prompt, however, will remain in existence until the shell is terminated. The format for creating a variable is: <variable name> =<value> Example Name=ram Page 7

Unix, C & C++


Variables do not have to be explicitly declared. They can be created at any point of time by a simple assignment of values. Environment Variables An environment is an area in memory where one can place definitions of shell variables. So that they can be accessible to programs. a. The Ps1 variable

This variable contains the system prompt, that is. the $ symbol. Changing the value of the ps1 variable can change the system prompt. Example $ps1=Hello <ret> Hello b. The PATH variable A list of filenames that are to be searched for Unix commands execution are contained in this variable. The echo command is used to display the content of the variable. c. The HOME variable

This variable contains the Home directory where the login shell is initially located after logging in. d. The LOGNAME variable This variable contains the users login name or user name. The contents of this variable cannot be changed by the user but can be displayed. e. The .Profile file

Some environmental variables like the HOME and the LOGNAME variable are set automatically each time the user logs in. The others, however, have to be set. The .profile file is used for this purpose. This file is a special shell script that is executed as soon as the user logs in. It is similar to the autoexec.bat batch file in DOS. Referencing Variables A variable is referenced as follows: Command ${variable name} The braces are optional, but the $ is essential. To assign value of one variable to another variable, the command would be: Variable1=$variable2

Page 8

Unix, C & C++


Reading a value into a Variable The read command is used to enter value into a variable from the keyboard during execution of a shell script. The syntax of read command is: read variablename The read command on execution waits for a user to input the value of the variable. The read command does not display any message to the user to enter data. Local and Global shell Variables The environment variables are known only to that particular shell in which the user is working. If a new shell (child shell) is created, then the child shell cannot use the variables available in the parent shell. The same variable name can be given a different value without the parent knowing about it. Such a variable is called a local variable. Example $name = ram $echo${name} ram $sh //creating a new shell// $echo${name} $name = sam //new value is assigned to name.// $echo${name} sam $ctrl d //return to parent shell $echo${name} ram $sh $echo ${name} $ ctrl d //parent unaware of the child variable value sam\\ //child value destroyed //

The environment variable can be transferred to the child shell using the export command. The export command causes a copy of the variable name and values to be passed onto a child shell process. 1.2.9 Shell Meta characters

UNIX offers the facility to perform an operation on a set of files without having to list out the names of all the files on which the operation has to be performed. Page 9

Unix, C & C++


This is made possible by the use of certain special characters in the command in place of the actual file-names. These special characters are interpreted by UNIX as a specific pattern of characters. Unix then compares all file-names under the directory specified in the command to find out which file-names match that pattern. The command is executed on files whose names match the pattern. Example To display the contents of the three files, sample, sampletest, samplesort. All the three file-names begin with sample. Instead of writing all three file-names in the cat command, the following command can be used: $ cat sample* <ret> The asterisk is referred to as a wild-card. UNIX interprets the wild-card * in the command line as a specific pattern and automatically substitutes this pattern with filenames that match the pattern. Following is the list of wild-cards that are used in UNIX. * ? [] The * wild-card * is used for representing any number of characters when used in the prefix or in the suffix. Example To display files which start with sa $ ls sa* sample sample file $ The ? wild-card ? is used to represent one character only either in the prefix or in the suffix of the filename. Example To display a file called test. $ ls tes? test $

Page 10

Unix, C & C++


1.2.10 Text Processing Text processing programs such as grep, egrep, sed, awk and vi, are used to search the patterns instead of fixed strings. These text patterns are known as regular expressions. The user can form a regular expression by combining normal characters and special characters, with the rules below. With these regular expressions the user can do pattern matching on text data. Regular expressions come in three different forms: 1. Anchors : which tie the pattern to a location on the line. 2. Character sets : which match a character at a single position. 3. Modifiers : which specify how many times to repeat the previous expression. Few meta-characters are used only by awk and egrep. These are : + - match one or more of the preceding expression ? - match zero or more of the preceding expression | - separator. Match either the preceding or following expression. () - group the regular expressions within and apply the match to the set. Commonly used regular expressions are : xy*z - any occurrence of an x, followed by zero or more ys, followed by a z. ^cat - cat at the beginning of the line cat$ - cat at the end of the line \* - any occurrence of an asterisk [0-9]$ - any line ending with number [A-Z][A-Z]* - zero or more upper case letters 1.2.11 Program Development Tools Editing is one of the most frequently used functions of a general purpose computer. No matter what the user is planning to do, basic editing is sure to be the first step. Many users prefer to use a screen editor to create or modify their files. The only restriction of the screen editor is that the user must have a video (screen) terminal. A screen editor allows the user to see the portions of the file on the terminal screen and to modify characters and lines by simply typing at the current cursor position. Vi is the most popular screen editor. One important aspect of vi is that it does not provide the user with the original copy of the file when invoked. It provides with a editing buffer. All changes are done to that copy in the buffer. Only when the file is saved and when the user quits the vi, then the original file is replaced. The make program The UNIX system provides a powerful program called make. This program is particularly useful for the development of program systems that comprise more than one file. make automatically keeps track of files that have changed and causes their recompilation when necessary. It also automatically relinks the programs if required.

Page 11

Unix, C & C++


The make program takes a file, known as the makefile as its input. This file describes the following to make: The name of the files that make up the program system. Their interdependencies. How to regenerate the program system.

This makefile is typically called makefile or Makefile by convention, although any file name will do. However, if the user uses a different name, the user has to supply the name as an argument whenever the make command is executed. Once this information has been described to make, make takes over and does the rest. Simply typing the command make causes the program to examine the makefile and regenerate the system according to the rules laid out in the file created by the user. The automatic method of program generation saves the user from the bother of having to keep track of the files the user has changed and also recompiling each one by the user itself. When one file is changed the associated file is also changed. By specifying this dependency in the makefile, make will take care of recompiling the necessary files whenever that particular file is changed. 1.2.12 Debugging Tools The most generally available debugger is dbx. Prerequisites for dbx The dbx debugger on one hand is a source level debugger. This means that even though it is running the object code, it is smart enough to direct to the statement in the source that has caused the problem in the object code. On the other hand it is a symbolic debugger that it is smart enough to tell the name and value of the variable on which the program is having its difficulties. In order to use the debuggers one must first compile the entire program with the g option. One should run the debugger on nonoptimized code only. Some of the subcommands given from within dbx include the following: Help Tell me what I may command. Run Execute the program. Print Display value of var. Where Print currently active procedures. Stop at line - suspended execution at line. Stop in proc - suspended execution in subprogram proc. Cont - Resume execution Edit - Invoke vi editor
Step Execute 1 line

Page 12

Unix, C & C++


There are more subcommand available for use with dbx, which can be got from the help utility. 1.2.13 Program Maintenance Tools An important program maintenance tool is make command. The make command, allows recompiling portions of a program without having to recompile the full package. % make f make_command_file target<CR> Where the make_command_file is the name of the file containing the directives for make and target is the name of the target. Some common targets are usually all components to be compiled and linked. Usually, install causes all components to be moved to a predetermined location on the system. If the components are not compiled, it should also trigger the compilation as well. Clean usually deletes any files with an extension of .o as well as the filename Core. Dist usually deletes the same files as Clean would have and the actual executable version as well.

1.3

Revision Points

Operating System An operating system is a collection of system programs that together controls the operation of a computer system. The operating system along with hardware, application and other system softwares, and users constitute a computer system. It has two objectives. Firstly, an operating system controls computers hardware. The second objective is to provide an interactive interface to the user and interpret commands so that it can communicate with hardware. UNIX The UNIX operating system was developed at AT&T Bell Laboratories by Ken Thompson and Dennis Ritchie in 1969 and the early 1970s. UNIX is a multi-user system that supports networking and distributed file systems such as Sun Microsystems NFS (Network File System) or the Open Software Foundations implementation of the AFS (Andrew File System).UNIX highly portable and it now contains system components written in a common, well-known programming language that are easily recompiled to work on a variety of systems. UNIX file system The UNIX file system is a hierarchical, tree-structured namespace that is designed to help users organize and access files. The namespace consists of directories that hold files. The UNIX file system consists of the following: Disk based file systems Network file systems Pseudo file systems Page 13

Unix, C & C++ 1.4 Intext Questions


1. Explain about Operating System and its function. 2. Write a note on UNIX fundamentals. 3. Write a note on different communication commands. 4. Write a note on Unix Environment. 5. Write a note on shell variables and shell Meta characters.

1.5

Summary
An operating system (OS) is a collection of software programs that coordinates the operation of computer hardware and software. The Unix Operating System can be broken down into three basic components, namely the Scheduler, the File System and the Shell. The UNIX scheduler is a program that allows several users to access the system simultaneously. UNIX operating system structure consists of Kernel, Shell and tools and applications. UNIXs shell interacts with the user and the computer.

1.6

Terminal Exercises
1. The operating system that allows only one program to run at a time is __________. 2. Define Network file system. 3. List out the functions of kernel. 4. What are the features of shell programming? 5. Mention the use of wall command.

1.7

Supplementary Materials
1. Sumitabha Dass, Unix Concepts and Applications, Second Edition, TMH, 1998

Page 14

Unix, C & C++


2. B.W.Kernighan, D.M.Ritchie, The Programming Language, PHI, 1988.

1.8

Assignments
1. Describe the directory structure of the UNIX file system. 2. Distinguish between absolute path names and relative path names. 3. Why UNIX systems become the favored operating systems for implementing the open systems philosophy?

1.9

Suggested Reading/Reference Books/Set Books


1. http://www.opengroup.org 2. http://www.unix.org 3. http://www.yahoo.com/Computers_and_Internet/Operating_Systems/Unix

1.10 Learning Activities


1. What features of UNIX systems are especially useful from a software engineering standpoint? 2. UNIX systems provide an environment in which large tasks may be accomplished by combining existing small programs rather tan developing new programs. Why is this important?

1.11 Keywords
Operating System Shell Network File System (NFS) UNIX systems Kernel Andrew File System (AFS)

Page 15

Das könnte Ihnen auch gefallen