Processes

A process in computer science has a very similar meaning to our common understanding of a process. The word process comes from ‘procedere’ in Latin which means to proceed or to move. In Computer Science, a process is a program in execution and a program is just a set of instructions. So a process is merely the executing of those instructions.

For example, imagine you are in your kitchen and you’re feeling kind of snackish so you decide to cook something. You take out your recipe book and decide to bake some cookies. The recipe for the cookies is a program. It is a set of steps telling you how to do something, in this case how to bake cookies. Now, once you start gathering all your ingredients and mixing them together to put in the oven, you are in the process of making cookies. So you’ve gone from having a set of instructions (the recipe) to following those instructions (making the cookies) and hopefully ended up with a warm plate of cookies at the end.

A process must execute the steps in a program sequentially. This makes sense why if we consider trying to put all the unmixed ingredients into the oven for our cookies and then trying to mix them after. We would end up with a very odd mixture that we would hardly call cookies. A program is a passive entity as it does not act itself, whereas a process is an active entity as it performs the actions specified in the program. A program can become a process once it is loaded into main memory in a computer.

Parts of a Process

There are multiple parts of a process:

  • The set of instructions, this is called the program code or text section.
  • All current activity relating to the process. A program counter contains the address of the next instruction to be executed. So if we have assembled all our ingredients and the next step is to pour them into a bowl, the program counter would contain (2) for the next instruction. So each process has a program counter.
  • The process also has a stack. A stack is a structure for holding data that has a certain order in which you put data in and take data out. It’s best to think of this as a literal stack of plates. So I first put one plate (A) in the sink. Then I put another two plates in the sink (B then C) in the sink. If I need to take a plate out, I take plate C out as this is on the top. Then B, then A. The plates here represent items of data in the stack. The last item you put on the stack is the first one you put on. See the diagram below, where in 1, A is first put on then B and so on until H. Then H is taken off 1 and placed on 2, then G and so on until we get to the bottom of 1 with A.
  • The process also contains global variables. A variable is just a holding place for data and a global variable means that it can be accessed by all processes. This is called the Data Section.
  • It contains a heap, which is just another data structure for storing data.

States of a Process

There are different states a process can be in. These are:

  • New: The process is created.
  • Running: Instructions are being executed.
  • Waiting: The process is waiting for some event to occur.
  • Ready: The process is waiting to be run. It needs to be assigned a processor in order to be run.
  • Terminated: Process has ended.

So as we can see in the diagram above, a process is first created and is moved into the new state. Then once it is ready to be run, it is moved into the ready state. If there is an available processor, this process may be assigned it and it can start to be run. It is now in the running state. At some point here it may need to wait for an event to occur such as an I/O operation and so will be moved to the waiting state. If this event has occurred and it be placed back in the ready state and wait to be assigned to a processor again. A process can be moved from the ready to running state if the processor has been interrupted. This means the processor is being called to run something that is more important so it will stop running the process and deal with the interrupt and then will continue where it left off when it it has finished. Once there is nothing left to be executed by the process, the process is now terminated.

Process Control Block

The Process Control Block (PCB) is all the information relating to a process. This is kept so that processes can be scheduled effectively. It contains the:

  • Process State
  • Program Counter
  • Registers: These hold data for the processes.
  • CPU Scheduling Information: This contains the priorities of the processor about which process should be run next.
  • Memory Management Information: Information about what memory has been allocated to the process.
  • Accounting Information: Information about which processor is running the process, how long the process has been run for and when the process what created.
  • I/O Status Information: Information about which I/O devices have been allocated to the process.

Process Scehduling

If there are multiple processes that need to be run, the computer Operating System needs to know which process to run first. The process scheduler selects which process the processor should execute next. The process scheduler maintains scheduling queues which contain processes to be run. The Job Queue is a set of all the processes in the system. A Ready Queue is the set of all processes that are ready to be run in the system. A Device Queue is the set of all processes waiting for an I/O device.

Process Schedulers

