Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semaphore and Multithreading

Similar presentations


Presentation on theme: "Semaphore and Multithreading"— Presentation transcript:

1 Semaphore and Multithreading
-Sagar Panchariya

2 Semaphore A hardware or software flag.
A Semaphore is a variable with a value that indicates the status of a common resource. It's used to lock the resource that is being used. A process needing the resource checks the semaphore to determine the resource's status and then decides how to proceed

3 Resource Semaphore Load data(signal); Wait(signal); Process(data);)
Buffer I/O Device Start processing Load data

4 Multithreading The ability to execute different parts of a program, called threads, simultaneously. Different parts of a program may be: I/O operations Data Processing Advantages:- Multithreading, when done correctly, offers better utilization of processors and other system resources.

5 Multithreading Problems to be taken care of: Race conditions:
Two or more threads try and update the same resource <variable> without proper synchronization. Solution: Use of semaphores, to bring in synchronization among threads.

6 C” Programming example
Mono Memory Mono Memory Input Data Read Sem async_memcpym2p Read Sem async_memcpyp2m Output Data Write Sem SRC_addr DST_addr 96 PE Array Memory Background buffer Elements[0] Active buffer Elements[1]

7 1. Read into the active buffer.
2. Wait for the background buffer to be empty then read the next chunk into it. 3. Wait for the active buffer to be full then process the contents. 4. Write out the active buffer. 5. Swap the active and background buffers. 6. Repeat from 2 until all chunks are processed Wait for the write of the last active buffer to finish then print the results to the console.

8 Two semaphores are used:
one to synchronize reading a buffer and processing that data; the second to synchronize writing the data with refilling the buffer Code snapshot:-- async_memcpym2p(READ_SEMAPHORE_a, &element[active], src_addr, sizeof(float)); sem_wait(READ_SEMAPHORE_a); //do the processing on all data in parallel element[active] *= scale; async_memcpyp2m(WRITE_SEMAPHORE, dst_addr, &element[active], sizeof(float));

9 Semaphore debug commands
sem help To give description of all semaphore commands sem display all Displays all semaphore sem display allval Display all semaphores with value Sem display <semaphore no>

10 Thanks


Download ppt "Semaphore and Multithreading"

Similar presentations


Ads by Google