Tải bản đầy đủ (.pdf) (546 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.69 MB, 546 trang )

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
Contents ix
9.7.3 barrier directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
9.7.4 atomic construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
9.7.5 flush directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
9.7.6 ordered construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
9.8 Data environment: The threadprivate directive . . . . . . . . . . . . . . . . . . . . 354
9.9 Data-sharing attribute clauses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
9.9.1 private clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
9.9.2 firstprivate clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
9.9.3 lastprivate clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.9.4 shared clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
9.9.5 default clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
9.9.6 reduction clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
9.9.7 copyin clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.9.8 copyprivate clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
9.10 Run-time library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
9.10.1 Execution environment functions . . . . . . . . . . . . . . . . . . . . . . . . . 364
9.10.2 Lock functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
9.10.3 Example usage of run-time library functions . . . . . . . . . . . . . . . . . 370
9.11 Environment variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
9.11.1 OMP_SCHEDULE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

9.11.2 OMP_NUM_THREADS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
9.11.3 OMP_DYNAMIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
9.11.4 OMP_NESTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Chapter 10. Dealing with C++ templates . . . . . . . . . . . . . . . . . . . . . . . . . . 377
10.1 What is a template. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
10.2 AIX template implementations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
10.2.1 Generated function bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
10.3 Simple code layout method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
10.3.1 Disadvantages of the simple method . . . . . . . . . . . . . . . . . . . . . . 381
10.4 Template instantiation file method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
10.4.1 The -qtempinc option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
10.4.2 Contents of the tempinc directory . . . . . . . . . . . . . . . . . . . . . . . . . 385
10.4.3 Forcing template instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
10.5 Template registry: The preferred method . . . . . . . . . . . . . . . . . . . . . . . 387
10.5.1 The -qtemplateregistry option . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
10.5.2 The -qtemplaterecompile option . . . . . . . . . . . . . . . . . . . . . . . . . . 388
10.6 Standard C++ Library and STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
10.6.1 Standard Template Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
10.6.2 A STL example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Chapter 11. Creating shared objects from C++ source codes . . . . . . . . 393
11.1 Creating shared objects from C++ source codes . . . . . . . . . . . . . . . . . 394
x Developing and Porting C and C++ Applications on AIX
11.1.1 Creating a C++ shared object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
11.1.2 Generating an export file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
11.1.3 The -qmkshrobj option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
11.1.4 Mixing C and C++ object files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
11.1.5 Order of initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
11.2 Shared objects with templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
11.2.1 Templates and makeC++SharedLib . . . . . . . . . . . . . . . . . . . . . . . 402
11.2.2 Templates and -qmkshrobj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404

Chapter 12. Packaging your applications . . . . . . . . . . . . . . . . . . . . . . . . . 405
12.1 Understanding the AIX standard packaging . . . . . . . . . . . . . . . . . . . . . 406
12.1.1 Filesets and package files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
12.1.2 Bundles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
12.1.3 Managing filesets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
12.1.4 Viewing the TOC file (.toc) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
12.1.5 Viewing package files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
12.2 Packaging applications using mkinstallp . . . . . . . . . . . . . . . . . . . . . . . . 418
12.2.1 mkinstallp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
12.2.2 Packaging examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
12.2.3 Verification of packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
12.2.4 Optional installation control executable files . . . . . . . . . . . . . . . . . 426
Appendix A. Previous versions of C and C++ compiler products . . . . . 429
Compiler product similarities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Multiple command line drivers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Installation directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
IBM C compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
IBM XL C Version 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
IBM C for AIX Version 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
IBM C for AIX Version 4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
IBM C for AIX Version 4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
IBM C for AIX Version 5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
C compiler summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
IBM C++ compilers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
IBM C Set ++ for AIX Version 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
IBM C and C++ compilers Version 3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
IBM VisualAge C++ Professional for AIX Version 4 . . . . . . . . . . . . . . . . . 437
IBM VisualAge C++ Professional for AIX Version 5 . . . . . . . . . . . . . . . . . 438
C++ compiler summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Appendix B. Useful information for linking and loading on AIX. . . . . . . 441

A brief history of UNIX programming development . . . . . . . . . . . . . . . . . . . . 442
Historical view of linking and loading in AIX . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Contents xi
Appendix C. Subroutine references for shmat and mmap services. . . . 447
References for shmat services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The ftok() subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The shmat() routine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
The shmctl() subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
The shmget() routine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
The shmdt() subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
References for mmap services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
The mmap() subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
The mprotect() subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
The msync() subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
The munmap() subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Appendix D. Subroutine references for POSIX threads. . . . . . . . . . . . . . 473
Subroutines defined in the POSIX thread standard . . . . . . . . . . . . . . . . . . . . 474
Subroutines defined in the UNIX 98 Specification . . . . . . . . . . . . . . . . . . . . . 479
Extensions to POSIX thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Appendix E. Supported IBM SMP directives . . . . . . . . . . . . . . . . . . . . . . 483
IBM SMP directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
The IBM SMP directives syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Appendix F. Sample compiler listing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Compiler listing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Abbreviations and acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Related publications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
IBM Redbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
AIX official publications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
pSeries hardware related publications . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

