Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.

Similar presentations


Presentation on theme: "Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003."— Presentation transcript:

1 Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.

2 2 POSIX Standard POSIX: Portable Operating System Interface (pronounced pahz-icks) Standardized the AT&T System V and the BSD flavors of UNIX Consists of a joint document first started in 1988 and finally approved in 2001 by both IEEE and the Open Group –The standardization effort was hurried up in 1998 by the threat of world domination of computer operating systems by Microsoft Windows ISO/IEC* approved POSIX in 2002 The standard is referred to as –The Single UNIX Specification, Version 3 (or) –IEEE Std. 1003.1-2001, POSIX The POSIX standard also incorporates the international standard for ISO/ANSI C A POSIX-compliant implementation must support the POSIX base standard –Examples: Solaris 9, Redhat Linux 8, and MAC OS 10.2 * ISO/IEC : International Standards Organization / International Electrotechnical Commission

3 2.1 From Program to Process

4 4 From Program to Process Program – A prepared sequence of instructions to accomplish a defined task –A text editor is used to enter the human readable version of a program into a source code file –A compiler is used to convert the source code into a computer readable version that is stored in an object code file –A linker is used to combine the functions stored in one or more object code files or libraries into a single executable file –A loader is used to copy the contents of an executable file into a program image in main memory Process – An instance of a program that is running (or scheduled to run) in a computer Thread – A specific flow of control in a process –It represents a thread of execution within a process –Each thread has its own execution stack program counter register set process state (if using kernel threads)

5 2.3 Layout of a Program Image

6 6 Program Image in RAM

7 7 Portable Executable (PE) File Format DOS Header (64 bytes) MS-DOS Stub (57 bytes) PE Signature (4 bytes) File Header (20 bytes) Optional Header (224 bytes) Section Table Symbol Table String Table.text Section.data Section.rdata Section.idata Section (Import Table).edata Section (Export Table) 0 n

8 8 helloworld.exe (Cygwin) (1 of 2) ADDRESS DESCRIPTION 0 +-------------------------------------------------------------+ DOS Header [64 bytes] 63 +-------------------------------------------------------------+ 64 +-------------------------------------------------------------+ MS-DOS Stub [57 bytes] 120 +-------------------------------------------------------------+ 121 +-------------------------------------------------------------+ (Contents not known) [7 bytes] 127 +-------------------------------------------------------------+ 128 +-------------------------------------------------------------+ PE Signature [4 bytes] 131 +-------------------------------------------------------------+ 132 +-------------------------------------------------------------+ File Header [20 bytes] 151 +-------------------------------------------------------------+ 152 +-------------------------------------------------------------+ Optional Header [224 bytes] 375 +-------------------------------------------------------------+ 376 +-------------------------------------------------------------+ Section Table [200 bytes] 575 +-------------------------------------------------------------+ 576 +-------------------------------------------------------------+ (** Zero-filled region **) [448 bytes] 1023 +-------------------------------------------------------------+ 1024 +-------------------------------------------------------------+.text section [176128 bytes] 177151 +-------------------------------------------------------------+ (Source: J. J. Tevis, findssv Presentation, August 2005)

9 9 helloworld.exe (Cygwin) (2 of 2) ADDRESS DESCRIPTION 177152 +-------------------------------------------------------------+.data section [4608 bytes] 181759 +-------------------------------------------------------------+ 181760 +-------------------------------------------------------------+.rdata section [35840 bytes] 217599 +-------------------------------------------------------------+ 217600 +-------------------------------------------------------------+.idata section [1183 bytes] 218782 +-------------------------------------------------------------+ 218783 +-------------------------------------------------------------+ More of Import Table (.idata section) [241 bytes] 219023 +-------------------------------------------------------------+ 219024 +-------------------------------------------------------------+ (** Zero-filled region **) [112 bytes] 219135 +-------------------------------------------------------------+ (Source: J. J. Tevis, findssv Presentation, August 2005)

