Developing and Porting C and C++ Applications on Aix pdf

546 2.6K 0
Developing and Porting C and C++ Applications on Aix pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

IBM ibm.com/redbooks Developing and Porting C and C++ Applications on AIX Keigo Matsubara Edison Kwok Inge Rodriguez Murali Paramasivam Detailed explanations about 32- and 64-bit process models Effective management of shared objects and libraries Exploring parallel programming using OpenMP Front cover Developing and Porting C and C++ Applications on AIX June 2003 International Technical Support Organization SG24-5674-01 © Copyright International Business Machines Corporation 2000, 2003. All rights reserved. Note to U.S. Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Second Edition (June 2003) This edition applies to C for AIX (program number 5765-F57) and VisualAge C++ for AIX Version 6.0 (product number 5765-F56) installed on AIX 5L Version 5.2 (product number 5765-E62). Note: Before using this information and the product it supports, read the information in “Notices” on page xvii. © Copyright IBM Corp. 2000, 2003. All rights reserved. iii Contents Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix The team that wrote this redbook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Become a published author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Comments welcome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Summary of changes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii June 2003, Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii September 2000, First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Chapter 1. C and C++ compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 C for AIX Version 6.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.1 New or improved optimization features. . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.2 ISO C Standard conformance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.3 GNU C compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.1.4 Enhanced language level support. . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2 VisualAge C++ for AIX Version 6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.2.1 New or improved optimization features. . . . . . . . . . . . . . . . . . . . . . . 16 1.2.2 OpenMP support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2.3 Automatic parallelization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.4 Improved template handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.5 C99 features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.6 GNU G++ compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.3 Installing the compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.3.1 Install compiler filesets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.3.2 Retaining a previous version of the compiler . . . . . . . . . . . . . . . . . . 22 1.4 Activating the compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.4.1 What is LUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.4.2 Configuring LUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.5 Activating the LUM server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.6 Enrolling a product license . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.6.1 Enrolling a concurrent license . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.6.2 Enrolling a simple nodelock license . . . . . . . . . . . . . . . . . . . . . . . . . 29 iv Developing and Porting C and C++ Applications on AIX 1.7 Invoking the compilers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.7.1 Default compiler drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1.8 Where to find help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.8.1 Online documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.8.2 Viewing online documentation remotely . . . . . . . . . . . . . . . . . . . . . . 31 1.8.3 Where to find help on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.8.4 Applying fixes and service updates. . . . . . . . . . . . . . . . . . . . . . . . . . 35 Chapter 2. Compiling and linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.1 32- and 64-bit development environments . . . . . . . . . . . . . . . . . . . . . . . . 38 2.1.1 The 64-bit advantage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.1.2 Compiler support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.1.3 Utility commands support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.2 Compiling and linking: A quick overview . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.2.1 Building C and C++ programs with system libraries . . . . . . . . . . . . . 43 2.2.2 Objects and libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2.2.3 Difference between shared object and library on AIX . . . . . . . . . . . . 49 2.2.4 Difference between shared and static objects on AIX. . . . . . . . . . . . 51 2.3 Resolving symbols at link-time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.3.1 The -L linker option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.3.2 Searching objects and libraries at link-time. . . . . . . . . . . . . . . . . . . . 56 2.3.3 LIBPATH environment variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.3.4 Link-time and load-time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.4 Supported link methods on AIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.4.1 AIX default linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 2.4.2 Static linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 2.4.3 Lazy loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.5 Run-time linking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 2.5.1 How to use run-time linking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 2.5.2 Examining the executable and shared objects using dump . . . . . . . 75 2.5.3 Enabling the main program object as run-time linking . . . . . . . . . . . 78 2.5.4 Rebinding symbols at the program load-time . . . . . . . . . . . . . . . . . . 79 2.5.5 Extended search order with the -brtl linker option. . . . . . . . . . . . . . . 81 2.6 Dynamic loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 2.7 Commands when manipulating objects and libraries . . . . . . . . . . . . . . . . 85 2.7.1 dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 2.7.2 genkld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 2.7.3 ldd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 2.7.4 nm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 2.7.5 rtl_enable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.7.6 slibclean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.8 Creating shared objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 2.8.1 Import and export files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Contents v 2.8.2 A self-contained shared object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 2.8.3 Interdependent shared objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 2.8.4 Initialization and termination routines . . . . . . . . . . . . . . . . . . . . . . . . 99 2.9 Shared libraries in a development environment . . . . . . . . . . . . . . . . . . . . 99 2.9.1 Production and development environments . . . . . . . . . . . . . . . . . . 100 2.9.2 Private shared objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 2.9.3 NFS consideration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 2.9.4 Sufficient free disk space on the target directory and /tmp . . . . . . . 102 Chapter 3. Understanding user process models . . . . . . . . . . . . . . . . . . . 105 3.1 User process models on AIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 3.1.1 How to determine hardware bit mode . . . . . . . . . . . . . . . . . . . . . . . 107 3.1.2 How to determine kernel bit mode . . . . . . . . . . . . . . . . . . . . . . . . . 107 3.1.3 How to determine user process bit mode . . . . . . . . . . . . . . . . . . . . 108 3.2 The 32-bit user process model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 3.2.1 Default memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 3.2.2 Large memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 3.2.3 Very large memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 3.2.4 Using the large and very large memory model . . . . . . . . . . . . . . . . 121 3.2.5 Checking large memory model executables . . . . . . . . . . . . . . . . . . 124 3.2.6 Resource limits in 32-bit model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 3.2.7 Large file support in a 32-bit model. . . . . . . . . . . . . . . . . . . . . . . . . 129 3.3 The 64-bit user process model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 3.3.1 The first 16 segments (0 - 4 GB). . . . . . . . . . . . . . . . . . . . . . . . . . . 132 3.3.2 Application text, data, and heap (4 GB - 448 PB) . . . . . . . . . . . . . . 133 3.3.3 Default shared memory segments (448 - 512 PB) . . . . . . . . . . . . . 135 3.3.4 Privately loaded objects (512 - 576 PB) . . . . . . . . . . . . . . . . . . . . . 135 3.3.5 Shared text and data (576 - 640 PB) . . . . . . . . . . . . . . . . . . . . . . . 135 3.3.6 System reserved (640 - 960 PB). . . . . . . . . . . . . . . . . . . . . . . . . . . 136 3.3.7 User process stack (960 PB - 1 EB) . . . . . . . . . . . . . . . . . . . . . . . . 136 3.3.8 Resource limits in 64-bit mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 3.3.9 Large file support in 64-bit model . . . . . . . . . . . . . . . . . . . . . . . . . . 139 3.4 Introduction to shared memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 3.4.1 The shmat services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 3.4.2 The mmap services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 3.4.3 Difference between shmat and mmap services . . . . . . . . . . . . . . . 147 3.4.4 Shared memory limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 3.5 Shared memory segments allocation order. . . . . . . . . . . . . . . . . . . . . . . 149 3.5.1 Order in the 32-bit default memory model. . . . . . . . . . . . . . . . . . . . 153 3.5.2 Order in the 32-bit very large memory model with DSA . . . . . . . . . 154 3.5.3 Extended mode shared memory segments . . . . . . . . . . . . . . . . . . 155 3.5.4 Order in the 64-bit memory model . . . . . . . . . . . . . . . . . . . . . . . . . 156 3.6 Large page support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 vi Developing and Porting C and C++ Applications on AIX 3.6.1 Large page support overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 3.6.2 Large page application usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 3.6.3 Large page usage security capability . . . . . . . . . . . . . . . . . . . . . . . 161 3.6.4 Configuring system to use large pages. . . . . . . . . . . . . . . . . . . . . . 162 3.6.5 Other system changes for large pages . . . . . . . . . . . . . . . . . . . . . . 164 3.6.6 Large page usage considerations . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Chapter 4. Managing the memory heap . . . . . . . . . . . . . . . . . . . . . . . . . . 165 4.1 Malloc subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 4.1.1 malloc(), calloc(), valloc(), and alloca() . . . . . . . . . . . . . . . . . . . . . . 167 4.1.2 mallopt(), mallinfo, and mallinfo_heap() . . . . . . . . . . . . . . . . . . . . . 168 4.1.3 disclaim(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 4.2 Memory allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 4.2.1 The 3.1 memory allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 4.2.2 The default memory allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 4.2.3 The default memory allocator with the malloc buckets extension . . 173 4.2.4 The debug malloc allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 4.2.5 User-defined malloc replacement . . . . . . . . . . . . . . . . . . . . . . . . . . 178 4.2.6 Malloc multiheap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 4.3 Use of MALLOCDEBUG options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 4.3.1 MALLOCDEBUG with the debug memory allocator . . . . . . . . . . . . 182 4.3.2 MALLOCDEBUG with memory allocators other than debug. . . . . . 190 4.4 Heap management using MEMDBG . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 4.4.1 How to handle a user-created heap . . . . . . . . . . . . . . . . . . . . . . . . 200 4.4.2 A user-defined heap allocated from shared memory segments . . . 202 Chapter 5. Creating DLPAR-aware applications. . . . . . . . . . . . . . . . . . . . 207 5.1 Dynamic logical partitioning overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 5.2 The process flow of a DLPAR operation . . . . . . . . . . . . . . . . . . . . . . . . . 210 5.3 DLPAR-safe and DLPAR-aware applications . . . . . . . . . . . . . . . . . . . . . 214 5.3.1 DLPAR-safe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 5.3.2 DLPAR-aware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 5.4 Integrating a DLPAR operation into the application . . . . . . . . . . . . . . . . 217 Chapter 6. Programming hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . 219 6.1 Programming recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 6.1.1 Variables and data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 6.1.2 Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 6.1.3 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 6.1.4 Arithmetic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 6.1.5 Selection and iteration statements . . . . . . . . . . . . . . . . . . . . . . . . . 223 6.1.6 Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 6.1.7 Memory usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 6.1.8 Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Contents vii 6.1.9 Virtual functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.2 Diagnosing compile-time errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 6.2.1 Anatomy of a message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 6.2.2 Useful options and compiler aids . . . . . . . . . . . . . . . . . . . . . . . . . . 228 6.2.3 Migrating from 32-bit to 64-bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 6.3 Diagnosing link-time errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 6.3.1 Unresolved symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 6.3.2 Duplicate symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 6.3.3 Insufficient memory for the linker process. . . . . . . . . . . . . . . . . . . . 243 6.3.4 The c++filt utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 6.4 Diagnosing run-time errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.4.1 Uninitialized variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.4.2 Run-time checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 6.4.3 Unsignedness preservation in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 6.4.4 ANSI aliasing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 6.4.5 #pragma option_override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Chapter 7. Debugging your applications . . . . . . . . . . . . . . . . . . . . . . . . . 249 7.1 Working with core files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 7.1.1 Core file naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 7.1.2 Creating core files with assert(). . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7.1.3 Creating core files with coredump() . . . . . . . . . . . . . . . . . . . . . . . . 251 7.1.4 Including shared memory information in the core file . . . . . . . . . . . 253 7.1.5 Gathering core files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 7.1.6 AIX error log entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 7.1.7 Lightweight core file support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 7.2 Using the printf()-debug method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 7.3 Preparing your application for debugging . . . . . . . . . . . . . . . . . . . . . . . . 259 7.4 Using dbx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 7.4.1 Starting a dbx session. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 7.4.2 Customizing a dbx session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 7.4.3 Working with breakpoints: The stop subcommand . . . . . . . . . . . . . 263 7.4.4 Redirection of library location in object files with the -p flag . . . . . . 263 7.4.5 Using dbx with gcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 7.5 Debugging with the truss command . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 7.6 Using the trace facility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 7.6.1 Introduction to trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 7.6.2 Tracing an application on the command line. . . . . . . . . . . . . . . . . . 268 7.6.3 Tracing an application with subroutine calls . . . . . . . . . . . . . . . . . . 271 Chapter 8. Introduction to POSIX threads. . . . . . . . . . . . . . . . . . . . . . . . . 275 8.1 Overview of threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 8.1.1 Relationship between a process and a user thread . . . . . . . . . . . . 276 viii Developing and Porting C and C++ Applications on AIX 8.2 POSIX threads (Pthreads) on AIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 8.2.1 Advantages of using Pthreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 8.2.2 The POSIX threads API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 8.2.3 Multi- and single-threaded processes . . . . . . . . . . . . . . . . . . . . . . . 280 8.3 Pthread management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 8.3.1 Creating and terminating Pthreads . . . . . . . . . . . . . . . . . . . . . . . . . 284 8.3.2 Joining Pthreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 8.3.3 Detaching a Pthread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 8.3.4 Thread stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 8.4 Data synchronization between Pthreads. . . . . . . . . . . . . . . . . . . . . . . . . 296 8.4.1 Synchronizing Pthreads with mutexes . . . . . . . . . . . . . . . . . . . . . . 297 8.4.2 Synchronizing Pthreads with condition variables . . . . . . . . . . . . . . 303 8.4.3 Synchronizing Pthreads with read-write locks. . . . . . . . . . . . . . . . . 310 8.5 Thread-specific data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 8.5.1 Allocating thread-specific data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 8.5.2 Accessing thread-specific data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 8.5.3 Deleting thread-specific data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 8.5.4 Thread-safe and reentrant functions . . . . . . . . . . . . . . . . . . . . . . . . 319 8.6 Pthread cancellation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 8.7 Pthread priority and scheduling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 8.7.1 Thread models in AIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 8.7.2 Scheduling Pthreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 8.7.3 Scheduling limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 8.8 Pthread specific environment variables in AIX . . . . . . . . . . . . . . . . . . . . 328 8.9 User API for Solaris threaded applications . . . . . . . . . . . . . . . . . . . . . . . 332 8.9.1 Application binary interface (ABI) . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Chapter 9. Program parallelization using OpenMP . . . . . . . . . . . . . . . . . 335 9.1 Introduction to OpenMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 9.2 The OpenMP programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 9.3 Classification of OpenMP directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 9.3.1 The OpenMP directive format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 9.4 Parallel region construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 9.5 Work-sharing constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 9.5.1 for construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 9.5.2 sections construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 9.5.3 single construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 9.6 Combined parallel work-sharing constructs . . . . . . . . . . . . . . . . . . . . . . 347 9.6.1 parallel for construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 9.6.2 parallel sections construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 9.7 Synchronization constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 9.7.1 master construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 9.7.2 critical construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 [...]... Porting C and C+ + Applications on AIX 1 Chapter 1 C and C+ + compilers This chapter focuses on the latest versions of the IBM C and C+ + compiler products for AIX: C for AIX Version 6.0 and VisualAge C+ + for AIX Version 6.0 The latest compiler products offer enhanced support in optimizations, POWER4™ architecture exploitation, the latest ISO C and C+ + Standard conformance, as well as compatibility features... of changes This section describes the technical changes made in this edition of the book and in previous editions This edition may also include minor corrections and editorial changes that are not identified Summary of Changes for SG24-5674-01 for Developing and Porting C and C+ + Applications on AIX as created or updated on March 25, 2009 June 2003, Second Edition This revision reflects the addition,... GNU C/ C++ portability, explained in the first two sections: Section 1.1, C for AIX Version 6.0” on page 2 Section 1.2, “VisualAge C+ + for AIX Version 6.0” on page 16 The other sections provide a comprehensive guide to installing and configuring the compiler products on your AIX systems: Section 1.3, “Installing the compilers” on page 19 Section 1.4, “Activating the compilers” on page 23 Section 1.5,... book, C and C+ + Application Development on AIX, SG24-5674 was written by the following authors: Richard Cutler, Francois Armingaud, Eduardo Conejo, Kumaravel Nagarajan The following list shows contributors for the first version of this book, C and C+ + Application Development on AIX, SG24-5674: IBM Toronto Derek Truong, Mark Changfoot, Paul Pacholski, Rene Matteau xxiv Developing and Porting C and C+ + Applications. .. macros, as the system header file stdbool.h already defines the macros true and false 4 Developing and Porting C and C+ + Applications on AIX long long data type Unlike the -qlonglong option in the previous versions of the C for AIX compilers, the addition of the long long data type in C9 9 changes the semantics for integer constants In C8 9, the type of an unsuffixed integer constant is either int, long... option, is now part of the C9 9 standard It is used to write characters that are not in the basic character set You can have universal characters in identifiers, string literals, and comments 6 Developing and Porting C and C+ + Applications on AIX func Similar to the C for AIX compiler predefined macro FUNCTION , func is a compiler generated internal variable that has the following declaration:... owned by SET Secure Electronic Transaction LLC Other company, product and service names may be trademarks or service marks of others xviii Developing and Porting C and C+ + Applications on AIX Preface This IBM Redbook will help experienced UNIX application developers who are new to the AIX operating system The book explains the many concepts in detail, including the following: Enhancements and new features... function calls (for example, parameter passing, stack allocation and adjustment, and so on) are completely eliminated Please refer to C for AIX Compiler Reference, SC09-4960 for a list of built-in functions supported in C for AIX Version 6.0 1.1.2 ISO C Standard conformance The IBM C for AIX Version 6.0 compiler supports the latest ISO/IEC 9899:1999 International Standard, commonly referred to as C9 9... details Profile-directed feedback With profile-directed feedback, or PDF, special instrumentation code is inserted in the executable to gather information about the program’s execution pattern Information gathered from the execution is then fed back to the compiler for a second compilation, and the compiler performs optimization based on the code execution frequency and conditional branch pattern In order... Section 1.5, “Activating the LUM server” on page 26 Section 1.6, “Enrolling a product license” on page 27 Section 1.7, “Invoking the compilers” on page 29 Section 1.8, “Where to find help” on page 31 For a description of the previous versions of IBM C and C+ + compiler products, please refer to Appendix A, “Previous versions of C and C+ + compiler products” on page 429 for details © Copyright IBM Corp 2000, . and Porting C and C+ + Applications on AIX June 2003 International Technical Support Organization SG24-5674-01 © Copyright International Business Machines. 501 xii Developing and Porting C and C+ + Applications on AIX © Copyright IBM Corp. 2000, 2003. All rights reserved. xiii Figures 1-1 Core and orthogonal extensions

Ngày đăng: 17/03/2014, 13:20

Từ khóa liên quan

Mục lục

  • Front cover

  • Contents

  • Figures

  • Tables

  • Notices

    • Trademarks

    • Preface

      • The team that wrote this redbook

      • Become a published author

      • Comments welcome

      • Summary of changes

        • June 2003, Second Edition

        • September 2000, First Edition

        • Chapter 1. C and C++ compilers

          • 1.1 C for AIX Version 6.0

            • 1.1.1 New or improved optimization features

            • 1.1.2 ISO C Standard conformance

            • 1.1.3 GNU C compatibility

            • 1.1.4 Enhanced language level support

            • 1.2 VisualAge C++ for AIX Version 6.0

              • 1.2.1 New or improved optimization features

              • 1.2.2 OpenMP support

              • 1.2.3 Automatic parallelization

              • 1.2.4 Improved template handling

              • 1.2.5 C99 features

              • 1.2.6 GNU G++ compatibility

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan