0% found this document useful (0 votes)
33 views42 pages

UNIT-1-USP

Uploaded by

h c srihari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views42 pages

UNIT-1-USP

Uploaded by

h c srihari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction 1-1

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.

Functions of Operating System


1. Memory Management
2. Processor Management/Scheduling
3. Device Management
4. File Management

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.

Examples : Payroll system, Bank statement, etc.

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.

Examples : LOCUS , Solaris etc.

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.

Examples : UNIX/Linux and the Microsoft family of Windows Servers


5. Real-Time OS (RTOS)

In these systems, the time interval for processing and responding to


inputs is very small. Therefore, due to this quality, these are used in
real-time situations.
Examples : missile systems, robots, etc.

There are two categories which are as follows:


a) Hard Real-Time Systems : In this, the time constraint is very short
and strict. Even seconds of delay is not acceptable.
b) Soft Real-Time Systems : In this, the time constraint is not so
short and strict.

Examples : FreeRTOS, VxWorks, QNX, ThreadX, Nucleus RTOS


etc.
Introduction: UNIX and ANSI Standards


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

• UNIX systems have been developed from AT&T system V and


BSD 4.x UNIX.
• In late 1980, AT&T and Sun Microsystems created UNIX system
V , Release 4 (V.4).
• In late 1980s, few organizations, proposed several standards for a
UNIX like O.S. & the C language programming environment.
These are,
• ANSI C(American National Standard Institute) &
• POSIX (IEEE Institute of Electrical and Electronics Engineering).

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

UNIX : Is a computer operating system originally developed in 1969 by a


group of AT&T employees at Bell Labs, including Ken Thompson, Dennis
Ritchie, Douglas McElroy and Joe Ossanna.

The ANSI C Standard


In 1989, American National Standard Institute (ANSI) proposed C
programming language standard X3.159-1989 to standardize the language
constructs and libraries. This is termed as ANSI C standard. This attempt to
unify the implementation of the C language supported on all computer
system.

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);

eg: int printf(const char* fmt,...........);


specify variable number of arguments.

Introduction 1-13
• Support of the const and volatile data type qualifiers

• The const keyword declares that some data cannot be changed.

Eg: int printf( const char* fmt,...........);


Declares a fmt argument that is of a const char * data type, meaning that
the function printf cannot modify data in any character array that is
passed as an actual argument value to fmt.

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

ANSI C supports internationalisation by allowing C-program


to use wide characters. Wide characters use more than one
byte of storage per character.

 ANSI C defines the setlocale function, which allows users to


specify the format of date, monetary and real number
representations.
eg: most countries display the date in dd/mm/yyyy format
whereas US displays it in mm/dd/yyyy format

Introduction 1-16
Function prototype of setlocale function is:

#include< locale.h>
char setlocale (int category, const char* locale);

• The setlocale function prototype and possible values of the


category argument are declared in the <locale.h> header.
• The category values specify what format class(es) is to be
changed.
• Some of the possible values of the category argument are:

Introduction 1-17
• Permit function pointers without dereferencing

ANSI C specifies that a function pointer may be used like a function


name. No referencing is needed when calling a function whose address is
contained in the pointer.
Example : The following statement given below defines a function
pointer funptr, which contains the address of the function foo.

extern void foo(double xyz,const int *ptr);


void (*funptr)(double,const int *)=foo;
The function foo may be invoked by either directly calling foo or via the
funptr.
foo(12.78,”Hello world”);
funptr(12.78,”Hello world”);

K&R C requires funptr be dereferenced to call foo.


(* funptr) (13.48,”Hello usp”);

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.

_TIME_ Value is the time 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:

• POSIX or “Portable Operating System Interface” is the name of a


family of related standards specified by the IEEE to define the
application-programming interface (API).
• Its subgroups are , POSIX.1, POSIX.1b & POSIX.1c. These are
concerned with the development of set of standards for system
developers.
• POSIX.1 : It proposes a standard for a base operating system API;
it specifies APIs for the manipulating of files and processes.
• It is formally known as IEEE standard 1003.1-1990 and it was
also adopted by the ISO as the international standard ISO/IEC
9945:1:1990.

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

• _POSIX_JOB_CONTROL The system supports the BSD style job control


• _POSIX_SAVED_IDS Each process running on the system keeps the saved
set UID and the set- GID, so that they can change its
effective user-ID and group-ID to those
values via seteuid and setegid API's.
• _POSIX_CHOWN_RESTRICTED If the defined value is -1, users may change
ownership of files owned by them, otherwise
only users with special privilege may change
ownership of any file on the system.
• _POSIX_NO_TRUNC If the defined value is -1, any long pathname passed
to an API 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.

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 :

• API : 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.

Introduction 1-30
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.
Following is a list of POSIX.1 – defined constants in the <limits.h> header.

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.

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.

Introduction 1-41
List of commonly occur error status codes and their meanings:

Introduction 1-42

You might also like