10 10 pdf995s.exe (1 of 2) ADDRESS DESCRIPTION 0 +-------------------------------------------------------------+ DOS Header [64 bytes] 63 +-------------------------------------------------------------+ 64 +-------------------------------------------------------------+ MS-DOS Stub [57 bytes] 120 +-------------------------------------------------------------+ 121 +-------------------------------------------------------------+ (Contents not known) [79 bytes] 199 +-------------------------------------------------------------+ 200 +-------------------------------------------------------------+ PE Signature [4 bytes] 203 +-------------------------------------------------------------+ 204 +-------------------------------------------------------------+ File Header [20 bytes] 223 +-------------------------------------------------------------+ 224 +-------------------------------------------------------------+ Optional Header [224 bytes] 447 +-------------------------------------------------------------+ 448 +-------------------------------------------------------------+ Section Table [200 bytes] 647 +-------------------------------------------------------------+ 648 +-------------------------------------------------------------+ (** Zero-filled region **) [376 bytes] 1023 +-------------------------------------------------------------+ 1024 +-------------------------------------------------------------+.text section [22016 bytes] 23039 +-------------------------------------------------------------+ (Source: J. J. Tevis, findssv Presentation, August 2005)

11 11 pdf995s.exe (2 of 2) ADDRESS DESCRIPTION 23040 +-------------------------------------------------------------+.rdata section [3072 bytes] 26111 +-------------------------------------------------------------+ 23040 +-----------------------------------------------------+ (No additional details) [492 bytes] 23531 +-----------------------------------------------------+ 23532 +-----------------------------------------------------+ Import Table (.idata section) [2303 bytes] 25834 +-----------------------------------------------------+ 25835 +-----------------------------------------------------+ (No additional details) [181 bytes] 26015 +-----------------------------------------------------+ 26016 +-----------------------------------------------------+ Export Table (.edata section) [50 bytes] 26065 +-----------------------------------------------------+ 26066 +-----------------------------------------------------+ (No additional details) [46 bytes] 26111 +-----------------------------------------------------+ 26112 +-------------------------------------------------------------+.data section [3584 bytes] 29695 +-------------------------------------------------------------+ 29696 +-------------------------------------------------------------+.rsrc section [1536 bytes] 31231 +-------------------------------------------------------------+ 31232 +-------------------------------------------------------------+ _winzip_ section [1329152 bytes] 1360383 +-------------------------------------------------------------+ (Source: J. J. Tevis, findssv Presentation, August 2005)

12 2.5 Function Return Values

13 13 Function Return Values Traditional UNIX functions usually return –1 (or sometimes NULL ) to indicate that an error has occurred –They also set the global variable errno to a value that indicates the specific error –A program should always check the return value of a function New POSIX functions do not use errno ; instead, the function returns the error number directly The perror() function outputs to standard error a message corresponding to the current value of errno –void perror(const char *s); // #include –If the value of s is not NULL, perror outputs the character string pointed to by s and follows it with a colon, a space, an error message, and a newline character The strerror() function returns a pointer to the system error message corresponding to the error code passed to it –char *strerror(int errorNumber); // #include –If successful, strerror() returns a pointer to the error string; no values are reserved for failure

14 14 Example Use of perror() int fileDescriptor; int status; // Robbins approach if (close(fileDescriptor) == -1) perror("Failed to close the file"); // Preferred approach status = close(fileDescriptor); if (status == -1) perror("Failed to close the file"); int close(int fildes); #include Preprocessor DirectivesFunction Prototype Source Code Example Failed to close the file: Invalid file descriptor Program Output

15 15 Example Use of strerror() int fileDescriptor; int status; status = close(fileDescriptor); if (status == -1) fprintf(stderr, "Error message: %s", strerror(errno)); int close(int fildes); #include Preprocessor DirectivesFunction Prototype Source Code Example Error message: Invalid file descriptor Program Output

16 2.10 Process Environment

17 17 Process Environment Environment list: an array of pointers to character strings of the form "name=value" –name specifies a specific environment variable –value specifies a character string value associated with the environment variable –The last entry of the array is NULL The external variable environ points to the process environment list when a process begins executing –extern char **environ; The getenv() function determines whether a specific variable has a value in the process environment –char *getenv(const char *name); // #include –If the variable has a value, getenv() returns a pointer to the character string containing the value; otherwise, it returns NULL –Example use: char *pathPtr; pathPtr = (char *) getenv("PATH"); if (pathPtr == NULL) printf("No path environment variable was found\n"); else printf("Contents of Path: %s\n", pathPtr);

