Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Chapter 3:

Processes: States and Control Blocks (the friend way)

Imagine you're juggling multiple tasks at once: cooking dinner, talking to a friend on the
phone, and keeping an eye on a movie playing in the background. Your computer
handles similar situations with processes, which are like mini-programs running at the
same time.
Process States: Just like you can be in different states while doing these tasks
(actively cooking, listening intently, or checking on the movie), processes also have
different states:
 Running: This is the active state, like when you're fully engaged in cooking. The CPU is
currently executing instructions for this process.
 Ready: The process is ready to run but patiently waiting for its turn, like when you're
waiting for the phone call to end to grab another ingredient for dinner.
 Waiting: The process is paused, waiting for something specific, like waiting for the oven
to preheat before you can bake something.
 Terminated: The process has finished its job and is no longer running, like when you've
finished cooking and cleaned up.
Process Control Block (PCB): To keep track of all these processes and their states,
your computer uses a PCB, which is like a personal information card for each process.
It holds details like:
 Process ID (PID): A unique identifier, like your social security number, but for the
process.
 State: The current state the process is in (running, ready, waiting, or terminated).
 Memory allocation: Where in memory the process's instructions and data are stored.
 CPU registers: Information saved while the process is waiting, like your place in a
recipe you're following.
 Open files: Any files the process is currently using, like the recipe you might have open
on your computer.
Process Scheduling: The Juggling Act (a friendly explanation)

Imagine you're at a circus, where the ringmaster juggles multiple balls at once. Process
scheduling in an operating system is similar, but instead of balls, it juggles processes
(running programs). Here's how it works:

Scheduling Queues: These are like holding areas for processes, categorized based on
their current state. Think of them as different lines at the circus:
 Ready Queue: Processes waiting their turn to be executed by the CPU. This is like the
line for the main juggling show.
 Device Queue: Processes waiting for a specific hardware device, like a printer or disk.
This is like the line for popcorn or cotton candy.
 I/O Completion Queue: Processes that have finished using a device and are waiting to
return to the ready queue. This is like the line to throw away your empty popcorn bag.
Schedulers: The ringmaster in this analogy is the scheduler. It decides which process
gets the CPU next, like the ringmaster deciding which ball to throw in the air next.
Schedulers use different algorithms to prioritize processes, ensuring fair and efficient
execution.
Context Switch: When the scheduler switches the CPU from one process to another,
it's like the ringmaster switching his focus between juggling balls. This involves:
 Saving the state of the current process (context) in its PCB: Like the ringmaster
remembering the position of each ball in the air, the computer stores the current state of
the running process in its PCB.
 Loading the state of the next process from its PCB: Just like the ringmaster
prepares to throw a different ball, the computer loads the information about the next
process from its PCB.
Talking Between Programs: Inter-Process Communication (IPC) Explained
Imagine you're working on a group project, but you can't directly talk to your teammates.
Instead, you have to leave notes on a whiteboard or send messages through an app.
This is similar to how inter-process communication (IPC) works on computers.
What is IPC?
IPC is the mechanism that allows different processes (running programs) on a
computer to communicate and share data with each other. Without IPC, processes
would be isolated islands, unable to collaborate or exchange information.
Two Main IPC Systems:

There are two main ways processes can communicate:

Shared Memory System:

 Think of it like a shared whiteboard where both teammates can write and read
information.
 A shared memory segment is created in the system's memory, accessible by multiple
processes.
 One process writes data to the shared memory, and another process can read it.
 Pros: Fast and efficient for large data transfers.
 Cons: Requires careful synchronization to avoid data corruption when multiple
processes access the same memory location.
Message Passing System:
 Imagine sending messages through an app in your project.
 Processes send and receive messages through channels or queues.
 Messages contain data and information to be exchanged.
 Pros: More flexible and secure than shared memory, as processes don't directly access
each other's memory.
 Cons: Can be slower than shared memory, especially for frequent communication.

Threads: The Art of Multitasking Within a Process.

Imagine you're working on a single project (a process) but want to handle multiple tasks
simultaneously within it. Threads come into play here, acting like mini-processes inside
a single process, allowing it to perform multiple tasks concurrently.
Benefits of Threads:
 Improved Performance: By dividing tasks into threads, you can potentially achieve
better performance, especially on multi-core processors. Each core can handle a
different thread, speeding up the overall process.
 Responsiveness: Threads can improve the responsiveness of an application. Even if
one thread is blocked (waiting for something), other threads can continue running,
keeping the program feeling responsive to the user.
 Resource Sharing: Threads within the same process share memory and other
resources, making data exchange and communication efficient.
Types of Threads:
 User-Level Threads: These are managed by the application itself, without involving the
operating system directly. They are simpler to create and manage but can be less
efficient and lead to performance issues. Think of them as tasks you manage within
your project using your own methods, like dividing work among team members.
 Kernel-Level Threads: These are managed by the operating system kernel, providing
better performance and control. However, they are more complex to create and
manage. Imagine having a project manager assigned by the company (OS kernel) to
help you organize and manage tasks within your project.
Multithreading Models:

These models define how user-level threads map to kernel-level threads, impacting
performance and flexibility. Here are three common models:

 Many-to-One Model: Multiple user-level threads map to a single kernel-level thread.


This is efficient for I/O-bound tasks (waiting for external data) but not for CPU-bound
tasks (intensive calculations). Imagine multiple team members working together on a
single task that requires external resources.
 One-to-One Model: Each user-level thread has its own dedicated kernel-level thread.
This provides the best performance but can be resource-intensive. Think of each team
member having their own individual assistant to help them complete their tasks
independently.
 Many-to-Many Model: This model allows for flexible mapping of user-level threads to
kernel-level threads, providing a balance between performance and resource usage.
Imagine team members being able to share resources and collaborate while also having
dedicated support when needed.
PS (process status):
 This command displays information about the currently running processes. It can
show details like the process ID (PID), username, memory usage, and CPU
usage.
 Example: ps (This will list all processes) ps aux (This will show a more detailed
listing of processes)
Wait:
 This command causes the shell script (a series of commands) to wait for a
background process to finish before continuing.
 Example: program & wait (This runs the program in the background and then
waits for it to finish before continuing)
Sleep:
 This command suspends the execution of the shell script for a specified number
of seconds. This can be useful for creating delays or pauses in your script.
 Example: sleep 5 (This will pause the script for 5 seconds)
 Exit:
 This command exits the shell script. You can optionally specify an exit code to
indicate success (0) or an error (any other number).
 Example: exit 0 (This exits the script with a success code)
Kill:
 This command is used to send a signal to a running process. Different signals
have different effects. For example, SIGTERM (usually sent with kill) politely
asks the process to terminate, while SIGKILL (usually sent with kill -9)
forcefully terminates the process.
Example: kill 1234 (This sends the default signal, SIGTERM, to the process with
PID 1234) kill -9 1234 (This forcefully terminates the process with PID 1234)

You might also like