+ CMPS 105 Systems Programming Prof. Darrell Long E2.371 darrell@ucsc.edu
+ Chapter 7: The Environment of a UNIX process
+ Introduction
+ The main() fuction n int main(int argc, char* argv[]); n argc = number of command-line arguments n argv = array of pointers to the (string) arguments n main() is the first thing called in the program n A special start-up routine is called first n This is what sets up the parameters to main
+ Process Termination n Five ways to terminate a process n Normal termination n Return from main() n Call exit() n call _exit() n Abnormal termination n Call abort() n Terminate by a signal
+ exit() and _exit() n exit() Performs a clean shutdown of the standard I/O library n void exit(int status); n _exit() Same as exit except doesn t call atexit() n void _exit(int status); n No cleanup, returns to kernel immediately n Exit status undefined if not specified
+
+ atexit() n atexit() Calls funct without args when program terminates n int atexit(void (*func)(void)); n func is a pointer to a function that takes no parameters n ANSI C: A process can register up to 32 handler functions to execute when the program exits
+
+ Command-line arguments n Programs can pass command-line parameters
+ Environment List n Each program is passed an environment list n Extern char** environ; n Each environment string consists of name=value n Most names are uppercase n Usually ignored, but can be useful n Why? n Historically environment was input to main n int main(int argc, char* argv[], char* envp[]);
+ Environment structure
+ Memory Layout of a C Program n Text segment n The machine instructions of the program n Usually sharable and read-only n Data segment (initialized data) n Global variables that are initialized in the program n BSS (uninitialized data) n Global variables that are not initialized in the program n Initialized to zero or null pointers
+ C program layout cont. n Stack (automatic variables) n Function return information n Local variables n Heap n Dynamic memory allocation
+ Shared Libraries n Single shared copy of common library routines n Instead of each one being copied in each program n Big space savings n 805,175 vs. 1,696 for hello world
+ Memory Allocation n malloc() Allocates the specified number of bytes n void* malloc(size_t size); n Uninitialized n calloc() Allocates space for the specified number of objects n void* calloc(size_t nobj, size_t size); n Initialized to all 0 s n realloc() Changes the size of a previously allocated area n void* realloc(void* ptr, size_t newsize); n May move to a new location (and copy old contents) n New area is uninitialized
+ Memory Allocation n Memory is over-allocated n Additional space at the end of segment for record keeping n Writing past the end or before the start can overwrite the record
+ Freeing memory n free() Frees allocated space n void* free(void* ptr); n Mistakes with free n Call free twice on same pointer n Call free on pointer not obtained from alloc function n Not calling it causes leaks
+ alloca n Allocates memory from stack n Doesn t have to be freed n Doesn t live past the return from the calling function
+ Environment variables n Used by applications only n Not the kernel n Name= value n Common variables: HOME, USER, PRINTER, etc n getenv() gets environment variables n char* getenv(const char* name); n Returns null if not found
+
+ Other environment functions n putenv() creates (or overwrites) environment variable n int putenv(const char* str); n setenv()- same as putenv except does nothing if rewrite = 0 and old value exists n int setenv(const char* name, const char* value, int rewrite); n unsetenv() Clears an environment variable n int unsetenv(const char* name);
+
+ setjmp() and longjmp() n Allow goto s from lower in a call stack to higher in a call stack n setjmp() sets up location to jump to n int setjmp(jmp_buf env) n longjmp() jumps to location set by setjmp() n int longjmp(jmp_buf env, int val); n Parameter contains the environment of the function that will be jumped to n Spaghetti is good (delicious), but not for coding
+
+
+
+ getrlimit() and setrlimit() n getrlimit() get resource limit n int getrlimit(int resource, struct rlimit* rlptr); n setrlimit() set resource limit n int setrlimit(int resource, const struct rlimit* rlptr); struct rlimit{ rlim_t rlim_cur; /* Soft limit */ rlim_t rlim_max; /* hard limit */ };
+