Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hewlett-Packard Company

Similar presentations


Presentation on theme: "Hewlett-Packard Company"— Presentation transcript:

1 Hewlett-Packard Company
5/12/2018 PROGRAMMING AND POSIX Steve Hoogheem Hewlett-Packard Company Everybody got a handout? Introduce myself. Who’s new to MPE? Who’s new to POSIX?

2 Contents Getting Started A Simple Program and a CGI Program
5/12/2018 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

3 Getting Started Logon: Enter the POSIX shell: Exit the POSIX shell:
5/12/2018 Getting Started Logon: :hello <user>.<account> Enter the POSIX shell: :sh if HPPXUDC.PUB.SYS UDC set or :xeq sh.hpbin.sys -L Exit the POSIX shell: > exit POSIX shell vs. MPE CI or Command Interpreter

4 A Simple Program and A CGI Program
5/12/2018 A Simple Program and A CGI Program A Simple Program Create the file Compile and link Run it A CGI Program Test it Run it from a web browser

5 A Simple Program - 1 Create the source file:
5/12/2018 A Simple Program - 1 Create the source file: > cat >hw.c Enter/Return to start new line #include <stdio.h> /* printf() */ main() { printf("hello world\n"); } :eod or CTRL-Y to end or > vi hw.c

6 A Simple Program - 2 Compile and link the source file:
5/12/2018 A Simple Program - 2 Compile and link the source file: > c89 -o hw -D_POSIX_SOURCE hw.c Run the program: > hw hello world Use compile directive _POSIX_SOURCE Alternatively, you can define it in the source itself Who’s new to c89?

7 A CGI program - 1 Edit the source file: #define _POSIX_SOURCE
5/12/2018 A CGI program - 1 Edit the source file: > cp hw.c hwcgi.c > vi hwcgi.c #define _POSIX_SOURCE #include <stdio.h> main() { printf("Content-type: text/plain\n\n"); printf("hello world\n"); } Compile and link the program: > c89 -o hwcgi hwcgi.c

8 A CGI program - 2 Test the CGI program:
5/12/2018 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:

9 5/12/2018 A CGI program - 3

10 The Hierarchical File System (HFS)
5/12/2018 The Hierarchical File System (HFS) / APACHE SYS bin lib usr PUB PUB NET cgi-bin htdocs Absolute path: /APACHE/PUB/cgi-bin/hwcgi Relative path: ./hwcgi MPE commands and intrinsics handle HFS files too Absolute and relative pathnames MPE escaped syntax - ./file != file used in MPE command File types: bytestream & fixed record files - tobyte and frombyte shell commands convert one to the other POSIX shell vs. MPE Command Interpreter (CI)

11 Working with Files - A Review
5/12/2018 Working with Files - A Review Naming a file File Types - bytestream vs. fixed record Creating and listing files - cat >, vi, ls Viewing and printing a file - more, cat, lp Copying, renaming, and removing files - cp, mv, rm Displaying and changing a file’s permissions and ownership - chmod, chown, chgrp Naming a file - can contain up to 256 characters, except when the file is under an MPE group or account, then 16 characters is the limit - can be any combination of: - alphanumeric character - special character + - _ . - case matters - file1, FILE1, File1, fILE1 are different files Creating and listing files > cat >file Enter/Return to end each line, CTRL-Y to finish > vi file1 > ls Files starting with . (dot) are hidden and not normally listed, use ls -a Viewing and printing a file: > more file1 > cat file1 > lp file1 Copying, renaming, and removing files > cp file1 file If file2 exists, it is overwritten. > mv file1 file If file2 exists, it is overwritten. > rm file2 Displaying and Changing a File’s Permissions and Ownership Display a file’s permissions: > ll -rw-r--r CGI.APACHE APACHE Feb 2 19:26 file1 -rw-r--r CGI.APACHE APACHE Feb 2 19:26 file2 Change a file’s permissions: > chmod u+x file1 Change a file’s ownership: > chown MGR.APACHE file1

