We already know about process. Process is a program in execution. A process has a life cycle.
Earlier computer systems allowed only one program to be executed at a time. This program ruled the system. It had access to all resources. Nowadays, our systems are time sharing. Multiple programs can be loaded together. They execute concurrently. Here comes the role of process. A process is a program in execution i.e., to perform better, operating systems have a collection of processes.
- System Processes execute system code.
- User Processes executes user code.
These processes can run concurrently, which is achieved by switching the CPU between processes. Structure of a process is as shown below:
Figure - Structure of a process
You might be thinking what is the difference between a program and a process? Program is a passive entity. It contains the set of instructions. Process is an active entity. It contains the program counter which indicates next instruction to be executed. It is allocated with a set of resources. Program becomes process when executable file is loaded into memory. In case of multiple processes of same program, text section is same. Data, Heap and stack section varies.
126.96.36.199 Process States
We have different process states. Current activity of process determines its state. It may be in any one of the following states:
- New: The process is created.
- Running: Instructions are executed at this stage.
- Waiting: Process is waiting for some event to occur.
- Ready: Process is waiting to be assigned to a processor.
- Terminated: Execution is finished.
Figure - Process States
State names may vary across various operating systems but they are found on all systems. One important thing to note that there can be any number of processes in ready or waiting state but there can be only one process in running state.
188.8.131.52 Process Control Block (PCB)
In an operating system, each process is represented by a process control block (PCB) or Task Control Block. PCB can be represented as follows:
Figure - Process Control Block
Let us have a glance on the pieces of information contained in PCB and they are as follows:
- Process State: A process state may be new, ready, waiting, etc.
- Program Counter: It contains next executable instruction in this process.
- CPU registers: They provide storage and fast access. They include accumulators, index registers, stack pointers and general-purpose registers. It may contain a conditional code also.
- CPU- Scheduling Information: This includes information like which process having higher priority, pointers to scheduling queues, etc.
- Memory Management Information: This includes information about page tables, or segment tables etc, depending upon the memory system used by operating system.
- I/O status information: List of I/O devices, list of open files, etc.
- Accounting Information: This includes amount of CPU used, real time used, etc.
184.108.40.206 Processes and Android
When a new application starts a brand new Linux process is started by android system with a single thread called main thread. All components of same application run in same process and thread. If a process already exists then components of application are started in the same thread. This situation is by default. However new processes can be created for them.
In the manifest entry of each component element like <Service>, <Activity>, <provider> and <receiver> there is an android:process attribute. This attribute specifies the process in which the component has to run. Components of different applications can run in the same process if they fulfill following criteria:
- Same Linux User ID
- Signed with same certificate.
If there are other processes which are dealing with users immediately and memory is low or resources are in scarcity then android system may kill the process and associated components will be destroyed. If they are to be used again then a fresh process will be started. There is a life cycle of process.
Figure - Types of processes
Android system maintains processes as long as possible but sometimes they have to kill processes to recover resources. This killing procedure is decided by the importance hierarchy. Lowest priority gets out of the system first and processes with higher priority will be eliminated last. We already know about these processes. Their importance hierarchy is as shown below:
- Foreground Process
- Visible Process
- Service Process
- Background Process
- Empty Process.
We are done with section. Hope you enjoyed this section as well. See you in the next section with something new. Happy App Developing!!!