POSIX Programmer's Guide Writing Portable UNIX Programs with the POSIX.1 Standard

Total Page:16

File Type:pdf, Size:1020Kb

POSIX Programmer's Guide Writing Portable UNIX Programs with the POSIX.1 Standard Page iii POSIX Programmer's Guide Writing Portable UNIX Programs with the POSIX.1 Standard Donald A. Lewine Data General Corporation O'Reilly & Associates, Inc 103 Morris Street, Suite A Sebastopol, CA 95472 Page iv POSIX Programmer's Guide by Donald A. Lewine Editor: Dale Dougherty Copyright © 1991 O'Reilly & Associates, Inc. All rights reserved Printed in the United States of America Printing History April 1991: First edition December 1991: Minor corrections. Appendix G added. July 1992: Minor corrections. November 1992: Minor corrections. March 1994: Minor corrections and updates. NOTICE Portions of this text have been reprinted from IEEE Std 1003.1-1988, IEEE Standard Portable Operating System for Computer Environments, copyright © 1988 by the Institute of Electrical and Electronics Engineers, Inc., and IEEE Std 1003.1-1990, Information Technology—Portable Operating System Interface (POSIX)—Part 1: System Application Program Interface (API) [C Language], copyright © 1990 by the Institute of Electrical and Electronics Engineers, Inc., with the permission of the IEEE Standards Department. Nutshell Handbook and the Nutshell Handbook logo are registered trademarks of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly and Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Please address comments and questions in care of the publisher: O'Reilly & Associates, Inc. INTERNET: [email protected] 103 Morris Street, Suite A Sebastopol, CA 9547 (800) 998-9938 [8/98] ISBN: 0-937175-73-0 Page v To all my students To my wife, Susan, who convinced me to do this book and who put up with all the time this effort took Page vi Acknowledgments I would like to thank all of my students who put up with all of the beta test quality revisions. They provided many useful suggestions. I would like to thank some people who provided very complete technical reviews and provided useful comments: Hal Jespersen (Posix Software Group), Chuck Karish (Mindcraft, Inc.), Thomas Mitas (HBO & Company), Neil Todd (European UNIX Systems User Group), Andy Huber (Data General Corporation), Richard Eckhouse (University of Massachusetts), Andy Silverman (88open Consortium), Henry Spencer (University of Toronto), Jeffrey S. Haemer (Interactive Systems Corporation), Paul Rabin, Dave Kirschen, and Michael Meissner (Open Software Foundation), and John S. Quarterman (Texas Internet Consulting). Thanks go to the following readers of previous printings who pointed out errors and typos: Eric Boweles, Eric Hanchrow, Milt Ratcliff, Stephen J. Friedl, Ed Myer, Chesley Reyburn. Derek M. Jones, Todd Stevenson, Bob Burchfield, Anthony Scian, and Wayne Pallock Thanks to Allen Gray for his help with the reference material. Thanks to Mike Sierra and Ellie Cutler of O'Reilly & Associates for doing the production work and for writing the index. Special thanks to Dale Dougherty for a great job of editing. His many useful suggestions were worth the months they took to implement. Thanks to the POSIX standards committees for making this book possible. Page vii Table of Contents Preface xxiii The POSIX Standard Documents xxiv Guide to POSIX for Programmers xxv Programming Guide xxv Reference Guide and Appendixes xxvi Assumptions xxvi Conventions xxvii Chapter 1 Introduction to POSIX and Portability 1 Who is Backing POSIX? 2 The POSIX Family of Standards 3 The POSIX.1 Standard Document 5 The Design of POSIX 7 POSIX and UNIX 7 7 POSIX and Non-UNIX Operating Systems 8 POSIX, C, ANSI C, and Standard C 8 Why Standard C? 9 Working Outside the Standards 10 Finding The POSIX Libraries 11 Converting Existing Programs 11 Page viii Chapter 2 Developing POSIX Applications 13 The POSIX Development Environment 13 The Standard C Compiler 13 POSIX and C Libraries 14 Converting Existing Programs 15 A Porting Example 16 An Alternate Approach 19 Standard Header Files 20 Template for a POSIX Application 24 /* Feature test switches */ 25 /* System headers */ 25 /* Local headers */ 25 /* Macros */ 25 /* File scope variables */ 25 /* External variables */ 26 /* External functions */ 26 /* Structures and unions */ 26 /* Signal catching functions */ 26 26 /* Functions */ 26 /* Main */ 26 Sample Program 26 Portability Lab 38 Chapter 3 Standard File and Terminal I/O 39 Libraries and System Calls 39 Standard Files 39 Formatted Output 40 Examples 41 Pitfalls 42 The vfprintf(), vprintf(), and vsprintf() Functions 43 Character Output Functions 45 The fputs() and puts() Functions 45 The fputc(), putc(), and putchar() Functions 45 Reading Lines of Input 45 Pitfalls 47 Additional Pitfall 47 Page ix Other Character Input Functions 47 The fgetc(), getc() and getchar() Functions 47 The fgets() Function 48 The gets() Function 48 The ungetc() Function 48 Opening and Closing Files 49 Direct Input/Output functions 50 50 The fwrite() and fread() Functions 50 File Positioning Functions 50 The fgetpos() and fsetpos() Functions 51 The ftell() and fseek() Function 51 The rewind() Function 51 Managing Buffers 52 Sample Program 53 Portability Lab 60 Chapter 4 Files and Directories 63 Portable Filenames 63 Directory Tree 64 Current Working Directory 64 Making and Removing Directories 66 The rmdir() Function 67 Simulating the mkdir() and rmdir() Functions 67 Directory Structure 67 Manipulating Directories 68 Linking to a File 69 Removing a File 69 Renaming a File 69 File Characteristics 69 Retrieving a File's Characteristics 72 Changing File Accessibility 73 Changing the Owner of a File 73 Setting File Access and Modification Times 74 Page x Reading Directories 75 The opendir() Function 76 The readdir() Function 76 The closedir() Function 76 The rewinddir() Function 76 General Comments 76 Complete Example 77 Pitfall: Symbolic Links 83 Portability Lab 84 Chapter 5 Advanced File Operations 85 Primitive File Operations 86 File Descriptors 87 Opening a file 87 Reading from a File 89 Writing to a File 89 Fast File Copy Example 90 Control Operations on a File 92 F_GETFD/F_SETFD 92 F_GETFL/F_SETFL 93 F_SETLK/F_SETLKW/F_GETLK 94 F_DUPFD 95 Setting the File Position 95 The dup() and dup2() Functions 96 Closing a File 96 FIFOs and Pipes 96 96 File Creation Mask 97 The umask() Function 98 Mixing the Levels 98 The fdopen() Function 98 The fileno() Function 98 Pitfalls 99 Portability Lab 99 Page xi Chapter 6 Working with Processes 101 Process Creation 101 The fork() Function 101 The exec() Family of Function 102 Example: Piping Output Through more 103 Portability Note 105 Process Termination 106 The wait() and waitpid() Functions 106 Terminating the Current Process 108 Returning from main() 108 Calling exit() 109 Calling _exit() 109 Calling abort() 110 Terminating Another Process 110 Signals 110 Signal Actions 112 Signal-Catching Functions 112 112 Examine and Change Signal Action 113 Standard C Signals 113 POSIX Signals 114 Example: Timing a System Function 116 Signal Sets 117 The sigemptyset() Function 118 The sigfillset() Function 118 The sigaddset() Function 118 The sigdelset() Function 118 Using the sigset Functions 118 The sigismember() Function 119 The sigprocmask() Function 119 The sigpending() Function 119 Wait for a Signal 120 Sending a Signal 121 Portability Lab 122 Page xii Chapter 7 Obtaining Information at Run-time 123 Process Identification 123 User Identification 123 User IDs 125 Group IDs 126 System Identification 132 Date and Time 133 The time() Function 133 133 The localtime() and gmtime() Functions 133 The mktime() Function 135 The strftime() Function 135 The asctime() and ctime() Functions 137 The difftime() Function 137 The clock() and times() Functions 137 Environment Variables 138 The getenv() Function 139 The sysconf() Function 140 The pathconf() and fpathconf() Functions 142 Portability Lab 143 Chapter 8 Terminal I/0 145 Terminal Concepts 146 Setting Terminal Parameters 147 The tcsetattr() and tcgetattr() Functions 147 The termios Structure 148 System V termio and POSIX termios Structures 149 Example: Reading a Password 150 Input Processing 152 Output Processing 152 Modem Control 153 Non-Canonical I/O 153 Input Modes 153 Output Modes 154 Control Modes 155 Local Modes 155 155 Control Characters 156 Speed Storing Functions 158 Page xiii Line Control Functions 160 The tcsendbreak() Function 160 The tcdrain() Function 160 The tcflush( Function 160 The tcflow() Function 161 Avoiding Pitfalls 161 Example: Computer-to-Computer Communications 162 Process Groups and Job Control 166 Process Groups 167 Foreground Process 167 Background Process 167 Session 167 Controlling Terminal 168 Get/Set Process Group 168 The setsid() Function 168 The setpgid() Function 168 The tcsetpgrp() Function 169 Portability Lab 169 Chapter 9 POSIX and Standard C 171 Common Usage C 171 Standard C 171 Getting Standard C 171 171 The Standard C Preprocessor 172 Translation Phases 172 Macro Replacement 172 Conversion of Macro Arguments to Strings 173 Token Pasting 173 New Directives 174 Namespace Issues 174 Names Reserved by the C Language 174 Names Reserved by Header Files 175 C Library Functions 176 POSIX Library Functions 177 Avoiding Pitfalls 177 Page xiv Function Prototypes 178 Avoiding Pitfalls 179 Writing New Programs 180 Maintaining Old Programs
Recommended publications
  • UNIX and Computer Science Spreading UNIX Around the World: by Ronda Hauben an Interview with John Lions
    Winter/Spring 1994 Celebrating 25 Years of UNIX Volume 6 No 1 "I believe all significant software movements start at the grassroots level. UNIX, after all, was not developed by the President of AT&T." Kouichi Kishida, UNIX Review, Feb., 1987 UNIX and Computer Science Spreading UNIX Around the World: by Ronda Hauben An Interview with John Lions [Editor's Note: This year, 1994, is the 25th anniversary of the [Editor's Note: Looking through some magazines in a local invention of UNIX in 1969 at Bell Labs. The following is university library, I came upon back issues of UNIX Review from a "Work In Progress" introduced at the USENIX from the mid 1980's. In these issues were articles by or inter- Summer 1993 Conference in Cincinnati, Ohio. This article is views with several of the pioneers who developed UNIX. As intended as a contribution to a discussion about the sig- part of my research for a paper about the history and devel- nificance of the UNIX breakthrough and the lessons to be opment of the early days of UNIX, I felt it would be helpful learned from it for making the next step forward.] to be able to ask some of these pioneers additional questions The Multics collaboration (1964-1968) had been created to based on the events and developments described in the UNIX "show that general-purpose, multiuser, timesharing systems Review Interviews. were viable." Based on the results of research gained at MIT Following is an interview conducted via E-mail with John using the MIT Compatible Time-Sharing System (CTSS), Lions, who wrote A Commentary on the UNIX Operating AT&T and GE agreed to work with MIT to build a "new System describing Version 6 UNIX to accompany the "UNIX hardware, a new operating system, a new file system, and a Operating System Source Code Level 6" for the students in new user interface." Though the project proceeded slowly his operating systems class at the University of New South and it took years to develop Multics, Doug Comer, a Profes- Wales in Australia.
    [Show full text]
  • PL/SQL Multi-Diagrammatic Source Code Visualization
    SQLFlow: PL/SQL Multi-Diagrammatic Source Code Visualization Samir Tartir Ayman Issa Department of Computer Science University of the West of England (UWE), University of Georgia Coldharbour Lane, Frenchay, Bristol BS16 1QY Athens, Georgia 30602 United Kingdom USA Email: [email protected] Email: [email protected] ABSTRACT: A major problem in software introducing new problems to other code that was perfectly maintenance is the lack of a well-documented source code working before. Therefore, educating programmers about in software applications. This has led to serious the behaviors of the target code before start modifying it difficulties in software maintenance and evolution. In is not an easy task. Source code visualization is one of the particular, for those developers who are faced with the heavily used techniques in the literature to overcome this task of fixing or modifying a piece of code they never lack of documentation problem, and its consequent even knew existed before. Database triggers and knowledge-sharing problem [7, 9]. procedures are parts of almost every application, are typical examples of badly documented software Therefore, this research aims at investigating the current components being the hidden back end of software database source code visualization tools and identifying applications. Source code visualization is one of the the main open issues for research. Consequently, the heavily used techniques in the literature so as to mostly used PL/SQL database programming language has familiarize software developers with new source code, been selected for a flowcharting reverse engineering and compensate for the knowledge-sharing problem. prototype tool called "SQLFlow". SQLFlow has been Therefore, a new PL/SQL flowcharting reverse designed, implemented, and tested to parse the target engineering tool, SQLFlow, has been designed and PL/SQL code stored in database procedures, analyze its developed.
    [Show full text]
  • Technical Standard
    Technical Standard X/Open Curses, Issue 7 The Open Group ©November 2009, The Open Group All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the copyright owners. Technical Standard X/Open Curses, Issue 7 ISBN: 1-931624-83-6 Document Number: C094 Published in the U.K. by The Open Group, November 2009. This standardhas been prepared by The Open Group Base Working Group. Feedback relating to the material contained within this standardmay be submitted by using the web site at http://austingroupbugs.net with the Project field set to "Xcurses Issue 7". ii Technical Standard 2009 Contents Chapter 1 Introduction........................................................................................... 1 1.1 This Document ........................................................................................ 1 1.1.1 Relationship to Previous Issues ......................................................... 1 1.1.2 Features Introduced in Issue 7 ........................................................... 2 1.1.3 Features Withdrawn in Issue 7........................................................... 2 1.1.4 Features Introduced in Issue 4 ........................................................... 2 1.2 Conformance............................................................................................ 3 1.2.1 Base Curses Conformance .................................................................
    [Show full text]
  • Validated Products List, 1995 No. 3: Programming Languages, Database
    NISTIR 5693 (Supersedes NISTIR 5629) VALIDATED PRODUCTS LIST Volume 1 1995 No. 3 Programming Languages Database Language SQL Graphics POSIX Computer Security Judy B. Kailey Product Data - IGES Editor U.S. DEPARTMENT OF COMMERCE Technology Administration National Institute of Standards and Technology Computer Systems Laboratory Software Standards Validation Group Gaithersburg, MD 20899 July 1995 QC 100 NIST .056 NO. 5693 1995 NISTIR 5693 (Supersedes NISTIR 5629) VALIDATED PRODUCTS LIST Volume 1 1995 No. 3 Programming Languages Database Language SQL Graphics POSIX Computer Security Judy B. Kailey Product Data - IGES Editor U.S. DEPARTMENT OF COMMERCE Technology Administration National Institute of Standards and Technology Computer Systems Laboratory Software Standards Validation Group Gaithersburg, MD 20899 July 1995 (Supersedes April 1995 issue) U.S. DEPARTMENT OF COMMERCE Ronald H. Brown, Secretary TECHNOLOGY ADMINISTRATION Mary L. Good, Under Secretary for Technology NATIONAL INSTITUTE OF STANDARDS AND TECHNOLOGY Arati Prabhakar, Director FOREWORD The Validated Products List (VPL) identifies information technology products that have been tested for conformance to Federal Information Processing Standards (FIPS) in accordance with Computer Systems Laboratory (CSL) conformance testing procedures, and have a current validation certificate or registered test report. The VPL also contains information about the organizations, test methods and procedures that support the validation programs for the FIPS identified in this document. The VPL includes computer language processors for programming languages COBOL, Fortran, Ada, Pascal, C, M[UMPS], and database language SQL; computer graphic implementations for GKS, COM, PHIGS, and Raster Graphics; operating system implementations for POSIX; Open Systems Interconnection implementations; and computer security implementations for DES, MAC and Key Management.
    [Show full text]
  • Modern Programming Languages CS508 Virtual University of Pakistan
    Modern Programming Languages (CS508) VU Modern Programming Languages CS508 Virtual University of Pakistan Leaders in Education Technology 1 © Copyright Virtual University of Pakistan Modern Programming Languages (CS508) VU TABLE of CONTENTS Course Objectives...........................................................................................................................4 Introduction and Historical Background (Lecture 1-8)..............................................................5 Language Evaluation Criterion.....................................................................................................6 Language Evaluation Criterion...................................................................................................15 An Introduction to SNOBOL (Lecture 9-12).............................................................................32 Ada Programming Language: An Introduction (Lecture 13-17).............................................45 LISP Programming Language: An Introduction (Lecture 18-21)...........................................63 PROLOG - Programming in Logic (Lecture 22-26) .................................................................77 Java Programming Language (Lecture 27-30)..........................................................................92 C# Programming Language (Lecture 31-34) ...........................................................................111 PHP – Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)........................129 Modern Programming Languages-JavaScript
    [Show full text]
  • UBS Release Notes Version 8.10
    Uniplex Release Notes Version 8.10 Manual version: 8.10 Document version: V1.0 COPYRIGHT NOTICE Copyright© 1987-1995 Uniplex Limited. All rights reserved. Unpublished - rights reserved under Copyright Laws. Licensed software and documentation. Use, copy and disclosure restricted by license agreement. ©Copyright 1989-1992, Bitstream Inc. Cambridge, MA. All rights reserved. U.S. Patent No. 5,009,435. ©Copyright 1991-1992, Bitstream Inc. Cambridge, MA. Portions copyright by Data General Corporation (1993) ©Gradient Technologies, Inc. 1991, 1992. ©Hewlett Packard 1988, 1990. Copyright© Harlequin Ltd. 1989, 1990, 1991, 1992. All rights reserved. ©Hewlett-Packard Company 1987-1993. All rights reserved. OpenMail (A.01.00) Copyright© Hewlett-Packard Company 1989, 1990, 1992. Portion Copyright Informix Software, Inc. IXI X.desktop Copyright© 1988-1993, IXI Limited, Cambridge, England. IXI Deskterm Copyright© 1988-1993, IXI Limited, Cambridge, England. Featuring MultiView DeskTerm Copyright© 1990-1992 JSB Computer Systems Ltd. Word for Word, Copyright, Mastersoft, Inc., 1986-1993. Tel: (602)-948-4888 Font Data copyright© The Monotype Corporation Plc 1989. All rights reserved. Copyright© 1990-1991, NBI, Inc. All rights reserved. Created using Netwise SystemTM software. Copyright 1984-1992 Soft-Art, Inc. All rights reserved. Copyrighted work incorporating TypeScalerTM, Copyright© Sun Microsystems Inc. 1989, 1987. All rights reserved. Copyright© VisionWare Ltd. 1989-1992. All Rights Reserved. ©1987-1993 XVT Software Inc. All rights reserved. Uniplex is a trademark of Redwood International Limited in the UK and other countries. onGO, Uniplex II PlusTM, Uniplex Advanced Office SystemTM, Uniplex Advanced GraphicsTM, Uniplex Business SoftwareTM, Uniplex DOSTM, Uniplex DatalinkTM and Uniplex WindowsTM are trademarks of Uniplex Limited. PostScript® is a registered trademark of Adobe Systems Inc.
    [Show full text]
  • About ILE C/C++ Compiler Reference
    IBM i 7.3 Programming IBM Rational Development Studio for i ILE C/C++ Compiler Reference IBM SC09-4816-07 Note Before using this information and the product it supports, read the information in “Notices” on page 121. This edition applies to IBM® Rational® Development Studio for i (product number 5770-WDS) and to all subsequent releases and modifications until otherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC) models nor does it run on CISC models. This document may contain references to Licensed Internal Code. Licensed Internal Code is Machine Code and is licensed to you under the terms of the IBM License Agreement for Machine Code. © Copyright International Business Machines Corporation 1993, 2015. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents ILE C/C++ Compiler Reference............................................................................... 1 What is new for IBM i 7.3.............................................................................................................................3 PDF file for ILE C/C++ Compiler Reference.................................................................................................5 About ILE C/C++ Compiler Reference......................................................................................................... 7 Prerequisite and Related Information..................................................................................................
    [Show full text]
  • A Prolog-Based Proof Tool for Type Theory Taλ and Implicational Intuitionistic-Logic
    A Prolog-based Proof Tool for Type Theory TAλ and Implicational Intuitionistic-Logic L. Yohanes Stefanus University of Indonesia Depok 16424, Indonesia [email protected] and Ario Santoso∗ Technische Universit¨atDresden Dresden 01187, Germany [email protected] Abstract Studies on type theory have brought numerous important contributions to computer science. In this paper we present a GUI-based proof tool that provides assistance in constructing deductions in type theory and validating implicational intuitionistic-logic for- mulas. As such, this proof tool is a testbed for learning type theory and implicational intuitionistic-logic. This proof tool focuses on an important variant of type theory named TAλ, especially on its two core algorithms: the principal-type algorithm and the type inhabitant search algorithm. The former algorithm finds a most general type assignable to a given λ-term, while the latter finds inhabitants (closed λ-terms in β-normal form) to which a given type can be assigned. By the Curry{Howard correspondence, the latter algorithm provides provability for implicational formulas in intuitionistic logic. We elab- orate on how to implement those two algorithms declaratively in Prolog and the overall GUI-based program architecture. In our implementation, we make some modification to improve the performance of the principal-type algorithm. We have also built a web-based version of the proof tool called λ-Guru. 1 Introduction In 1902, Russell revealed the inconsistency in naive set theory. This inconsistency challenged the foundation of mathematics at that time. To solve that problem, type theory was introduced [1, 5]. As time goes on, type theory becomes widely used, it becomes a logician's standard tool, especially in proof theory.
    [Show full text]
  • Ifdef Considered Harmful, Or Portability Experience with C News Henry Spencer – Zoology Computer Systems, University of Toronto Geoff Collyer – Software Tool & Die
    #ifdef Considered Harmful, or Portability Experience With C News Henry Spencer – Zoology Computer Systems, University of Toronto Geoff Collyer – Software Tool & Die ABSTRACT We believe that a C programmer’s impulse to use #ifdef in an attempt at portability is usually a mistake. Portability is generally the result of advance planning rather than trench warfare involving #ifdef. In the course of developing C News on different systems, we evolved various tactics for dealing with differences among systems without producing a welter of #ifdefs at points of difference. We discuss the alternatives to, and occasional proper use of, #ifdef. Introduction portability problems are repeatedly worked around rather than solved. The result is a tangled and often With UNIX running on many different comput- impenetrable web. Here’s a noteworthy example ers, vaguely UNIX-like systems running on still more, from a popular newsreader.1 See Figure 1. Observe and C running on practically everything, many peo- that, not content with merely nesting #ifdefs, the ple are suddenly finding it necessary to port C author has #ifdef and ordinary if statements (plus the software from one machine to another. When differ- mysterious IF macros) interweaving. This makes ences among systems cause trouble, the usual first the structure almost impossible to follow without impulse is to write two different versions of the going over it repeatedly, one case at a time. code—one per system—and use #ifdef to choose the appropriate one. This is usually a mistake. Furthermore, given worst case elaboration and nesting (each #ifdef always has a matching #else), Simple use of #ifdef works acceptably well the number of alternative code paths doubles with when differences are localized and only two versions each extra level of #ifdef.
    [Show full text]
  • Technical Study Desktop Internationalization
    Technical Study Desktop Internationalization NIC CH A E L T S T U D Y [This page intentionally left blank] X/Open Technical Study Desktop Internationalisation X/Open Company Ltd. December 1995, X/Open Company Limited All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the copyright owners. X/Open Technical Study Desktop Internationalisation X/Open Document Number: E501 Published by X/Open Company Ltd., U.K. Any comments relating to the material contained in this document may be submitted to X/Open at: X/Open Company Limited Apex Plaza Forbury Road Reading Berkshire, RG1 1AX United Kingdom or by Electronic Mail to: [email protected] ii X/Open Technical Study (1995) Contents Chapter 1 Internationalisation.............................................................................. 1 1.1 Introduction ................................................................................................. 1 1.2 Character Sets and Encodings.................................................................. 2 1.3 The C Programming Language................................................................ 5 1.4 Internationalisation Support in POSIX .................................................. 6 1.5 Internationalisation Support in the X/Open CAE............................... 7 1.5.1 XPG4 Facilities.........................................................................................
    [Show full text]
  • Unix Standardization and Implementation
    Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control 9. Signals 10.Inter-process Communication * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003. Standardization and Implementation Why Standardization? Proliferation of UNIX versions What should be done? The specifications of limits that each implementation must define! * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003. 1 UNIX Standardization ANSI C American National Standards Institute ISO/IEC 9899:1990 International Organization for Standardization (ISO) Syntax/Semantics of C, a standard library Purpose: Provide portability of conforming C programs to a wide variety of OS’s. 15 areas: Fig 2.1 – Page 27 * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003. UNIX Standardization ANSIC C <assert.h> - verify program assertion <ctype.h> - char types <errno.h> - error codes <float.h> - float point constants <limits.h> - implementation constants <locale.h> - locale catalogs <math.h> - mathematical constants <setjmp.h> - nonlocal goto <signal.h> - signals <stdarg.h> - variable argument lists <stddef.h> - standard definitions <stdio.h> - standard library <stdlib.h> - utilities functions <string.h> - string operations <time.h> - time and date * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003. 2 UNIX Standardization POSIX.1 (Portable Operating System Interface) developed by IEEE Not restricted for Unix-like systems and no distinction for system calls and library functions Originally IEEE Standard 1003.1-1988 1003.2: shells and utilities, 1003.7: system administrator, > 15 other communities Published as IEEE std 1003.1-1990, ISO/IEC9945-1:1990 New: the inclusion of symbolic links No superuser notion * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.
    [Show full text]
  • Synchronizing Threads with POSIX Semaphores
    3/17/2016 POSIX Semaphores Synchronizing Threads with POSIX Semaphores 1. Why semaphores? 2. Posix semaphores are easy to use sem_init sem_wait sem_post sem_getvalue sem_destroy 3. Activities 1 2 Now it is time to take a look at some code that does something a little unexpected. The program badcnt.c creates two new threads, both of which increment a global variable called cnt exactly NITER, with NITER = 1,000,000. But the program produces unexpected results. Activity 1. Create a directory called posixsem in your class Unix directory. Download in this directory the code badcnt.c and compile it using gcc badcnt.c -o badcnt -lpthread Run the executable badcnt and observe the ouput. Try it on both tanner and felix. Quite unexpected! Since cnt starts at 0, and both threads increment it NITER times, we should see cnt equal to 2*NITER at the end of the program. What happens? Threads can greatly simplify writing elegant and efficient programs. However, there are problems when multiple threads share a common address space, like the variable cnt in our earlier example. To understand what might happen, let us analyze this simple piece of code: THREAD 1 THREAD 2 a = data; b = data; a++; b--; data = a; data = b; Now if this code is executed serially (for instance, THREAD 1 first and then THREAD 2), there are no problems. However threads execute in an arbitrary order, so consider the following situation: Thread 1 Thread 2 data a = data; --- 0 a = a+1; --- 0 --- b = data; // 0 0 --- b = b + 1; 0 data = a; // 1 --- 1 --- data = b; // 1 1 So data could end up +1, 0, -1, and there is NO WAY to know which value! It is completely non- deterministic! http://www.csc.villanova.edu/~mdamian/threads/posixsem.html 1/4 3/17/2016 POSIX Semaphores The solution to this is to provide functions that will block a thread if another thread is accessing data that it is using.
    [Show full text]