Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 System Programming Chapter 7. 2 Fun Stuff Touch-based Communication.

Similar presentations


Presentation on theme: "1 System Programming Chapter 7. 2 Fun Stuff Touch-based Communication."— Presentation transcript:

1 1 System Programming Chapter 7

2 2 Fun Stuff Touch-based Communication

3 3 The Environment of a Unix Process How a process is executed and terminates? How the command line arguments are passed to the new process? What is the typical memory layout? How the process can use environment variables? Related functions and resource limits.

4 4 main Function int main(int argc, char *argv[]) The starting point of a C program. Programs written in different languages have a different name. Program is invoked through exec call. A special start-up routine is called to set things up first before call main() –Set up by the link editor (invoked by the compiler) Disk Memory Program Counter

5 5 Process Termination Five ways to terminate: –Normal termination Return from main(). –exit(main(argc, argv)); Call exit() // “clean” shutdown Call _exit() or _Exit() // immediate shutdown –Abnormal termination (Chapter 10) Call abort() Be terminated by a signal.

6 6 How a C program is started and terminated. kernel C start-up routine main function user functions exit function exit handler exit handler … standard I/O cleanup exec user process _exit call return call return exit (does not return) exit (does not return) exit (does not return) call return call return call return _exit

7 7 Process Termination #include void exit(int status); –ANSI C –Perform cleanup processing Close and flush I/O streams (fclose) #include void _exit(int status); –POSIX.1 Undefined exit status: –Exit/return without status. #include main() { printf(“hello world\n”); }

8 8 Process Termination #include int atexit(void (*func)(void)); –Up to 32 functions called by exit – ANSI C, supported by SVR4&4.3+BSD –The same exit functions can be registered for several times. –Exit functions will be called in reverse order of their registration. Program 7.3 – Page 184 –Exit handlers

9 9 #include "apue.h" static voidmy_exit1(void); static voidmy_exit2(void); int main(void) { if (atexit(my_exit2) != 0) err_sys("can't register my_exit2"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); printf("main is done\n"); return(0); } static void my_exit1(void) { printf("first exit handler\n"); } static void my_exit2(void) { printf("second exit handler\n"); } $./a.out main is done ??

10 10 Command-Line Arguments #include "apue.h" int main(int argc, char *argv[]) { inti; for (i = 0; i < argc; i++)/* echo all command-line args */ printf("argv[%d]: %s\n", i, argv[i]); exit(0); } $./echoarg arg1 TEST foo argv[0]:./echoarg argv[1]: arg1 argv[2]: TEST argv[3]: foo argv[4] (argv[argc]) is a null pointer (guaranteed by ISO C and POSIX.1)

11 11 Environment Variables Each program is passed an environment list –Consists of name=value strings –Access through getenv and putenv functions (more later) extern char **environ; NULL HOME=/home/stevens\0 PATH=:/bin:/usr/bin\0 SHELL=/bin/sh\0 USER=stevens\0 LOGNAME=stevens\0 environment list environment strings

12 12 Memory Layout Pieces of a process (C program) –Text Segment Read-only usually, sharable –Initialized Data Segment int maxcount = 10; –Uninitialized Data Segment – bss (Block Started by Symbol) Initialized to 0 by exec long sum[1000]; –Stack – return addr, automatic var, etc. –Heap – dynamic memory allocation int maxcount = 10; long sum[1000]; long* sum2; void func() { int abc; sum2 = malloc(sizeof(long) * 1000); return; } Read from program file by exec

13 13 Memory Layout (Linux on x86) text heap stack command-line args and env variables initialized Data uninitialized Data (bss) low address high address

14 14 size command reports sizes of text/data/bss segments linux1:~/sys_prog_06/apue.2e> size /usr/bin/cc /bin/sh text data bss dec hex filename 90444 1664 944 93052 16b7c /usr/bin/cc 660278 22856 19336 702470 ab806 /bin/sh

15 15 Shared Library vs. Static Library A program usually calls standard C/C++ library, POSIX functions, etc. The binary codes of the pre-compiled function call have to be linked with the program. Static library: –the library and program are complied into one file. –In Linux, static library ends with.a. Problems: –When the library is updated, the program has to be re-linked. –The file size will be large.

16 16 linux1:~/sys_prog_06/test> gcc -static -I./include - L./lib/libapue.a -o fig3.1b.exe fig3.1.o error.o linux1:~/sys_prog_06/test> size fig3.1b.exe text data bss dec hex filename 437981 3336 4732 446049 6ce61 fig3.1b.exe linux1:~/sys_prog_06/test> gcc -I./include - L./lib/libapue.a -o fig3.1.exe fig3.1.o error.o linux1:~/sys_prog_06/test> size fig3.1.exe text data bss dec hex filename 2288 296 12 2596 a24 fig3.1.exe

