2 - ELEC 2602 – Object Oriented & Systems...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ELEC 2602 – Object Oriented & Systems Programming Module 2 – Unix O.S. Support (II) 1 Module 2: Unix O.S. Support (II) • Outline: – The Environment of A UNIX Process – Signals & Handlers – Advanced I/O Functions ** Acknowledgments and Disclaimer: The materials presented in this module are adopted from many textbooks, in particular, those listed in the References in Module 1A. 2 Environment of A UNIX Process: An Overview • The environment of a single process • The Environment list to define environ. variables • The main(…) function: command-line parameters • Memory layout • Process termination • Guarding against abnormal execution: longjmp(), setjmp() , and the C function stack 3 Environment of a UNIX Process: The Environment List • The environment list is represented by the pointer: extern char **environ; • Can be accessed through the functions also: geten v and putenv environment pointer environ: NULL HOME=/home/vtam\0 PATH=:/bin:/usr/bin\0 SHELL=/bn/sh\0 USER=vtam\0 LOGNAME=vtam\0 environment list environment strings 4 Environment of a UNIX Process: The main(…) Function in C • A C program starts execution with the main function: int main(int argc, char *argv);- argc is the number of command-line arguments; argv is an array of pointers to the arguments (array of strings) • When a C program is started by the kernel (by one of the exec functions, elaborated later), a special start-up function is called before the main function is called • In fact, as prepared by the compiler (e.g., gcc), the executable program file specifies this start-up function as the starting address for the program • The start-up function takes values supplied by the kernel (i.e., the command line arguments, environment variables), and sets up the execution environment before it calls the main function 5 Environment of a UNIX Process: Termination 1. Normal termination: – return from the main function – calling exit – calling _exit 2. Abnormal termination (elaborated later): – calling abort – terminated by a signal [to be discussed later] 6 Environment of a UNIX Process: The exit() Functions #include <stdlib.h> void exit(int status); int atexit(void (*func)(void)); /* to register an exit handler*/ #include <unistd.h> void _exit(int status); • The exit function is designed to perform a clean shutdown of the standard I/O library: the fclose function is called for all open streams (flush all buffers as well) • Both exit and _exit functions expect a single integer argument, called the exit status • The exit status can be examined by the user through the shell • A good practice: call exit(0) at the end of the main function • We can call the atexit() function to register the exit handler function so that the handler will be called by the exit() function, in a reverse order of the exit handler registration 7 The Logical Flow Diagram of exit() & _exit() Functions exit function main function functions user C start-up function...
View Full Document

Page1 / 45

2 - ELEC 2602 – Object Oriented & Systems...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online