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
Shared Memory Programming Superior University, Gold Campus Shared memory is an operating-system feature that allows the database server threads and processes to share data by sharing access to pools of memory. The database server uses shared memory for the following purposes: To reduce memory usage and disk I/O. To perform high-speed communication between processes.
Shared Memory Programming Superior University, Gold Campus We know that to communicate between two or more processes, we use shared memory but before using the shared memory what needs to be done with the system calls.
Shared Memory Programming Superior University, Gold Campus Shared memory enables the database server to reduce overall memory usage because the participating processes, in this case, virtual processors, do not require maintaining private copies of the data that is in shared memory. Shared memory reduces disk I/O, because buffers, which are managed as a common pool, are flushed on a database server-wide basis instead of a per-process basis.
Shared Memory Programming Superior University, Gold Campus Furthermore , a virtual processor can often avoid reading data from disk because the data is already in shared memory as a result of an earlier read operation. The reduction in disk I/O reduces execution time. Shared memory provides the fastest method of interprocess communication, because it processes read and write messages at the speed of memory transfers.
Working of Shared Memory Superior University, Gold Campus Let us consider two processes P1 and P2 that want to perform Inter-process communication using a shared memory. P1 has an address space, let us say A1 and P2 has an address space, let us say A2. Now , P1 takes up some of the available address space as a shared memory space, let us say S1. Since P1 has taken up this space, it can decide which other processes can read and write data from the shared memory space.
Working of Shared Memory Superior University, Gold Campus For now, we will assume that P1 has given only reading rights to other processes with respect to the shared memory. So, the flow of Inter-process communication will be as follows : Process P1 takes up some of the available space as shared memory S1 Process P1 writes the data to be shared in S1 Process P2 reads the shared data from S1
Working of Shared Memory Superior University, Gold Campus
Working of Shared Memory Superior University, Gold Campus Now, let us assume that P1 has given write rights to P2 as well. So the communication will shown in the below diagram: Since P1 took up the space for shared memory i.e. since process P1 is the creator process, only it has the right to destroy the shared memory as well.
Advantages of Shared Memory Superior University, Gold Campus Shared memory is one of the fastest means of IPC since it avoids overheads. Easy access to data once set up. It is memory efficient as processes do not need to separately store shared data.
Disadvantages of Shared Memory Superior University, Gold Campus Since it is operating system specific, it is difficult to implement common synchronization and authorization techniques. Memory leaks can take place. If the processes wait indefinitely for each other in order to release the shared memory space, a deadlock can occur.
Threads Superior University, Gold Campus A running program may consist of multiple subprograms that maintain their own independent control flow and that are allowed to run concurrently. These subprograms are defined as threads. Communication between threads is via updates and access to memory appearing in the same address space.
Threads Superior University, Gold Campus It's important to note that while threads share memory, they also maintain private memory, making the design of thread interactions critical for data integrity and application stability. Remember to double-check your design to avoid common pitfalls in multi-threaded applications.
Difference between Threads and Processes Superior University, Gold Campus Threads and processes are both fundamental concepts in operating systems, but they have important differences : Processes: Definition : A process is an instance of a program that is being executed. It includes its own memory space, code, data, and system resources. Memory : Each process has its own separate memory space. This isolation helps prevent processes from interfering with each other's data.
Difference between Threads and Processes Superior University, Gold Campus Overhead : Creating and managing processes generally has more overhead because the operating system must allocate resources and create independent memory spaces. Communication : Communication between processes (Inter-Process Communication, or IPC) can be more complex and slower, often requiring mechanisms like pipes, message queues, or shared memory. Example : Running two separate applications like a web browser and a text editor.
Difference between Threads and Processes Superior University, Gold Campus Threads: Definition : A thread is a lightweight unit of execution within a process. Multiple threads can exist within the same process, sharing the same memory space. Memory : Threads within the same process share the same memory and resources, which allows for easier data sharing but increases the risk of data races and inconsistencies . Overhead: Threads are easier and less resource-intensive to create and manage compared to processes because they share the same memory space and certain resources of their parent process.
Difference between Threads and Processes Superior University, Gold Campus Communication : Communication between threads is generally faster and simpler since they can directly access shared data in memory. Example : A web server handling multiple client requests in separate threads.