Course 102: Lecture 18: Process Life Cycle

25
Linux For Embedded Systems For Arabs Ahmed ElArabawy Course 102: Understanding Linux

Transcript of Course 102: Lecture 18: Process Life Cycle

Page 1: Course 102: Lecture 18: Process Life Cycle

Linux For Embedded Systems For Arabs

Ahmed ElArabawy

Course 102: Understanding Linux

Page 2: Course 102: Lecture 18: Process Life Cycle

Lecture 18: Process Life-Cycle

Page 3: Course 102: Lecture 18: Process Life Cycle

PROCESS CREATION

Page 4: Course 102: Lecture 18: Process Life Cycle

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

Page 5: Course 102: Lecture 18: Process Life Cycle

Fork Execute

Page 6: Course 102: Lecture 18: Process Life Cycle

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

• ….

Page 7: Course 102: Lecture 18: Process Life Cycle

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

Page 8: Course 102: Lecture 18: Process Life Cycle

Process

Address Space

Process Creation: Parent Process

Page 9: Course 102: Lecture 18: Process Life Cycle

Process

Address Space

Child Process

Fork

Process Creation: Fork Procedure

Page 10: Course 102: Lecture 18: Process Life Cycle

Process

Address Space

Child Process

Fork

COPY Copied Address Space

Process Creation: Copying Address Space

Page 11: Course 102: Lecture 18: Process Life Cycle

Process Creation: Exec Procedure

Process

Address Space

Child Process

Exec

New Address Space

Page 12: Course 102: Lecture 18: Process Life Cycle

Process Creation: Parent and Child Processes

Process

Address Space

Child Process

New Address Space

Page 13: Course 102: Lecture 18: Process Life Cycle

Process Vs. Thread

Page 14: Course 102: Lecture 18: Process Life Cycle

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

Page 15: Course 102: Lecture 18: Process Life Cycle

Creating a Thread: Parent Process

Process

Address Space

Page 16: Course 102: Lecture 18: Process Life Cycle

Creating a Thread: Fork Procedure

Process

Address Space

Child Process

Fork

Page 17: Course 102: Lecture 18: Process Life Cycle

Process Termination

Page 18: Course 102: Lecture 18: Process Life Cycle

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

Page 19: Course 102: Lecture 18: Process Life Cycle

Process States

Page 20: Course 102: Lecture 18: Process Life Cycle

Running

Page 21: Course 102: Lecture 18: Process Life Cycle

Stopped

Page 22: Course 102: Lecture 18: Process Life Cycle

Waiting for some Event

Page 23: Course 102: Lecture 18: Process Life Cycle

Zombie

Page 24: Course 102: Lecture 18: Process Life Cycle

Process States

Page 25: Course 102: Lecture 18: Process Life Cycle

http://Linux4EmbeddedSystems.com