SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
TLPI - 6
Processes
Shu-Yu Fu (shuyufu@gmail.com)
In This Chapter
● the structure of a process
● the attributes of a process
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
Process ID and Parent
Process ID
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
Memory Layout of a
Process
● 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
Locations of program variables in
process 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);
                                                             }
Virtual Memory
Management
● 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.
Virtual Memory
Management (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
  computer's 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.
Page Table
Virtual Memory
Management (cont.)
● The page table describes the location of
  each page in the process's 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 process's range of valid virtual addresses
  can change over its lifetime.
  ○ stack grows, brk(), sbrk(), malloc() family, shmat(),
    shmdt(), mmap(), munmap()
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 don't 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.
The Stack and Stack
Frames
● 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
Command-Line Arguments
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.
Echoing Command-Line
Arguments
#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);
}
Environment List
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 parent's environment.
● The environment list of any process can be
  examined via the Linux-specific
  /proc/PID/environ file.
Displaying the Process
Environment
#include <stdlib.h>

extern char **environ;

int main (int argc, char * argv[])
{
  char **ep;

    for (ep = environ; *ep; ep++)
      puts (*ep);

    return (EXIT_SUCCESS);
}
Accessing the Environment From a
Program
● #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);
Accessing the Environment From a
Program (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 doesn't free this
  buffer.
Performing a Nonlocal
Goto
#include <setjmp.h>
int setjmp(jmp_buf env);
void longjmp(jmp_buf env, int val);
Demonstrate the Use of
setjmp() and longjmp()
#include <setjmp.h>
                                                             $./longjmp
static 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);
}
Restrictions on The Use of
setjmp()
● 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
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 can't 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.
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_vars
Inside doJump(): nvar=777 rvar=888 vvar=999
After longjmp(): nvar=111 rvar=222 vvar=999
Summary
●   Each proess has q unique process ID and maintains a record of its
    parent's 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 parent's 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

Contenu connexe

Tendances

Code GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowCode GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowMarina Kolpakova
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionMarina Kolpakova
 
Code GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersCode GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersMarina Kolpakova
 
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler OptimizationsPragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler OptimizationsMarina Kolpakova
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about CYi-Hsiu Hsu
 
4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...PROIDEA
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROPSaumil Shah
 
Code GPU with CUDA - Memory Subsystem
Code GPU with CUDA - Memory SubsystemCode GPU with CUDA - Memory Subsystem
Code GPU with CUDA - Memory SubsystemMarina Kolpakova
 

Tendances (20)

Code GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flowCode GPU with CUDA - Optimizing memory and control flow
Code GPU with CUDA - Optimizing memory and control flow
 
Lecture6
Lecture6Lecture6
Lecture6
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 
Advance ROP Attacks
Advance ROP AttacksAdvance ROP Attacks
Advance ROP Attacks
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introduction
 
Code GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersCode GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limiters
 
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler OptimizationsPragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
 
Machine Trace Metrics
Machine Trace MetricsMachine Trace Metrics
Machine Trace Metrics
 
Unit 4
Unit 4Unit 4
Unit 4
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
04 sequentialbasics 1
04 sequentialbasics 104 sequentialbasics 1
04 sequentialbasics 1
 
ROP
ROPROP
ROP
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
Dafunctor
DafunctorDafunctor
Dafunctor
 
4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...4Developers 2018: The turbulent road to byte-addressable storage support at t...
4Developers 2018: The turbulent road to byte-addressable storage support at t...
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Code GPU with CUDA - SIMT
Code GPU with CUDA - SIMTCode GPU with CUDA - SIMT
Code GPU with CUDA - SIMT
 
First session quiz
First session quizFirst session quiz
First session quiz
 
An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
 
Code GPU with CUDA - Memory Subsystem
Code GPU with CUDA - Memory SubsystemCode GPU with CUDA - Memory Subsystem
Code GPU with CUDA - Memory Subsystem
 

