Usp 1
Usp 1
Usp 1
MODULE 1
Getting Started & Understanding UNIX Commands: Operating System, UNIX
Operating System, UNIX architecture, Features of UNIX, The POSIX Standards.
UNIX and POSIX APIs: The POSIX APIs, The UNIX and POSIX Development
Environment, API Common Characteristics.
It is loaded into memory when a computer is booted and remains active as long as the
machine is up.
When OS’s allow multiple programs to be in the memory they are called as
multiprogramming.
Key functionality of Operating System
1. Memory Allocation- OS allocates memory for the program and loads the
program to the allocated memory.
2. CPU Management-
a. Allocates a time slot for a program to run in CPU.
b. Loads the CPU registers with control information related to the
program.
c. The operating system keeps track of the instruction that was last
executed.
3. Hardware Management- If the program needs to access the hardware; it makes
a call to the operating system rather than attempting to do the job itself.
4. After the program has completed execution, the operating system cleans up the
memory and registers and makes them available for the next program.
UNIX is a giant operating system and is way ahead of other systems in sheer power. It
practically runs on every hardware and provided inspiration to the Open Source movement. It
was developed by programmers for their own use. UNIX is not written in assembler but in C.
C is a high-level language that was designed with portability considerations in mind, which
explains why UNIX systems are available on practically every hardware platform. Apart
from handling the basic operating system functions, UNIX offers a host of applications that
benefit users, programmers, and system administrators. It encourages users to combine
multiple programs to solve a complex problem. For programmers, UNIX offers a rich set of
programming tools that aid in developing, debugging, and maintaining programs. UNIX is
also more easily maintained than most systems. One of these programs is the system’s
command interpreter, called the shell. You interact with a UNIX system through the shell.
Following are some of the important features of Unix Operating System
• Portable
• Open Source
• Multi-User
• Multiprogramming
• Hierarchical File System
• Shell
• Security
The system’s bootstrap program (a small piece of program code) loads the kernel into
memory at startup.
The kernel comprises a set of routines mostly written in C that communicate with the
hardware directly.
User programs (the applications) that need to access the hardware (like the hard disk
or the terminal) communicate with the kernel using a set of functions called system
calls
It manages the system’s memory, schedules processes, decides their priorities, and
performs other tasks
Shell
Shell interact with the user
Computers don’t have any inherent ability to translate user commands into action.
This is done by the shell which is a command interpreter
Interface between the user and the kernel.
There can be only kernel running on the system but there can be several shells in
action.
The shell examines the command for special characters. If it finds any, it rebuilds a
simplified command line, and finally communicates with the kernel to see that the
command is executed.
Two simple entities support the UNIX system- the file and process.
A file is an array of bytes that stores information
Files are containers for storing static information.
Even directories and devices are considered files.
A file is related to another file by being part of a single hierarchical structure called
the file system.
The process represents a program in execution.
Like files, processes also form a hierarchy.
1. Multiuser System
It is a multiprogramming system, it permits multiple programs to remain in
memory and compete for the attention of the CPU.
These programs can be run by different users; UNIX is also a multiuser
system.
For cycling through multiple jobs, the kernel uses the principle of time-
sharing. It breaks up a unit of time into several slices, and a user’s job runs for
the duration of a slice. The moment the allocated time expires, the previous
job is kept on hold and the next job is taken up. This process goes on until the
clock has turned full circle and the first job is taken up once again. This
switching happens several times in one second, so every user has the feeling
that the machine is completely dedicated to the person.
2. A Multitasking System.
A single user can also run multiple tasks concurrently
Users edit a file, print another one on the printer, send email to a friend, and
browse the World Wide Web, all without leaving any of the applications.
In a multitasking environment, a user sees one job running in the foreground;
the rest run in the background.
In UNIX, it is possible to edit a C program with the vi editor and then suspend
the vi process to run the gcc compiler.
3. Inter process Communication
UNIX tools singly are not that powerful.
Using Interprocess communication feature, the Shell can arrange for a
command to pass on data to another command.
Thus by interconnecting a number of tools, an enormous number of
combinations of their usage.
5. Pattern Matching
UNIX features very sophisticated pattern matching features. Many commands take file name
as argument or string as argument. All C programs have the .c extension, to represent all the c
files *.c can be used. The * is a special character (known as a meta-character) that is used by
the shell to match a number of characters.
Some advanced tools (like grep, sed and awk) also use a different meta-character set for
matching strings contained in files.
e.g. ls unix* lists out all filenames with unix.
6. Programming Facility
The UNIX shell is also a programming language; it was designed for a programmer, not a
casual end user. It has all the necessary ingredients, like control structures, loops, and
variables that establish it as a powerful programming language in its own right. These
features are used to design shell scripts; programs that run UNIX commands in a batch.
In UNIX, once software has been developed in any UNIX system it can be easily
ported to any other UNIX machine, since the same system calls are available to
whoever wants to develop tools of their own.
System Calls are special function which communicate with the kernel
Often the same system call can access both a file and a device; the open system call
opens both.
write system call can be used to write on disk, device and on the monitor.
POSIX specifies the system calls that all UNIX systems must implement. Once
software has been developed on one UNIX system using the calls mandated by
POSIX, it can be easily moved to another UNIX machine.
8. Documentation
The principal online help facility available is the man command, which remains the most
important reference for commands and their configuration files. There’s a vast ocean of
UNIX resources available on the Internet, several newsgroups on UNIX to post queries. The
FAQ (Frequently Asked Questions), a document that addresses common problems is also
widely available on the Net. Then there are numerous articles published in magazines and
journals and lecture notes made available by universities on their Web sites.
Internal commands are those commands that are executed directly by the shell. These
commands will not have a separate process running for each command. Shell built-in
commands. These commands are normally not found anywhere even if they are specified in
any directory. E.g. echo, pwd, cd
External commands are those commands that are executed by the kernel. These commands
will have a process id running for it. It is stored as separate binaries in /usr/bin directory.
They are those commands that have an independent existence and is executed from the /bin
directory. E.g. ls, cal, who
A UNIX command sequence has five words. The first word is the command itself and the
remaining ones are its arguments. The ls command is specified here with four arguments.
Two of the arguments begin with a hyphen (-l and -t) and are appropriately called options.
The entire line is referred to as the command line. A command line is executed only after you
hit [Enter].
Every command has a fixed set of options. An option changes a command’s default behavior,
so if ls shows only the filenames, the -l and -t options show their attributes as well.
This ; is a special character is called as meta-character. Commands on either side of the ; are
processed separately.
Some commands have lengthy syntaxes, and you may often find it desirable and sometimes
necessary to spread the command line into multiple lines. The shell then issues a secondary
prompt usually >, to indicate that the command line isn’t complete. This is how the echo
command works:
$ echo “This is
>a three line
>text message”
Output will be
This is
a three line
text message
man is the most complete and authoritative guide to UNIX system. man presents the first
page and pauses, press a key (either [spacebar] or [enter]) to see next page and press q to
quit. The structure of the man page is divided into a number of compulsory and optional
sections where each section is preceded by a header.
Every command doesn’t have all the headers but has most of them; the first three (NAME,
SYNOPSIS, and DESCRIPTION) are generally seen in all man pages.
NAME: name of the command with a brief description of its function.
SYNOPSIS: the syntax- the options and arguments used with the command
DESCRIPTION: Largest section of manual page. It contains a detailed exposition of the
command with specific explanation of every option.
POSIX.1 :
POSIX.1c:
To ensure a user program conforms to the POSIX.1 standard, the user should define the
manifested constant _POSIX_SOURCE at the beginning of each program(before the
inclusion of any header files) as:
#define _ POSIX_SOURCE or
To ensure a user program conforms to the POSIX.1b standard, the user should define the
manifested constant
#define _POSIX_C_SOURCE
value----Meaning
In general a user program that must be strictly POSIX.1and POSIX.1b compliant may be
written as follows:
#define _POSIX_SOURCE
#include <stdio.h>
#include <unistd.h>
int main( )
....
Example program:
#define _POSIX_SOURCE
#include<stdio.h>
#include<unistd.h>
int main()
#ifdef _POSIX_VERSION
#else
#endif
return 0;
C and C++ header files are just headers C and C++ header files
not header files
Does not support the concept of super Super user has special privilege and the
user nor the ID is 0 super user ID is always 0
_POSIX_NO_TRUNC If the defined value is -1, any long path name passed to
an API is silently truncated to NAME_MAX bytes,
otherwise error is generated.
The POSIX.1 and POSIX.1b standards specify a number of parameters that describe
capacity limitations of the system.
Limits are defined in <limits.h>
#include <unistd.h>
For pathconf(), the path argument points to the pathname of a file or directory.
he limit_name argument value is manifested constant as defined in <unistd.h> header. The
possible values and the corresponding data returned by the sysconf function are
A user mode is the normal execution context of any user process, and it allows the
process to access its specific data only.
A kernel mode is the protective execution environment that allows a user process to
access kernels data in a restricted manner.
When the APIs execution completes, the user process is switched back to the user mode.
This context switching for each API call ensures that process access kernels data in a
controlled manner and minimizes any chance of a runway user application may damage
an entire system. So in general calling an APIs is more time consuming than calling a
user function due to the context switching. Thus for those time critical applications, user
should call their system APIs only if it is necessary.
An APIs common Characteristics
Most system calls return a special value to indicate that they have failed. The special
value is typically -1, a null pointer, or a constant such as EOF that is defined for that
purpose.
To find out what kind of error it was, you need to look at the error code stored in the
variable errno. This variable is declared in the header file errno.h as shown below.
volatile int errno
The variable errno contains the system error number.
void perror (const char*message)
Errors Meaning
EPERM API was aborted because the calling process does not have the super user
privilege.
ENOEXEC A process could not execute program via one of the Exec API.
ECHILD A process does not have any child process which it can wait on.
EAGAIN An API was aborted because some system resource it is requested was
ENOMEM An API was aborted because it could not allocate dynamic memory.
EACCESS The process does not have enough privilege to perform the operation.
..................................................