PROGRAMMING AND POSIX Steve Hoogheem Hewlett-Packard Company steve_hoogheem@hp.
Contents • • • • • • • Getting Started A Simple Program and a CGI Program The Hierarchical File System (HFS) Files and Directories - A Review Creating and Linking with Libraries POSIX Topics Additional Programming Topics 2
Getting Started • Logon: :hello . • Enter the POSIX shell: :sh or :xeq sh.hpbin.sys -L if HPPXUDC.PUB.
A Simple Program and A CGI Program • A Simple Program – Create the file – Compile and link – Run it • A CGI Program – – – – Create the file Compile and link Test it Run it from a web browser 4
A Simple Program - 1 • Create the source file: > cat >hw.c Enter/Return to start new line #include /* printf() */ main() { printf("hello world\n"); } :eod or CTRL-Y to end or > vi hw.
A Simple Program - 2 • Compile and link the source file: > c89 -o hw -D_POSIX_SOURCE hw.
A CGI program - 1 • Edit the source file: > cp hw.c hwcgi.c > vi hwcgi.c #define _POSIX_SOURCE #include main() { printf("Content-type: text/plain\n\n"); printf("hello world\n"); } • Compile and link the program: > c89 -o hwcgi hwcgi.
A CGI program - 2 • Test the CGI program: > echo foo | hwcgi | cat Content-type: text/plain hello world • Copy CGI program to cgi-bin directory: > cp hwcgi /APACHE/PUB/cgi-bin • Point browser at: http://systemname/cgi-bin/hwcgi 8
A CGI program - 3 9
The Hierarchical File System (HFS) / APACHE PUB SYS PUB bin lib usr NET cgi-bin htdocs Absolute path: /APACHE/PUB/cgi-bin/hwcgi Relative path: .
Working with Files - A Review • • • • • Naming a file File Types - bytestream vs.
Organizing Files with Directories - A Review • • • • • Displaying your current directory - pwd Absolute and relative pathnames Changing to a different directory - cd Creating a new directory - mkdir Removing a directory - rmdir 12
Linking with System Libraries • Libc is included in link by default > c89 -o hwcgi hwcgi.c • System libraries located in /lib and /usr/lib – libc, libsvipc are in /lib – libsocket are in /usr/lib • System libraries exist in both archive and shared form (as of MPE 6.0). During link, – archive library (.a suffix) merged into program – shared library (.
Linking with Libraries - Syntax • -lfoo means link with library libfoo.a – -lc is included in link by default • -Lpath tells where library is located – -L/lib -L/usr/lib is included in link by default • Link with libsvipc archive library > c89 -o hwcgi hwcgi.c -lsvipc • Link with libsvipc shared library > c89 -o hwcgi hwcgi.c -WL,XL=/lib/libsvipc.
Creating an Archive Library - 1 • Write new helloworld() function: > cat >helloworld.c #define _POSIX_SOURCE #include helloworld() { printf("hello world\n"); } > c89 -c helloworld.c • Create the archive library: > ar -rv libhw.a helloworld.
Creating an Archive Library - 2 • Have our main program: > cat >hwcgimain.c #include extern void helloworld(void); main() { printf("Content-type: text/plain\n\n"); helloworld(); } > c89 -c -D_POSIX_SOURCE hwcgimain.c • Link the program: > c89 -o hwcgi hwcgimain.o -L. -lhw – -L switch specifies library location (.
Creating a Shared Library • Create the shared library: > ld -b -o libhw.sl helloworld.o • Link with the shared library: > c89 -o hwcgi hwcgimain.o -WL,cap=ph,XL=/APACHE/CGISRC/libhw.
Programming Review • • • • • Create files - cat >, vi Compile C source code - c89 Manage archive libraries - ar Create shared libraries - ld -b, c89 -b Link programs - ld, c89 18
POSIX Topics • File Sharing • Process Management – fork – exec • • • • InterProcess Communication Signals Sockets Error handling 19
File Sharing • Duplicating file descriptors - dup & dup2 • File management - fcntl – – – – Duplicate an existing file descriptor Get & set file descriptor flags Get & set file status flags Record locking 20
Process Management - fork - 1 • pid_t fork(void); if ( (pid = fork()) < 0) perror("fork"); else if (pid == 0) /* child */ { printf(“child: here\n”); } else /* pid > 0 */ /* parent */ { printf(“parent: here\n”); } 21
Process Management - fork - 2 • Compile & link sample program > c89 -o forkt forkt.c • Program & user must have PH capability – c89 link adds PH capability by default to program – if -W option is used to add shared library, must specify cap=ph >c89 -o … -WL,cap=ph -W,XL=/lib/libsvipc.
Process Management - exec • 6 forms: execl, execve, execvp, execv, execve, execvp if ( (pid = fork()) < 0) perrror("fork"); else if (pid == 0) /* child */ { if (execl("/bin/echo", "echo", "child:", "hello", "world", (char *) 0) < 0) perror("execl"); printf("child: this never prints\n"); } 23
Process Management - execl • Compile & link sample program > c89 -o execlt execlt.
InterProcess Communication (IPC) • Pipes – pipe(fd[2]) • FIFOs – mkfifo(pathname) • Message queues • Semaphores • Shared memory 25
InterProcess Communication pipes • Pipes easy to demonstrate in shell: > who am i STEVE,CGI.APACHE@SYSTEMNAME ldev5 > who am I | cut -f1 -d’ ‘ STEVE,CGI.
Sockets • InterProcess communciation across systems via socket address: – 32-bit IPv4 address – Internet or Unix (local) – Port number • Functions – Server: socket, bind, listen, accept, read – Client: socket, write connect, 27
Signals • signal() & raise() are ANSI C, not POSIX.
Error Handling • errno is a system global defined in
Additional Programming Topics • • • • • • Debugging Your Application Shell Scripts make utility Development Tools GNU Tools Porting Wrappers 30
Debugging Your Application - 1 • Add printf() statements in your code – use #ifdef DEBUG compile directive • Add perror() statements in your code – use #ifdef PRINT_ERROR compile directive if ( (fd = open(pathname, O_RDWR)) < 0) { /* errno already set by open() */ #if defined(DEBUG) || defined(PRINT_ERROR) sprintf(msg, "functionX(): open(%s, O_RDWR)”, pathname); perror(msg); #endif return -1; } 31
Debugging Your Application - 2 • perror() continued: if ( functionX(argv[1]) < 0) { /* errno already set by functionX() */ #if defined(DEBUG) || defined(PRINT_ERROR) sprintf(msg, "functionY(): functionX(%s)”, argv[1]); perror(msg); #endif return -1; } 32
Debugging Your Application - 3 • MPE System Debugger > callci “run ./program ;debug” • Symbolic debugger - xdb – use -g switch during compile > c89 -g ... – link with /SYS/LIB/XDBEND – first, as MANAGER.SYS: > cd /SYS/LIB; ln -s XDBEND end.o > c89 -o … /SYS/LIB/end.
Shell Scripts • Automate steps with a shell script > cat >hwcgi.sh #!/bin/sh c89 -c helloworld.c ar -rv libhw.a helloworld.o c89 -c hwcgimain.c c89 -o hwcgi hwcgimain.o -L. -lhw • Execute permission required to execute > chmod u+x hwcgi.sh > hwcgi.sh • Special scripts: /etc/profile and .
Make utility • Rebuilds only components which need rebuilding > cat >Makefile all: hwcgi hwcgi: hwcgimain.o libhw.a $(CC) -o $@ hwcgimain.o -L. -lhw libhw.a: helloworld.
Development Tools • Terminal Emulaters on Windows – Reflection - http://www.wrq.com/products/refprod.htm – Qterm - http://aics-research.com/qcterm/ • Edit files from another system – Samba - http: ://jazz.external.hp.com/src/, select Samba/iX • Development Environments – Whisper Technology http://www.whispertech.com/pstudio.
GNU Tools • Downloadable software from: http: ://jazz.external.hp.
Porting Wrappers • Downloadable software from: http: ://jazz.external.hp.
Error Handling with MPE Intrinsics • _mpe_errno, _mpe_intrinsic are system globals defined in – Requires _MPEXL_SOURCE compile directive to use • Porting Wrappers has functions pmpeerror() & strmpeerror() plus header file #include
Summary • • • • • • • Getting Started A Simple Program and a CGI Program The Hierarchical File System (HFS) Files and Directories - A Review Creating and Linking with Libraries POSIX Topics Additional Programming Topics 40
Additional Resources • MPE/iX manuals: http://www.docs.hp.com – HP C/iX Library Reference Manual - function man pages – MPE/iX Developer’s Kit Reference Manual - function man pages – MPE/iX Shell and Utilities User’s Guide - commands, shell, vi, make – New Features of MPE/iX: Using the Hierarchical File System - commands • Programming with examples: – “Advanced Programming in the UNIX Environment” by W. Richard Stevens http://www.kohala.com/start/apue.
Additional Resources • POSIX – “POSIX Programmer's Guide” by Donald Lewine http://www.oreilly.com/catalog/posix/ – “The POSIX.1 Standard - A Programmer’s Guide” by Fred Zlotnick – POSIX Specifications from IEEE - very detailed http://standards.ieee.org/catalog/posix.html#gen22 • make – “Managing Projects with make” by Andrew Oram and Steve Talbott http://www.oreilly.com/catalog/make2/ – MPE vs.