Processes

Zhao Cong

Process Concept

A question that arises in discussing operating systems involves what to call all the CPU activities. Early computers were batch systems that executed jobs, followed by the emergence of time-shared systems that ran user programs, or tasks.

The Process

  • A process is a program in execution.
  • The status of the current activity of a process is represented by the value of the program counter and the contents of the processor’s registers.
  • The memory layout of a process is typically divided into multiple sections:
    • Text section—the executable code
    • Data section—global variables
    • Heap section—memory that is dynamically allocated during program run time
    • Stack section—temporary data storage when invoking functions (such as function parameters, return addresses, and local variables)
  • the sizes of the text and data sections are fixed,the stack and heap sections can shrink and grow dynamically during program execution
  • The stack and heap sections grow toward one another, the operating system must ensure they do not overlap one another
  • We emphasize that a program by itself is not a process. A program is a passive entity,such as a file containing a list of instructions stored on disk (often called an executable fil ).
  • A process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources
  • A program becomes a process when an executable file is loaded into memory
  • Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences.
  • A process can itself be an execution environment for other code.(JAVA)

Process State

  • As a process executes, it changes state. The state of a process is defined in part by the current activity of that process.
  • A process may be in one of the following states:
    • 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.
  • Only one process can be running on any processor core at any instant. Many processes may be ready and waiting

Process Control Block

  • Each process is represented in the operating system by a process control block (PCB)—also called a task control block
  • It including :
    • 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. 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
    • Memory-management information
    • 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.
  • the PCB simply serves as the repository for all the data needed to start, or restart, a process, along with some accounting data

Threads

  • 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
  • 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

Process Scheduling

  • The objective of multiprogramming is to have some process running at all times so as to maximize CPU utilization
  • The objective of time sharing is to switch a CPU core among processes so frequently that users can interact with each program while it is running.
  • The process scheduler selects an available process (possibly from a set of several available processes) for program execution on a core.
  • Each CPU core can run one process at a time.multicore system can run multiple processes at one time
  • The number of processes currently in memory is known as the degree of multiprogramming
  • 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

Scheduling Queues

  • As processes enter the system, they are put into a ready queue,This queue is generally stored as a linked list
  • Processes that are waiting for a certain event to occur — such as completion of I/O — are placed in a wait queue
  • 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

CPU Scheduling

  • The role of the CPU scheduler is to select from among the processes that are in the ready queue and allocate a CPU core to one of them. The CPU scheduler must select a new process for the CPU frequently
  • Some operating systems have an intermediate form of scheduling, known as swapping
  • This scheme is known as swapping because a process can be “swapped out” from memory to disk, where its current status is saved, and later “swapped in”from disk back to memory, where its status is restored

Context Switch

  • interrupts cause the operating system to change a CPU core from its current task and to run a kernel routine
  • When an interrupt occurs, the system needs to save the current context of the process running on the CPU core so that it can restore that context when its processing is done, essentially suspending the process and then resuming it
  • Generically, we perform a state save of the current state of the CPU core, be it in kernel or user mode, and then a state restore to resume operations.
  • Switching the CPU core to another process requires performing a state save of the current process and a state restore of a different process.This task is known as a context switch
  • Context switch time is pure overhead, because the system does no useful work while switching.
  • Context-switch times are highly dependent on hardware support.the more complex the operating system, the greater the amount of work that must be done during a context switch.

Operations on Processes

Process Creation

  • 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.
  • Most operating systems (including UNIX, Linux, and Windows) identify processes according to a unique process identifie (or pid), which is typically an integer number.
  • 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
    • 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
    • The child process has a new program loaded into it
  • 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.

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)
  • A parent may terminate the execution of one of its children for a variety of reasons:
    • The child has exceeded its usage of some of the resources that it has been allocated.
    • The task assigned to the child is no longer required
    • The parent is exiting
  • Some systems do not allow a child to exist if its parent has terminated.This phenomenon, referred to as cascading termination
  • A parent process may wait for the termination of a child process by using the wait() system call.
  • A process that has terminated, but whose parent has not yet called wait(), is known as a zombie process.
  • Now consider what would happen if a parent did not invoke wait() and instead terminated, thereby leaving its child processes as orphans

