All about Process in Linux/Unix – Developer Perspective
PROCESS:
In Linux every thing we sees executing or taking some action will be by the process. Processes carry out tasks within the operating system. For definition ” Any program in execution is called process”. A program is a set of machine code instructions and data stored in an executable area of memory.
Processes are separate tasks each with their own rights and responsibilities. Each individual process runs in its own address space and not interacting with another process except through secure, kernel managed mechanisms.
LINUX PROCESS:-
An instance of a program is called a Process. In simple terms, any command that you give to terminal on your Linux machine starts a new process. Having multiple processes for the same program is possible. A process itself not a process, until it comes info execution.
In a Linux system no process is independent of any other process. Every process in the system, except the initial process has a parent process. New processes are not created, they are copied, or cloned from previous processes.
Types of Process:-
- Foreground Process: A foreground process is any command or task you run directly and wait for it complete. They run on the screen and need input from the user.
A foreground Process begins by typing a command at prompt. For example, list of files in current directory
>ls |
- Background Processes: In Linux, a background process is nothing but process running independently. They run in the background and usually do not need user input.
For example
- Antivirus in your computer, normally it is running in background.
Basic command for Linux process:-
ps:–
The ps command is used to provide information about the currently running process, including their process identification number(PID).
The basic syntax of ps is
>ps |
When ps is used without any option, which display default information of process currently running on the system .
The four items are labeled PID,TTY,TIME,CMD
PID- Process identification number.
TTY- TTY is the name of console or terminal shows currently console.
TIME- Time is the amount of CPU, time in minutes and seconds that the process has been running.
CMD- Cmd is the name of the command that launched the process.
ps –e:-
ps –e command is used for list of all process running on the system in a single shot
>ps –e |
- –e show all process not just those belonging to user.
pstree:-
pstree is a command that shows the running processes as a tree fromat. It is used as a alternative to the ps command.
The basic syntax of the pstree is
>pstree |
pstree command is similar to the ps command, but instead of listing the running processes ,it shows in a tree format.
bg:-
Linux treats everything as file, even the processes as files under /proc directories. The processes are basically program in execution, they also called jobs
The bg command is the processes that have been stopped by some interrupt signal can be continued background with bg command
Syntax for bg command
- ./output &
fg:-
The fg command is like bg command Except that instead of sending a command in the background it runs them in foreground and occupies the current terminal and wait for process to exit.
top:-
In Linux, top is a command used to display processor activity of your linux , this command shows the information of the system and the list of processes which are currently running in the Linux
- PID:Shows task’s unique process id.
- PR: Stands for priority of the task.
- SHR:Represents the amount of shared memory used by a task.
- VIRT:Total virtual memory used by the task.
- USER:User name of owner of task.
- %CPU: Represents the CPU usage.
- TIME+:CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
- SHR:Represents the Shared Memory size (kb) used by a task.
- NI:Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
- %MEM: Shows the Memory usage of task.
Kill
kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process which terminates the process.
EXAMPLES
> kill -9 -1 Kill all processes you can kill.
> kill -l 11 Translate number 11 into a signal name.
> kill -L List the available signal choices in a nice table.
> kill 3453 Send the default signal, SIGTERM, to all those processes.
Now we will learn the Process with the developer prospective, here user can write C program using any editor wish and compile code using GCC.
Process Identification:-
In Linux, User can be running various programs in the system, each running program must be unique identified by the kernel and program id identified by its own process is called “PID”, therefore process can be further categorized into
- Parent process.
- Child process.
Parent process-Parent process is processes that create other processes during run-time.
In command prompt, Command to get the parent process identity.
Header file need to include for getpid():
- Sys/types.h
- unistd.h
getppid() |
Child process-Child process are created by the parent process by cone of it ,it will be behaves same as parent.
In command prompt, Command to get the child process identity.
Header file need to include for getppid():
- Sys/types.h
- unistd.h
getpid() |
Now our first steps is to create process, fork is the system call which is used to create process. read on and follow –
Frok():-
Fork is a way to create new processes, fork() operation create a separate processes as a copy of itself then, the copy called the “child process”.
The child process has an exact copy of the parent process. After the fork, both processes not only run the same program
The child has its own unique process ID and this PID not match the ID of any existing process
The child parent process ID is the same as the parent’s process ID.
Return value:
On success, the PID of the child is returned in the parent, and “0” is returned in the child. On failure, “-1” is returned in the parent, no child process is created and errno is set appropriately.
Header file need to include for fork:
unistd.h |
Sleep():-
In Linux, Sleep() I s causes the calling thread to sleep either until of real-time specified in seconds have to elapsed .
Steps for sleep():-
Step 1– write a source code for sleep(), as shown in below image.
Step 2– Search for PID in all current running processes, by giving command ps.
Step 3-Enter in /proc and search that Pid by using ls command.
Step 4– Enter the Pid proc type cat status for the process states. Now you should be in /proc/pidof_yourprocess. Steps shown below.
$cd /proc <┘
$ls <┘
$cd /proc/pid <┘
$cat status <┘
Now see the status of process, it will show process is sleeping.
Now check the status of process, that is sleeping.
wait system call
wait() blocks the calling process until one of its child processes exits or a signal is received. After child process terminates, parent continues its execution after wait system call instruction.
Header files for wait:
- sys/wait.h
Exec System calls family:-
- Exec family of function or system call replace current process with new process.
- There are various function used in the Exec like
- Execl
- Exclp
- Execle
- Execve
- Execvp
- Execvpe
Header file need to include is
unistd.h |
Example for execlp:-
Orphan process:-
Orphan processes are those processes that are still running even though their parent process has terminated or finished. A process can be orphaned intentionally or unintentionally.
An intentionally orphaned process runs in the background without any manual support. This is usually done to start an indefinitely running service or to complete a long-running job without user attention.
An unintentionally orphaned process is created when its parent process crashes or terminates. Unintentional orphan processes can be avoided using the process group mechanism.
Zombie process
Zombie processes usually occur for child processes, as the parent process still needs to read its child’s exit or crashes . Once this is done using the wait system call, the zombie process is eliminated from the process . This is known as the zombie process.
Professional Training Institute is the top embedded training institute in Bangalore, where student can learn the practical embedded system, we believe in hands on knowledge of subject such that student are not only getting the practical knowledge but also getting experience in embedded system development. All our students are able to get placed in top companies with the knowledge they gain in our institute. come and explore the possibilities, of amazing knowledge you can gain.