Module-1 Getting Started and Understanding Unix Commands
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.
The Operating System
Life giver to a computer-provides with basic intelligence for
computer to work with.
A program which resides in the main memory all the time from
the time,the system is booted.
Interacts with two agencies-Application program and the
command interpreter which translates the user commands to
instructions which will be understood by the operating
system.
Controls various functionalities like opening and closing of
files,accessing the printer,writing to memory etc.
An operating system runs programs and controls all resources
that program requires by providing all the services that the
operating system requires.
Without an operating system,any powerful system will be
useless
SERVICES OFFERED BY THE OPERATING SYSTEM
Scheduling Decision about which program to run,and for how
long in a multiprogrammed environment .
Memory management accross various applications.
Device management ie Access control to various hardware
devices like hard disks,terminals,printers,modems etc.
Error management ie Reporting errors to application programs
Providing basic user tools like creating directories,creating
,deleting,copying files,sending mails,performing backups etc.
Resource management.
------------------------------------------------------------------
THE UNIX OPERATING SYSTEM
It is an operating system which arrived earlier than the
windows and DOS operating systems,initially developed by
programmers for their own use and has more powerful features.
Unix(Uniplexed Information and computing services) is
developed in Bell Labs in 1970 by Ken thompson with dennis
ritchie.
The orginal Unix OS is designed to be as simple and elegant
as possible.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 1
Module-1 Getting Started and Understanding Unix Commands
UNIX has introduced many concepts which were previously
unknown to computing community like many complicated tasks
can be done in few keystrokes.
In UNIX,we can combine many simple commands to do a complex
task.
-------------------------------------------------------------
THE UNIX ARCHITECTURE
The UNIX architecture comprises of two major components-The
shell and the Kernel.
The divison of labour approach between the kernel and the
shell is one of the most powerful feature of the UNIX
architecture.The Shell interacts with the user and the kernel
with the machine hardware.
THE KERNEL
The kernel is the core of the operating system.
It is a programs Gateway to computers resources
It is loaded into memory from the time system is booted.
It manages the systems resources and,allocates time between
the users and the processes ,decides process priorities and
many more tasks for the smooth functioning of the system.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 2
Module-1 Getting Started and Understanding Unix Commands
The user programs which require hardware services,use the
services of the kernel and get the job done via a set of
system calls.
THE SHELL
It is also known as the command Interpreter.The shell takes
command from the user,deciphers the special characters that
it finds and rebuilds a complete command line.
It also communicates with the kernel to see that command is
executed.
It acts as an interface between the user and the kernel,which
effectively insulates the user from knowledge of kernel
functions.
The shell is also a programming language. As a programming
language, it permits you to control how and when commands are
carried out.
The shell is generally represented by /bin/sh
There can be many shells running in action for each user
logged in.
The FILE and the PROCESS
File is an array of bytes that store the information.Files
are organized in hierarchical structure.
A program in execution is a process ie.time image of a
program.Process is also organized in heirarchical structure.
THE SYSTEM CALLS
System calls are used to communicate with the kernel.All the
UNIX flavours use one thing in common -they all use the same
system calls.
System calls are described in POSIX specification.
Eg:To perform the actual write operation it uses write()
system call,open() system call can be used to open a file or
a device etc.
-------------------------------------------------------------
FEATURES OF UNIX
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 3
Module-1 Getting Started and Understanding Unix Commands
1. Multiuser support
In Unix environment resources are actually shared between all
users.Unix is a multiuser system.For creating the illusionary
effect,the computer breaks up a unit of time into several
segments and each user is allotted a segment.Once the time
expires the CPU is gven to another user and the switching is
done in such a small fraction that the user gets the illusion
that all the resources are just dedicated to him solely.
2. Multitasking environment
In a unix environment,a single user can also perform multiple
tasks concurrently,like editing a file,listening to
music,printing another file,sending email to a friend,browse
etc.In such a multitasking environment only one job is kept
in the foreground and all others in the background and the
switching is done between them.
3. Building block approach/Do one thing well approach
The UNIX system is built with the concept of “small is
beautiful”.Each of the command is designed to perform only a
simple task.But the fact that commands can be combined to
perform a complex task makes UNIX a powerful architecture.The
shell uses the Interprocess communication feature to pass
data from one command to another one.
Eg:ls|wc
ls -command to list all the contents of a directory
wc-command to print the newline,word and byte counts for each
file.
ls|wc-counts the number of files or directories in a
directory
4. Featureless file
Files have places and processes have life.Files have location
and can be moved from one location to another.The files in
UNIX need not have a compulsory extension like .c,.doc,.txt
etc as UNIX perceives a file as an array of bytes which can
contain virtually anything-text,object code or directory
structure.
5. Unix tool kit
UNIX comes with a wide range of general purpose tools,text
manipulation utilities called
filters,compilers,interpreters,system administrator tools
etc.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 4
Module-1 Getting Started and Understanding Unix Commands
6. Pattern matching
Unix supports many pattern matching features which helps in
the reduction of the typing payload.Some of the pattern
matching features are the use of metacharacters .One such
metacharacter is “*”,which stands for zero or more characters
.chap* can indicate all strings starting with chap followed
by zero or more characters.
7. Programming Facility
The UNIX Shell is also a programming language,initially
dessigned for programmer.It has control structures,loops and
variables which can be used to design shell scripts and can
be used for text manipulation tasks.
8. Portability and system calls
All flavours of UNIX operating system uses the same set of
system calls.A programmer can write into a file without
knowing the complexities of the underlying write() operation.
9. Documentation
Unix has very good documentation with principal online help
available with the man command.Also one can browse through
rich internet sources,Newsgroups,FAQ,Magazine etc.
-------------------------------------------------------------
Working out commands
Change Password:passwd
All Unix systems require passwords to help ensure that your files
and data remain your own and that the system itself is secure from
hackers and crackers. Here are the steps to change your password:
1. To start, type passwd at command prompt as shown below.
2. Enter your old password the one you're currently using.
3. Type in your new password. Always keep your password complex
enough so that no body can guess it. But make sure, you remember
it.
Eg:$ passwd
Changing password for cse
(current) Unix password:******
New UNIX password:*******
Retype new UNIX password:*******
passwd: all authentication tokens updated successfully
If the old password is forgotten only administrator can change the
passwd of any user by executing passwd command
eg:passwd cse
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 5
Module-1 Getting Started and Understanding Unix Commands
who:Who are the users logged in
To know who is logged in to the computer at the same time.
There are three commands are available to get you this
information, based on how much you'd like to learn about
the other users: users, who, and w.
$users
cse
$who
cse tty7 2017-09-11 09:18 (:0)
If multiple users are logged in at the same time to know the login
of the session we can use the command whoami
$ whoami
cse
w command on your system would list down few more information
associated with
the users logged in the system.
$w
14:33:19 up 9 days, 5:14, 3 users, load average: 0.24, 0.29,
0.35
USER TTY FROM LOGIN@ IDLE JCPU PCPU
WHAT
cse tty7 :0 11Sep17 9days 1:04m 0.12s
/bin/sh /usr/li
tty:Know your terminal name
$tty
/dev/pts/0
NOTE:In Unix even devices are treated as files.
Know your Shell and Terminal
To know the shell which one is using use the command
$echo $SHELL
/bin/bash(Bourne again Shell)
In linux we have bash shell.Other types of shells are K Shell,C
Shell,Bourne Shell etc
$echo $TERM-To know the type of terminal
xterm-256color
NOTE:TERM and SHELL are builtin variables
IMPORTANT OBSERVATIONS ABOUT UNIX COMMANDS
Commands are case sensitive
passwd and PASSWD are different
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 6
Module-1 Getting Started and Understanding Unix Commands
commands are very short
Each command is designed to do a simple task.
Every command to execute one has to hit [Enter] Key
When things go wrong there are many key shortcuts that one
can use
If backspace doesnt work -ctrl+h
If command has to be interrupted-ctrl+c
To logout:ctrl+d
To kill a command line without execution:ctrl+u
To stop Scrolling and to lock the keyboard:ctrl+s and to come
out of the same ctrl+q
Alternative to [enter] key:ctrl+j,ctrl+m
Understanding UNIX commands
General Features of a Unix Command
commands are very short(ls,cat,who)
Each command is designed to do a simple task.
In unix ,commands are also files generally stored either
in /bin or /bin or /usr/bin,/usr/sbin.
Commands are case sensitive
The PATH:Locating commands
The PATH environment variable
When you specify a command like date, the system will locate the
associated file from a list of directories specified in the PATH
variable and then executes it. The PATH variable normally includes
the current directory also.
Whenever you enter any UNIX command, you are actually specifying
the name of an executable file located somewhere on the system.
The system goes through the following steps in order to determine
which program to execute:
1. Built in commands (such as cd and history) are executed within
the shell.
2. If an absolute path name (such as /bin/ls) or a relative path
name (such as ./myprog),
the system executes the program from the specified directory.
3. Otherwise the PATH variable is used.
The POSIX standards
With the budding of many UNIX flavours with their own application
program interface,it was difficulty for the application
programmers to port their applications.To overcome this problem
IEEE society formed a special task force caalled POSIX in 1980s to
create a set of standards for OS interfacing.The Portable
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 7
Module-1 Getting Started and Understanding Unix Commands
Operating System Interface (POSIX) is an IEEE standard that helps
compatibility and portability between operating systems.
Theoretically, POSIX compliant source code should be seamlessly
portable. In the real world, application transition often runs
into system specific issues. But POSIX compliance makes it simpler
to port applications.Several subgroups of POSIX such as
POSIX.1,POSIX.1b,and POSIX.1c are concerned with the development
of set of standards for system developers.
POSIX.1 committe proposes a standard for a base operating system
application program interface-A standard set of APIs for
manipulation of files and processes .It was formally known as the
IEEE standard 1003.1-1990.
POSIX 1.b(1003.4-1994) -a set of standards for a real time
operating system interface which includes interprocess
communication(IPC)
POSIX 1.c-a set of standards for multithreading programming
interface.
All the modern Operating systems developed are all POSIX.1
compliant,though they might also have system specific APIs.
To ensure a user program conforms to the POSIX.1 standard,user
should either define the manifested constant -POSIX_SOURCE at the
beginning of each source module of the program before inclusion of
any headers as
#define POSIX_SOURCE
or specify the -D_POSIX_SOURCE option to a C++ compiler in a
compilation
%cc -D_POSIX_SOURCE *.C
This manifested constant is used by C++ compiler to filter out all
non POSIX.1 and non ANSI C standard codes(functions,data
types,manifested constants etc) from headers used by the user
program.
POSIX.1b defines a different manifested constant to check
conformance of user programs to that standard.The new macro is
POSIX_C_SOURCE and its value is timestamp indicating the POSIX
version to which the user program conforms.Possible values of this
macro are
_POSIX _C _SOURCE MEANING
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 8
Module-1 Getting Started and Understanding Unix Commands
VALUE(YYYYMMLong Integer)
198808L FIRST VERSION OF POSIX.1
COMPLIANCE
199009L SECOND VERSION OF POSIX.1
COMPLIANCE
199309L POSIX.1 AND POSIX.1b COMPLIANCE
_POSIX_C_SOURCE may be used in place of _POSIX_SOURCE. However,
some systems that support POSIX.1 only may not accept the
POSIX_C_SOURCE definition. There is also a _POSIX_VERSION constant
defined in unistd.h header file which conforms to the POSIX
VERSION to which the system conforms.
The POSIX Environment
Although POSIX was developed on UNIX, a POSIX complaint system is
not necessarily a UNIX system. A few UNIX conventions have
different meanings according to the POSIX standards. Most C and C+
+ header files are stored under the /usr/include directory in any
UNIX system and each of them is referenced by #include
<header_file_name>
This method is adopted in POSIX. There need not be a physical file
of that name existing on a POSIX conforming system.
The POSIX Feature Test Macros
POSIX.1 defines a set of feature test macro’s which if defined on
a system, means that the system has implemented the corresponding
features. All these test macros are defined in <unistd.h> header.
FEATURE TEST MACRO EFFECTS IF DEFINED
_POSIX_JOB_CONTROL The system supports job control
_POSIX_SAVED_ID keeps saved set-UID
and set-GID
_POSIX_CHOWN_RESTRICTED If return value==-1,then
user may change ownership of
files owned by them else
only users with special
privilege can do so
_POSIX_NO_TRUNC If the defined value is -1, any
long pathname passed to an API
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 9
Module-1 Getting Started and Understanding Unix Commands
is silently
truncated to NAME_MAX bytes,
otherwise error is generated.
_POSIX_VDISABLE If defined value is -1, there is
no disabling character for
special characters for all
terminal device files. Otherwise
the value is the disabling
character value.
Program to print POSIX defined configuration options supported on
any given system.
/* show_test_macros.C */
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE
199309L
#include<iostream.h>
#include<unistd.h>
int main()
{
#ifdef _POSIX_JOB_CONTROL
cout<<“system supports job control”;
#else
cout<<“ system does not support job control\n”;
#endif
#ifdef _POSIX_SAVED_IDS
cout<<“ system supports saved set-UID and set-GID”;
#else
cout<<“ system does not support set-uid and gid\n”;
#endif
#ifdef _POSIX_CHOWN_RESTRICTED
cout<<“chown_restricted option is :”
<<_POSIX_CHOWN_RESTRICTED<<endl;
#else
cout<<”system does not support”
<<” chown_restricted option\n”;
#endif
#ifdef _POSIX_NO_TRUNC
cout<<”pathname trunc option is:”
<< _POSIX_NO_TRUNC<<endl;
#else
cout<<” system does not support system-wide pathname”
<<”trunc option\n”;
#endif
#ifdef _POSIX_VDISABLE
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 10
Module-1 Getting Started and Understanding Unix Commands
cout<<“disable char. for terminal files is:”
<<_POSIX_VDISABLE<<endl;
#else
cout<<“ system does not support _POSIX_VDISABLE \n”;
#endif
return 0;
}
Limits checking at Compile time and at Run time
POSIX.1 and POSIX.1b defines a set of system configuration limits
in the form of manifested constants in the <limits.h> header.
The following is a list of POSIX.1 – defined constants in the
<limits.h> header.
Compile time limit Min Value Meaning
_POSIX_CHILD_MAX 6 Maximum number of
child processes that
may be created at
any one time by a
process.
_POSIX_OPEN_MAX 16 Maximum number of
files that a process
can open
simultaneously.
_POSIX_STREAM_MAX 8 Maximum number of I/O
streams opened by a
process
simultaneously.
_POSIX_ARG_MAX 4096 Maximum size, in bytes
of arguments that may
be passed to
an exec function.
_POSIX_NGROUP_MAX 0 Maximum number of
supplemental groups to
which a
process may belong
_POSIX_PATH_MAX 255 Maximum number of
characters allowed in
a path name
_POSIX_NAME_MAX 14 Maximum number of
characters allowed in
a file name
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 11
Module-1 Getting Started and Understanding Unix Commands
_POSIX_LINK_MAX 8 Maximum number of
links a file may have
_POSIX_PIPE_BUF 512 Maximum size of a
block of data that may
be atomically read
from or written to a
pipe
_POSIX_MAX_INPUT 255 Maximum capacity of a
terminal’s input queue
(bytes)
_POSIX_MAX_CANON 255 Maximum size of a
terminal’s canonical
input queue
_POSIX_SSIZE_MAX 32767 Maximum value that can
be stored in a
ssize_t-typed object
_POSIX_TZNAME_MAX 3 Maximum number of
characters in a time
zone name
The following is a list of POSIX.1b – defined constants:
Compile time limit Min. Value Meaning
_POSIX_AIO_MAX 1 Number of simultaneous
asynchronous I/O.
_POSIX_AIO_LISTIO_MAX 2 Maximum number of
operations in one listio.
_POSIX_TIMER_MAX 32 Maximum number of timers
that can be used
simultaneously by
a process.
_POSIX_DELAYTIMER_MAX 32 Maximum number of overruns
allowed per timer.
_POSIX_MQ_OPEN_MAX 2 Maximum number of message
queues that may be accessed
simultaneously per process
_POSIX_MQ_PRIO_MAX 2 Maximum number of message
priorities that can be
assigned to the messages
_POSIX_RTSIG_MAX 8 Maximum number of real time
signals.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 12
Module-1 Getting Started and Understanding Unix Commands
_POSIX_SIGQUEUE_MAX 32 Maximum number of real time
signals that a process may
queue at any time.
_POSIX_SEM_NSEMS_MAX 256 Maximum number of semaphores
that may be used
simultaneously per process.
_POSIX_SEM_VALUE_MAX 32767 Maximum value that may be
assigned to a semaphore.
POSIX defined constants specify only the minimum values for some
system configuration limits.A POSIX conforming system may be
configured with higher values for these limits.
To find out the actual implemented configuration limits system-
wide or on individual objects,we can use the sysconf,pathconf,and
fpathconf functions to query these limits values at runtime.
Thesysconf() function provides a method for the application to
determine the current value of a configurable system limit or
option (variable)
Thefpathconf() and pathconf() functions shall determine the
current value of a configurable limit or option ( variable) that is
associated with a file or directory. It Returns file related
configuration limits.
The prototype for these functions are as follows
#include<unistd.h>
long sysconf(const int limit_name);
long pathconf(const char *pathname, int flimit_name);
long fpathconf(const int fd, int flimit_name);
The limit_name argument value is a manifested constant as defined
in the <unistd.h> header. The possible values and the
corresponding data returned by the sysconf function are:
Limit Sysconf return data value
_SC_ARG_MAX Maximum size of argument values
(in bytes) that may be passed to
an exec API call
_SC_CHILD_MAX Maximum number of child
processes that may be owned by a
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 13
Module-1 Getting Started and Understanding Unix Commands
process simultaneously
_SC_OPEN_MAX Maximum number of opened files
per process
_SC_NGROUPS_MAX Maximum number of supplemental
groups per process
_SC_CLK_TCK The number of clock ticks per
second
_SC_JOB_CONTROL The _POSIX_JOB_CONTROL value
_SC_SAVED_IDS The _POSIX_SAVED_IDS value
_SC_VERSION The _POSIX_VERSION value
_SC_TIMERS The _POSIX_TIMERS value
_SC_DELAYTIMERS_MAX Maximum number of overruns
allowed per timer
_SC_RTSIG_MAX Maximum number of real time
signals.
_SC_MQ_OPEN_MAX Maximum number of messages
queues per process.
_SC_MQ_PRIO_MAX Maximum priority value
assignable to a message
_SC_SEM_MSEMS_MAX Maximum number of semaphores per
process
_SC_SEM_VALUE_MAX Maximum value assignable to a
semaphore.
_SC_SIGQUEUE_MAX Maximum number of real time
signals that a process may queue
at any one time
_SC_AIO_LISTIO_MAX Maximum number of operations in
one listio.
_SC_AIO_MAX Number of simultaneous
asynchronous I/O
All constants used as a sysconf argument value have the _SC
prefix. Similarly the flimit_name argument value is a manifested
constant defined by the <unistd.h> header. These constants all
have the _PC_ prefix. Following is the list of some of the
constants and their corresponding return values from either
pathconf or fpathconf functions for a named file object.
Limit pathconf return data value
_PC_CHOWN_RESTRICTED The _POSIX_CHOWN_RESTRICTED
value
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 14
Module-1 Getting Started and Understanding Unix Commands
_PC_NO_TRUNC Returns the _POSIX_NO_TRUNC
value
_PC_VDISABLE Returns the _POSIX_VDISABLE
value
_PC_PATH_MAX Maximum length of a pathname (in
bytes)
_PC_NAME_MAX Maximum length of a filename (in
bytes)
_PC_LINK_MAX Maximum number of links a file
may have
_PC_PIPE_BUF Maximum size of a block of data
that may be read from or written
to a pipe
_PC_MAX_CANON maximum size of a terminal’s
canonical input queue
_PC_MAX_INPUT Maximum capacity of a terminal’s
input queue
These variables may be used at compile time, such as the
following:
char pathname [ _POSIX_PATH_MAX + 1 ];
for (int i=0; i < _POSIX_OPEN_MAX; i++)
close(i);
//close all file descriptors
The following test_config.C illustrates the use of sysconf,
pathcong and fpathconf:
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include<stdio.h>
#include<iostream.h>
#include<unistd.h>
int main()
{
int res;
if((res=sysconf(_SC_OPEN_MAX))==-1)
perror(“sysconf”);
else
cout<<”OPEN_MAX:”<<res<<endl;
if((res=pathconf(“/”,_PC_PATH_MAX))==-1)
perror(“pathconf”);
else
cout<<”max path name:”<<(res+1)<<endl;
if((res=fpathconf(0,_PC_CHOWN_RESTRICTED))==-1)
perror(“fpathconf”);
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 15
Module-1 Getting Started and Understanding Unix Commands
else
cout<<”chown_restricted for stdin:”<<res<<endl;
return 0;
}
UNIX AND POSIX APIs
API ‘s are a set of application programming interface functions
that can be called by user programs to perform system specific
functions.
Most UNIX systems provide a common set of API’s to perform the
following functions.
• Determine the system configuration and user information.
• Files manipulation.
• Processes creation and control.
• Inter-process communication.
• Signals and daemons
• Network communication.
The POSIX APIs
In general POSIX API’s uses and behaviours’ are similar to those
of Unix API’s. However, user’s programs should define the
_POSIX_SOURCE or _POSIX_C_SOURCE in their programs to enable the
POSIX API’s declaration in header files that they include.
The UNIX and POSIX Development Environment
POSIX provides portability at the source level. This means that
you transport your source program to the target machine, compile
it with the standard C compiler using conforming headers and link
it with the standard libraries Some commonly used POSIX.1 and
UNIX API’s are declared in <unistd.h> header. Most of POSIX.1,
POSIX.1b and UNIX API object code is stored in the libc.a and
lib.so libraries.
API Common Characteristics
Many APIs returns an integer value which indicates the termination
status of their execution.
API return -1 to indicate the execution has failed, and the global
variable errno is set with an error code. A user process may call
perror function to print a diagnostic message of the failure to
the std o/p, or it may call strerror function and gives it errno
as the actual argument value; the strerror function returns a
diagnostic message string and the user process may print that
message in its preferred way the possible error status codes that
may be assigned to errno by any API are defined in the <errno.h>
header.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 16
Module-1 Getting Started and Understanding Unix Commands
Following is a list of commonly occur error status codes and their
meanings:
ERROR STATUS CODE MEANING
EACCESS A process does not have access
permission to perform an
operation via a API.
EPERM A API was aborted because the
calling process does not have the
superuser privilege.
ENOENT An invalid filename was specified
to an API.
BADF A API was called with invalid
file descriptor.
EINTR A API execution was aborted due
to a signal interruption
EAGAIN A API was aborted because some
system resource it requested was
temporarily unavailable. The API
should be called again later.
ENOMEM A API was aborted because it
could not allocate dynamic
memory.
EIO I/O error occurred in a API
execution.
EPIPE A API attempted to write data to
a pipe which has no reader.
EFAULT A API was passed an invalid
address in one of its argument.
ENOEXEC A API could not execute a program
via one of the exec API
ECHILD A process does not have any child
process which it can wait on.
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 17
Module-1 Getting Started and Understanding Unix Commands
PREPARED BY Ms.UMA.N,ASST.PROF,NHCE 18