ebook img

Hands On: C/C++ Programming and Unix Application Design: UNIX PDF

1063 Pages·2002·3.17 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Hands On: C/C++ Programming and Unix Application Design: UNIX

Hands On: C/C++ Programming and Unix Application Design: UNIX System Calls and Subroutines using C, Motif, C++ (cid:1)c A. D. Marshall 1998/9 ii Contents 1 The Common Desktop Environment 1 1.1 The front panel . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 The file manager . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.3 The application manager . . . . . . . . . . . . . . . . . . . . . 3 1.4 The session manager . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 Other CDE desktop tools. . . . . . . . . . . . . . . . . . . . . 4 1.6 Application development tools . . . . . . . . . . . . . . . . . . 5 1.7 Application integration . . . . . . . . . . . . . . . . . . . . . . 5 1.8 Windows and the Window Manager . . . . . . . . . . . . . . . 6 1.9 The Root Menu . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 C/C++ Program Compilation 13 2.1 Creating, Compiling and Running Your Program . . . . . . . 13 2.1.1 Creating the program . . . . . . . . . . . . . . . . . . . 13 2.1.2 Compilation . . . . . . . . . . . . . . . . . . . . . . . . 14 2.1.3 Running the program . . . . . . . . . . . . . . . . . . . 15 2.2 The C Compilation Model . . . . . . . . . . . . . . . . . . . . 15 2.2.1 The Preprocessor . . . . . . . . . . . . . . . . . . . . . 15 2.2.2 C Compiler . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2.3 Assembler . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2.4 Link Editor . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2.5 Some Useful Compiler Options . . . . . . . . . . . . . 17 2.2.6 Using Libraries . . . . . . . . . . . . . . . . . . . . . . 18 2.2.7 UNIX Library Functions . . . . . . . . . . . . . . . . . 19 2.2.8 Finding Information about Library Functions . . . . . 19 2.3 Lint — A C program verifier . . . . . . . . . . . . . . . . . . . 20 2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 iii iv CONTENTS 3 C Basics 23 3.1 History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Characteristics of C . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 C Program Structure . . . . . . . . . . . . . . . . . . . . . . . 26 3.4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.4.1 Defining Global Variables . . . . . . . . . . . . . . . . 29 3.4.2 Printing Out and Inputting Variables . . . . . . . . . . 31 3.5 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.6 Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . 32 3.7 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . 33 3.8 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.9 Order of Precedence . . . . . . . . . . . . . . . . . . . . . . . 34 3.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4 Conditionals 39 4.1 The if statement . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 The ? operator . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.3 The switch statement . . . . . . . . . . . . . . . . . . . . . . 41 4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5 Looping and Iteration 45 5.1 The for statement . . . . . . . . . . . . . . . . . . . . . . . . 45 5.2 The while statement . . . . . . . . . . . . . . . . . . . . . . . 46 5.3 The do-while statement . . . . . . . . . . . . . . . . . . . . . 48 5.4 break and continue . . . . . . . . . . . . . . . . . . . . . . . 49 5.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6 Arrays and Strings 55 6.1 Single and Multi-dimensional Arrays . . . . . . . . . . . . . . 55 6.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7 Functions 59 7.1 void functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 7.2 Functions and Arrays . . . . . . . . . . . . . . . . . . . . . . . 60 7.3 Function Prototyping . . . . . . . . . . . . . . . . . . . . . . . 61 7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 CONTENTS v 8 Further Data Types 69 8.1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 8.1.1 Defining New Data Types . . . . . . . . . . . . . . . . 70 8.2 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 8.3 Coercion or Type-Casting . . . . . . . . . . . . . . . . . . . . 73 8.4 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . 74 8.5 Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 75 8.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 9 Pointers 77 9.1 What is a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . 77 9.2 Pointer and Functions . . . . . . . . . . . . . . . . . . . . . . 81 9.3 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . 83 9.4 Arrays of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . 85 9.5 Multidimensional arrays and pointers . . . . . . . . . . . . . . 86 9.6 Static Initialisation of Pointer Arrays . . . . . . . . . . . . . . 89 9.7 Pointers and Structures . . . . . . . . . . . . . . . . . . . . . . 89 9.8 Common Pointer Pitfalls . . . . . . . . . . . . . . . . . . . . . 90 9.8.1 Notassigningapointertomemoryaddressbeforeusingit 90 9.8.2 Illegal indirection . . . . . . . . . . . . . . . . . . . . . 91 9.9 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 10 Dynamic Memory Allocation and Dynamic Structures 93 10.1 Malloc, Sizeof, and Free . . . . . . . . . . . . . . . . . . . . . 93 10.2 Calloc and Realloc . . . . . . . . . . . . . . . . . . . . . . . . 95 10.3 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 10.4 Full Program: queue.c . . . . . . . . . . . . . . . . . . . . . . 96 10.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 11 Advanced Pointer Topics 103 11.1 Pointers to Pointers . . . . . . . . . . . . . . . . . . . . . . . . 103 11.2 Command line input . . . . . . . . . . . . . . . . . . . . . . . 105 11.3 Pointers to a Function . . . . . . . . . . . . . . . . . . . . . . 106 11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 12 Low Level Operators and Bit Fields 111 12.1 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . 111 12.2 Bit Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 vi CONTENTS 12.2.1 Bit Fields: Practical Example . . . . . . . . . . . . . . 114 12.2.2 A note of caution: Portability . . . . . . . . . . . . . . 116 12.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 13 The C Preprocessor 119 13.1 #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 13.2 #undef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 13.3 #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 13.4 #if — Conditional inclusion . . . . . . . . . . . . . . . . . . . 121 13.5 Preprocessor Compiler Control . . . . . . . . . . . . . . . . . . 122 13.6 Other Preprocessor Commands . . . . . . . . . . . . . . . . . 123 13.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 14 C, UNIX and Standard Libraries 125 14.1 Advantages of using UNIX with C . . . . . . . . . . . . . . . . 125 14.2 Using UNIX System Calls and Library Functions . . . . . . . 126 15 IntegerFunctions, RandomNumber, StringConversion, Search- ing and Sorting: <stdlib.h> 129 15.1 Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . . . 129 15.2 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 131 15.3 String Conversion . . . . . . . . . . . . . . . . . . . . . . . . . 133 15.4 Searching and Sorting . . . . . . . . . . . . . . . . . . . . . . 134 15.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 16 Mathematics: <math.h> 137 16.1 Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 137 16.2 Math Constants . . . . . . . . . . . . . . . . . . . . . . . . . . 138 17 Input and Output (I/O):stdio.h 141 17.1 Reporting Errors . . . . . . . . . . . . . . . . . . . . . . . . . 141 17.1.1 perror() . . . . . . . . . . . . . . . . . . . . . . . . . 141 17.1.2 errno . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 17.1.3 exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 17.2 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 17.2.1 Predefined Streams . . . . . . . . . . . . . . . . . . . . 143 17.3 Basic I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 17.4 Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . . . 145 CONTENTS vii 17.4.1 Printf . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 17.5 scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 17.6 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 17.6.1 Reading and writing files . . . . . . . . . . . . . . . . . 148 17.7 sprintf and sscanf . . . . . . . . . . . . . . . . . . . . . . . . . 149 17.7.1 Stream Status Enquiries . . . . . . . . . . . . . . . . . 150 17.8 Low Level I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 17.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 18 String Handling: <string.h> 155 18.1 Basic String Handling Functions . . . . . . . . . . . . . . . . . 155 18.1.1 String Searching . . . . . . . . . . . . . . . . . . . . . 157 18.2 Character conversions and testing: ctype.h . . . . . . . . . . 159 18.3 Memory Operations: <memory.h> . . . . . . . . . . . . . . . 159 18.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 19 File Access and Directory System Calls 163 19.1 Directory handling functions: <unistd.h> . . . . . . . . . . . 163 19.1.1 ScanningandSortingDirectories:<sys/types.h>,<sys/dir.h>164 19.2 File Manipulation Routines: unistd.h, sys/types.h, sys/stat.h . 167 19.2.1 File Access . . . . . . . . . . . . . . . . . . . . . . . . 167 19.2.2 File Status . . . . . . . . . . . . . . . . . . . . . . . . . 168 19.2.3 File Manipulation:stdio.h, unistd.h . . . . . . . . . . . 169 19.2.4 Creating Temporary FIles:<stdio.h> . . . . . . . . . . 170 19.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 20 Time Functions 173 20.1 Basic time functions . . . . . . . . . . . . . . . . . . . . . . . 173 20.2 Example time applications . . . . . . . . . . . . . . . . . . . . 174 20.2.1 Example 1: Time (in seconds) to perform some com- putation . . . . . . . . . . . . . . . . . . . . . . . . . . 175 20.2.2 Example 2: Set a random number seed . . . . . . . . . 175 20.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 21 Process Control: <stdlib.h>,<unistd.h> 177 21.1 Running UNIX Commands from C . . . . . . . . . . . . . . . 177 21.2 execl() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 21.3 fork() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 viii CONTENTS 21.4 wait() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 21.5 exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 21.6 Exerises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 22 Interprocess Communication (IPC), Pipes 185 22.1 Piping in a C program: <stdio.h> . . . . . . . . . . . . . . 185 22.2 popen() — Formatted Piping . . . . . . . . . . . . . . . . . . 186 22.3 pipe() — Low level Piping . . . . . . . . . . . . . . . . . . . 186 22.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 23 IPC:Interrupts and Signals: <signal.h> 193 23.1 Sending Signals — kill(), raise() . . . . . . . . . . . . . . 194 23.2 Signal Handling — signal() . . . . . . . . . . . . . . . . . . 195 23.3 sig talk.c — complete example program . . . . . . . . . . . 197 23.4 Other signal functions . . . . . . . . . . . . . . . . . . . . . . 199 24 IPC:Message Queues:<sys/msg.h> 201 24.1 Initialising the Message Queue . . . . . . . . . . . . . . . . . . 203 24.2 IPCFunctions,KeyArguments,andCreationFlags: <sys/ipc.h>204 24.3 Controlling message queues . . . . . . . . . . . . . . . . . . . 205 24.4 Sending and Receiving Messages . . . . . . . . . . . . . . . . . 206 24.5 POSIX Messages: <mqueue.h> . . . . . . . . . . . . . . . . . 208 24.6 Example: Sending messages between two processes . . . . . . 209 24.6.1 message send.c — creating and sending to a simple message queue . . . . . . . . . . . . . . . . . . . . . . . 209 24.6.2 message rec.c — receiving the above message . . . . 211 24.7 Some further example message queue programs . . . . . . . . 213 24.7.1 msgget.c: Simple Program to illustrate msget() . . . 213 24.7.2 msgctl.cSample Program to Illustrate msgctl() . . . 215 24.7.3 msgop.c: Sample Program to Illustrate msgsnd() and msgrcv() . . . . . . . . . . . . . . . . . . . . . . . . . 219 24.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 25 IPC:Semaphores 227 25.1 Initializing a Semaphore Set . . . . . . . . . . . . . . . . . . . 228 25.2 Controlling Semaphores . . . . . . . . . . . . . . . . . . . . . 229 25.3 Semaphore Operations . . . . . . . . . . . . . . . . . . . . . . 231 25.4 POSIX Semaphores: <semaphore.h> . . . . . . . . . . . . . . 234 CONTENTS ix 25.5 semaphore.c: Illustration of simple semaphore passing . . . . 234 25.6 Some further example semaphore programs . . . . . . . . . . . 240 25.6.1 semget.c: Illustrate the semget() function . . . . . . 240 25.6.2 semctl.c: Illustrate the semctl() function . . . . . . 241 25.6.3 semop() Sample Program to Illustrate semop() . . . . 248 25.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 26 IPC:Shared Memory 255 26.1 Accessing a Shared Memory Segment . . . . . . . . . . . . . . 256 26.1.1 Controlling a Shared Memory Segment . . . . . . . . . 257 26.2 Attaching and Detaching a Shared Memory Segment . . . . . 258 26.3 Exampletwoprocessescomunicatingviasharedmemory:shm server.c, shm client.c . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 26.3.1 shm server.c . . . . . . . . . . . . . . . . . . . . . . . 260 26.3.2 shm client.c . . . . . . . . . . . . . . . . . . . . . . . 262 26.4 POSIX Shared Memory . . . . . . . . . . . . . . . . . . . . . 263 26.5 Mapped memory . . . . . . . . . . . . . . . . . . . . . . . . . 263 26.5.1 Address Spaces and Mapping . . . . . . . . . . . . . . 264 26.5.2 Coherence . . . . . . . . . . . . . . . . . . . . . . . . . 265 26.5.3 Creating and Using Mappings . . . . . . . . . . . . . . 265 26.5.4 Other Memory Control Functions . . . . . . . . . . . . 266 26.6 Some further example shared memory programs . . . . . . . . 267 26.6.1 shmget.c:Sample Program to Illustrate shmget() . . . 268 26.6.2 shmctl.c: Sample Program to Illustrate shmctl() . . 270 26.6.3 shmop.c: Sample Program to Illustrate shmat() and shmdt() . . . . . . . . . . . . . . . . . . . . . . . . . . 274 26.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 27 IPC:Sockets 281 27.1 Socket Creation and Naming . . . . . . . . . . . . . . . . . . . 282 27.2 Connecting Stream Sockets . . . . . . . . . . . . . . . . . . . . 283 27.3 Stream Data Transfer and Closing . . . . . . . . . . . . . . . . 284 27.4 Datagram sockets . . . . . . . . . . . . . . . . . . . . . . . . . 284 27.5 Socket Options . . . . . . . . . . . . . . . . . . . . . . . . . . 285 27.6 Example Socket Programs:socket server.c,socket client . 285 27.6.1 socket server.c . . . . . . . . . . . . . . . . . . . . . 285 27.6.2 socket client.c . . . . . . . . . . . . . . . . . . . . . 288 27.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 x CONTENTS 28 Threads: Basic Theory and Libraries 293 28.1 Processes and Threads . . . . . . . . . . . . . . . . . . . . . . 293 28.1.1 Benefits of Threads vs Processes . . . . . . . . . . . . . 294 28.1.2 Multithreading vs. Single threading . . . . . . . . . . . 295 28.1.3 Some Example applications of threads . . . . . . . . . 296 28.2 Thread Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 28.2.1 User-Level Threads (ULT) . . . . . . . . . . . . . . . . 297 28.2.2 Kernel-Level Threads (KLT) . . . . . . . . . . . . . . . 299 28.2.3 Combined ULT/KLT Approaches . . . . . . . . . . . . 299 28.3 Threads libraries . . . . . . . . . . . . . . . . . . . . . . . . . 300 28.4 The POSIX Threads Library:libpthread, <pthread.h> . . . 301 28.4.1 Creating a (Default) Thread . . . . . . . . . . . . . . . 301 28.4.2 Wait for Thread Termination . . . . . . . . . . . . . . 302 28.4.3 A Simple Threads Example . . . . . . . . . . . . . . . 303 28.4.4 Detaching a Thread . . . . . . . . . . . . . . . . . . . . 304 28.4.5 Create a Key for Thread-Specific Data . . . . . . . . . 305 28.4.6 Delete the Thread-Specific Data Key . . . . . . . . . . 306 28.4.7 Set the Thread-Specific Data Key . . . . . . . . . . . . 306 28.4.8 Get the Thread-Specific Data Key . . . . . . . . . . . . 307 28.4.9 Global and Private Thread-Specific Data Example . . . 308 28.4.10Getting the Thread Identifiers . . . . . . . . . . . . . . 310 28.4.11Comparing Thread IDs . . . . . . . . . . . . . . . . . . 311 28.4.12Initializing Threads . . . . . . . . . . . . . . . . . . . . 311 28.4.13Yield Thread Execution . . . . . . . . . . . . . . . . . 311 28.4.14Set the Thread Priority . . . . . . . . . . . . . . . . . 312 28.4.15Get the Thread Priority . . . . . . . . . . . . . . . . . 312 28.4.16Send a Signal to a Thread . . . . . . . . . . . . . . . . 313 28.4.17Access the Signal Mask of the Calling Thread . . . . . 313 28.4.18Terminate a Thread . . . . . . . . . . . . . . . . . . . 314 28.5 Solaris Threads: <thread.h> . . . . . . . . . . . . . . . . . . 315 28.5.1 Unique Solaris Threads Functions . . . . . . . . . . . . 316 28.5.2 Similar Solaris Threads Functions . . . . . . . . . . . . 322 28.6 Compiling a Multithreaded Application . . . . . . . . . . . . . 328 28.6.1 Preparing for Compilation . . . . . . . . . . . . . . . . 329 28.6.2 Debugging a Multithreaded Program . . . . . . . . . . 330

Description:
UNIX System Calls and Subroutines using C,. Motif, C++ 2.1 Creating, Compiling and Running Your Program . 18.1 Basic String Handling Functions .
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.