TLPI - 6 Process


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

TLPI - 6 Process

  1. 1. TLPI - 6ProcessesShu-Yu Fu (
  2. 2. In This Chapter● the structure of a process● the attributes of a process
  3. 3. Processes and Programs● A process is an instance of an executing program.● A program is a file containing a range of information that describes how to construct a process at run time. ○ Binary format identification (COFF, PE2, ELF) ○ Machine-language instructions (text section) ○ Program entry-point address (ld-script, ld --verbose) ○ Data (data and rodata sections) ○ Symbol and relocation tables (symbol relocation) ○ Shared-library and dynamic-linking information ○ Other information
  4. 4. Process ID and ParentProcess ID#include <unistd.h>pid_t getpid(void);pid_t getppid(void);
  5. 5. Memory Layout of aProcess● The text segment● The initialized data segment (data)● The uninitialized data segment (bss)● The stack● The heap The top end of the heap is called the program break
  6. 6. Locations of program variables inprocess memory segments#include <stdio.h> static void#include <stdlib.h> doCalc (int val) { /* Allocated in frame for doCalc() */ printf ("The square of %d is %dn", val, square/* Uninitialized data segment */ (val));char globBuf[65536]; if (val < 1000) { int t; /* Allocated in frame for doCalc() *//* Initialized data segment */ t = val * val * val;int primes[] = {2, 3, 5, 7}; printf ("The cube of %d is %dn", val, t); }static int }square (int x) { /* Allocated in frame for square() */ int result; /* Allocated in frame for square() */ int result = x * x; main (int argc, char *argv[]) { /* Allocated in frame for main() */ return result; /* Return value passed via register) */ static int key = 993; /* Initialized data segment */} static char mbuf[64]; /* Uninitialized data segment */ char *p; /* Allocated in frame for main() */ p = malloc (8); /* Points to memory in heap segment */ doCalc (key); exit (EXIT_SUCCESS); }
  7. 7. Virtual MemoryManagement● A virtual memory scheme splits the memory used by each program into small, fixed-size units called pages.● At any one time, only some of the pages of a program need to be resident in physical memory page frames; these pages form the so-called resident set.
  8. 8. Virtual MemoryManagement (cont.)● Copies of the unused pages of a program are maintained in the swap area--a reserved area of disk space used to supplement the computers RAM--and loaded into physical memory only as required.● When a process reference a page that is not currently resident in physical memory, a page fault occurs.● The kernel maintains a page table for each process.
  9. 9. Page Table
  10. 10. Virtual MemoryManagement (cont.)● The page table describes the location of each page in the processs virtual address space.● If a process tries to access an address for which there is no corresponding page-table entry, it receives a SIGSEGV signal.● A processs range of valid virtual addresses can change over its lifetime. ○ stack grows, brk(), sbrk(), malloc() family, shmat(), shmdt(), mmap(), munmap()
  11. 11. Advantages● Processes are isolated from one another and from the kernel.● Where appropriate, two or more processes can share memory.● Page-table entries can be marked to indicate that the contents of the corresponding page are readable, writable, executable, or some combination of these protections.● Programmers dont need to be concerned with the physical layout of the program in RAM.● Because only a part of a program needs to reside in memory, the program loads and runs faster.
  12. 12. The Stack and StackFrames● A special-purpose register, the stack pointer, tracks the current top of the stack.● Sometimes, the term user stack is used to distinguish the stack we describe here from the kernel stack.● Each (user) stack frame contains the following information: ○ Function arguments and local variables ○ Call linkage information
  13. 13. Command-Line Arguments
  14. 14. Command-Line Arguments(cont.)● The fact that argv[0] contains the name used to invoke the program can be employed to perform a useful trick.● The command-line arguments of any process can be read via the Linux-specific /proc/PID/cmdline file.● The argv and environ arrays reside in a single continuous area of memory which has an upper limit on the total number of bytes that can be stored in this area.
  15. 15. Echoing Command-LineArguments#include <stdlib.h>int main (int argc, char * argv[]){ int j; for (j = 0; j < argc; j++) printf ("argv[%d] = %sn", j, argv[j]); return (EXIT_SUCCESS);}
  16. 16. Environment List
  17. 17. Environment List (cont.)● Each process has an associated array of strings called the environment list.● Each of these strings is a definition of the form name=value.● When a new process is created, it inherits a copy of its parents environment.● The environment list of any process can be examined via the Linux-specific /proc/PID/environ file.
  18. 18. Displaying the ProcessEnvironment#include <stdlib.h>extern char **environ;int main (int argc, char * argv[]){ char **ep; for (ep = environ; *ep; ep++) puts (*ep); return (EXIT_SUCCESS);}
  19. 19. Accessing the Environment From aProgram● #define _BSD_SOURCE● #include <stdlib.h>● int main(int argc, char *argv[], char *envp[]);● char * getenv(const char *name);● int putenv(char *string);● int setenv(const char *name, const char *value, int overwrite);● int unsetenv(const char *name);● int clearenv(void);
  20. 20. Accessing the Environment From aProgram (cont.)● In some circumstances, the use of setenv() and clearenv() can lead to memory leaks in a proram.● We noted above the setenv() allocates a memory buffer that is then made part of the environment.● When we call clearenv(), it doesnt free this buffer.
  21. 21. Performing a NonlocalGoto#include <setjmp.h>int setjmp(jmp_buf env);void longjmp(jmp_buf env, int val);
  22. 22. Demonstrate the Use ofsetjmp() and longjmp()#include <setjmp.h> $./longjmpstatic jmp_buf env;static void f2(void) { Calling f1() after initial setjmp() longjmp(env, 2); We jumped back from f1()}static void f1(int argc) { if (argc == 1) longjmp(env, 1); $./longjmp x f2();} Calling f1() after initial setjmp()int main(int argc, char *argv[]) { We jumped back from f2() switch (setjmp(env)) { case 0: printf("Calling f2() after initial setjmp()n"); f1(argc); break; case 1: printf("We jumped back from f1()n"); break; case 2: printf("We jumped back from f2()n"); break; } exit(EXIT_SUCCESS);}
  23. 23. Restrictions on The Use ofsetjmp()● A call to setjmp() may appear only in the following contexts: ○ if, switch, or iteration statement ○ A unary ! operator ○ comparison (==, !=, <, and so on) with integer ○ A free-standing function call
  24. 24. Abusing longjmp()1. Call a function x() that uses setjmp() to establish a jump target in the global variable env.2. Return from function x().3. Call a function y() that does a longjmp() using env.4. This is a serious error. We cant do a longjmp() into a function that has already returned.5. In a multithreaded program, a similar abuse is to call longjmp() in a different thread from that in which setjmp() was called.
  25. 25. Problems with Optimizing Compilers #include <stdio.h> int main(int argc, char *argv[]) #include <stdlib.h> { #include <setjmp.h> int nvar; static jmp_buf env; register int rvar; static void doJump(int nvar, int rvar, int vvar) volatile int vvar; { nvar = 111; printf("Inside doJump(): nvar=%d rvar=%d rvar = 222; vvar=%dn", nvar, rvar, vvar); vvar = 333; longjmp(env, 1); if (setjmp(env) == 0) { } nvar = 777; rvar = 888; vvar = 999; doJump(nvar, rvar, vvar); } else { printf("After longjmp(): nvar=%d rvar=%d$ cc -o setjmp_vars setjmp_vars.c vvar=%dn", nvar, rvar, vvar);$ ./setjmp_vars }Inside doJump(): nvar=777 rvar=888 vvar=999 exit(EXIT_SUCCESS);After longjmp(): nvar=777 rvar=888 vvar=999 }$ cc -O =o setjmp_vars setjmp_vars.c$ ./setjmp_varsInside doJump(): nvar=777 rvar=888 vvar=999After longjmp(): nvar=111 rvar=222 vvar=999
  26. 26. Summary● Each proess has q unique process ID and maintains a record of its parents process ID.● The virtual memory of a process is logically divided into a number of segments.● The stack consists of a series of frames.● The command-line arguments supplied when a program is invoked are made available via the argc and argv arguments to main().● Each process receives a copy of its parents environment list.● The setjmp() and longjmp() functions provide a way to perform nonlocal goto from one function to another. ○ Avoid setjmp() and longjmp() where possible