0% found this document useful (0 votes)
3 views

OS_CO3,4,

The document discusses concurrency, threads, and real-time systems, emphasizing the importance of synchronization mechanisms like mutexes and condition variables to prevent issues such as race conditions and deadlocks. It also covers data integrity protection methods and the implementation of real-time operating systems, including scheduling strategies and protection mechanisms. Additionally, it provides examples of thread management using the POSIX threads library in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OS_CO3,4,

The document discusses concurrency, threads, and real-time systems, emphasizing the importance of synchronization mechanisms like mutexes and condition variables to prevent issues such as race conditions and deadlocks. It also covers data integrity protection methods and the implementation of real-time operating systems, including scheduling strategies and protection mechanisms. Additionally, it provides examples of thread management using the POSIX threads library in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Os_co-3&co-4 ~nikhil

Concurrency

Concurrency refers to the execution of multiple tasks or processes simultaneously. In


a concurrent system, tasks are interleaved, which means they progress simultaneously
but not necessarily at the same time. Concurrency is essential for improving the
efficiency and responsiveness of applications, especially those that involve I/O
operations or need to handle multiple user requests simultaneously.

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.

Multithreading: The ability of a CPU, or a single core in a multi-core processor, to


execute multiple threads concurrently. This can lead to better utilization of resources
and improved application performance.

Threads API: The POSIX threads (pthreads) library in C provides a set of functions to
create and manage threads. Key functions include:

• pthread_create:Creates a new thread.


• pthread_join: Waits for a thread to terminate.
• pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock,

pthread_mutex_destroy: Functions to work with mutexes. Common

Concurrency Problems

Race Conditions: • Occur when multiple threads access and modify shared data

concurrently.

• The outcome depends on the timing of the threads' execution, leading to


unpredictable results.
Os_co-3&co-4 ~nikhil

• 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:

• Occurs when a thread is perpetually denied access to resources because other


threads are constantly using them.
• Example: A low-priority thread never gets CPU time because higher-priority
threads keep occupying the CPU.

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.

• Examples of Locked Data Structures:


o Thread-Safe Queue: A queue that uses a mutex to protect its enqueue
and dequeue operations. This ensures that only one thread can modify
the queue at a time.

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

Mutex (Mutual Exclusion): A mutex is a synchronization primitive that ensures mutual


exclusion. It allows only one thread to execute a critical section of code at a time, preventing
race conditions.

• 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/

Data Integrity Protection


Data integrity ensures that data is accurate and consistent over its lifecycle. Key mechanisms
include:

• Checksums: Simple error-detection schemes that use algorithms to create a unique


value based on data. When data is read or transmitted, the checksum is recalculated
and compared to detect errors.
• Error Detection Codes (EDC): Techniques like parity bits, Hamming codes, and
cyclic redundancy checks (CRC) used to detect and correct errors in data.
• Validation Processes: Procedures to ensure data meets predefined criteria. Examples
include format validation (e.g., checking if an email address is valid) and consistency
checks (e.g., verifying that related data fields align).

Real-Time Systems
Real-time systems are designed to respond to inputs or events within a specific time frame.
They are classified into:

• Hard Real-Time Systems: Missing a deadline can cause catastrophic failure.


Examples include pacemakers and automotive airbag systems.
• Soft Real-Time Systems: Missing a deadline results in degraded performance but not
catastrophic failure. Examples include video streaming and online gaming.
Os_co-3&co-4 ~nikhil

Real-Time Kernels: Specialized operating systems or kernel extensions that provide


deterministic scheduling and low-latency features required for real-time applications.

Implementing Real-Time Operating Systems


Implementing a real-time operating system involves:

Real-Time CPU Scheduling:

• 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.

Example: Real-Time Task Scheduling in C:

#include <stdio.h>

#include <pthread.h> #include

<unistd.h> void*

realTimeTask(void* arg) {

while (1) { printf("Real-time task executing...\n");

usleep(1000000); // Simulate task execution time (1 second)

return NULL;

} int main() { pthread_t rtThread;

pthread_create(&rtThread, NULL, realTimeTask, 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

CO-3 IMPORTANT QUESTIONS


Os_co-3&co-4 ~nikhil

CO-4 IMP QUESTIONS

Other resources:

https://www.geeksforgeeks.org/disk-scheduling-algorithms/
practice all disk scheduling algorithms

You might also like