12 Organizing Files with Directories - A Review
5/12/2018 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

13 Linking with System Libraries
5/12/2018 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 (.sl suffix) is NOT merged

14 Linking with Libraries - Syntax
5/12/2018 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.sl -WL switch specifies shared library must specify full pathname

15 Creating an Archive Library - 1
5/12/2018 Creating an Archive Library - 1 Write new helloworld() function: > cat >helloworld.c #define _POSIX_SOURCE #include <stdio.h> helloworld() { printf("hello world\n"); } > c89 -c helloworld.c Create the archive library: > ar -rv libhw.a helloworld.o

16 Creating an Archive Library - 2
5/12/2018 Creating an Archive Library - 2 Have our main program: > cat >hwcgimain.c #include <stdio.h> 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 (. is CWD)

17 Creating a Shared Library
5/12/2018 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.sl must specify full pathname

18 Programming Review Create files - cat >, vi
5/12/2018 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 POSIX C compiler - based on 1996 POSIX.1 standard vs. ISO C - based on 1994 standard

19 POSIX Topics File Sharing Process Management
5/12/2018 POSIX Topics File Sharing Process Management fork exec InterProcess Communication Signals Sockets Error handling

20 File Sharing Duplicating file descriptors - dup & dup2
5/12/2018 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 Supply actual 2nd descriptor with dup2 - If fd2 is open, it is first closed Fcntl - 4 different functions in 1 1) Duplicate 2) File descriptor flags - only 1 - FD_CLOEXEC - file is closed across an exec 3) File status flags - O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_TRUNC, O_EXCL, O_NONBLOCK -set: only O_APPEND and O_NONBLOCK can be changed 4) Record or range locking or byte locking

21 Process Management - fork - 1
5/12/2018 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”);

22 Process Management - fork - 2
5/12/2018 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.sl Run sample program > forkt child: here parent: here User must have PH capability : altacct <account> ;cap=+ph done by user with SM capability :altuser <user>.<account> ;cap=+ph done by AM with AM capability Like 2 programs in one More common to start some other program

23 Process Management - exec
5/12/2018 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"); }

24 Process Management - execl
5/12/2018 Process Management - execl Compile & link sample program > c89 -o execlt execlt.c Run sample program > execlt child: hello world parent: exiting Daemons on MPE - no orphaned processes today - use MPE batch job for process independent of session - nohup in Porting Wrappers makes this easy

25 InterProcess Communication (IPC)
5/12/2018 InterProcess Communication (IPC) Pipes pipe(fd[2]) FIFOs mkfifo(pathname) Message queues Semaphores Shared memory Pipes - unidirectional communication from sender to receiver - can only be used between related processes FIFOs - also called named pipes - unrelated processes can exchange data Message queues - linked list of messages, identified by a message queue identifier Semaphones - a counter used to provide access to a shared data object for multiple processes Shared memory - memory region shared by multiple processes - fastest form of IPC since data is not copied >>> these 3 function groups found in library /lib/libsvipc.a

26 InterProcess Communication - pipes
5/12/2018 InterProcess Communication - pipes Pipes easy to demonstrate in shell: > who am i ldev TUE 1:04P > who am I | cut -f1 -d’ ‘ int pipe(int filedes[2]); Pipes - unidirectional form of communication is the most common

27 Sockets InterProcess communciation across systems via socket address:
5/12/2018 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, connect, write Sockets - more commonly called Berkeley Sockets

28 Signals signal() & raise() are ANSI C, not POSIX.1
5/12/2018 Signals signal() & raise() are ANSI C, not POSIX.1 Use sigaction() instead Signal is generated, pending, delivered Signal not delivered if process is executing in system code; signal is delivered upon exit of system code Process can: Ignore the signal Execute a signal-handling function; process resumes where it was interrupted Restore the default action of the signal Signal - “a mechanism by which a process may be notified of, or affected by, an event occurring in the system” Executing system code - if system code is being executed because of, for example, a read, the signal is not delivered until the read completes or errors

