UNIX and POSIX Standards

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

C Language.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Guide To UNIX Using Linux Third Edition
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 5 Acknowledgements: The syllabus and power point presentations are modified versions.
C Programming language
C Programming Lecture 4 : Variables , Data Types
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
UNIX and Shell Programming (06CS36) Unit 1 Continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of.
Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 2: Linux & POSIX “She sells bash shells by the C shore”
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 C++ Syntax and Semantics, and the Program Development Process.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Advanced Programming in the UNIX Environment Hop Lee.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C is a high level language (HLL)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 15 - C++ As A "Better C"
Chapter 13 Introduction to C++ Language
Computer Science 210 Computer Organization
Introduction to C++ Systems Programming.
Chapter 7 Text Input/Output Objectives
Advanced UNIX progamming
History of ‘C’ Root of the morden language is ALGOL It’s first
Unix and Posix Standards
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Computer Science 210 Computer Organization
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Chapter 14 - Advanced C Topics
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Advanced UNIX progamming
Variables have attributes
The C Language: Intro.
Review.
Presentation transcript:

UNIX and POSIX Standards

The ANSI C Standard ANSIC Standard X3.159-1989 The difference between ANSI C AND K&R C Function prototyping: * ANSI C : data-type function-name (data type parameter name,……………………..); Ex: int f1(int a , int b);

* K&R C : data-type function-name (parameter name,……………………..) EX: int f1(a , b); int a, b;

Constant and volatile qualifiers * Present in ANSI C not in K&R C * const-implies data can’t be changed /*here printf can’t change the value of x */ int printf(const char* x,….) { }

Volatile qualifier : implies the compiler can’t make any optimization of the variable EX : char get_io() { volatile char* io_port=0x7777; char ch=*io_port; ch = *io_port; }

Wide character support and internationalization *support to store characters which occupy more than one byte *ANSI C defines SETLOCALE function *which helps to specify the format of date monetary and real number presentation

SETLOCALE #include <locale.h> Char setlocale (int category, const char* locale); Category 1 Category 2 LC_TYPE en_US//US LC_MONETARY fr_FR//French LC_NUMERIC de_DE//German LC_TIME LC_ALL

Permit function pointers to be used without dereferencing *ANSI C –a function pointer can be used like a function *K&R C – requires the pointer to be de-referenced to call the function

Feature test macros _STDC_ : 1-if underlying system is ANSI C compliant 0-Otherwise _LINE_ : Physical line number of the module _FILE_ : filename of module where the symbol is present _DATE_ : date of compilation of the module _TIME_ : time of compilation of the module

#include <stdio.h> int main() { #if __STDC__ == 0 && !defined(__cplusplus) printf("cc is not ANSI C compliant\n"); #else printf(" %s compiled at %s:%s. This statement is at line %d\n", __FILE__, __DATE__, __TIME__, __LINE__); #endif return 0; }

THE ANSI/ISO C++STANDARD WG21-ISO and ANSI X3J16 : ANSI C/ISO C++ standard Version 3.0 report : c++ should have * classes * derived classes * virtual classes * operator overloading * template classes * template function * exception handling * io stream

ANSI C AND ANSI C++ ANSI C -default prototype if called before declaration or defn -int f1() is same as int f1(…) -no type safe linkage ANSI C++ - prototype is mandatory int f1(void) -type safe linkage

Posix.1 : IEEE 1003.1-1990 adapted by ISO THE POSIX STANDARDS Posix.1 : IEEE 1003.1-1990 adapted by ISO as ISO/IEC 9945:1:1990 standard *gives standard for base operating system API Posix.1b : IEEE 1003.4-1993 * gives standard APIs for real time operating system interface including interprocess communication

Other POSIX compliant systems Posix.1c : specifies multithread programming interface Other POSIX compliant systems *VMS of DEC *OS/2 of IBM *W-NT of Microsoft *Sun solaris 2.t *HP-UX 9.05

To ensure program confirms to POSIX.1 standard user should define _POSIX_SOURCE as #define _POSIX_SOURCE OR Specify -D _POSIX_SOURCE to a C++ compiler

_POSIX_C_SOURCE : its value indicating POSIX version _POSIX_C_SOURCE value----Meaning 198808L---- First version of POSIX.1 compliance 199009L---- Second version of POSIX.1 199309L---- POSIX.1 and POSIX.1b

#define _POSIX_SOURCE #define _POSIX_C_SOURCE 199309L #include <iostream.h> #include <unistd.h> int main() { #ifdef _POSIX_VERSION cout << "System conforms to POSIX: " << _POSIX_VERSION << endl; #else cout << "_POSIX_VERSION is undefined\n"; #endif return 0; }

