OS_CO3,4,
OS_CO3,4,
Concurrency
Key Points:
Context Switching: The process of storing the state of a process or thread and
restoring the state of another, allowing multiple processes to share a single CPU.
Synchronization: Mechanisms to control the access to shared resources by
multiple threads to prevent data races and ensure data consistency. Common
synchronization primitives include locks, semaphores, and monitors.
Threads
Threads are the smallest units of execution within a process. A single process can
contain multiple threads, all of which share the same memory space but can execute
independently. This makes threads a powerful tool for achieving concurrency.
Threads API: The POSIX threads (pthreads) library in C provides a set of functions to
create and manage threads. Key functions include:
Concurrency Problems
Race Conditions: • Occur when multiple threads access and modify shared data
concurrently.
• Example: Two threads incrementing the same global counter without proper
synchronization can lead to incorrect final values.
Deadlocks:
• Occur when two or more threads are waiting indefinitely for each other to
release resources.
• Example: Thread A holds a lock on resource X and waits for resource Y, while
Thread B holds a lock on resource Y and waits for resource X.
Resource Starvation:
Locked Data Structures: Locked data structures are designed to be accessed safely by
multiple threads concurrently. They achieve this by using synchronization
mechanisms like mutexes to ensure that only one thread can modify the data at a time,
preventing race conditions and data corruption.
Condition Variables: Condition variables are synchronization primitives that allo+w threads
to wait for certain conditions to be true. They are typically used in conjunction with a mutex
to coordinate the execution of threads.
• Usage:
o Wait: A thread can wait for a condition to be met using pthread_cond_wait.
This function atomically releases the associated mutex and puts the thread to
sleep until the condition is signaled. o Signal: Another thread can
signal that the condition has been met using
pthread_cond_signal or pthread_cond_broadcast.
pthread_cond_signal wakes up one waiting thread, while
pthread_cond_broadcast wakes up all waiting threads.
• Key Functions:
o pthread_cond_wait: Waitsfor a condition to be signaled.
o pthread_cond_signal: Signals one waiting thread. o
pthread_cond_broadcast: Signals all waiting threads.
Os_co-3&co-4 ~nikhil
• Usage:
o Lock: A thread acquires the mutex before entering the critical section using
pthread_mutex_lock. o Unlock: The thread releases the mutex after
exiting the critical section using
pthread_mutex_unlock.
• Redundant Disk Arrays (RAID)
• RAID (Redundant Array of Independent Disks) is a technology that combines
multiple physical disk drives into a single logical unit for data redundancy or
performance improvement.
Disk scheduling algorithms: https://www.geeksforgeeks.org/disk-
scheduling-algorithms/
Real-Time Systems
Real-time systems are designed to respond to inputs or events within a specific time frame.
They are classified into:
• Priority-Based Scheduling: Tasks are assigned priorities, and the scheduler ensures
high-priority tasks are executed first.
• Deadline-Based Scheduling: Tasks are scheduled based on their deadlines, ensuring
they complete within specified time constraints.
Protection Mechanisms:
• Access Control: Ensures that only authorized tasks or users can access specific
resources or perform certain actions.
• Memory Protection: Prevents tasks from accessing or modifying each other's
memory, ensuring system stability and integrity.
#include <stdio.h>
<unistd.h> void*
realTimeTask(void* arg) {
return NULL;
pthread_join(rtThread, NULL);
return 0;
Os_co-3&co-4 ~nikhil
}
Os_co-3&co-4 ~nikhil
**IMP
Os_co-3&co-4 ~nikhil
Os_co-3&co-4 ~nikhil
Os_co-3&co-4 ~nikhil
Other resources:
https://www.geeksforgeeks.org/disk-scheduling-algorithms/
practice all disk scheduling algorithms