What is a Process?
Linux is a multiuser (multiple users can login to Linux and share its resources) and multitasking operating system. It means you can run multiple commands and carry out multiple tasks at a time.
More about Process
For example, you can write a c program, while listening mp3s or download files using P2P in background.
- A process is program (or command typed by user) to perform specific Job.
- In Linux when you start a process, it is given a unique number called a PID or process-id.
- PIDs start from 0 to 65535.
- PID 1 is always assigned to init process, which is the first process started at boot time. Type the following command to verify that init always has PID 1:
ps -C init -o pid=,cmd
CMD 1 /sbin/init
Parent and Child Processes
- A parent process is a Linux process that has created one or more child processes.
- A process can fork a child i.e create a child process.
- For example, if a user types the ls command at a shell prompt.
- The shell executes ls command.
- The Linux kernel will duplicate the shell's pages of memory and then execute the ls command.
- In UNIX, every process is created using fork and exec method. However, this model results in a waste of system resources.
- Under Linux, the fork method is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.
- The copy-on-write model avoids creation of unnecessary copies of structures when creating new processes.
- For example, user types ls command at a shell prompt.
- The Linux kernel will fork and create a subprocess or child process of bash.
- In this example, bash is parent and ls command is child. In other words, the ls command is pointed to the same pages of memory as the bash shell.
- Then, the child execs the ls command using copy-on-write.
Every Linux process has its own life cycle such as creation, execution, termination, and removal.Every process has its own state that shows what is currently happening in the process. The status of the process which can be one of the following:
- D (uninterruptible sleep) - Process is sleeping and cannot be bring back until an event such as I/O occurred. For example, process foo is a process waiting for keyboard interrupt.
- R (running) - Process is running or executing.
- S (sleeping) - Process is not running and is waiting for an event or a signal.
- T (traced or stopped) - Process is stopped by signals such as SIGINT or SIGSTOP.
- Z (zombie or defunct) - Processes marked <defunct> are dead processes (so-called "zombies") that remain because their parent has not destroyed them properly. These processes will be destroyed by init if the parent process exits.
How do I view Process states
To view states of a process, enter:
ps -C processName -o pid=,cmd,stat
For example, to display states of lighttpd, php-cgi and firefox-bin processes, enter:
ps -C firefox-bin -o pid=,cmd,stat ps -C lighttpd -o pid=,cmd,stat ps -C php-cgi -o pid=,cmd,stat
CMD STAT 7633 /opt/firefox/firefox-bin Sl CMD STAT 32082 /usr/sbin/lighttpd -f /etc/ S 32326 /usr/sbin/lighttpd -f /etc/ S CMD STAT 1644 /usr/bin/php-cgi S 31331 /usr/bin/php-cgi S 31332 /usr/bin/php-cgi S 31538 /usr/bin/php-cgi S
- ↑ Chapter 3: Process - Understanding the Linux Kernel, Third Edition, O'Reilly Media.