En vedette

Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programsShu-Yu Fu
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsShu-Yu Fu
 
TLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosTLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosShu-Yu Fu
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5Shu-Yu Fu
 
程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1Shu-Yu Fu
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4Shu-Yu Fu
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體Shu-Yu Fu
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8Shu-Yu Fu
 

En vedette (8)

Tlpi chapter 38 writing secure privileged programs
Tlpi   chapter 38 writing secure privileged programsTlpi   chapter 38 writing secure privileged programs
Tlpi chapter 38 writing secure privileged programs
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
 
TLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and FifosTLPI - Chapter 44 Pipe and Fifos
TLPI - Chapter 44 Pipe and Fifos
 
程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5程式設計師的自我修養 Chapter 5
程式設計師的自我修養 Chapter 5
 
程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1程式設計師的自我修養 Chapter 1
程式設計師的自我修養 Chapter 1
 
程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4程式設計師的自我修養 Chapter 3.4
程式設計師的自我修養 Chapter 3.4
 
程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體程式設計師的自我修養 Chapter 10 記憶體
程式設計師的自我修養 Chapter 10 記憶體
 
程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8程式設計師的自我修養 Chapter 8
程式設計師的自我修養 Chapter 8
 

Similaire à TLPI - 6 Process

ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfabdulrahamanbags
 
please help me with this and explain in details also in the first qu.pdf
please help me with this and explain in details also in the first qu.pdfplease help me with this and explain in details also in the first qu.pdf
please help me with this and explain in details also in the first qu.pdfnewfaransportsfitnes
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_mapslcplcp1
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaPatrick Allaert
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docxtarifarmarie
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 
CUDA Deep Dive
CUDA Deep DiveCUDA Deep Dive
CUDA Deep Divekrasul
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisFastly
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 

Similaire à TLPI - 6 Process (20)

ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
 
please help me with this and explain in details also in the first qu.pdf
please help me with this and explain in details also in the first qu.pdfplease help me with this and explain in details also in the first qu.pdf
please help me with this and explain in details also in the first qu.pdf
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
CUDA Deep Dive
CUDA Deep DiveCUDA Deep Dive
CUDA Deep Dive
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
3
33
3
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 

TLPI - 6 Process

  • 1. TLPI - 6 Processes Shu-Yu Fu (shuyufu@gmail.com)
  • 2. In This Chapter ● the structure of a process ● the attributes of a process
  • 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. Process ID and Parent Process ID #include <unistd.h> pid_t getpid(void); pid_t getppid(void);
  • 5. Memory Layout of a Process ● 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. Locations of program variables in process 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. Virtual Memory Management ● 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. Virtual Memory Management (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 computer's 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.
  • 10. Virtual Memory Management (cont.) ● The page table describes the location of each page in the process's 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 process's range of valid virtual addresses can change over its lifetime. ○ stack grows, brk(), sbrk(), malloc() family, shmat(), shmdt(), mmap(), munmap()
  • 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 don't 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. The Stack and Stack Frames ● 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
  • 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. Echoing Command-Line Arguments #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); }
  • 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 parent's environment. ● The environment list of any process can be examined via the Linux-specific /proc/PID/environ file.
  • 18. Displaying the Process Environment #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. Accessing the Environment From a Program ● #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. Accessing the Environment From a Program (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 doesn't free this buffer.
  • 21. Performing a Nonlocal Goto #include <setjmp.h> int setjmp(jmp_buf env); void longjmp(jmp_buf env, int val);
  • 22. Demonstrate the Use of setjmp() and longjmp() #include <setjmp.h> $./longjmp static 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. Restrictions on The Use of setjmp() ● 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. 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 can't 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. 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_vars Inside doJump(): nvar=777 rvar=888 vvar=999 After longjmp(): nvar=111 rvar=222 vvar=999
  • 26. Summary ● Each proess has q unique process ID and maintains a record of its parent's 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 parent's 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