Process and images are the two important terms that describe the environment and the execution of commands in Linux.
What is an image?
The image is an environment for the execution which includes a core image, general register values, status of open files, current directory, and the like. Therefore, it depicts the entire picture of the state of the Linux kernel. The components of the image in Linux are:
What is a process?
The process is the execution of an image. Thus, it is important for the image to be present in the core during the execution of the process.
There are 3 segments of the user-core part of an image:
1. Program text segment
2. Data segment
3. Stack segment
All processes share a single copy of the program text segment, during the execution of an image. Data segment begins in the virtual address space at the top of the text segment. It is a non-shared writable segment whose size can be extended by a system call. The Stack segment begins at the highest address in the virtual address space which automatically increases its length as the stack pointer fluctuates.
Processes can be executed by placing a fork call as follows:
processid = fork(label)
Fork call splits into two independent processes which are commonly called, parent process and child process. Each process resides on different memory space and has a separate copy of the original core image. In the parent process, the control returns directly from the fork, whereas in the child process, the control is passed to a location label. The process id which is returned by the fork call is the identification of the other processes.
Process in Linux: Communication
Kernel coordinates the activities of the process in Linux and is responsible for the communication between processes. Linux supports various Inter-Process Communication(IPC) mechanisms. For example:
- Shared files
- Shared memory
- Pipes
- Signals
Lets learn about these Inter-Process communication mechanisms:
1. Shared Files
They are the most basic type of sharing mechanisms offered by Linux. It comprises a producer that creates and writes to a file and a consumer that consumes or reads from that file. Shared Files is not a safe mechanism to use as the problem of race-conditioning might arise. Therefore, to avoid this problem, it can be secured by applying locks such that prevent the conflict between read and write.
2. Shared Memory
Shared memory is an extra piece of memory. It is attached to some address spaces for their owners to use. Consequently, that is how the process, sharing the same memory segment, can access it. It can too result in the race-condition if not handled properly.
3. Pipes
A pipe is used for Inter-process communication. The call
filep = pipe()
return a file descriptor filep and create an interprocess channel called a pipe. It is passed from parent to child process in the image by a fork call. Pipe falls under the category of one-way communication. A read using a pipe file descriptor waits for the other process to complete the write. There are two types of pipes in Linux:
(a) Ordinary Pipes or Unnamed Pipes : They are unidirectional and used for the communication between two processes, most commonly between a parent and a child process.
(b) Named Pipes : They can be used as the bidirectional pipes as they can be written by several writers. As a result, it provides two-way communication between two unrelated processes.
4. Signals
A signal for a process in Linux is similar to an interrupt used for communication. Therefore, it notifies the occurrence of an event to the processes. Signals can be specified with a number or a name.
The following system calls and library function allow the caller to send a signal:
Raise(3) | Sends a signal to the calling thread. |
Kill(2) | Sends a signal to a specified process, to all member of a specified process group, or to all processes on the system. |
Killpg(3) | Sends a signal to all of the members of a specified process group. |
pthread_kill(3) | Sends a signal to a specified POSIX thread in the same process as the caller. |
tgkill(2) | Sends a signal to a specified thread within a specific process. (This is the system call used to implement pthread_kill(3).) |
sigqueue(3) | Sends a real-time signal with accompanying data to a specified process. |
Execution of Programs
The execution of a program is invoked by
execute(file, arg1, arg2, ..., argn)
This system call will request the system to read in and execute the program named by the file along with its arguments. All the code and data in the process using execution are replaced from the file, but open files, current directory, and interprocess relationships are unaltered. For instance, if the call fails because a file could not be found or because its execute-permission bit was not set, does a return take place from the execute primitive; it resembles a “jump” machine instruction rather than a subroutine call.
Process in Linux: Synchronization
It involves a time slice for each process in Linux so that each process gets the required time for its execution. Fork() creates a child process that is a duplicate of the parent process but has a different PID. When a parent process is terminated before the termination of the child process, we call the child process as an orphaned process.
Wait() command is used to provide the synchronization between the processes. As a result, Linux detects the deadlock between the processes and sorts it out.
Process in Linux: Termination
It is the last condition for the process, which is called by
exit(status)
In conclusion, this deletes a process, destroys its image, and closes its open files. Termination of a child process is notified to the parent processes as well.