C for AIX official publications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
VisualAge C++ for AIX official publications . . . . . . . . . . . . . . . . . . . . . . . . 499
Other publications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Referenced Web sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
How to get IBM Redbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
IBM Redbooks collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2-1 Object files and a library archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2-2 Compiling and linking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2-3 An XCOFF format executable file and exec() . . . . . . . . . . . . . . . . . . . . 63
2-4 Static and shared text code in the executable file . . . . . . . . . . . . . . . . . 65
2-5 The -bdynamic and -bstatic linker options . . . . . . . . . . . . . . . . . . . . . . . 66
2-6 Function calling relationship. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2-7 Function calling relationship after rebinding symbols . . . . . . . . . . . . . . 81
2-8 Function calling relationship for an interdependent shared object. . . . . 98
3-1 Hardware, kernel, and user process relationships. . . . . . . . . . . . . . . . 106
3-2 Default memory model (segment usage). . . . . . . . . . . . . . . . . . . . . . . 110
3-3 Default memory model (detail). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
3-4 Large memory model (segment usage) . . . . . . . . . . . . . . . . . . . . . . . . 116
3-5 Very large memory model (0 < maxdata < 0xB0000000) . . . . . . . . . . 118
3-6 Very large memory model (0xB0000000 =< maxdata < 0xD0000000) 119
3-7 Very large memory model (maxdata = 0). . . . . . . . . . . . . . . . . . . . . . . 120
3-8 Data and stack resource limits (default 32-bit process model) . . . . . . 126
3-9 The 64-bit memory model (1EB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
3-10 The 64-bit memory model (4 GB, the first 16 segments) . . . . . . . . . . . 132
3-11 Shared memory segments between two processes . . . . . . . . . . . . . . 143

5-1 Process flow of a DLPAR operation. . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6-1 Structure padding in 32-bit mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
6-2 Structure padding in 64-bit mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
6-3 Structure with user-defined paddings in both 32-bit and 64-bit mode . 239
7-1 Definition of HkWord . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
8-1 Two user threads in a process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
8-2 Five Pthreads created by pthread_create() . . . . . . . . . . . . . . . . . . . . . 285
8-3 Thread stacks (default 32-bit process model) . . . . . . . . . . . . . . . . . . . 293
8-4 Thread models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
9-1 Concept of barrier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
11-1 Illustration of objects in fish.o and animals.o . . . . . . . . . . . . . . . . . . . . 399
12-1 Relationship among filesets, packages, and bundles . . . . . . . . . . . . . 406
12-2 State diagram between applied and committed state . . . . . . . . . . . . . 410
12-3 Relationship between APARs and update fileset. . . . . . . . . . . . . . . . . 411
12-4 Sample .toc file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
12-5 Directory structure for packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
xiv Developing and Porting C and C++ Applications on AIX
© Copyright IBM Corp. 2000, 2003. All rights reserved. xv
Tables
1-1 C for AIX Version 6.0 packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1-2 VisualAge C++ for AIX Version 6.0 packages . . . . . . . . . . . . . . . . . . . . 19
1-3 License certificate locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1-4 Compiler driver extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2-1 C and C++ data type sizes in bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2-2 XCOFF headers and loading target segments . . . . . . . . . . . . . . . . . . . 62
2-3 Supported link methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2-4 LDLAZYDEBUG environment variable values. . . . . . . . . . . . . . . . . . . . 68
2-5 Linker options equivalent to -G . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2-6 IMPid values for imported symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
2-7 Directive lines for import files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

3-1 Shared memory limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
4-1 Memory allocators and MALLOCTYPE . . . . . . . . . . . . . . . . . . . . . . . . 170
4-2 Default configuration values for malloc buckets. . . . . . . . . . . . . . . . . . 176
4-3 User-defined replacement subroutines . . . . . . . . . . . . . . . . . . . . . . . . 179
4-4 MALLOCDEBUG options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5-1 Applications that should be DLPAR-aware . . . . . . . . . . . . . . . . . . . . . 216
6-1 ISO C99 integer constant type selection . . . . . . . . . . . . . . . . . . . . . . . 236
8-1 Filesets for Solaris user thread library . . . . . . . . . . . . . . . . . . . . . . . . . 333
9-1 OpenMP components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
9-2 OpenMP directive categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
11-1 Order of initialization of objects in prriolib.a . . . . . . . . . . . . . . . . . . . . . 401
12-1 Fileset state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
12-2 Fields description of the .toc file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
12-3 Definition of entries in <fileset_name>.inventory . . . . . . . . . . . . . . . . . 417
12-4 Template file keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
12-5 Optional installation control executable files . . . . . . . . . . . . . . . . . . . . 427
A-1 IBM C compilers for AIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
A-2 C++ compiler products. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
B-1 Definitions of terms regarding linking and loading process on AIX . . . 443
C-1 Values for the third parameter of shmat() . . . . . . . . . . . . . . . . . . . . . . 452
C-2 Values for the third parameter of shmctl() . . . . . . . . . . . . . . . . . . . . . . 455
C-3 Values for the third parameter of shmget() . . . . . . . . . . . . . . . . . . . . . 458
C-4 Values for the sixth parameter of mmap() . . . . . . . . . . . . . . . . . . . . . . 465
C-5 The third parameter of msync() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
D-1 Thread management sub-routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
D-2 Execution scheduling sub-routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
D-3 Synchronization sub-routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
xvi Developing and Porting C and C++ Applications on AIX
D-4 Thread-specific data sub-routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
D-5 Read-write lock sub-routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

D-6 Additional POSIX threads sub-routines defined in UNIX 98 . . . . . . . . 481
D-7 Non-portable thread routines in AIX. . . . . . . . . . . . . . . . . . . . . . . . . . . 482
E-1 Regular expressions for countable loops. . . . . . . . . . . . . . . . . . . . . . . 484
E-2 Supported IBM pragma directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
© Copyright IBM Corp. 2000, 2003. All rights reserved. xvii
Notices
This information was developed for products and services offered in the U.S.A.
IBM may not offer the products, services, or features discussed in this document in other countries. Consult
your local IBM representative for information on the products and services currently available in your area.
Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM
product, program, or service may be used. Any functionally equivalent product, program, or service that
does not infringe any IBM intellectual property right may be used instead. However, it is the user's
responsibility to evaluate and verify the operation of any non-IBM product, program, or service.
IBM may have patents or pending patent applications covering subject matter described in this document.
The furnishing of this document does not give you any license to these patents. You can send license
inquiries, in writing, to:
IBM Director of Licensing, IBM Corporation, North Castle Drive Armonk, NY 10504-1785 U.S.A.
The following paragraph does not apply to the United Kingdom or any other country where such provisions
are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES
THIS PUBLICATION "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer
of express or implied warranties in certain transactions, therefore, this statement may not apply to you.
This information could include technical inaccuracies or typographical errors. Changes are periodically made
to the information herein; these changes will be incorporated in new editions of the publication. IBM may
make improvements and/or changes in the product(s) and/or the program(s) described in this publication at
any time without notice.
Any references in this information to non-IBM Web sites are provided for convenience only and do not in any
manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the
materials for this IBM product and use of those Web sites is at your own risk.

IBM may use or distribute any of the information you supply in any way it believes appropriate without
incurring any obligation to you.
Information concerning non-IBM products was obtained from the suppliers of those products, their published
announcements or other publicly available sources. IBM has not tested those products and cannot confirm
the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on
the capabilities of non-IBM products should be addressed to the suppliers of those products.
This information contains examples of data and reports used in daily business operations. To illustrate them
as completely as possible, the examples include the names of individuals, companies, brands, and products.
All of these names are fictitious and any similarity to the names and addresses used by an actual business
enterprise is entirely coincidental.
COPYRIGHT LICENSE:
This information contains sample application programs in source language, which illustrates programming
techniques on various operating platforms. You may copy, modify, and distribute these sample programs in
any form without payment to IBM, for the purposes of developing, using, marketing or distributing application
programs conforming to the application programming interface for the operating platform for which the
sample programs are written. These examples have not been thoroughly tested under all conditions. IBM,
therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. You may copy,
modify, and distribute these sample programs in any form without payment to IBM for the purposes of
developing, using, marketing, or distributing application programs conforming to IBM's application
programming interfaces.
xviii Developing and Porting C and C++ Applications on AIX
Trademarks
The following terms are trademarks of the International Business Machines Corporation in the United States,
other countries, or both:
AIX 5L™
AIX®
C Set ++®

^™
IBM eServer™

IBM®
Open Class®
OS/2®
PartnerWorld®
POWER4™
PowerPC®
pSeries™
Redbooks (logo)™
Redbooks™
RISC System/6000®
VisualAge®
zSeries™
The following terms are trademarks of other companies:
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other
countries, or both.
Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the
United States, other countries, or both.
Intel, Intel Inside (logos), MMX and Pentium are trademarks of Intel Corporation in the United States, other
countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other countries.
SET and the SET Logo are trademarks owned by SET Secure Electronic Transaction LLC.
Other company, product and service names may be trademarks or service marks of others.
© Copyright IBM Corp. 2000, 2003. All rights reserved. xix
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 provided by the latest C and C++ compilers
for AIX
 Compiling and linking tasks required to effectively use and manage shared

libraries and run-time linking
 Use of process heap and shared memory in the 32- and 64-bit user process
models
 A new programming paradigm in a partitioned environment where resources
can be dynamically changed
 Parallel programming using POSIX threads and OpenMP
The following chapters are also useful for system administrators who are
responsible for the software problem determination and application software
release level management on AIX systems:
 Chapter 3, “Understanding user process models” on page 105
 Chapter 7, “Debugging your applications” on page 249
 Chapter 12, “Packaging your applications” on page 405
This publication expands on the information found in the AIX 5L Porting Guide,
SG24-6034.
The team that wrote this redbook
This redbook was produced by a team of specialists from around the world
working at the International Technical Support Organization, Austin Center.
Keigo Matsubara is an advisory IT specialist at the International Technical
Support Organization (ITSO), Austin Center. Before joining the ITSO, he worked
in the System and Web Solution Center in Japan as a Field Technical Support
Specialist (FTSS) for pSeries™. He has been working for IBM® for 11 years.
Edison Kwok is a senior Software Developer in the IBM Toronto Software
Laboratory in Canada. He has eight years of experience in C and C++ compiler
development on the zSeries™ and pSeries platforms. He holds a degree in
xx Developing and Porting C and C++ Applications on AIX
electrical engineering from the University of Victoria. His area of expertise
include compiler construction, C language standard, and C and C++
programming on various UNIX operating systems and the mainframe.
Inge Rodriguez is an IT specialist from IBM Germany. She has 20 years of
experience in UNIX application development. She has been working for IBM for

three years. Her main responsibility is support for ISVs regarding application
development and porting. She holds a MSc in Medical Computer Science of
University Heidelberg.
Murali Paramasivam is a Software Engineer from IBM India. He has nearly
three years of application development experience in C and C++ on various
UNIX operating systems. He holds an engineering degree in Material Science
and Metallurgy. His areas of expertise include shared libraries, multi-threaded
programming, and C/C++ compilation and linking concepts in AIX®.
Thanks to the following people for their contributions to this project:
International Technical Support Organization, Austin Center
Scott Vetter and Wade Wallace
IBM Austin
Alfredo Mendoza, Ann Wigginton, Betty Riggle, Donald Stence, David Hepkin,
Gary Hook, Joel H Schopp, Julie Craft, Kedron J Touvell, Kenji Kindo, Kevin W
Monroe, Luke Browning, Mark Rogers, Michael Mall, Nathan Fontenot, Randy
Swanberg, Richard Cutler, Sara D Epsztein, Steven Molis
IBM Japan
Hajime Mita and Tomoyuki Niijima
IBM Toronto
Steven E. Hikida, Wang Chen, Sean Perry, Roger E. Pett.
Become a published author
Join us for a two- to six-week residency program! Help write an IBM Redbook
dealing with specific products or solutions, while getting hands-on experience
with leading-edge technologies. You'll team with IBM technical professionals,
Business Partners and/or customers.
Your efforts will help increase product acceptance and customer satisfaction. As
a bonus, you'll develop a network of contacts in IBM development labs, and
increase your productivity and marketability.
Preface xxi
Find out more about the residency program, browse the residency index, and

apply online at:
ibm.com/redbooks/residencies.html
Comments welcome
Your comments are important to us!
We want our Redbooks™ to be as helpful as possible. Send us your comments
about this or other Redbooks in one of the following ways:
 Use the online Contact us review redbook form found at:
ibm.com/redbooks
 Send your comments in an Internet note to:

 Mail your comments to:
IBM Corporation, International Technical Support Organization
Dept. JN9B Building 003 Internal Zip 2834
11400 Burnet Road
Austin, Texas 78758-3493
xxii Developing and Porting C and C++ Applications on AIX
© Copyright IBM Corp. 2000, 2003. All rights reserved. xxiii
Summary 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, deletion, or modification of new and changed
information described below.
New information
The following chapters are new:

 Chapter 2, “Compiling and linking” on page 37
 Chapter 3, “Understanding user process models” on page 105
 Chapter 4, “Managing the memory heap” on page 165
 Chapter 5, “Creating DLPAR-aware applications” on page 207
 Chapter 6, “Programming hints and tips” on page 219
 Chapter 7, “Debugging your applications” on page 249
 Chapter 9, “Program parallelization using OpenMP” on page 335
 Chapter 12, “Packaging your applications” on page 405
Changed information
The following chapters were rewritten in order to cover new features and
enhancements provided by the latest products:
 Chapter 1, “C and C++ compilers” on page 1
 Chapter 8, “Introduction to POSIX threads” on page 275

×