Linux Processes

Download Report

Transcript Linux Processes

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
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.
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.
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
PCB diagram
pt_regs
• Registers are actually stored in a struct
called pt_regs.
• This is an architecture-specific data
structure.
CPU Switch From Process to Process
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
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
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
Process Creation
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.
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.
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
C Program Forking Separate Process
int 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);
}
}
A tree of processes on a typical Solaris
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
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?
References
• www.wikipedia.org
• CS430 Class slides
• Love, Robert. Linux Kernel Development.
Second edition.