Chapter 3A Flashcards
(106 cards)
A process
is a program in execution, and the status of the current activity of a process is represented by
the program counter, as well as other registers.
A process is independent
if it does not share data with any other processes executing in the system
process is cooperating
if it can affect or be affected by the other processes executing in the system.
Clearly, any process that shares data with other processes is a cooperating process
As a process executes, it changes state
The state of a process is defined in part by the current activity of
that process
Types of processes:
- New. The process is being created.
- Running. Instructions are being executed.
- Waiting. The process is waiting for some event to occur (such as an I/O completion or reception of a
signal). - Ready. The process is waiting to be assigned to a processor.
- Terminated. The process has finished execution
Each process is represented in the operating system by a process control block (PCB)—also called a task
control block
. A PCB is shown in Figure 3.3. It contains many pieces of information associated with a
specific process, including these: * Process state. The state may be new, ready, running, waiting, halted,
and so on. * Program counter. The counter indicates the address of the next instruction to be executed for
this process.
*
- CPU registers.
The registers vary in number and type, depending on the computer architecture. They
include accumulators, index registers, stack pointers, and general-purpose registers, plus any
condition-code information. Along with the program counter, this state information must be saved when
an interrupt occurs, to allow the process to be continued correctly afterward when it is rescheduled to run.
- CPU-scheduling information
This information includes a process priority, pointers to scheduling
queues, and any other scheduling parameters. (Chapter 5 describes process scheduling.) *
Memory-management information
This information may include such items as the value of the base and
limit registers and the page tables, or the segment tables, depending on the memory system used by the
operating system (Chapter 9). *
- Accounting information.
This information includes the amount of CPU
and real time used, time limits, account numbers, job or process numbers, and so on.
- I/O status
information.
This information includes the list of I/O devices allocated to the process, a list of open files,
and so on.
Threads
The process model discussed so far has implied that a process is a program that performs a single
thread of execution. For example, when a process is running a word-processor program, a single thread of
instructions is being executed. This single thread of control allows the process to perform only one task at
a time. Thus, the user cannot simultaneously type in characters and run the spell checker. Most modern
operating systems have extended the process concept to allow a process to have multiple threads of
execution and thus to perform more than one task at a time. This feature is especially beneficial on
multicore systems, where multiple threads can run in parallel. A multithreaded word processor could, for
example, assign one thread to manage user input while another thread runs the spell checker. On systems
that support threads, the PCB is expanded to include information for each thread. Other changes
throughout the system are also needed to support threads.
The number of processes currently in memory is known as the degree of multiprogramming.
Balancing
the objectives of multiprogramming and time sharing also requires taking the general behavior of a
process into account. In general, most processes can be described as either I/O bound or CPU bound. An
I/O-bound process is one that spends more of its time doing I/O than it spends doing computations. A
CPU-bound process, in contrast, generates I/O requests infrequently, using more of its time doing
computations.
As processes enter the system, they are put into a ready queue, where they are ready and waiting to
execute on a CPU’s core
This queue is generally stored as a linked list; a ready-queue header contains
pointers to the first PCB in the list, and each PCB includes a pointer field that points to the next PCB in
the ready queue. The system also includes other queues. When a process is allocated a CPU core, it
executes for a while and eventually terminates, is interrupted, or waits for the occurrence of a particular
event, such as the completion of an I/O request. Suppose the process makes an I/O request to a device
such as a disk. Since devices run significantly slower than processors, the process will have to wait for the
I/O to become available. Processes that are waiting for a certain event to occur — such as completion of
I/O — are placed in a wait queue (Figure 3.4). A common representation of process scheduling is a
queueing diagram, such as that in Figure 3.5. Two types of queues are present: the ready queue and a set
of wait queues. The circles represent the resources that serve the queues, and the arrows indicate the flow
of processes in the system.
Once the process is allocated a CPU core and is executing, one of several events could occur:
- The process could issue an I/O request and then be placed in an I/O wait queue. * The process could
create a new child process and then be placed in a wait queue while it awaits the child’s termination. * The
process could be removed forcibly from the core, as a result of an interrupt or having its time slice expire,
and be put back in the ready queue. In the first two cases, the process eventually switches from the
waiting state to the ready state and is then put back in the ready queue. A process continues this cycle
until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated.
Tree of processes
During the course of execution, a process may create several new processes. As
mentioned earlier, the creating process is called a parent process, and the new processes are called the
children of that process. Each of these new processes may in turn create other processes, forming a tree of
processes
PID -> Most operating systems (including UNIX, Linux, and Windows) identify processes according to a
unique process identifier (or pid), which is typically an integer number. The pid provides a unique value
for each process in the system, and it can be used as an index to access various attributes of a process
within the kernel.
In general, when a process creates a child process,
s, that child process will need certain resources (CPU
time, memory, files, I/O devices) to accomplish its task. A child process may be able to obtain its
resources directly from the operating system, or it may be constrained to a subset of the resources of the
parent process. The parent may have to partition its resources among its children, or it may be able to
share some resources (such as memory or files) among several of its children. Restricting a child process
to a subset of the parent’s resources prevents any process from overloading the system by creating too
many child processes.
When a process creates a new process, two possibilities for execution exist:
- The parent continues to execute concurrently with its children. 2. The parent waits until some or all of
its children have terminated.
There are also two address-space possibilities for the new process:
- The child process is a duplicate of the parent process (it has the same program and data as the parent).
- The child process has a new program loaded into it.
FORK() SYSTEM CALL
A new process is created by the fork() system call. The new process consists of
a copy of the address space of the original process. This mechanism allows the parent process to
communicate easily with its child process. Both processes (the parent and the child) continue execution at
the instruction after the fork(), with one difference: the return code for the fork() is zero for the new
(child) process, whereas the (nonzero) process identifier of the child is returned to the parent
EXEC() SYSTEM CALL
exec() system call to replace the process’s memory space with a new program.
The exec() system call loads a binary file into memory (destroying the memory image of the program
containing the exec() system call) and starts its execution. In this manner, the two processes are able to
communicate and then go their separate ways. The parent can then create more children; or, if it has
nothing else to do while the child runs, it can issue a wait() system call to move itself off the ready queue
until the termination of the child. Because the call to exec() overlays the process’s address space with a
new program, exec() does not return control unless an error occurs.
The parent waits for the child process
s to complete with the wait() system call. When the child process
completes (by either implicitly or explicitly invoking exit()), the parent process resumes from the call to
wait(), where it completes using the exit() system call.
Windows system calls
As an alternative example, we next consider process creation in Windows.
Processes are created in the Windows API using the CreateProcess() function, which is similar to fork() in
that a parent creates a new child process. However, whereas fork() has the child process inheriting the
address space of its parent, CreateProcess() requires loading a specified program into the address space of
the child process at process creation. Furthermore, whereas fork() is passed no parameters,
CreateProcess() expects no fewer than ten parameters. The C program shown in Figure 3.10 illustrates the
CreateProcess() function, which creates a child process that loads the application mspaint.exe. We opt for
many of the default values of the ten parameters passed to CreateProcess(). Readers interested in pursuing
the details of process creation and management in the Windows API are encouraged to consult the
bibliographical notes at the end of this chapter. The two parameters passed to the CreateProcess() function
are instances of the STARTUPINFO and PROCESS INFORMATION structures. STARTUPINFO
specifies many properties of the new process, such as window size and appearance and handles to
standard input and output files. The PROCESS INFORMATION structure contains a handle and the
identifiers to the newly created process and its thread. We invoke the ZeroMemory() function to allocate
memory for each of these structures before proceeding with CreateProcess(). The first two parameters
passed to CreateProcess() are the application name and command-line parameters. If the application name
is NULL (as it is in this case), the command-line parameter specifies the application to load.
Process Termination
A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit() system call. At that point, the process may return a status
value (typically an integer) to its waiting parent process (via the wait() system call). All the resources of
the process —including physical and virtual memory, open files, and I/O buffers—are deallocated and
reclaimed by the operating system. Termination can occur in other circumstances as well. A process can
cause the termination of another process via an appropriate system call (for example, TerminateProcess()
in Windows). Usually, such a system call can be invoked only by the parent of the process that is to be
terminated. Otherwise, a user— or a misbehaving application—could arbitrarily kill another user’s
processes. Note that a parent needs to know the identities of its children if it is to terminate them. Thus,
when one process creates a new process, the identity of the newly created process is passed to the parent.
A parent may terminate the execution of one of its children for a variety of reasons, such as these: * The
child has exceeded its usage of some of the resources that it has been allocated. (To determine whether
this has occurred, the parent must have a mechanism to inspect the state of its children.) * The task
assigned to the child is no longer required. * The parent is exiting, and the operating system does not
allow a child to continue if its parent terminates.