Linux processes

Process in operating system is a program under execution. A process has its own memory space. In some operating systems, the term 'task' is used instead.

When a program starts execution, the process is created. And the process is destroyed when the program ends. A process may consist of one or more threads which execute instructions concurrently.

Process ID - pid 

Every process has a unique identifier called process id or pid. And each process also has a parent process id called ppid. Parent is the process which started the execution of given process.

List of running processes

To know which processes are running at any given time, you can use ps command. But ps will show only processes belonging to the given terminal from where you are running ps command.

ps -el

This will list all processes running. l here is for long listing, where pid, ppid, command of the program and other informations are listed. 

Given below is the screenshot of ps -el|less command 


If you really want to know the tree structure of processes, you can given pstree command.


There are many more options available for ps command. 

top command also lists the running processes dynamically in descending order of their cpu usage. It gives other informations such as memory usage, how long system has been up etc. 


Pid of my program?

You can get the pid of your program and also ppid using two library functions, called - you get it - getpid() and getppid(). You need to include the header file unistd.h for these functions.



#include<stdio.h>
#include<unistd.h>
int main()
{
 printf("my pid is %u",getpid());
 printf("my ppid is %u",getppid());
 return 0;
}

And when you run the program, you notice that the ppid of the process is the pid of the shell.

Don't expect to see this pid of your program next time you run ps -el command. Your program runs two statements and ends. So the process is no longer present. 

Can I create a new process through program? 

Yes, you can create your own process using a function called fork(). fork() creates a new process by replicating the existing process. The new process is called child process and the existing one is called parent process. 

The child process has ppid as that of  parent process. As the child is the exact duplicate of parent process, it has  a copy of all parent's variables, open file descriptor, shared memories.

fork() returns pid of the child process in parent's context and 0 in child's context. This can be used for differentiating between parent process and child process. 


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
 int a = 10;
 int b=11;
 pid_t pid;
 printf("before fork, pid is %d\n",getpid());
 pid = fork();
 if(pid==0)
 {
  printf("I am child process. My pid is %d\n",getpid());
  printf("Child saying hello");
 }
 else 
 {
  printf("I am parent process. My pid is %d\n",getpid());
  printf("parent saying hello");
 }
}

The output of the program will be

before fork, pid is 5195
I am parent process. My pid is 5195
parent saying helloI am child process. My pid is 5196
Child saying hello


In the next post we will discus s more about fork and vfork and wait functions

Comments

Popular Posts