Process in linux


Advertisements

In this article we will create, monitor different types of linux processes and illustrate IPC process communication applications using practical examples, code snippets.



Learn linux process, creating a process, states of linux process, zombie process, creating process using fork system call, getting process ID(PID) and group info using system calls.

What is a linux process?


A process is an active program i.e. a program that is under execution.

  • Process is more than the program code as it includes the program counter, process stack, registers, program code etc. Compared to this, the program code is only the text section.
  • A process is a program or a command executed by user to perform specific task. Hence task and process are terms that Linux uses interchangeably
  • Each process in linux is given a unique process ID called a PID.
  • Linux is a multitasking system which means several processes/tasks run at the same time
  • Processes communicate between then using Inter process communication methods such as signals, pipes, semaphores and also the System V IPC mechanisms of shared memory, semaphores and message queues.
  • Along with normal processes, Linux supports real time processes. Real time processes have to react very quickly to external events (switch, buttons etc...) and they are treated differently from normal user processes by the scheduler.
  • Each process in linux is represented by a task_struct data structure

  • Creating a Linux process

    Let's write the following small program square.c and create an executable SQUARE for it.

    
    #include "stdio.h"
    void main()
    {
            int num;
            start:
            printf("Enter a number\n");
            scanf("%d",&num);
            while(num < 10)
            {
                    printf("Square of your number:%d\n",num * num);
                    goto start;
            }
    }
    
    
    Output:
    $ gcc square.c -o SQUARE
    $ ./SQUARE
    Enter a number
    3
    Square of your number:9
    Enter a number
    5
    Square of your number:25
    Enter a number
    

    Let's open another console and execute ps -aux command to observe the process we have created.

    $ ps -aux
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.6  26936  5940 ?        Ss   01:46   0:02 /sbin/init splash
    pi        2063  0.0  0.0   1812   328 pts/0    S+   05:09   0:00 ./SQUARE
    pi        2115  0.0  0.3   7736  2868 pts/1    R+   05:13   0:00 ps -aux
    $ 
    

    When we executed the ps -aux command we got all processes information detail. I have intentionally removed the other processes and kept few lines for your observation

  • Observation I: Init is the first process created in linux startup that is given PID as 1 and root is the user for it.
  • Observation II: Our program executable SQUARE is also now a process and we see it is running with pi as the user.
  • Observation III: The ps -aux command we executed to observe the running processes in Linux is also treated as another process and pi is the user for it.

  • States of process, Zombie process

    As a process executes it changes state according to its circumstances. Linux processes have the following states:

  • Created When a process is created, the initial memory is allocated, stack is set up, and the PCB is fully initialized, the process is then ready to run and is moved to the ready state and placed on the run queue.
  • Running in user mode Running in user mode is the normal execution of a program: the CPU is executing the instructions that constitute your program: the compiled code and the various libraries that make up the program.
  • Waiting The process is waiting for an event or for a resource. Linux differentiates between two types of waiting process i.e. interruptible and uninterruptible. Interruptible waiting processes can be interrupted by signals whereas uninterruptible waiting processes are waiting directly on hardware conditions and cannot be interrupted under any circumstances.
  • Stopped The process has been stopped, usually by receiving a signal. A process that is being debugged can be in a stopped state.
  • Zombie When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in the task_struct data structure in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls 'wait()'; In the interval between the child terminating and the parent calling 'wait()', the child is said to be a 'zombie' (If you do 'ps', the child will have a 'Z' in its status field to indicate this.)
    Zombie : The process is dead but have not been removed from the process table.
  • Ready to run When it is time for another process to run, the next scheduled process is taken off the run queue. If it is time for a preempted process to run again, the kernel completes the return from the interrupt that caused it to preempt the process.

  • course thumb
    Linux process states and transitions.

    Creating a process using fork system call

    The fork system call clones a process into two processes running the same code. Fork returns a value of 0 to the child and a value of the process ID number (pid) to the parent. A value of –1 is returned on failure.

    
    #include "stdio.h"
    #include "stdlib.h"
    void main(int argc, char **argv) {
    	switch (fork()) {
    	case 0:
    		printf("child: pid=%d\n", getpid());
    		break;
    	default:
    		printf("parent: pid=%d\n", getpid());
    		break;
    	case -1:
    		perror("fork");
    	}
    }
    
    

    Getting process ID(PID) and group in a program

    A process can find its process ID with the getpid system call. It can find its process group number with the getpgrp system call, and it can find its parent’s process ID with getppid. This is shown in the following program

    
    #include "stdio.h"
    #include "stdlib.h"
    void main() {
    	printf("my pid is %d\n", getpid());
    	printf("my parent’s pid is %d\n", getppid());
    	printf("I’m in group %d\n", getpgrp());
    }
    
    

    Want to contribute or ask a new article? Write and upload your article info .

    Share

     Articles

     Linux Software

     C Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

    Glass Elite Apple iPhone 11 Pro (Case Friendly)

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software