LINUX OPERATING
SYSTEMS
Nithya A B
[Link] Computer Applications
Bvoc Software Development
[Link]’s College Ernakulam
Process in Linux
• A process is a program in execution. It's an active entity, unlike a program
which is a passive set of instructions stored on disk.
• When you run a command or start an application, the system creates a process to
carry out that task.
• When a program or command is launched, the Linux kernel creates a new process
to manage it, allocating resources such as CPU time, memory, and file
descriptors.
• Each process has a unique identifier known as its process ID (PID).
Key Components of a Process
Each process contains:
Component Description
PID (Process ID) Unique identifier for the process
PPID (Parent PID) The process that created this one
Program Code The instructions the process executes
Memory Code, stack and data segments
Registers and Stack CPU state and function call data
Environment Variables and settings used during execution
Process types
• Foreground processes (interactive): these processes are launched and controlled
through the command line in a terminal session and require user input.
• Background processes (non-interactive): these processes run continuously in the
background, providing system-level services and often waiting for specific events or
tasks, without requiring user input or a controlling terminal.
Life Cycle of a Process in Linux
The process life cycle refers to the various stages a process goes through from creation
to termination:
1. New (Created)
• The process is created using fork() or other system calls.
• It is assigned a PID and resources.
2. Ready
• The process is ready to run and waiting in the ready queue for CPU allocation.
3. Running
• The scheduler selects the process, and it is executed on the CPU.
4. Waiting (Blocked)
• If the process needs to wait for input/output or a resource, it enters the waiting state.
5. Terminated (Exit)
• The process completes or is killed, and its resources are released.
• A zombie process may appear temporarily until the parent reads the exit status.
Process States
• A process can be in one of the following states:
State Description
R (Running) Executing or ready to run
S (Sleeping) Waiting for an event
D (Uninterruptible Sleep) Waiting for I/O
Z (Zombie) Finished but parent not collected the exit status
T (Stopped) Suspended (e.g., Ctrl+Z)
Connecting Process With Pipes
• In Linux, pipes are a fundamental mechanism for inter-process communication
(IPC) that allows the output of one process to be used as the input for another
process.
• This creates a pipeline, where data flows sequentially through a chain of commands
or programs.
• The pipe is used to combine two or more commands, and in this, the output of
one command acts as input to another command, and this command's output
may act as input to the next command, and so on.
• It can also be visualized as a temporary connection between two or more commands/
programs/ processes.
• This direct connection between commands/ programs/ processes allows them to
operate simultaneously and permits data to be transferred between them continuously
rather than having to pass it through temporary text files or through the display
screen.
• Pipes are unidirectional i.e., Data flows from left to right through the pipeline.
Syntax:
command_1 | command_2 | command_3 | .... | command_N
Input Output Redirection In Linux
• In Linux, whenever an individual runs a command, it can take input, give output, or do
both. Redirection helps us redirect these input and output functionalities to the files or
folders we want, and we can use special commands or characters to do so.
• For example, if we run the "date" command, it gives us output on the screen. However,
if we want to save the result in a file instead, we can use output redirection.
• This way, we can store the output of the date command in a file and refer to it later.
• These redirections can come in handy when we work with multiple and large outputs
or inputs since we can use file data directly as input and store results in files.
Types of redirection
1. Overwrite redirection:
• Overwrite redirection is useful when you want to store/save the output of a command to a file and
replace all the existing content of that file.
• For example, if you run a command that gives a report, and you want to save the report to the existing
file of the previous report you can use overwrite redirection to do this.
• ">" Standard output
• "<" Standard input
2. Append redirection:
• With the help of this redirection, you can append the output to the file without compromising the existing
data of the file.
• ">>" Standard output
• "<<" Standard input
3. Merge redirection:
• This allows you to redirect the output of a command or a program to a specific file descriptor instead of
standard output. The syntax for using this is ">&" operator followed by the file descriptor number.
• "P >& q" merges output from stream p with stream q
• "P <& q" merges input from stream p with stream q
Background process in Linux
• In Linux, a background process is a process that runs independently of the shell,
allowing the terminal to be used for other commands.
• It's useful for long-running tasks such as downloading files, compiling programs, or
running scripts.
How to run a process in the background
You can run a command in the background by adding & at the end:
Syntax
Command &
Example
Sleep 60 &
This runs the sleep command in the background for 60 seconds.
Checking background processes
• jobs – lists the jobs running in the current terminal session:
• ps – shows all running processes:
• top or htop – live monitoring of running processes.
Process Scheduling in Linux
• Process scheduling is a core function of the linux operating system, responsible for
determining which process gets access to the CPU and for how long.
• This mechanism enables multitasking by giving the illusion that multiple processes are
running simultaneously, even though a cpu can only execute one task at a time
Types of process scheduling
1. Preemptive scheduling
• In preemptive scheduling, the operating system can forcibly remove a process from
the CPU before it finishes its execution in order to give another process a turn.
• This ensures fairness and responsiveness, especially in multi-user or real-time
systems.
Key features:
• The CPU can be taken from a running process.
• Supports time-sharing systems.
• Improves response time for high-priority or interactive tasks.
Example in Linux:
• Two processes are running. If a new process with higher priority becomes
ready, the currently running process is preempted, and the new process is
scheduled.
2. Non-preemptive scheduling
• In non-preemptive scheduling, once a process is given the CPU, it runs to
completion or until it blocks itself (e.g., For I/O). The CPU cannot be taken away
forcefully.
• This leads to simpler design but poorer responsiveness.
Key features:
• A process runs until it voluntarily yields CPU (e.g., Finishes or waits for I/O).
• No process interruption by the OS.
Batch Command In Linux
• The batch command is used to schedule a process to run once, when the system
load is low.
• It is part of the at command family (at, batch, atq, atrm), and is used to execute
commands in the background when the system is not busy.
• Purpose of batch To defer non-urgent tasks (e.g., backups, file copies, compiles)
until the system has free CPU.
Syntax
batch
After typing batch, you enter a prompt where you can type commands to schedule,
then press Ctrl+D to save and exit.
Command Description
batch Schedules job when load is low
atq Lists scheduled jobs
atrm <job_id> Removes a scheduled job
Ctrl + D Ends input in interactive mode
kill, ps , who commands
In Linux, kill, ps, and who are fundamental commands used for process management
and user information. They are often used in combination to achieve specific tasks.
1. ps (process status)
• The ps command is a powerful utility that provides information about running
processes on your system.
• It gives you a snapshot of processes at the time the command is run, including
their Process ID (PID), User ID (UID), CPU usage, memory usage, and the command
that started the process.
• By default, it shows processes associated with the current terminal.
Examples
• ps: Lists processes associated with the current shell.
• ps aux: Displays all running processes on the system, providing detailed information
about resource usage, process IDs, and more.
• ps -ef: Displays a full listing of processes with additional information, including the
user ID, parent process ID, CPU usage, and start time.
• ps -u <username>: Lists processes owned by a specific user.
• ps -eo pid,args: Displays the PID and arguments of all processes, useful for finding
processes based on their arguments.
2. kill
• The kill command is used to terminate or modify the behavior of processes by
sending signals to them.
• By default, it sends the SIGTERM signal (signal number 15), requesting the
process to terminate gracefully.
Examples:
➢ kill <PID>: Sends the SIGTERM signal to the process with the specified PID.
➢ kill -9 <PID> or kill -SIGKILL <PID>: Sends the SIGKILL signal (signal
number 9), which immediately terminates the process without allowing it to
perform cleanup routines.
➢ kill -1 <PID> or kill -SIGHUP <PID>: Sends the SIGHUP signal (signal number
1), often used to instruct processes to reload their configuration files.
➢ kill -STOP <PID> or kill -SIGSTOP <PID>: Pauses a process.
➢ kill -CONT <PID> or kill -SIGCONT <PID>: Continues a paused process.
3. who
• The who command displays information about users currently logged into the
system.
• It shows details such as the username, terminal, login time, and the remote
host from which the user is logged in.
Examples:
➢ who: Lists the users currently logged in.
➢ who -a: Displays all available information, including the idle time and the process ID
of the user's shell.
➢ who -H: Displays a header line, making the output easier to read.
➢who am i: Displays information about the current user.