Parllel Prgramming Systems Dr. Shahid Naseem Assistant Professor (IT) Division of Science & Technology University of Education, Township Campus, Lahore [email protected] Superior University, Gold Campus
P threads Superior University, Gold Campus In P threads programs, global variables are shared by all the threads. Local variables and function arguments—that is, variables declared in functions—are (ordinarily) private to the thread executing the function.
P threads Superior University, Gold Campus
P threads Superior University, Gold Campus
P threads Superior University, Gold Campus Pthreads contain support for Creating parallelism and synchronization No explicit support for communication, because shared memory is implicit; a pointer to shared data is passed to a thread
P threads Superior University, Gold Campus
P threads Superior University, Gold Campus
Semaphores Superior University, Gold Campus A semaphore in shared memory is a synchronization mechanism used in concurrent programming to control access to a common resource by multiple processes or threads. It is essentially an integer that indicates the number of available resources. The primary operations for semaphores are typically "wait" (decrease the value) and "signal" (increase the value), which must be performed atomically to prevent race conditions.
Semaphores Superior University, Gold Campus Semaphores help in process synchronization by ensuring that only a limited number of processes can access a resource at the same time. It's important to ensure that the semaphore's value never falls below zero, which could lead to system errors or deadlocks.
Types of Semaphores Superior University, Gold Campus There are primarily two types of semaphores used in concurrent programming: Binary Semaphore ( Mutex ): A binary semaphore can take only two values: 0 and 1. It is used to implement mutual exclusion, ensuring that only one process or thread can access a critical section of code at any one time. It is often referred to as a mutex (mutual exclusion) lock. When a thread locks the mutex , it sets the value to 0, and when it releases the mutex , it sets it back to 1.
Types of Semaphores Superior University, Gold Campus 2. Counting Semaphore: A counting semaphore can take non-negative integer values, allowing it to count multiple resources. It can be initialized to any positive integer value and is useful for managing access to a pool of identical resources, like a limited number of licenses or connections. The value of the semaphore represents the number of available resources. If a thread wants to access a resource, it decrements the semaphore, and when it releases the resource, it increments the semaphore.
1. Initializing Semaphores: In many programming environments, such as POSIX systems, you can use functions like sem_init () to initialize a semaphore that resides in shared memory. sem_t * sem = mmap (NULL, sizeof ( sem_t ), PROT_READ | PROT_WRITE, MAP_SHARED, fd , 0); sem_init ( sem , 1, 1); // 1 for shared between processes, initialized to 1 Implementation of Semaphores Superior University, Gold Campus
Implementation of Semaphores Superior University, Gold Campus 2. Using Semaphores: Before accessing shared resources, processes will often wait on a semaphore, which reduces its count. After finishing with the resource, they signal the semaphore, increasing its count. sem_wait ( sem ); // Access shared resource sem_post ( sem );
Implementation of Semaphores Superior University, Gold Campus 3. Cleanup : When the semaphore is no longer needed, or before the shared memory is released, it should be destroyed using sem_destroy ().