Back to Blog

OS - Resources

September 24, 2024 (1y ago)

Welcome back!! In this article, we going to see about resources in OS.

If you have landed on this article without prior knowledge of processes, I kindly suggest reviewing the previous article on processes first. We frequently reference terminology related to processes, and having a foundational understanding will help, as the topics are closely interconnected.

The OS maintains numerous resources that processes can request and release at runtime. Ex: Hardware resources include keyboards, pointing devices, printers, disk and tape drives, cameras, speakers, and many other types of devices. Software resources include input/output buffers, locks on database tables, messages, timers, and many others.

Representation of Resource

Most resources can be represented and handled uniformly using the same data structure and operations. Similar to a PCB, a resource control block (RCB) is a data structure that represents a resource. The specific implementation and the contents of the RCB vary between different OSs but the following is a generic structure that can handle many types of resources:

Requesting a resource

A resource is allocated to a process if the process has access to and is able to utilize the resource. A resource is free if the resource may be allocated to a requesting process.

When the currently running process needs a resource, the process invokes a request() function. The request resource function allocates a resource r to a process p or blocks p if r is currently allocated to another process.

Releasing a resource

When the currently running process, p, wishes to release a resource r, the process invokes a release() function. The release resource function allocates the resource r to the next process on the r's waiting list. If the waiting list is empty, r is marked as free.

How does the OS manage resources?

To monitor and control the availability and utilization of system resources like CPU, memory, and I/O devices, the OS makes use of a variety of data structures. Every kind of resource has a unique management system and set of data structures that enable the operating system to keep track of its state. Here's a brief overview:

  1. Process Management (CPU): The OS uses queues (ready and blocked) and Process Control Blocks (PCBs) to track process status and CPU allocation.
  2. Memory Management: Free lists, page tables, and memory management algorithms like the buddy system help the OS manage memory allocation and availability.
  3. I/O Device Management: Device status tables and buffers track the state of I/O devices, including their readiness and usage.
  4. File System Management: Data structures like the File Allocation Table (FAT) and open file tables manage file storage and access.
  5. Resource Allocation: Resource allocation graphs and descriptors monitor which resources are in use and help prevent conflicts or deadlocks.
  6. Network Management: Connection tables and routing tables keep track of network connections and available routes.

Most of this above data structures are in main memory for quick access. Some data structures like File Allocation Table (FAT) in secondary memory.

Surprisingly, we have covered important stuffs about resources.

Since we are now familiar with processes and resources, it's a good moment to briefly discuss schedule function.

The scheduler

The scheduler function determines which process should run next and starts the process. The scheduler function is called at the end of each of the process and resource management functions: create, destroy, request, release.

Assuming the RL is implemented as a priority list, the scheduler() function performs the following tasks:

  1. Find the highest priority process q on the RL.
  2. Perform a context switch from p to q if either of the following conditions is met:

We will see different scheduling algorithms and their purpose in future articles.

Hope you have learned something new. In the next article, we are going to see about Threads.** **Stay tuned. Happy learning.