Operating Systems Concepts: Processes
Tags: computerscience, linux, systems, codenewbie
Published: 2021-07-31 23:10:14

A couple of weeks ago I came across a very interesting book titled [Operating Systems Concepts](https://www.amazon.com/Operating-System-Concepts-Abraham-Silberschatz-ebook/dp/B07CVKH7BD) or "the dinosaur book" by **Abraham Silberschatz**, **Greg Gagne** and **Peter B. Galvin**, which has a very good reputation, and it's one of the best operating system textbooks. The book discusses a number of different concepts related to Processes, Threads, CPU Scheduling, Deadlocks, Memory, Storage, File-Systems, Security and more.

One of my friends on Twitter recommended it to me a few days ago, so I said that it's a good time to start reading it!

I started reading, and then I decided that it would be better to write a form of summaries or personal notes, so to speak based on the book, I'll start summarizing from chapter 3 because the first two chapters are talking about an introduction to the operating systems and its structure. So without further ado, let's Get Started!

----

#✍ Processes

Early computers allowed only one program to be executed at a time. This program had complete control of the system and had access to all the [system’s resources](https://en.m.wikipedia.org/wiki/System_resource). In contrast, contemporary computer systems allow multiple programs to be loaded into memory and executed concurrently.

**The process** is a program in execution and the unit of work in a modern computing system. Although its main concern is the execution of user programs, it also needs to take care of various system tasks that are best done in [user space](https://en.wikipedia.org/wiki/User_space), rather than within the kernel space.

#📌 Process Concept

When we read about **processes**, we may face some other terminologies like **job** or **task**, they can be all considered the same thing, it really depends on the context. A [“job”](https://en.wikipedia.org/wiki/Job_(computing)) often means a set of processes, while a [“task”](https://en.wikipedia.org/wiki/Task_(computing)) is often a part of a job – sometimes the only part.

##🧱 Memory layout of a process 

It's typically divided into multiple sections, these sections include:
![A process in memory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vmh3af0dg5981rj5c4x.jpg)
1. **Text section**: the executable code, this is read-only and might be shared by a number of processes
2. **Data section**: containing global variables
3. **Heap section**: containing memory dynamically allocated during the program run time
4. **Stack section**: containing temporary data
 (such as function parameters, return addresses, and local variables)

**Memory layout of a *C* program:**
![code+memory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b98px5p2nzv4de0roha.png)
Notice that the sizes of the **text** and **data** sections are fixed, as their sizes do not change during program run time. However, the **stack** and **heap** sections can shrink and grow dynamically during program execution, for example, in case of sending parameters or returning from a function.

##🆚 Program Vs. Process

- **A program** is a passive entity, such as a file containing a list of instructions stored on disk
(often called an executable file). 
- **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.

##⚙️ Process 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:
![process state](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf39iuzkb011oq8e6dkb.jpg) 
• **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.

##📦 Process Control Block

For each process there is a Process Control Block(PCB) – also called a **task control block**, which stores the following process-specific information:
![Process Control Block](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhwx0s2a045tyixp5cu2.jpg)

- **Process State** – running, waiting, etc., as discussed above
- **Process Number "PID"** – is a number used by most operating system kernels to uniquely identify an active process
- **Program counter** – the counter indicates the address of the next instruction to be executed for this process
- **CPU Registers** – tell us the particular registers that are being used by a particular process
- **CPU-Scheduling information** – this information includes a process priority, pointers to scheduling queues, and any other scheduling parameters
- **Memory-Management information** – e.g., page tables or segment tables, depending on the memory system used by the operating
system
- **Accounting information** – they are the resources that are used by a specific process e.g., the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on
- **I/O Status information** - e.g., a list of I/O devices allocated to the process, a list of open files, and so on.

##🐧 Process Representation in Linux 
The process control block in the Linux operating system is represented by the C structure `task_struct`, which is found in the [Linux](https://github.com/torvalds/linux) repository through this path [include/linux/sched.h] (https://github.com/torvalds/linux/blob/master/include/linux/sched.h) The following structure contains all the necessary information for representing a process.
```c
long state; /* state of the process */
struct sched_entity se; /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space */
```
Within the Linux kernel, all active processes are represented
using a `doubly linked list` of *task_struct*. The kernel maintains a pointer `current` to the process currently executing on the system, as shown below:
![Process as a linkedlist](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5h22cjy6arvhpzk3i1a.png) 
If `current` is a pointer to the process currently executing, its state is changed with the following:
```c
current->state = new state;
```
#📌 Process Scheduling
- The objective of **multiprogramming** is to have some processes running at all times, to maximize CPU utilization
- The objective of **time sharing** is to switch the CPU among processes so frequently that users can interact with each program while it is running

To meet these objectives, the process scheduler selects an available process (possibly from a set of several available processes) for program execution on the CPU.

- Each CPU core can run one process at a time
- If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled.

##📅 Scheduling Queues
- **Job Queue** – As processes enter the system, they are put into a *job queue*, which consists of all processes in the system
- **Ready Queue** – The processes that are residing in main memory and are ready and waiting to execute are kept on a list called *ready queue*
- **Device Queue** – Processes waiting for a device to become available or to deliver data are placed in *device queues*.

![Queueing-diagram representation of process scheduling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rabscl3imhfj37frnogj.jpg)
The above figure is a Queueing-diagram representation of process scheduling.

##🔄 Process Swapping
Some operating systems have an intermediate form of scheduling, known as **swapping**, whose key idea is to remove a process from the memory or loading the process again from disk back to memory.

![Scheduling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar4x8vsertq1af8483cu.jpg)

##🔗 Context Switch
When an interrupt occurs, the system needs to **save** the current **context** of the process currently running on the CPU so that it can **restore** that context when its processing is done, essentially suspending the process and then resuming it, this process is known as a `context switch`
- Interrupts cause the operating system to change a CPU from its current task "process" to run a kernel routine
- Such operations happen frequently on general-purpose systems
- The context is represented in the PCP `Process Control Block` of the process
- Context Switch time is an overhead because the system does no useful work while switching
- Its speed varies from machine to machine, depending on the memory speed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers).
![Context Switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b26t24g4pnpwgk4kvoce.jpg)

##📱 Multitasking In Mobile Systems (Android)
Since its origins, **Android** has supported **multitasking** and does not place constraints on the types of applications that can run in the background. If an application requires processing while in the background, the application must use a **service**, a separate application component that runs on behalf of the background process.

Consider a streaming audio application: if the application moves to the background, the service continues to send audio data to the audio device driver on behalf of the background application. In fact, the service will continue to run even if the background application is suspended. Services do not have a user interface and have a small memory footprint, thus providing an efficient technique for multitasking in a mobile environment.

---

##Conclusion

In this article, we summarized chapter 3 – part 1, and talked about Process Concept, The memory layout of a process, Process State, PCB, Process Scheduling, Context Switch, and Multitasking in mobile systems. See you soon in the next article, and I will be talking about part 2 of chapter 3.

##Previous Articles

- [Top 10 Useful GitHub Browser Extensions](https://dev.to/abanoub7asaad/top-10-useful-github-browser-extensions-2k2d)