18 18 Example Use of environ #include extern char **environ; int main(void) { int i; for (i = 0; environ[i] != NULL; i++) printf("%s\n", environ[i]); return 0; } // End main

19 19 Example contents of environ array USER=jjt107 LOGNAME=jjt107 HOME=/home/jjt107 PATH=/opt/local/bin:/usr/openwin/bin:/usr/bin:/opt/local/prog/bin:. SHELL=/bin/csh TZ=US/Central SSH_CLIENT=207.144.241.241 46207 22 SSH_TTY=/dev/pts/4 TERM=vt100 SSH_AUTH_SOCK=/tmp/ssh-KeY22175/agent.22175 PWD=/home/jjt107/Files/CS410 OPENWINHOME=/usr/openwin MANPATH=/usr/man:/opt/local/man:/opt/local/common/man EDITOR=/usr/ucb/vi MOST_HELP=/opt/local/common/lib/most.doc XAPPLRESDIR=/opt/local/common/lib/app-defaults:/usr/openwin/lib/app-defaults LD_LIBRARY_PATH=/usr/local/netlib/lib LPDEST= LM_LICENSE_FILE=/opt/SUNWspro/license_dir/sunpro.lic,sp A_TERMCAP=/usr/local/uxb1/lib/acucobol-85/a_termcap A_CONFIG=/usr/local/uxb1/lib/acucobol-85/cblconfig

20 20 Example Use of getenv() int main(void) { char *variablePtr; variablePtr = (char *) getenv("PATH"); if (variablePtr == NULL) printf("No path environment variable was found\n"); else printf("Contents of Path: %s\n", variablePtr); return 0; } // End main

21 2.11 Terminating a Process

22 22 Process Termination When a process terminates, timers and signals are canceled, virtual memory resources are released, and opened files are closed The operating system then notifies the parent of the process in response to a wait() function call In UNIX, a process does not completely release its resources after termination until the parent waits for it –If its parent is not waiting when the process terminates, the process becomes a zombie –In UNIX, the init process adopts orphaned processes and periodically waits for children processes so that they will terminate normally Normal termination occurs under the following conditions –Explicit return from the main function –Implicit return from the main function (the function ends) –Explicit call to the exit() function

23 23 The exit() and atexit() Functions void exit(int status); // #include int atexit(void (*func)(void)); The exit() function calls user-defined exit handlers that were registered by the atexit() function in the reverse order of registration After calling the user-defined handlers, the exit() function flushes any open streams that have unwritten buffered data, closes all open streams, and terminates process control Using the return statement from the main function has the same effect as calling the exit() function with the corresponding status value –An implicit end of the main function has the same effect as calling exit(0) The atexit() function installs a user-defined exit handler and takes a single parameter: the name of the function to be executed as a handler Multiple calls to atexit() are used to install more exit handlers If successful, atexit() returns 0; otherwise, it returns a nonzero value

24 24 Example use of exit() and atexit() functions #include // Function prototypes void showDateAndTime(void); // ************************************************************* int main(void) { int answer; int value; int status; status = atexit(showDateAndTime); if (status != 0) { fprintf(stderr, "Error: Failed to install exit handler\n"); exit(1); } // End if srand(time(NULL)); // Set seed value for random number generator if ((rand() % 3) == 0) // Results in true condition about 1/3 of the time { answer = value / 0; // Causes a runtime abort to occur } // End if else fprintf(stderr, "Normal process termination\n"); return 0; } // End main (More on next slide)

25 25 Example exit handler // ********************************************************* void showDateAndTime(void) { time_t currentTime; currentTime = time(NULL); printf("Current date and time: %s\n", ctime(&currentTime)); } // End showDateAndTime

26 26 Example Program Run uxb3% a.out Arithmetic Exception uxb3% a.out Normal process termination Current date and time: Tue Feb 20 11:32:59 2007 uxb3% a.out Arithmetic Exception


Download ppt "Chapter 2 Programs, Processes, and Threads Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003."

Similar presentations


Ads by Google