
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.
- If r is currently free, the state of r is changed to allocated and a pointer to r is inserted into the list of other_resources of p.
- If r is not free, the calling process p is blocked. p's PCB is moved from the RL to the waiting_list of r. Since the calling process, p, is now blocked, the scheduler must be called to select another process to run.
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.
- If r's waiting_list contains no processes then the state of r is changed to free and p continues executing.
- If the waiting list of r is not empty then the process q at the head of the list is allocated r, the state of q is changed to ready, and q is moved from the waiting_list to RL. Since a new process (q) is now on RL, the scheduler must be called to decide which process (p or q) should continue to run.
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:
- Process Management (CPU): The OS uses queues (ready and blocked) and Process Control Blocks (PCBs) to track process status and CPU allocation.
- Memory Management: Free lists, page tables, and memory management algorithms like the buddy system help the OS manage memory allocation and availability.
- I/O Device Management: Device status tables and buffers track the state of I/O devices, including their readiness and usage.
- File System Management: Data structures like the File Allocation Table (FAT) and open file tables manage file storage and access.
- Resource Allocation: Resource allocation graphs and descriptors monitor which resources are in use and help prevent conflicts or deadlocks.
- 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:
- Find the highest priority process q on the RL.
- Perform a context switch from p to q if either of the following conditions is met:
- The priority of the running process p is less than the priority of q. This condition can be true when scheduler() is called from create() or from release(). In both cases the process q could have a higher priority than p.
- The state of the running process p is blocked. This condition is true when scheduler() is called from request() and the resource is not available. The request function changes the status of p to blocked, but the process continues executing the request and scheduler functions until the context switch is performed at the end.
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.