21
Linux Processes Travis Willey Jeff Mihalik

Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Embed Size (px)

Citation preview

Page 1: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Linux Processes

Travis WilleyJeff Mihalik

Page 2: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

What is a process?

• A process is a program in execution

• A process includes:– program counter– stack– data section

Page 3: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

A Process In Memory

• Stack:– Local variables declared inside

functions– Function Parameters

• Heap:– Dynamically allocated memory

• Data:– Global variables, etc.

• Text:– Program Instructions

Page 4: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process State

• As a process executes, it changes state– TASK_RUNNING: Task is either running or on a runqueue

waiting to run.

– TASK_INTERRUPTIBLE: The process is waiting for some event to occur. Becomes running when event occurs.

– TASK_UNINTERRUPTIBLE: Identical to TASK_INTERRUPTIBLE except it does not wake up and become runnable if it receives a signal. Used rarely.

– TASK_ZOMBIE: The task has been terminated, but its parent has not yet issued a wait4() system call. The task's process descriptor must remain in case the parent wants to access it. If parent calls wait4(), descriptor is deallocated.

– TASK_STOPPED: Process execution has stopped; the task is not running nor is it eligible to run.

Page 5: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Family Hierarchy

• All processes are born from a parent, and all have 0 or more children.

• If the parent of a process dies, it becomes an orphan. The process is then adopted by the init process (PID 1).

• When a process dies, but is still in the queue, it is a zombie process.

Page 6: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process Control Block (PCB)

• Information from various process is stored here,

also known as the task_struct:

– Process state

– Program counter

– CPU registers

– CPU scheduling information

– Memory-management information

– Accounting information

– I/O status information

Page 7: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

PCB diagram

Page 8: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

pt_regs

• Registers are actually stored in a struct

called pt_regs.

• This is an architecture-specific data

structure.

Page 9: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

CPU Switch From Process to Process

Page 10: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Context Switch

• When CPU switches to another process,

the system must save the state of the old

process and load the saved state for the

new process

• Context-switch time is overhead; the

system does no useful work while switching

• Time dependent on hardware support

Page 11: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process Creation

• Parent process create children processes, which, in turn

create other processes, forming a tree of processes

• Resource sharing

– Parent and children share all resources

– Children share subset of parent’s resources

– Parent and child share no resources

• Execution

– Parent and children execute concurrently

– Parent waits until children terminate

Page 12: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process Creation (Cont.)

• Address space

– Child duplicate of parent

– Child has a program loaded into it

• UNIX examples

– fork system call creates new process

– exec system call used after a fork to replace

the process’ memory space with a new

program

Page 13: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process Creation

Page 14: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Copy-on-Write• Copy-on-write is a technique that prevents copying all the data

when a new process is born. Parent and child processes share

a single copy of the data. If any of the data is written to, it is

marked and then a duplicate is made; each process receives its

own unique copy.

• Linux implements fork() via the clone() system call. The bulk of

the work is done in do_fork(), which calls copy_process(), then

the process starts running.

Page 15: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

copy_process()• Calls dup_task_struct(), which creates a new kernel stack, thread_info structure, and task_struct for

the new process.

• Checks that new child will not exceed the resource limits on number of processes for the current

user.

• The child's state is set to TASK_UNINTERRUPTIBLE, to ensure that it does not yet run.

• Calls copy_flags() to update the flags member of the task_struct. The PF_SUPERPRIV flag, which

denotes whether a task used super-user privileges, is cleared. The PF_FORKNOEXEC flag, which

denotes a process that has not called exec(), is set.

• Now the child needs to differentiate itself from its parent. Various members of the process

descriptor are cleared or set to initial values. Members of the process descriptor that are not

inherited are primarily statistically information. The bulk of the data in the process descriptor is

shared.

Page 16: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

copy_process() (cont'd)• Next, it calls get_pid() to assign an available PID to the new task

• Depending on the flags passed to clone(), copy_process() then either duplicates

or shares open files, filesystem information, signal handlers, process address

space, and namespace. These resources are typically shared between threads in

a given process; otherwise they are unique and copied here.

• The remaining timeslice between the parent and child is split between the two.

• Finally, copy_process() cleans up and returns to the caller a pointer to the new

child

Page 17: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

C Program Forking Separate Processint main(){pid_t pid;

/* fork another process */

pid = fork();

if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");

exit(-1);

}

else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);

}

else { /* parent process */

/* parent will wait for the child to complete */

wait (NULL);

printf ("Child Complete");

exit(0);

}}

Page 18: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

A tree of processes on a typical Solaris

Page 19: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

Process Termination• Process executes last statement and asks the operating system to delete it

(exit)

– Output data from child to parent (via wait)

– Process’ resources are deallocated by operating system

• Parent may terminate execution of children processes (abort)

– Child has exceeded allocated resources

– Task assigned to child is no longer required

– If parent is exiting

• Some operating system do not allow child to continue if its parent

terminates

– All children terminated - cascading termination

Page 20: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

EXAM!

• What are the five states a process can be

in?

• Label the ps -ef columns.

• What happens to a Linux process when its

parents die?

Page 21: Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section

References

• www.wikipedia.org

• CS430 Class slides

• Love, Robert. Linux Kernel Development.

Second edition.