Android Process Hierarchy

  • Foreground process
  • Visible process
  • Service process
  • Background process
  • Empty process

Interprocess Communication

  • A process is independent if it does not share data with any other processes executing in the system.
  • A process is cooperating if it can affect or be affected by the other processes executing in the system.
  • There are several reasons for providing an environment that allows process cooperation:
    • Information sharing
    • Computation speedup
    • Modularity
  • Cooperating processes require an interprocess communication (IPC)
  • There are two fundamental models of interprocess communication: shared memory and message passing
  • Message passing is useful for exchanging smaller amounts of data, because no conflicts need be avoided. Message passing is also easier to implement in a distributed system than shared memory
  • Shared memory can be faster than message passing, since message-passing systems are typically implemented using system calls and thus require the more time-consuming task of kernel intervention

IPC in Shared-Memory Systems

  • IPC using shared memory requires communicating processes to establish a region of shared memory.
  • Other processes that wish to communicate using this shared-memory segment must attach it to their address space.
  • Shared memory requires that two or more processes agree to remove this restriction(access)
  • The Producer–Consumer problem:. A producer process produces information that is consumed by a consumer process.
    • EX:compiler and assembler, assembler and loader ,client–server paradigm:web server and client web browser.
  • One solution to the producer–consumer problem uses shared memory.
    • To allow producer and consumer processes to run concurrently, we must have available a buffer of items that can be filled by the producer and emptied by the consumer.
    • Two types of buffers:
      • unbounded buffer:(no practical limit on the size of the buffer).The consumer may have to wait for new items, but the producer can always produce new items
      • bounded buffer:(a fixed buffer size)The consumer must wait if the buffer is empty, and the producer must wait if the buffer is full
    • A producer can produce one item while the consumer is consuming another item.
    • The producer and consumer must be synchronized
  • The scheme requires that these processes share a region of memory and that the code for accessing and manipulating the shared memory be written explicitly by the application programmer.

IPC in Message-Passing Systems

  • The operating system provides the means for cooperating processes to communicate with each other via a message-passing facility.
  • It is particularly useful in a distributed environment, where the communicating processes may reside on different computers connected by a network.
  • Fixed-sized messages can be sent, the system-level implementation is straightforward. However, makes the task of programming more difficult.Variable-sized messages require a more complex system-level implementation, but the programming task becomes simpler.This is a common kind of tradeoff seen throughout operating-system design.
  • A communication link must exist between processes.Several methods for logically implementing a link and the send()/receive() operations:
    • Direct or indirect communication
    • Synchronous or asynchronous communication
    • Automatic or explicit buffering

Naming

Direct communication

  • each process that wants to communicate must explicitly name the recipient or sender of the communication:
    • send(P, message)—Send a message to process P.
    • receive(Q, message)—Receive a message from process Q.
  • properties:
    • A link is established automatically between every pair of processes that want to communicate. The processes need to know only each other’s identity to communicate.
    • A link is associated with exactly two processes
    • Between each pair of processes, there exists exactly one link
  • This scheme exhibits symmetry in addressing that is, both the sender process and the receiver process must name the other to communicate.
  • A variant of this scheme employs asymmetry in addressing. Here, only the sender names the recipient
    • send(P, message)—Send a message to process P.
    • receive(id, message)—Receive a message from any process. The variable id is set to the name of the process with which communication has taken place.
  • Hard-coding techniques are that identifiers must be explicitly stated.They are less desirable than techniques involving indirection