Short Term Scheduler (CPU Scheduler): This selects which process should be run next and allocates them to the CPU. Some systems may contain only this scheduler. This scheduler is invoked very frequently and so must be fast.

Long Term Scheduler (Job Scheduler): This selects the processes to be moved into the Ready Queue. This scheduler is not invoked very frequently and so speed is not an issue.

Processes can be described in relation to their content and their needs. A process is said to be CPU bound if it spends a lot of its time doing computations. Otherwise, it can be I/O bound is it performs more I/O Operations.

Context Switching

So what happens when a processor switches between processes? The processor must save the state of the process and load the saved state of the new process. This is done by a context switch. This is a vital feature of a multitasking system as it allows multiple processes to be run by a single or few processors. Multitasking means the the processor switches between processes so fast that it appears like processes are running simultaneously. Context switching takes time to do and the processor isn’t completing any work while during a context switch and so most Operating Systems are designed in order to optimise context switching.

Process Creation

A process may create more processes. The process is then called a parent process and any processes that it creates are called child processes. Each process has a unique identifier with which it can be managed and identified. The child will be a duplicate of the parent and shares the same memory.

Process Termination

When a process has finished, it requests the Operating System to delete it using a system call. (This is a call to the kernel, for more on this see https://masteringperception.home.blog/2019/05/04/operatingsystems/ )

The status data of it’s children are returned to the parent and the resources that were being used by the process are deallocated. The parent may also terminate its child processes. It may do this if the child is no longer required, the child has exceeded its allocated resources or the Operating System doesn’t allow children to remain after the parent is terminated. This can result in a cascading termination and is initiated by the Operating System. Which means all the parent’s children and grandchildren etc. are terminated once the parent is. Otherwise, the parent may wait until its child has finished before terminating it.

Some strange occurrences can occur with child processes.

A zombie process is a child process that is in it’s terminated state but is still alive as the parent has not yet terminated it. An orphan process is a child process that is still running but it’s parent is dead.

Interprocess Communication

Processes in a system may be independent or co-operating. Independent processes cannot affect or be affected by other processes whereas co-operating processes can affect and be affected by other processes. Co-operating processes need some way to communicate with each other.

There are two ways that processes can communicate with each other: Shared Memory and Message Passing.

Shared Memory

This is an area of memory that is shared among communicating processes.

Message Passing

Two processes that wish to communicate can establish a communication link between them. They can both send and receive messages via this link. This can be done through direct communication or indirect communication.

Direct Communication: The processes must name each other when they are sending and receiving messages. With this method, the communication link is established automatically and a link is set up between one pair of processes. There is only one link between this pair. The link is usually bi-directional but can be uni-directional.

Indirect Communication: Messages are sent and received to/from mailboxes known as ports. Each port has a unique id and processes may only communicate if they share a mailbox. This communication link is only established if the processes share a mailbox. Many processes may be associated with this link. Each pair of processes can share multiple communication links. Links can be uni or bi-directional. The operations associated with indirect communication is to (1) Create a port, (2) Send/ Receive message to/from port and (3) Delete port.

Message Synchronisation

Message passing can be blocking or non-blocking. Blocking means that either the sender can be blocked until the receiver receives the message and the receiver is blocked until a message is available. Non-Blocking means that the sender can send a message and then continue doing what it was doing regardless of if the receiver receives it and the receiver may receive a valid message or a null message.

Pipes

Pipes allow two processes to communicate. They are usually created by a parent allow a parent and child to communicate with each other. Although they can also be used outside a parent-child relationship. A named pipe can last beyond the life of the process that has created it whereas an anonymous pipe won’t.

Anonymous Pipes: With these, a process can write to one end of the pipe (writing end) and another process can read from the other end of the pipe (reading end). These are therefore uni-directional. These require a parent-child relationship.

Named Process: Communication with these pipes is bi-directional. No parent-child relationship is required for this pipe and a single piped can be used by several processes.

And so we have taken a look at the world of processes and learnt about how they function and communicate. This article is a sister article to Operating Systems so if you enjoyed this or want more in depth knowledge of the place of a process in the system as a whole then be sure to check that one out.

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started