CS4023_-_lecture_08_..
Download
Report
Transcript CS4023_-_lecture_08_..
Lecture 8
Processes: Part 2
These slides are based on slides which are copyright Silberschatz, Galvin and Gagne, 2005
Context Switch in Detail
A context switch is the switching of the CPU (central
processing unit) from one process or thread to another.
A context is the contents of a CPU's registers and program
counter at any point in time.
A register is a small amount of very fast memory inside of a
CPU that is used to speed the execution
of computer programs by providing quick access to commonly
used values.
A program counter is a specialized register that indicates the
position of the CPU in its instruction sequence and which
holds either the address of the instruction being executed or
the address of the next instruction to be executed, depending
on the specific system.
http://www.linfo.org/context_switch.html
Context Switch in Detail
(contd.)
Context switching can be described in slightly
more detail as the kernel performing the following
activities with regard to processes (including
threads) on the CPU:
1. suspending the progression of one process and storing
the CPU's state (i.e., the context) for that process
somewhere in memory (as a PCB)
2. retrieving the context of the next process from memory
and restoring it in the CPU's registers and
3. returning to the location indicated by the program counter
(i.e., returning to the line of code at which the process
was interrupted) in order to resume the process.
http://www.linfo.org/context_switch.html
Context Switch in Detail
(contd.)
Context switches can occur only in kernel
mode (system mode). Kernel mode is a
privileged mode of the CPU in which only
the kernel runs and which provides access
to all memory locations and all other system
resources.
Other programs, including applications,
initially operate in user mode, but they can
run portions of the kernel code via system
calls.
http://www.linfo.org/context_switch.html
Context Switch in Detail
(contd.)
The Intel x86 CPU provides a way of context switching
completely in hardware, but for performance and
portability reasons most modern OS's do context
switches in software.
Software context switching can be used on all CPUs,
and can be used to save and reload only the state that
needs to be changed.
To use the hardware context switch you need to tell the
CPU where to save the existing CPU state, and where
to load the new CPU state from. The CPU state is
always stored in a special data structure called a TSS
(Task State Segment).
http://wiki.osdev.org/Context_Switching
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
Process Creation
POSIX Example
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);
}
}
Process Creation
Windows API Example
#include <windows.h>
#include <stdio.h>
int main( VOID )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
Process Creation
Windows API Example (contd.)
// Start the child process.
2. if (!CreateProcess( NULL,
// No module name.
3.
"C:\\WINDOWS\\system32\\mspaint.exe", //Command line.
4.
NULL, // Process handle not inheritable.
5.
NULL, // Thread handle not inheritable.
6.
FALSE, // Set handle inheritance to FALSE.
7.
0,
// No creation flags.
8.
NULL, // Use parent's environment block.
9.
NULL, // Use parent's starting directory.
10.
&si,
// Pointer to STARTUPINFO structure.
11.
&pi)
// Pointer to PROCESS_INFORMATION structure.
12. ){
13.
printf( "CreateProcess failed (%d).\n", GetLastError() );
14.
return -1;
15. }
1.
Process Creation
Windows API Example (contd.)
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
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