POSIX ENVIRONMENT Difference between POSIX and UNIX * In UNIX C and C++ header files are included in /usr/include In POSIX they are just headers not header files and /usr/include need not exist * UNIX – Super user has special previlege and the super user ID is always 0 POSIX – Does not support the concept of super user nor the ID is 0

THE POSIX FEATURE TEST MACROS _POSIX_JOB_CONTROL— The system supports BSD type job control _POSIX_SAVED_ID — keeps saved set-UID and set-GID _POSIX_CHOWN_RESTRICTED — If -1 user may change ownership of files owned by them else only users with special privilege can do so

_POSIX_NO_TRUNC — If -1 then any long path name is automatically truncated to NAME_MAX else an error is generated _POSIX_VDISABLE — If -1 then there is no disabling character for special characters for all terminal devices otherwise the value is the disabling character value

#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\n"; #else cout << "System does not support job control\n"; #endif

#ifdef _POSIX_SAVED_IDS cout << "System supports saved set-UID and saved set-GID\n"; #else cout << "System does not support saved set-UID and saved set-GID\n"; #endif

#ifdef _POSIX_CHOWN_RESTRICTED cout << "chown restricted option is: " << _POSIX_CHOWN_RESTRICTED <<endl; #else cout << "System does not support system-wide chown_restricted option\n"; #endif

#ifdef _POSIX_NO_TRUNC cout << "Pathname trucnation option is: " << _POSIX_NO_TRUNC << endl; #else cout << "System does not support system-wide pathname trucnation option\n"; #endif }

#ifdef _POSIX_VDISABLE cout << "Diable character for terminal files is: " << _POSIX_VDISABLE << endl; #else cout << "System does not support _POSIX_VDISABLE\n"; #endif return 0;

Certain constants defined in <limit.h> _POSIX_CHILD_MAX 6 max number of child processes that can be created at any one time by a process _POSIX_OPEN_MAX 16 max number of files that can be opened simultaneously by a process _POSIX_STREAM_MAX 8 max number of I/Ostreams that can be opened

_POSIX_ARG_MAX 4096 max size, in bytes of arguments that can be passed to an exec function call _POSIX_NGROUP_MAX 0 max number of supplemental groups to which a process may belong _POSIX_PATH_MAX 255 max number of characters allowed in a pathname

_POSIX_NAME_MAX 14 max number of characters allowed in a filename _POSIX_LINK_MAX 8 max number of links a file may have _POSIX_PIPE_BUF 512 max size of block of data that can be automatically read from or written to a pipe file

_POSIX_MAX_INPUT 255 max capacity, in bytes, of a terminal’s input queue _POSIX_MAX_CANON 255 canonical input queue _POSIX_SSIZE_MAX 32767 max value that can be stored in a ssize_t- typed object _POSIX_TZNAME_MAX 3 max number of characters in a time zone name

Long sysconf(const int limit_name); Long pathconf(const char* pathname,int flimit_name); Long fpathconf(const int fdesc,int flimitname);

Int res; If(res=sysconf(_SC_OPEN_MAX))==-1) perror(“sysconf”); Else cout<<res; res=pathconf(“/”,_PC_PATH_MAX); Res=fpathconf(0,_PC_CHOWN_RESTRICTED);

THE POSIX.1 FIPS STANDARD Job control : _POSIX_JOB_CONTROL must be defined Saved set-UID and set-GID : _POSIX_SAVED_IDS must be defined Long path name is supported _POSIX_NO_TRUNC != -1 _only authorised user can change ownership _POSIX_CHOWN_RESTRICTED != -1

_POSIX_VDISABLE should be defined NGROUP_MAX – value should be at least 8 Read and write APIs should return the number of bytes transferred after the APIs have been interrupted by signals The group id of newly created file must inherit group ID of its containing directory

THE X/OPEN STANDARDS X/Open portability guide, ISSUE 3 (XPG3) --- 1989 --- 1999 The portability guide specifies a set of common facilities and C application program interface function to be provided on all UNIX-based “open systems”

QUESTIONS What are the major differences between ANSI C and K & R C? explain (10) What is POSIX standard? Give the structure of the program to filter out non-POSIX compliant codes for a user program (10) What is an API ? How are they different from C library functions ? Calling an API is more time consuming than calling a user function . Justify or contradict (5)

Write a POSIX compliant C/C++ program to check following limits (10) Maximum path length Maximum characters in a file name Maximum number of open files per process What is POSIX standard? Explain different subsets of POSIX standard .write the structure of the program to filter out non-POSIX compliant codes for a user program (6)

Write a C++ program that prints the POSIX defined configuration options supported on any given system using feature test macros (8) List out all POSIX.1 and POSIX 1b defined system configuration limits in manifested constants with complete time limit , minimum value and meaning (10)