Linux For Embedded Systems
ForArabs
Ahmed ElArabawy
Course 102:
UnderstandingLinux
Lecture 18:
Process Life-Cycle
PROCESS CREATION
Fork-Exec Procedure
• Creating a new process happens in two steps
• Fork:
In this step the new process (child process) is created and inherits its
features from its parent
• Execute:
In this step the new process (child process) separates from its parent
and move on with its functionality
Fork Execute
Fork
• The Job of “Fork” in is to create a new process (child process)
and prepare it with what it needs to be able to run….
• This includes,
• Assign it a new pid
• Creates associated Structures in the Kernel
• Sets the ppid, pgid, sid, and any other attributes
• Add it to the Kernel scheduler queue to give it time slots to run
• Copy parent memory space (all memory associated with the
parent process) to the child process memory space
• Copy Other parent process resources,
• Normal file handlers
• Socket handlers
• Shared memory handlers
• ….
Exec
• The job of “Exec” is to send the child process on its own to do
its assigned job
• This means the child process will be loaded with,
• New memory space instead of its parent
• Will lose access to parent process resources
• A pointer to the new program to run
• This command separates the child process from its parent, and
the child process runs now on its own, using its own memory
address space, and running its own executable and
creating/reserving its own system resources
Process
Address Space
Process Creation:
Parent Process
Process
Address Space
Child Process
Fork
Process Creation:
Fork Procedure
Process
Address Space
Child Process
Fork
COPY
Copied Address Space
Process Creation:
Copying Address Space
Process Creation:
Exec Procedure
Process
Address Space
Child Process
Exec
New Address Space
Process Creation:
Parent and Child Processes
Process
Address Space
Child Process
New Address Space
Process Vs. Thread
Process Vs. Thread
• Both processes and threads are threads of execution that enable
parallel operation (multi-tasking) with the help of the scheduler in
the kernel
• In both cases, the Linux kernel assigns time slots for the
process/thread to execute before another one is assigned
• In Linux a thread is a process with some special differences:
• When creating a thread, the address space (and some other
resources) are not copied from the parent. They are shared with the
parent
• This means threads run within the same address space of its parent
and never get a new address space
• Other than that, threads are treated exactly the same as processes
• For example the kernel scheduler deals with each thread
independently
• So, threads created by the same process are multiple processes that
share the same address space and resources of their parent process
Creating a Thread:
Parent Process
Process
Address Space
Creating a Thread:
Fork Procedure
Process
Address Space
Child Process
Fork
Process Termination
Process Termination
• A process is terminated by
• Normal termination (reaching the end of its operation)
• Receiving a Signal that cause it to terminate itself
• Terminated by the kernel
• When the process is terminated, some of its resources are not
yet released, and it changes its state to the Zombie state.
• The parent process must be waiting for its children processes
to perform the required cleanup of resources
• Once the parent process perform the required cleanup, the
process state is no longer in Zombie
• This is why every process must have a living parent, and if its
parent process terminates, the child processes are re-
parented to the init process
Process States
Running
Stopped
Waiting for some Event
Zombie
Process States
http://Linux4EmbeddedSystems.com

Course 102: Lecture 18: Process Life Cycle

  • 1.
    Linux For EmbeddedSystems ForArabs Ahmed ElArabawy Course 102: UnderstandingLinux
  • 2.
  • 3.
  • 4.
    Fork-Exec Procedure • Creatinga new process happens in two steps • Fork: In this step the new process (child process) is created and inherits its features from its parent • Execute: In this step the new process (child process) separates from its parent and move on with its functionality
  • 5.
  • 6.
    Fork • The Jobof “Fork” in is to create a new process (child process) and prepare it with what it needs to be able to run…. • This includes, • Assign it a new pid • Creates associated Structures in the Kernel • Sets the ppid, pgid, sid, and any other attributes • Add it to the Kernel scheduler queue to give it time slots to run • Copy parent memory space (all memory associated with the parent process) to the child process memory space • Copy Other parent process resources, • Normal file handlers • Socket handlers • Shared memory handlers • ….
  • 7.
    Exec • The jobof “Exec” is to send the child process on its own to do its assigned job • This means the child process will be loaded with, • New memory space instead of its parent • Will lose access to parent process resources • A pointer to the new program to run • This command separates the child process from its parent, and the child process runs now on its own, using its own memory address space, and running its own executable and creating/reserving its own system resources
  • 8.
  • 9.
  • 10.
    Process Address Space Child Process Fork COPY CopiedAddress Space Process Creation: Copying Address Space
  • 11.
    Process Creation: Exec Procedure Process AddressSpace Child Process Exec New Address Space
  • 12.
    Process Creation: Parent andChild Processes Process Address Space Child Process New Address Space
  • 13.
  • 14.
    Process Vs. Thread •Both processes and threads are threads of execution that enable parallel operation (multi-tasking) with the help of the scheduler in the kernel • In both cases, the Linux kernel assigns time slots for the process/thread to execute before another one is assigned • In Linux a thread is a process with some special differences: • When creating a thread, the address space (and some other resources) are not copied from the parent. They are shared with the parent • This means threads run within the same address space of its parent and never get a new address space • Other than that, threads are treated exactly the same as processes • For example the kernel scheduler deals with each thread independently • So, threads created by the same process are multiple processes that share the same address space and resources of their parent process
  • 15.
    Creating a Thread: ParentProcess Process Address Space
  • 16.
    Creating a Thread: ForkProcedure Process Address Space Child Process Fork
  • 17.
  • 18.
    Process Termination • Aprocess is terminated by • Normal termination (reaching the end of its operation) • Receiving a Signal that cause it to terminate itself • Terminated by the kernel • When the process is terminated, some of its resources are not yet released, and it changes its state to the Zombie state. • The parent process must be waiting for its children processes to perform the required cleanup of resources • Once the parent process perform the required cleanup, the process state is no longer in Zombie • This is why every process must have a living parent, and if its parent process terminates, the child processes are re- parented to the init process
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.