Semaphore and Multithreading Sagar Panchariya Semaphore A hardware

  • Slides: 10
Download presentation
Semaphore and Multithreading -Sagar Panchariya

Semaphore and Multithreading -Sagar Panchariya

Semaphore • A hardware or software flag. • A Semaphore is a variable with

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

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

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

Multithreading • The ability to execute different parts of a program, called threads, simultaneously.

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.

Multithreading Problems to be taken care of: • Race conditions: Two or more threads

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.

C” Programming example Input Data async_memcpym 2 p async_memcpyp 2 m Output Data 0

C” Programming example Input Data async_memcpym 2 p async_memcpyp 2 m Output Data 0 1 2 3 96 192 Mono Memory 0 2 4 6 Mono Memory Read Sem 384 Write Sem SRC_addr DST_addr Elements[0] Elements[1] 96 PE Array Memory Background buffer Active buffer

 • 1. Read into the active buffer. • 2. Wait for the background

• 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. 7. Wait for the write of the last active buffer to finish then print the results to the console.

Two semaphores are used: • one to synchronize reading a buffer and processing that

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_memcpym 2 p(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_memcpyp 2 m(WRITE_SEMAPHORE, dst_addr, &element[active], sizeof(float));

Semaphore debug commands • sem help To give description of all semaphore commands •

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>

 • Thanks

• Thanks