tutorialc.pdf - Programming in C Programming in C UNIX System Calls and Subroutines using C A D Marshall 1994-9 Substantially Updated March 1999

tutorialc.pdf - Programming in C Programming in C UNIX...

This preview shows page 1 out of 847 pages.

You've reached the end of your free preview.

Want to read all 847 pages?

Unformatted text preview: Programming in C Programming in C UNIX System Calls and Subroutines using C, A. D. Marshall 1994-9 Substantially Updated March 1999 NetGuide Gold Site Next: Copyright Search for Keywords in C Notes Keyword Searcher Download Postscript Version of Notes Click Here to Download Course Notes. Local Students Only. (1 of 16) [25/03/2002 10:35:59] Programming in C Algorithm Animations Direct link to Java Algorithm Animations (C related) C COURSEWARE Lecture notes + integrated exercises, solutions and marking ● Contents ● The Common Desktop Environment ● ❍ The front panel ❍ The file manager ❍ The application manager ❍ The session manager ❍ Other CDE desktop tools ❍ Application development tools ❍ Application integration ❍ Windows and the Window Manager ❍ The Root Menu ❍ Exercises C/C++ Program Compilation ❍ ❍ Creating, Compiling and Running Your Program ■ Creating the program ■ Compilation ■ Running the program The C Compilation Model ■ The Preprocessor ■ C Compiler ■ Assembler ■ Link Editor ■ Some Useful Compiler Options (2 of 16) [25/03/2002 10:35:59] Programming in C ● ● ● ● ■ Using Libraries ■ UNIX Library Functions ■ Finding Information about Library Functions ❍ Lint -- A C program verifier ❍ Exercises C Basics ❍ History of C ❍ Characteristics of C ❍ C Program Structure ❍ Variables ■ Defining Global Variables ■ Printing Out and Inputting Variables ❍ Constants ❍ Arithmetic Operations ❍ Comparison Operators ❍ Logical Operators ❍ Order of Precedence ❍ Exercises Conditionals ❍ The if statement ❍ The ? operator ❍ The switch statement ❍ Exercises Looping and Iteration ❍ The for statement ❍ The while statement ❍ The do-while statement ❍ break and continue ❍ Exercises Arrays and Strings ❍ Single and Multi-dimensional Arrays (3 of 16) [25/03/2002 10:35:59] Programming in C ● ● ❍ Strings ❍ Exercises Functions ❍ void functions ❍ Functions and Arrays ❍ Function Prototyping ❍ Exercises Further Data Types ❍ Structures ■ ● ❍ Unions ❍ Coercion or Type-Casting ❍ Enumerated Types ❍ Static Variables ❍ Exercises Pointers ❍ What is a Pointer? ❍ Pointer and Functions ❍ Pointers and Arrays ❍ Arrays of Pointers ❍ Multidimensional arrays and pointers ❍ Static Initialisation of Pointer Arrays ❍ Pointers and Structures ❍ Common Pointer Pitfalls ❍ ● Defining New Data Types ■ Not assigning a pointer to memory address before using it ■ Illegal indirection Exercise Dynamic Memory Allocation and Dynamic Structures ❍ Malloc, Sizeof, and Free ❍ Calloc and Realloc ❍ Linked Lists (4 of 16) [25/03/2002 10:35:59] Programming in C ● ● ❍ Full Program: queue.c ❍ Exercises Advanced Pointer Topics ❍ Pointers to Pointers ❍ Command line input ❍ Pointers to a Function ❍ Exercises Low Level Operators and Bit Fields ❍ Bitwise Operators ❍ Bit Fields ❍ ● ● ● ● ■ Bit Fields: Practical Example ■ A note of caution: Portability Exercises The C Preprocessor ❍ #define ❍ #undef ❍ #include ❍ #if -- Conditional inclusion ❍ Preprocessor Compiler Control ❍ Other Preprocessor Commands ❍ Exercises C, UNIX and Standard Libraries ❍ Advantages of using UNIX with C ❍ Using UNIX System Calls and Library Functions Integer Functions, Random Number, String Conversion, Searching and Sorting: <stdlib.h> ❍ Arithmetic Functions ❍ Random Numbers ❍ String Conversion ❍ Searching and Sorting ❍ Exercises Mathematics: <math.h> (5 of 16) [25/03/2002 10:35:59] Programming in C ● ❍ Math Functions ❍ Math Constants Input and Output (I/O):stdio.h ❍ ❍ Reporting Errors ■ perror() errno ■ exit() Streams ■ Predefined Streams ■ ❍ Basic I/O ❍ Formatted I/O ■ ❍ scanf ❍ Files ■ ❍ Reading and writing files Stream Status Enquiries ❍ Low Level I/O ❍ Exercises String Handling: <string.h> ❍ Basic String Handling Functions ■ ● Printf sprintf and sscanf ■ ● Redirection String Searching ❍ Character conversions and testing: ctype.h ❍ Memory Operations: <memory.h> ❍ Exercises File Access and Directory System Calls ❍ Directory handling functions: <unistd.h> ■ ❍ Scanning and Sorting Directories: <sys/types.h>,<sys/dir.h> File Manipulation Routines: unistd.h, sys/types.h, sys/stat.h ■ File Access (6 of 16) [25/03/2002 10:35:59] Programming in C ■ ❍ ● ● ● ● ■ File Status ■ File Manipulation:stdio.h, unistd.h ■ Creating Temporary FIles:<stdio.h> Exercises Time Functions ❍ Basic time functions ❍ Example time applications ❍ ● errno ■ Example 1: Time (in seconds) to perform some computation ■ Example 2: Set a random number seed Exercises Process Control: <stdlib.h>,<unistd.h> ❍ Running UNIX Commands from C ❍ execl() ❍ fork() ❍ wait() ❍ exit() ❍ Exerises Interprocess Communication (IPC), Pipes ❍ Piping in a C program: <stdio.h> ❍ popen() -- Formatted Piping ❍ pipe() -- Low level Piping ❍ Exercises IPC:Interrupts and Signals: <signal.h> ❍ Sending Signals -- kill(), raise() ❍ Signal Handling -- signal() ❍ sig_talk.c -- complete example program ❍ Other signal functions IPC:Message Queues:<sys/msg.h> ❍ Initialising the Message Queue ❍ IPC Functions, Key Arguments, and Creation Flags: <sys/ipc.h> (7 of 16) [25/03/2002 10:35:59] Programming in C ❍ Controlling message queues ❍ Sending and Receiving Messages ❍ POSIX Messages: <mqueue.h> ❍ Example: Sending messages between two processes ❍ ❍ ● message_send.c -- creating and sending to a simple message queue ■ message_rec.c -- receiving the above message Some further example message queue programs ■ msgget.c: Simple Program to illustrate msget() ■ msgctl.cSample Program to Illustrate msgctl() ■ msgop.c: Sample Program to Illustrate msgsnd() and msgrcv() Exercises IPC:Semaphores ❍ Initializing a Semaphore Set ❍ Controlling Semaphores ❍ Semaphore Operations ❍ POSIX Semaphores: <semaphore.h> ❍ semaphore.c: Illustration of simple semaphore passing ❍ Some further example semaphore programs ❍ ● ■ ■ semget.c: Illustrate the semget() function ■ semctl.c: Illustrate the semctl() function ■ semop() Sample Program to Illustrate semop() Exercises IPC:Shared Memory ❍ Accessing a Shared Memory Segment ■ Controlling a Shared Memory Segment ❍ Attaching and Detaching a Shared Memory Segment ❍ Example two processes comunicating via shared memory: shm_server.c, shm_client.c ■ shm_server.c ■ shm_client.c (8 of 16) [25/03/2002 10:35:59] Programming in C ❍ POSIX Shared Memory ❍ Mapped memory ❍ ❍ ● Address Spaces and Mapping ■ Coherence ■ Creating and Using Mappings ■ Other Memory Control Functions Some further example shared memory programs ■ shmget.c:Sample Program to Illustrate shmget() ■ shmctl.c: Sample Program to Illustrate shmctl() ■ shmop.c: Sample Program to Illustrate shmat() and shmdt() Exercises IPC:Sockets ❍ Socket Creation and Naming ❍ Connecting Stream Sockets ❍ Stream Data Transfer and Closing ❍ Datagram sockets ❍ Socket Options ❍ Example Socket Programs:socket_server.c,socket_client ❍ ● ■ ■ socket_server.c ■ socket_client.c Exercises Threads: Basic Theory and Libraries ❍ ❍ Processes and Threads ■ Benefits of Threads vs Processes ■ Multithreading vs. Single threading ■ Some Example applications of threads Thread Levels ■ User-Level Threads (ULT) ■ Kernel-Level Threads (KLT) ■ Combined ULT/KLT Approaches (9 of 16) [25/03/2002 10:35:59] Programming in C ❍ Threads libraries ❍ The POSIX Threads Library:libpthread, <pthread.h> ❍ ■ Creating a (Default) Thread ■ Wait for Thread Termination ■ A Simple Threads Example ■ Detaching a Thread ■ Create a Key for Thread-Specific Data ■ Delete the Thread-Specific Data Key ■ Set the Thread-Specific Data Key ■ Get the Thread-Specific Data Key ■ Global and Private Thread-Specific Data Example ■ Getting the Thread Identifiers ■ Comparing Thread IDs ■ Initializing Threads ■ Yield Thread Execution ■ Set the Thread Priority ■ Get the Thread Priority ■ Send a Signal to a Thread ■ Access the Signal Mask of the Calling Thread ■ Terminate a Thread Solaris Threads: <thread.h> ■ ■ Unique Solaris Threads Functions ■ Suspend Thread Execution ■ Continue a Suspended Thread ■ Set Thread Concurrency Level ■ Readers/Writer Locks ■ Readers/Writer Lock Example Similar Solaris Threads Functions ■ Create a Thread ■ Get the Thread Identifier ■ Yield Thread Execution (10 of 16) [25/03/2002 10:35:59] Programming in C ❍ ● Signals and Solaris Threads ■ Terminating a Thread ■ Creating a Thread-Specific Data Key ■ Example Use of Thread Specific Data:Rethinking Global Variables Compiling a Multithreaded Application ■ Preparing for Compilation ■ Debugging a Multithreaded Program Further Threads Programming:Thread Attributes (POSIX) ❍ Attributes ❍ Initializing Thread Attributes ❍ Destroying Thread Attributes ❍ Thread's Detach State ❍ Thread's Set Scope ❍ Thread Scheduling Policy ❍ ■ Thread Inherited Scheduling Policy ■ Set Scheduling Parameters Thread Stack Size ■ ● ■ Building Your Own Thread Stack Further Threads Programming:Synchronization ❍ Mutual Exclusion Locks ■ Initializing a Mutex Attribute Object ■ Destroying a Mutex Attribute Object ■ The Scope of a Mutex ■ Initializing a Mutex ■ Locking a Mutex ■ Lock with a Nonblocking Mutex ■ Destroying a Mutex ■ Mutex Lock Code Examples ■ ■ Mutex Lock Example ■ Using Locking Hierarchies: Avoiding Deadlock Nested Locking with a Singly Linked List (11 of 16) [25/03/2002 10:35:59] Programming in C ■ ❍ ❍ ● ● Solaris Mutex Locks Condition Variable Attributes ■ Initializing a Condition Variable Attribute ■ Destoying a Condition Variable Attribute ■ The Scope of a Condition Variable ■ Initializing a Condition Variable ■ Block on a Condition Variable ■ Destroying a Condition Variable State ■ Solaris Condition Variables Threads and Semaphores ■ POSIX Semaphores ■ Basic Solaris Semaphore Functions Thread programming examples ❍ Using thr_create() and thr_join() ❍ Arrays ❍ Deadlock ❍ Signal Handler ❍ Interprocess Synchronization ❍ The Producer / Consumer Problem ❍ A Socket Server ❍ Using Many Threads ❍ Real-time Thread Example ❍ POSIX Cancellation ❍ Software Race Condition ❍ Tgrep: Threadeds version of UNIX grep ❍ Multithreaded Quicksort Remote Procedure Calls (RPC) ❍ What Is RPC ❍ How RPC Works ❍ RPC Application Development ■ Defining the Protocol (12 of 16) [25/03/2002 10:35:59] Programming in C ❍ ❍ ❍ ■ Defining Client and Server Application Code ■ Compliling and running the application Overview of Interface Routines ■ Simplified Level Routine Function ■ Top Level Routines Intermediate Level Routines ■ Expert Level Routines ■ Bottom Level Routines The Programmer's Interface to RPC ■ Simplified Interface ■ Passing Arbitrary Data Types ■ Developing High Level RPC Applications ■ ■ ❍ ● Sharing the data ■ The Server Side ■ The Client Side Exercise Protocol Compiling and Lower Level RPC Programming ❍ What is rpcgen ❍ An rpcgen Tutorial ■ ● Defining the protocol Converting Local Procedures to Remote Procedures ❍ Passing Complex Data Structures ❍ Preprocessing Directives ■ cpp Directives ■ Compile-Time Flags ■ Client and Server Templates ■ Example rpcgen compile options/templates ❍ Recommended Reading ❍ Exercises Writing Larger Programs ❍ Header files (13 of 16) [25/03/2002 10:35:59] Programming in C ❍ External variables and functions ■ ● Scope of externals ❍ Advantages of Using Several Files ❍ How to Divide a Program between Several Files ❍ Organisation of Data in each File ❍ The Make Utility ❍ Make Programming ❍ Creating a makefile ❍ Make macros ❍ Running Make Program Listings ❍ hello.c ❍ printf.c ❍ swap.c ❍ args.c ❍ arg.c ❍ average.c ❍ cio.c ❍ factorial ❍ power.c ❍ ptr_arr.c ❍ Modular Example ■ main.c ■ WriteMyString.c ■ header.h ■ Makefile ❍ static.c ❍ malloc.c ❍ queue.c ❍ bitcount.c ❍ lowio.c ❍ print.c (14 of 16) [25/03/2002 10:35:59] Programming in C ❍ cdir.c ❍ list.c ❍ list_c.c ❍ fork_eg.c ❍ fork.c ❍ signal.c ❍ sig_talk.c ❍ Piping ■ plot.c ■ plotter.c ■ externals.h ❍ random.c ❍ time.c ❍ timer.c Online Marking of C Programs --CEILIDH ● Ceilidh - On Line C Tutoring System ❍ Why Use CEILIDH ? ❍ Introduction ❍ Using Ceilidh as a Student ❍ ■ The course and unit level ■ The exercise level ■ Interpreted language exercises ■ Question/answer exercises The command line interface (TEXT CEILIDH ONLY) ■ Advantages of the command line interface ■ General points ❍ Conclusions ❍ How Ceilidh works, Ceilidh Course Notes, User Guides etc. (15 of 16) [25/03/2002 10:35:59] Programming in C ❍ ● References About this document ... Dave Marshall 29/3/1999 (16 of 16) [25/03/2002 10:35:59] Copyright Next: Books Up: Programming in C Previous: Programming in C Copyright All notes here are copyrighted. All copying etc. is not permitted. David Marshall 1994 [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:04] Books Next: About This Course Up: Programming in C Previous: Copyright Books ● ● ● ● ● ● ● Brian W Kernighan and Dennis M Ritchie, The C Programming Language 2nd Ed, Prentice-Hall, 1988. Kenneth E. Martin, C Through UNIX, WCB Group, 1992. Keith Tizzard, C for Professional Programmers, Ellis Horwood, 1986. Chris Carter, Structured Programming into ANSI C, Pittman, 1991. C. Charlton, P. Leng and Janet Little, A Course on C, McGraw Hill, 1992. G. Bronson and S. Menconi, A First Book on C: Fundamentals of C Programming (2nd ed.), West Publishing, 1991. Any book on ANSI C will probably do, some UNIX may help. [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:05] About This Course Next: Course Material and On-line facilities Up: Programming in C Previous: Books About This Course This course aims to teach a sound basis of C PROGRAMMING. We will start with basic ideas and hopefully extend these to include some advanced features of C. We will particularly look at how C uses pointers, references low level memory and bytes and how it interfaces with the operating system. ● Course Material and On-line facilities ● Exercises - Using X Windows, Editing and UNIX Basics [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:06] Course Material and On-line facilities Next: Exercises - Using X WindowsEditing and Up: About This Course Previous: About This Course Course Material and On-line facilities Obviously you have been provided with the course notes that you are reading. In addition several on line facilities will be employed in this course. ● Ceilidh - an online tutoring and program marking facility (see Appendix ● ● for details. All exercises given can be answered in Ceilidh. Some alternative C course notes are also available. Ceilidh will mark any exercise submitted very quickly. All program listings are available in the /well/dave/C/EXAMPLES directory. Feel free to copy these to help speed up your writing of programs. Mind Ceilidh helps with this also by providing skeleton programs. The course notes are also on-line. Run the mosaic program and select comma lecture notes. We will be using the departments Dec Workstations which are unix based. If you have not use unix or a workstation before do not worry the first tutorial session is to be used for this purpose. Details on how to use the system are in Appendix follow. [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:08] . Also try the exercises that Exercises - Using X Windows, Editing and UNIX Basics Next: The C Program Up: About This Course Previous: Course Material and On-line facilities Exercises - Using X Windows, Editing and UNIX Basics 1. Practice opening, closing and moving windows around the screen and to/from the background/foreground. Get used to using the mouse and its buttons for such tasks. 2. Figure out the function of each of the three mouse buttons. Pay particular attention to the different functions the buttons in different windows (applications) and also when the mouse is pointing to the background. 3. Find out how to resize windows etc. and practice this. 4. Fire up textedit application and practice editing text files. Create any files you wish for now. Figure out basic options like cut and paste of text around the file, saving and loading files, searching for strings in the text and replacing strings. Particularly pay attention in getting used to using the Key Strokes and / or mouse to perform the above tasks. 5. Use Unix Commands (see Appendix ) to 1. Copy a file (created by text editor or other means) to another file called spare. 2. Rename your original file to b called new. 3. Delete the file spare. 4. Display your original file on the terminal. 5. Print your file out. 6. Familiarise yourself with other UNIX functions by creating various files of text etc. and trying out the various functions listed in handouts. [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:10] Ceilidh - On Line C Tutoring System Next: Why Use CEILIDH ? Up: Programming in C Previous: Exercises Ceilidh - On Line C Tutoring System ● Why Use CEILIDH ? ● Introduction ● Using Ceilidh as a Student ● ❍ The course and unit level ❍ The exercise level ❍ Interpreted language exercises ❍ Question/answer exercises The command line interface (TEXT CEILIDH ONLY) ❍ Advantages of the command line interface ❍ General points ● Conclusions ● How Ceilidh works, Ceilidh Course Notes, User Guides etc. ● References [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:12] Using Dec Workstations and Unix Next: About this document ... Up: Programming in C Previous: timer.c Using Dec Workstations and Unix [email protected] Wed Sep 14 10:06:31 BST 1994 [25/03/2002 10:36:13] Contents Contents ● Contents ● The Common Desktop Environment ● ❍ The front panel ❍ The file manager ❍ The application manager ❍ The session manager ❍ Other CDE desktop tools ❍ Application development tools ❍ Application integration ❍ Windows and the Window Manager ❍ The Root Menu ❍ Exercises C/C++ Program Compilation ❍ ❍ Creating, Compiling and Running Your Program ■ Creating the program ■ Compilation ■ Running the program The C Compilation Model ■ The Preprocessor ■ C Compiler ■ Assembler ■ Link Editor ■ Some Useful Compiler Options ■ Using Libraries ■ UNIX Library Functions ■ Finding Information about Library Functions ❍ Lint -- A C program verifier ❍ Exercises (1 of 13) [25/03/2002 10:36:44] Contents ● ● ● ● ● C Basics ❍ History of C ❍ Characteristics of C ❍ C Program Structure ❍ Variables ■ Defining Global Variables ■ Printing Out and Inputting Variables ❍ Constants ❍ Arithmetic Operations ❍ Comparison Operators ❍ Logical Operators ❍ Order of Precedence ❍ Exercises Conditionals ❍ The if statement ❍ The ? operator ❍ The switch statement ❍ Exercises Looping and Iteration ❍ The for statement ❍ The while statement ❍ The do-while statement ❍ break and continue ❍ Exercises Arrays and Strings ❍ Single and Multi-dimensional Arrays ❍ Strings ❍ Exercises Functions ❍ void functions ❍ Functions and Arrays (2 of 13) [25/03/2002 10:36:44] Contents ● ❍ Function Prototyping ❍ Exercises Further Data Types ❍ Structures ■ ● ❍ Unions ❍ Coercion or Type-Casting ❍ Enumerated Types ❍ Static Variables ❍ Exercises Pointers ❍ What is a Pointer? ❍ Pointer and Functions ❍ Pointers and Arrays ❍ Arrays of Pointers ❍ Multidimensional arrays and pointers ❍ Static Initialisation of Pointer Arrays ❍ Pointers and Structures ❍ Common Pointer Pitfalls ❍ ● ● Defining New Data Types ■ Not assigning a pointer to memory address before using it ■ Illegal indirection Exercise Dynamic Memory Allocation and Dynamic Structures ❍ Malloc, Sizeof, and Free ❍ Calloc and Realloc ❍ Linked Lists ❍ Full Program: queue.c ❍ Exercises Advanced Pointer Topics ❍ Pointers to Pointers ❍ Command line input (3 of 13) [25/03/2002 10:36:44] Contents ● ❍ Pointers to a Function ❍ Exercises Low Level Operators and Bit Fields ❍ Bitwise Operators ❍ Bit Fields ❍ ● ● ● ● ● ■ Bit Fields: Practical Example ■ A note of caution: Portability Exercises The C Preprocessor ❍ #define ❍ #undef ❍ #include ❍ #if -- Conditional inclusion ❍ Preprocessor Compiler Control ❍ Other Preprocessor Commands ❍ Exercises C, UNIX and Standard Libraries ❍ Advantages of using UNIX with C ❍ Using UNIX System Calls and Library Functions Integer Functions, Random Number, String Conversion, Searching and Sorting: <st...
View Full Document

  • Fall '19
  • X Window System, desktop environment, Window manager

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture