UNIT-1-USP
UNIT-1-USP
Introduction 1-2
Unit – 1 8 Hours
Introduction to UNIX and its Commands: UNIX and ANSI Standards: The
ANSI C Standard, The POSIX Standards, UNIX and POSIX APIs: The
POSIX APIs, The UNIX and POSIX Development Environment, API
Common Characteristics,
Basics of working with UNIX Operating system and executing UNIX
General commands like calendar, date etc.
Introduction to the operating system (OS) :
Definition : An operating system (OS) is a program that acts as an
interface between the system hardware and the user.
• It handles all the interactions between the software and the hardware.
• It performs all the functions like handling memory, processes, the
interaction between hardware and software, etc.
Introduction 1-4
Types of Operating System :
1. Batch OS :
In this system, the OS does not forward the jobs/tasks directly to the
CPU. It works by grouping together similar types of jobs under one
category.
2. Time-Shared OS :
When more than one task execution takes place on the system it is
called time-shared OS. As multiple tasks can run at the system at a time
as per requirement, hence, they all share the CPU time one by one.
Therefore, the name multitasking. The time that each task gets is called
quantum.
Examples : LINUX,UNIX, MULTICS, Windows NT server,Windows
2000 server etc.
Introduction 1-5
3. Distributed OS :
n this system, there is more than one CPU present. The OS distributes the
tasks among all the processors. The processors do not share any memory or
clock time. OS handles all communication between them through various
communication lines.
4. Network OS
In these OS various systems are connected to a server. It allows the system
to share resources such as files, printers, applications, etc. Moreover, it gives
the capability to serve to manage these resources.
●
The ANSI C Standard
●
The ANSI/ISO C++ Standards
●
Difference between ANSI C and C++
●
The POSIX Standards
●
The POSIX.1 FIPS Standard
●
The X/Open Standards
Introduction: UNIX and ANSI Standards
Introduction 1-9
●
Why study standards ?
– ANSI C, IEEE POSIX.1, POSIX.1b, and POSIX.1c
– Develop applications that can be readily ported to different UNIX
systems, as well as to POSIX-compliant systems
– POSX and ANSI C standards help users create highly portable
applications on different computer platforms.
– Applications run on multiple UNIX platforms by various vendors
Introduction 1-10
Introduction: UNIX and ANSI Standards
Introduction 1-11
Differences between ANSI C and K&R C [Kernighan and
Ritchie]
Function prototyping.
Support of the const and volatile data type qualifiers.
Support for wide characters and internationalization.
Permit function pointers to be used without dereferencing.
• Function prototyping
ANSI C adopts C++ function prototype technique where function definition
and declaration include function names, arguments’ data types, and return
value data types. This enables ANSI C compilers to check for function calls
in user programs that pass invalid number of arguments or incompatible
arguments’ data type.
These fix a major weakness of K&R C compilers: invalid function calls in
user programs, often pass compilation but cause programs to crash when they
are executed.
Eg: unsigned long foo(char * fmt, double data)
{
/*body of foo*/
}
External declaration of this function foo is,
unsigned long foo(char * fmt, double data);
Introduction 1-13
• Support of the const and volatile data type qualifiers
The Volatile keyword specifies that the values of some variables may
change asynchronously, giving an hint to the compiler’s optimization
algorithm not to remove any “redundant” statements that involve
“volatile” objects.
Introduction 1-14
eg: char get_io()
{
volatile char* io_port = 0x7777;
char ch = *io_port; /*read first byte of data*/
ch = *io_port; /*read second byte of data*/
}
If io_port variable is not declared to be volatile when the program is
compiled, the compiler may eliminate second ch = *io_port statement, as
it is considered redundant with respect to the previous statement.
• The const and volatile data type qualifiers are also supported in C++.
Introduction 1-15
• Support wide characters and internationalization
Introduction 1-16
Function prototype of setlocale function is:
#include< locale.h>
char setlocale (int category, const char* locale);
Introduction 1-17
• Permit function pointers without dereferencing
Introduction 1-18
ANSI C also defines a set of C processor(cpp) symbols, which may be
used in user programs. These symbols are assigned actual values at
compilation time.
CPP Symbols USE
_STDC_ Feature Test Macro. Value is 1 if a
compiler is ANSI C conforming, 0
otherwise.
_LINE_ Evaluated to a physical line no. of a source
file for which this symbol is a reference.
_FILE_ Value is the file name of a module that
contains this symbol.
_DATE_ Value is the date that a module containing
this symbol is compiled.
Introduction 1-19
The following test_ansi_c.c program illustrates the use of these symbols:
#include<stdio.h>
int main()
{
#if _STDC_==0
printf(“cc is not ANSI C compliant”);
#else
printf(“%s compiled at %s:%s.
This statement is at line %d\n”,
_FILE_ , _DATE_ , _TIME_ , _LINE_ );
#endif
Return 0;
}
Introduction 1-20
The ANSI/ISO C++ Standard
These compilers support C++ classes, derived classes, virtual functions,
operator overloading. Furthermore, they should also support template
classes, template functions, exception handling and the iostream library
classes.
Differences between ANSI C and C++
Introduction 1-21
The POSIX standards:
Introduction 1-22
POSIX.1b
• This committee proposes a set of standard APIs for a real time OS
interface; these include IPC (inter-process communication).
• This standard is formally known as IEEE standard 1003.4-1993.
• POSIX.1c
• This standard specifies multi-threaded programming interface. This
is the newest POSIX standard.
• These standards are proposed for a generic OS that is not necessarily
be UNIX system.
• E.g.: VMS (vendor management system) from Digital Equipment
Corporation, OS/2 from IBM, & Windows NT from Microsoft
Corporation are POSIX-compliant, yet they are not UNIX systems.
Introduction 1-23
• To ensure a user program conforms to POSIX.1 standard, the user
should either define the manifested constant _POSIX_SOURCE at
the beginning of each source module of the program (before
inclusion of any header) as;
• #define _POSIX_SOURCE
• Or specify the -D_POSIX_SOURCE option to a C++ compiler
(CC) in a compilation;
• % CC -D_POSIX_SOURCE *.C
• POSIX.1b defines different manifested constant to check
conformance of user program to that standard. The new macro is
_POSIX_C_SOURCE and its value indicates POSIX version to
which a user program conforms. Its value can be,
Introduction 1-24
Program to check and display _POSIX_VERSION constant of the system on
which it is run.
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include<iostream.h> // provides basic input and output services for C++
programs.
#include<unistd.h> // is the name of the header file that provides access to the
POSIX operating system API.
int main()
{
#ifdef _POSIX_VERSION
cout<<“System conforms to POSIX”<<“_POSIX_VERSION<<endl;
#else
cout<<“_POSIX_VERSION undefined\n”;
#endif
return 0;
}
Introduction 1-25
The POSIX Environment
• Although POSIX was developed on UNIX, a POSIX complaint system
is not necessarily a UNIX system.
• 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.
Introduction 1-26
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
Introduction 1-27
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
Introduction 1-28
#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
cout<<“disable char. for terminal files is:”
<<_POSIX_VDISABLE<<endl;
#else
cout<<“ system does not support _POSIX_VDISABLE \n”;
#endif
return 0;
}
Introduction 1-29
UNIX AND POSIX APIs :
Introduction 1-30
Limits checking at Compile time and at Run time
Introduction 1-31
Introduction 1-32
The following is a list of POSIX.1b – defined constants:
Introduction 1-33
Prototypes:
#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,
Introduction 1-34
Introduction 1-35
• The flimit_name argument value is a manifested constant defined by
the <unistd.h> header. These constants all have the _PC_ prefix.
A list of some of the constants and their corresponding return values from
either pathconf or fpathconf functions for a named file object.
Introduction 1-36
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, pathconf 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;
Introduction 1-37
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”);
else
cout<<”chown_restricted for stdin:”<<res<<endl;
return 0;
}
Introduction 1-38
The POSIX.1 FIPS Standard
FIPS stands for Federal Information Processing Standard. It is a
restriction of the POSIx.1 – 1988 standard, and it requires the
following features to be implemented in all FIPS-conforming systems:
Job control.
Saved set-UID and saved set-GID.
Long path name is not supported.
The _POSIX_CHOWN_RESTRICTED must be defined.
The _POSIX_VDISABLE symbol must be defined.
The NGROUP_MAX symbol’s value must be at least 8.
The read and write API should return the number of bytes that have
been transferred after the APIs have been interrupted by signals.
The group ID of a newly created file must inherit the group ID of its
containing directory. The FIPS standard is a more restrictive version of
the POSIX.1 standard.
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.
Introduction 1-41
List of commonly occur error status codes and their meanings:
Introduction 1-42