Indirect communication

  • the messages are sent to and received from mailboxes, or ports
  • Each mailbox has a unique identification
  • two processes can communicate only if they have a shared mailbox
  • The send() and receive() :
    • send(A, message)—Send a message to mailbox A.
    • receive(A, message)—Receive a message from mailbox A.
  • Properties:
    • A link is established between a pair of processes only if both members of the pair have a shared mailbox
    • A link may be associated with more than two processes.
    • Between each pair of communicating processes, a number of different links may exist, with each link corresponding to one mailbox.
  • Now suppose that processes P1, P2, and P3all share mailbox A. Process P1 sends a message to A, while both P2and P3execute a receive() from A. Which process will receive the message sent by P1? The answer depends on which of the following methods we choose:
    • Allow a link to be associated with two processes at most.
    • Allow at most one process at a time to execute a receive() operation.
    • Allow the system to select arbitrarily which process will receive the message.
  • A mailbox may be owned either by a process or by the operating system
    • process:
      • we distinguish between the owner (which can only receive messages through this mailbox) and the user (which can only send messages to the mailbox).
      • When a process that owns a mailbox terminates, the mailbox disappears. Any process that subsequently sends a message to this mailbox must be notified that the mailbox no longer exists.
    • operating system:
      • It is independent and is not attached to any particular process
      • mechanism:Create a new mailbox,Send and receive messages through the mailbox,Delete a mailbox

Synchronization

  • Message passing may be either blocking or nonblocking—also known as synchronous and asynchronous.
  • Blocking send.The sending process is blocked until the message is received by the receiving process or by the mailbox
  • Nonblocking send. The sending process sends the message and resumes operation.
  • Blocking receive. The receiver blocks until a message is available.
  • Nonblocking receive. The receiver retrieves either a valid message or a null

Buffering

  • Whether communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue. Basically, such queues can be implemented in three ways:
    • Zero capacity:The queue has a maximum length of zero; thus, the link cannot have any messages waiting in it. In this case, the sender must block until the recipient receives the message
    • Bounded capacity:at most n messages can reside in it.If the link is full, the sender must block until space is available in the queue.
    • Unbounded capacity:any number of messages can wait in it. The sender never blocks
  • The zero-capacity case is sometimes referred to as a message system with no buffering. The other cases are referred to as systems with automatic buffering.

Examples of IPC Systems

POSIX Shared Memory

  • A process must first create a shared-memory object using the shm open() system call, as follows:
    1
    fd = shm-open (name,0_CREAT | 0RDWR,0666);
  • The first parameter specifies the name of the shared-memory object
  • The subsequent parameters specify that the shared-memory object is to be created if it does not yet exist (0_CREAT) and that the object is open for reading and writing (0_RDWR). The last parameter establishes the file-access permissions of the shared-memory object.
  • Once the object is established, the ftruncate() function is used to configure the size of the object in bytes.
    1
    ftruncate (fd,4096);
  • Finally, the mmap() function establishes a memory-mapped file containing the shared-memory object. It also returns a pointer to the memory-mapped file that is used for accessing the shared-memory object.

Mach Message Passing

  • Most communication in Mach—including all intertask communication—is carried out by messages. Messages are sent to, and received from, mailboxes, which are called ports in Mach.
  • Associated with each port is a collection of port rights that identify the capabilities necessary for a task to interact with the port.
  • When a task is created, two special ports—the Task Self port and the Notify port—are also created. The kernel has receive rights to the Task Self port, which allows a task to send messages to the kernel. The kernel can send notification of event occurrences to a task’s Notify port.

Windows

  • The message-passing facility in Windows is called the advanced local procedure call (ALPC) facility
  • Windows uses two types of ports: connection ports and communication ports.

Pipes

  • A pipe acts as a conduit allowing two processes to communicate. Pipes were one of the first IPC mechanisms in early UNIX systems.

Ordinary Pipes

Named Pipes

Communication in Client–Server Systems

Sockets

  • A socket is defined as an endpoint for communication. A pair of processes communicating over a network employs a pair of sockets—one for each process. ## Remote Procedure Calls

  • One of the most common forms of remote service is the RPC paradigm, which was designed as a way to abstract the procedure-call mechanism for use between systems with network connections.