17 17 Shared Library Why a shared library? –Remove common library routines from executable files. –Have an easy way for upgrading Problems –More overheads: dynamic linking Gcc: –Searches for shared libraries first. –Static linking can be forced with the -static option to gcc to avoid the use of shared libraries. Supported by many Unix systems

18 18 Memory Allocation Three ANSI C Functions: –malloc() – allocate a specified number of bytes of memory. Initial values are indeterminate. –calloc() – allocate a specified number of objects of a specified size. The space is initialized to all 0 bits. –realloc() – change the size of a previously allocated area. The initial value of increased space is indeterminate. –free() – free memory allocation

19 19 Memory Allocation #include void *malloc(size_t size); void *calloc(size_t nobj, size_t size); void *realloc(void *ptr, size_t newsize); –Suitable alignments for any data object –Generic void * pointer –free(void *ptr) to release space to a pool. –Forget to free? Leaking problem –Dangling pointer #include main() { char *ptr; char *ptr2; ptr = malloc(100); ptr = calloc(sizeof(int), 100); // memory leak ptr2 = ptr; ptr = realloc(ptr, sizeof(int)*200); // potential dangling pointer for ptr2 free(ptr); }

20 20 Memory Allocation realloc() could trigger moving of data  avoid pointers to that area! –prt == NULL  malloc() sbrk() is used to expand or contract the heap of a process – a malloc pool Record-keeping info is also reserved for memory allocation – do not move data inside. alloca() allocates space from the stack frame of the current function! –No needs for free with potential portability problems text heap stack command-line args and env variables initialized Data uninitialized Data (bss)

21 21 Environment Variables Name=value –Interpretation is up to applications. Setup automatically or manually E.g., HOME, USER, MAILPATH, etc. setenv FONTPATH $X11R6HOME/lib/X11/fonts\:$OPENWINHOME/lib/fonts #include char *getenv(const char *name); return a pointer to the value of a name=value string

22 22 Environment Variables #include int putenv(char *name-value); –Remove old definitions if they exist. –Place the address of string in environment list int setenv(const char *name, const char *value, int rewrite); –rewrite = 0  no change on existing values. –Create a new string to hold the new value –Rewrite > 0  overwriting existing values. int unsetenv(const char *name); –Remove any def of the name –No error msg if no such def exists.

23 23 Environment Variables Adding/Deleting/Modifying of Existing Strings –Modifying an existing name The size of a new value <= the size of the existing value  overwriting Otherwise; malloc() & redirect the ptr –Adding a new name Expand the size of the environment list But cannot expand upward or downward The first time we add a new name  malloc of pointer-list’s room in heap & update envron pointer Otherwise (already in the heap); copying. realloc() if needed. text heap stack command-line args and env variables initialized Data uninitialized Data (bss)

24 24 setjmp and longjmp Cannot goto a label in another function in C? –escape from a deeply nested function call –Program 7.9 – Program Skeleton stack frame for main stack frame for do_line stack frame for cmd_add Note: Automatic variables are allocated within the stack frames! line cmd token

