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

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Computer Architecture CSCE 350
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
System Files and Process Environment Password file Group file System identification Time Process environment.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
[Unix Programming] Process Basic Young-Ju, Han
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CSCE Systems Programming Lecture 05 - Processors, Memory CSCE 510 Jan 23, 2013.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Appendix A. Man Pages man whatis whatis(1) Typical Man Page Sections SectionContents User commands System calls C library functions.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Advanced Programming in the UNIX Environment Hop Lee.
System Calls 1.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
Topic 2d High-Level languages and Systems Software
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Minimal standard C program int main(void) { return 0 ; }
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lecture 3 Translation.
Dynamic Allocation in C
Chapter 7. Process Environment
Stack and Heap Memory Stack resident variables include:
Process Tables; Threads
Day 03 Introduction to C.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Day 03 Introduction to C.
Programmazione I a.a. 2017/2018.
Lecture 5: Process Creation
Unix Process Course code: 10CS62 Prepared by : Department of CSE.
Chapter 14 - Advanced C Topics
Process Tables; Threads
Memory Allocation CS 217.
Understanding Program Address Space
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
The Environment of Unix Process
CSCI 380: Operating Systems William Killian
Dynamic Memory – A Review
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

1 System Programming Chapter 7

2 Fun Stuff Touch-based Communication

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 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 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 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 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 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 #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 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 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 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 Memory Layout (Linux on x86) text heap stack command-line args and env variables initialized Data uninitialized Data (bss) low address high address

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 b7c /usr/bin/cc ab806 /bin/sh

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 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 ce61 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 a24 fig3.1.exe

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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)