29 Error Handling errno is a system global defined in <errno.h>
5/12/2018 Error Handling errno is a system global defined in <errno.h> Functions: char *strerror(int errnum); void perror(const char *msg); if ( (fd = open(pathname, O_RDWR)) < 0) { /* errno already set by open() */ perror("functionX(): open()"); return -1; }

30 Additional Programming Topics
5/12/2018 Additional Programming Topics Debugging Your Application Shell Scripts make utility Development Tools GNU Tools Porting Wrappers

31 Debugging Your Application - 1
5/12/2018 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; }

32 Debugging Your Application - 2
5/12/2018 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; }

33 Debugging Your Application - 3
5/12/2018 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.o > xdb -h program

34 Shell Scripts Automate steps with a shell script
5/12/2018 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 .profile Shell scripts - great way to automate repeating tasks Once you know shell commands, there’s no scripting language to learn

35 Make utility Rebuilds only components which need rebuilding all: hwcgi
5/12/2018 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.o $(AR) $(ARFLAGS) $? > make make -n to display commands without execution Make - a great way to automate building your application

36 Development Tools Terminal Emulaters on Windows
5/12/2018 Development Tools Terminal Emulaters on Windows Reflection - Qterm - Edit files from another system Samba - http: ://jazz.external.hp.com/src/, select Samba/iX Development Environments Whisper Technology -

37 GNU Tools Downloadable software from: Tools include:
5/12/2018 GNU Tools Downloadable software from: http: ://jazz.external.hp.com/src/, select GNU Tools include: gcc - C compiler gxx or g++ - C++ compiler gdb - debugger (port in progress) gmake - for building software gzip, gunzip - file compression and decompression cvs - Concurrent Version System for software control

38 Porting Wrappers Downloadable software from: Additional Functions:
5/12/2018 Porting Wrappers Downloadable software from: http: ://jazz.external.hp.com/src/#PortingWrappers Additional Functions: Error reporting: pmpeerror, strmpeerror Mapped regions: mmap, mprotect, msync, munmap Sockets enabled: fcntl, fstat, stat Additional Libraries & Header Files Additional Commands: ld, nm, nohup Command wrappers: ftp, ipcs, ipcrm, ping, xdb

39 Error Handling with MPE Intrinsics
5/12/2018 Error Handling with MPE Intrinsics _mpe_errno, _mpe_intrinsic are system globals defined in <errno.h> Requires _MPEXL_SOURCE compile directive to use Porting Wrappers has functions pmpeerror() & strmpeerror() plus header file <mpeerrno.h> #include <mpeerrno.h> #pragma intrinsic FCONTROL FCONTROL(_MPE_FILENO(fildes), 2, &dummy); if ( ( ccode_return = ccode() ) != CCE ) { errno = EINVAL; mpe_errno = ccode_return; mpe_intrinsic = FCONTROL_INTRINSIC; #if defined(DEBUG) || defined(PRINT_ERROR) pmpeerror("functionX(): FCONTROL(2)"); #endif return -1; }

40 Summary Getting Started A Simple Program and a CGI Program
5/12/2018 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

41 Additional Resources MPE/iX manuals: Programming with examples:
5/12/2018 Additional Resources MPE/iX manuals: 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 - directory util/apue in Porting Wrappers contains Stevens’ main header file and library

42 Additional Resources POSIX make
5/12/2018 Additional Resources POSIX “POSIX Programmer's Guide” by Donald Lewine “The POSIX.1 Standard - A Programmer’s Guide” by Fred Zlotnick POSIX Specifications from IEEE - very detailed make “Managing Projects with make” by Andrew Oram and Steve Talbott MPE vs. POSIX Functions and Commands


Download ppt "Hewlett-Packard Company"

Similar presentations


Ads by Google