25 25 main(void) { char line[MAXLINE]; while (fgets(line, MAXLINE, stdin) != NULL) do_line(line); exit(0); } char *tok_ptr; /* global pointer for get_token() */ void do_line(char *ptr) /* process one line of input */ { int cmd; tok_ptr = ptr; while ((cmd = get_token()) > 0) { switch (cmd) { /* one case for each command */ case TOK_ADD: cmd_add(); break; } void cmd_add(void) { int token; token = get_token(); /* rest of processing for this command */ /* nonfatal error -> invalid number -> go back to reading the next line */ } int get_token(void) { /* fetch next token from line pointed to by tok_ptr */ }

26 26 setjmp and longjmp Consider this as a non-local goto #include int setjmp(jmp_buf env); –Return 0 if called directly; otherwise, it could return a value val from longjmp(). –env tends to be a global variable. int longjmp(jmp_buf env, int val); –longjmp() unwinds the stack and affect some variables. Program 7.11 –setjmp and longjmp

27 27 main(void) { char line[MAXLINE]; if (setjmp(jmpbuffer) != 0) printf(“error”); while (fgets(line, MAXLINE, stdin) != NULL) do_line(line); exit(0); } char *tok_ptr; /* global pointer for get_token() */ void do_line(char *ptr) /* process one line of input */ { int cmd; tok_ptr = ptr; while ((cmd = get_token()) > 0) { switch (cmd) { /* one case for each command */ ….. } void cmd_add(void) { int token; token = get_token(); /* rest of processing for this command */ /* nonfatal error -> invalid number -> go back to reading the next line */ if (token < 0) { longjmp(jumpbuffer, 1); } int get_token(void) { /* fetch next token from line pointed to by tok_ptr */ }

28 28 setjmp and longjmp What are the states of the variables after longjmp? –[Automatic, Register, and Volatile Variables]: line –Values corresponding to calling setjmp (rollback) or longjmp (no rollback)? Answer: it depends … –Global and static variables are left alone when longjmp is executed –Normally no roll back on automatic and register variables –Sometimes roll back can happen due to variables updated in registers but not written back to memory Compiler optimization: register variables could be in memory –Portability Issues!

29 29 Types of variables Automatic –Default for function/block variables Volatile –References to the variable have side effects, or that the variable may change in ways the compiler cannot determine. Optimization will not eliminate any action involving the volatile variable. Changes to the value of the variable are then stored immediately, and uses of the variable will always cause it to be reloaded from memory. Register –register provides a hint to the compiler that you think a variable will be frequently used

30 30 setjmp and longjmp Program 7.5 – Page 179 –Effects of longjmp –Variables stored in memory have their values unchanged – no optimization… Potential Problems with Automatic Variables – Program 7.6 (Page 180) –Never be referenced after their stack frames are released.

31 31 Different behavior with setjmp int main(void) { int autoval; register int regival; volatile int volaval; static int statval; globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5; if (setjmp(jmpbuffer) != 0) { printf("after longjmp:\n"); printf("globval = %d, autoval = %d, regival = %d," " volaval = %d, statval = %d\n", globval, autoval, regival, volaval, statval); exit(0); } globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99; f1(autoval, regival, volaval, statval); /* never returns */ exit(0); } static void f1(int i, int j, int k, int l) { printf("in f1():\n"); printf("globval = %d, autoval = %d, regival = %d," " volaval = %d, statval = %d\n", globval, i, j, k, l); f2(); } static void f2(void) { longjmp(jmpbuffer, 1); }

32 32 linux1:~/sys_prog_06/test> gcc -O - I./include -c fig7.13.c linux1:~/sys_prog_06/test> gcc -O - I./include -L./lib/libapue.a -o fig7.13.exe fig7.13.o error.o linux1:~/sys_prog_06/test> linux1:~/sys_prog_06/test> fig7.13.exe in f1(): globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99 after longjmp: globval = 95, autoval = 2, regival = 3, volaval = 98, statval = 99 linux1:~/sys_prog_06/test> gcc - I./include -c fig7.13.c linux1:~/sys_prog_06/test> gcc - I./include -L./lib/libapue.a -o fig7.13.exe fig7.13.o error.o linux1:~/sys_prog_06/test> linux1:~/sys_prog_06/test> fig7.13.exe in f1(): globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99 after longjmp: globval = 95, autoval = 96, regival = 97, volaval = 98, statval = 99

33 33 Incorrect usage of automatic variable FILE * open_data(void) { FILE *fp; char databuf[BUFSIZ]; /* setvbuf makes this the stdio buffer */ if ((fp = fopen(DATAFILE, "r")) == NULL) return(NULL); if (setvbuf(fp, databuf, _IOLBF, BUFSIZ) != 0) /* change buffering for stdio */ return(NULL); return(fp); /* error */ }

34 34 getrlimit and setrlimit #include int getrlimit(int resource, struct rlimit *rlptr); int setrlimit(int resource, const struct rlimit *rlptr); –Not POSIX.1, but supported by XSI –Rules: Soft limit <= hard limit, the lowering of hard limits is irreversible. Only a superuser can raise a hard limit. struct rlimit { rlim_t rlim_cur; /* soft limit */ rlim_t rlim_max; /* hard limit */ }

35 35 getrlimit and setrlimit Resources (SVR4&4.3BSD) –RLIMIT_CORE (both), RLIMIT_CPU (both, SIGXCPU), RLIMIT_DATA (both), RLIMIT_FSIZE (both, SIGXFSZ), RLIMIT_MEMLOCK (4.3+BSD, no implementation), RLIMIT_NOFILE (SVR4, _SC_OPEN_MAX), RLIMIT_NPROC (4.3+BSD, _SC_CHILD_MAX), RLIMIT_OFILE (4.3+BSD=RLIMIT_NOFILE), RLIMIT_RSS (4.3+BSD, max memory resident size), RLIMIT_STACK (both), RLIMIT_VMEM (SVR4)

36 36 getrlimit and setrlimit Resource Limits  inheritance by processes –Built-in commands in shells umask, chdir, limit (C shall), ulimit –H and –S (Bourne shell and KornShell) Program 7.7 – Page 183 –Resource limits #define RLIM_NLIMITS 7 –doit(RLIMIT_CORE) = pr_limits(“RLIMIT_CORE”, RLIMIT_CORE) #define doit(name) pr_limits(#name, name)


Download ppt "1 System Programming Chapter 7. 2 Fun Stuff Touch-based Communication."

Similar presentations


Ads by Google