Unix Linux Programing

 Introduction of process :

A Process is a concept by which we can understand and control the execution of a program in an operating system. A process is also defined as a running instance of a program.

Each process in linux system is identified by a unique process identification number, also called as PID which is allcated to it by the kernel .

A Process Is Represented by the Type.

  Process = (Process ID , Code , Data , Register value , PC value)

  • PID ->  Every Process has Unique Process ID That distinguishes it from other process in the system.
  • Code-> Code is the data used by the program. Code that is under execution.
  • Data-> Data is the data used by the program code during execution. data may be variables, filed and so on.
  • Register Value-> Register value are the value shared in CPU register when the calculation work is perform during program execution.
  • PC value-> Adderss of the program counter from where the execution of the program starts or continues maximum value of PID thet can be assigned is 32767

Perent And Child Process

1.Creation of Child Processes:
  • The fork() system call is used to create a child process from a parent process.
  • When fork() is called, the operating system creates a new process by duplicating the existing parent
  •  process.
  • The child process receives a copy of the parent's memory, including variables, file descriptors, and program code.
  • The child process has a unique process identifier (PID), different from the parent's PID.
  • The child process starts execution from the point where the fork() call was made.
2.Differences between Parent and Child Processes:
  • After the fork() call, the return value differs in the parent and child processes. The return value in the parent process is the child's PID, while in the child process, it is 0.
  • The child process has its own copy of the variables inherited from the parent. Changes made to variables in the child process do not affect the parent process.
3.Communication between Parent and Child Processes:
  • Parent and child processes can communicate with each other using various inter-process communication (IPC) mechanisms.
  • Some commonly used IPC mechanisms include pipes, shared memory, message queues, sockets, signals, and semaphores.
  • Pipes: A pipe is a unidirectional communication channel that allows the parent and child processes to exchange data. It has a read end and a write end. Data written by one process can be read by the other process.
  • Shared Memory: Shared memory allows multiple processes to access the same memory region. Changes made by one process are visible to others. It requires proper synchronization mechanisms to prevent conflicts.
  • Message Queues: Message queues enable processes to exchange messages through a common queue. Each message has a type, and processes can send or receive messages based on their types.
  • Sockets: Sockets provide bidirectional communication between processes over a network or locally. They are commonly used for client-server communication.
  • Signals: Signals are used to notify processes of specific events or requests. They can be used to interrupt or terminate a process, among other actions.
  • Semaphores: Semaphores are used for process synchronization and coordination by allowing or blocking access to shared resources.
Univercity Exam Question:
 Describe the concept of parent and child processes in Linux programming. Explain how they are created and how they communicate with each other. Provide examples to illustrate your explanation.
ANS:
In Linux programming, processes can be categorized as parent and child processes. A parent process is the process that creates another process, known as the child process. The parent process is typically an already running program, while the child process is a new instance of that program or a different program altogether.

When a parent process creates a child process, the child process inherits various attributes from the parent, such as the environment variables, open file descriptors, and the program code. However, the child process has its own process identifier (PID) and memory space. The creation of a child process is typically done using the fork() system call. The fork() call creates an exact copy of the parent process, including all its resources. After the fork() call, both the parent and child processes start executing from the next instruction after the fork() statement. However, the fork() call returns different values to the parent and child processes. In the parent process, it returns the child's PID, while in the child process, it returns 0.
Communication between the parent and child processes can be established using various inter-process communication (IPC) mechanisms. Here are a few commonly used methods: Pipes: A pipe is a unidirectional data channel that allows communication between a parent and child process. One process writes to the pipe, and the other process reads from it. Pipes can be created using the pipe() system call. Example: #include <unistd.h> int main() { int pipefd[2]; char buffer[256]; pipe(pipefd); pid_t pid = fork(); if (pid == 0) { // Child process close(pipefd[1]); // Close the write end of the pipe read(pipefd[0], buffer, sizeof(buffer)); // Process data read from the pipe close(pipefd[0]); // Close the read end of the pipe } else { // Parent process close(pipefd[0]); // Close the read end of the pipe write(pipefd[1], "Hello, child!", 13); // Send data to the child process via the pipe close(pipefd[1]); // Close the write end of the pipe } return 0; }

Types of Process:

1.Interactive Processes:
  • Interactive processes are initiated by users and require direct interaction through input/output (I/O) devices, such as keyboards and displays.
  • These processes are designed to respond to user actions in real-time and often have a user interface for interaction.
  • Examples include command-line programs, graphical user interfaces (GUI), and applications that run in the foreground.
2.Batch Processes:
  • Batch processes are executed without any user interaction or in the background.
  • They typically perform tasks that require little or no user input, such as running scheduled jobs, processing large amounts of data, or performing system maintenance tasks.
  • Batch processes are often queued and executed sequentially.
3.Daemons:
  • Daemons are background processes that run continuously, typically providing services or performing system tasks.
  • They are usually initiated during system startup and continue running until the system shuts down.
  • Daemons often listen for specific events or requests, such as network requests or hardware events, and respond accordingly.
  • Examples include web servers (e.g., Apache), database servers (e.g., MySQL), and printing services.

Also Read 👇

 Starting And Stopping Process


Comments

Post a Comment