Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

7


Process Environment


7.1 Introduction


Beforelooking at the process control primitives in the next chapter, we need to examine
the environment of a single process. In this chapter,we’ll see how themainfunction is
called when the program is executed, how command-line arguments arepassed to the
new program, what the typical memory layout looks like, how to allocate additional
memory,how the process can use environment variables, and various ways for the
process to terminate. Additionally,we’ll look at thelongjmpandsetjmpfunctions
and their interaction with the stack. We finish the chapter by examining the resource
limits of a process.

7.2 mainFunction


ACprogram starts execution with a function calledmain.The prototype for themain
function is
int main(intargc,char *argv[]);
whereargcis the number of command-line arguments, andargvis an array of pointers
to the arguments. Wedescribe these arguments in Section 7.4.
When a C program is executed by the kernel—byone of theexecfunctions, which
we describe in Section 8.10—a special start-up routine is called beforethe main
function is called. The executable program file specifies this routine as the starting
address for the program; this is set up by the link editor when it is invoked by the C
compiler.This start-up routine takes values from the kernel—the command-line
arguments and the environment — and sets things up so that themainfunction is called
as shown earlier.

197
Free download pdf