3.9 Android Threads and processes: Page 2 of 2

3.9.2 Processes

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:

Structure of a process

                                                                 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.

3.9.2.1 Process States

We have different process states. Current activity of process determines its state. It may be in any one of the following states:

  1. New: The process is created.
  2. Running: Instructions are executed at this stage.
  3. Waiting: Process is waiting for some event to occur.
  4. Ready: Process is waiting to be assigned to a processor.
  5. Terminated: Execution is finished.

Process States

                                                                                              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.

3.9.2.2 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

                                                                                 Figure - Process Control Block

Let us have a glance on the pieces of information contained in PCB and they are as follows:

  1. Process State: A process state may be new, ready, waiting, etc.
  2. Program Counter: It contains next executable instruction in this process.
  3. 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.
  4. CPU- Scheduling Information: This includes information like which process having higher priority, pointers to scheduling queues, etc.
  5. Memory Management Information: This includes information about page tables, or segment tables etc, depending upon the memory system used by operating system.
  6. I/O status information: List of I/O devices, list of open files, etc.
  7. Accounting Information: This includes amount of CPU used, real time used, etc.

3.9.2.3 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:

  1. Same Linux User ID
  2. 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

                                                                                         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:

  1. Foreground Process
  2. Visible Process
  3. Service Process
  4. Background Process
  5. 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!!!