You are on page 1of 8

TOPIC:-

SUBMITED BY:-YATIN KUMAR REG. NUMBER:-11114230 SECTION:-K3R14 ROLL NO. :-A30

SUBMITTED TO:-

Introduction to Semaphores

A semaphore is a synchronization object. A thread must, in general, synchronize itself with other threads to properly execute a multithreaded program. In the fthreads system, a semaphore object is declared to be a variable of type semaphore_t, and must be initialized with a call to semaphore_init() before it may be used. When a program is finished using a semaphore, it may delete the semaphore with a call to semaphore_del(). The maximum number of semaphores which may be used by a program is set by the max_semaphores argument on the call to fthreads_init(). A semaphore has a property call its maximum count, which is a positive integer and is specified when the semaphore is initialized by calling semaphore_init(). Threads synchronize at a semaphore by calling semaphore_get() and semaphore_put(). When the semaphore is initialized, its maximum count is established. During execution of the program, the current count varies between zero and the maximum count. A call to semaphore_get() lowers the current count by one; a call to semaphore_put() raises the current count by one. When the count is zero, a thread will block in a call to semaphore_get() until the count is greater than zero (that is, until another thread calls semaphore_put()). For example, access to a stack may be controlled by a semaphore. The samephore is initialized with its maximum count being equal to the stack size. When the semaphore is initialized, it count is zero, which corresponds to an empty stack. When elements are pushed onto the stack, semaphore_put() is called to increase the semaphore's count. When elements are popped off the stack, semaphore_get() is called to lower the semaphore's count. When there are no elements in the stack, threads will block during the call to semaphore_get() until there are elements on the stack.

Semaphore Routines semaphore_init() The semaphore_init() routine initializes a new semaphore synchronization object. interface subroutine semaphore_init( s, tm, max_count, & name, trace_v, flag) type( semaphore_t), intent( out) :: s type( team_t), optional, & intent( in) :: tm integer, intent( in) :: max_count character( len= *), optional, & intent( in) :: name type( trace_t), optional, & intent( inout) :: trace_v integer, optional, & intent( out) :: flag end subroutine semaphore_init end interface

The semaphore_init() routine returns a type semaphore_t semaphore variable s. The semaphore maximum count is established by the integer max_count. The semaphore is available only to members of optional type team_t variable tm,

or to all workers. The optional character variable provides a semaphore name. The optional type trace variable trace_v and the optional integer variable flag provide tracing and feedback.

semaphore_del() The semaphore_del() routine deletes a semaphore synchronization object. interface subroutine semaphore_del( s, trace_v, & flag) type( semaphore_t), intent( inout) :: s type( trace_t), optional, & intent( inout) :: trace_v integer, optional, & intent( out) :: flag end subroutine semaphore_del end interface

The semaphore_del() routine deletes a type semaphore_t semaphore variable s. The optional type trace_t variable trace_v and the optional integer variable flag provide tracing and feedback.

semaphore_get()

The semaphore_get() routine synchronizes a thread at a semaphore. interface subroutine semaphore_get( s, th, & trace_v, flag) type( semaphore_t), intent( out) :: s type( thread_t), optional, & intent( in) :: th type( trace_t), optional, & intent( inout) :: trace_v integer, optional, & intent( out) :: flag end subroutine semaphore_get end interface

The semaphore_get() routine synchronizes the calling thread at the semaphore indicated by the type semaphore_t semaphore variable s. The semaphore's current count is decreased by one. If the current count is zero, the thread blocks until it is greater than zero. If the optional type thread_t variable th is present, the calling thread will be check for membership in the semaphore's team. The optional type trace_t variable trace_v and the optional integer variable flag provide tracing and feedback.

semaphore_put() The semaphore_put() routine synchronizes a thread at a semaphore. interface subroutine semaphore_put( s, th, & trace_v, flag) type( semaphore_t), intent( out) :: s type( thread_t), optional, & intent( in) :: th type( trace_t), optional, & intent( inout) :: trace_v integer, optional, & intent( out) :: flag end subroutine semaphore_put end interface

The semaphore_put() routine synchronizes the calling thread at the semaphore indicated by the type semaphore_t semaphore variable s. The semaphore's current count is increased by one. If the optional type thread_t variable th is present, the calling thread will be check for membership in the semaphore's team. The optional type trace_t variable trace_v and the optional integer variable flag provide tracing and feedback.

semaphore_id() The semaphore_id() return the semaphore id. interface integer function semaphore_id( b) type( semaphore_t), intent( in) :: b end function semaphore_id end interface

The integer function semaphore_id() returns the semaphore id. The semaphore id is a unique positive integer from one through the number of semaphores.

semaphore_status() The semaphore_status() routine returns a semaphore's statistics. interface subroutine semaphore_status( s, name, & get, put, max_count, & trace_v, flag) type( semaphore_t), intent( in) :: s character( len= *), optional, &

intent( out) :: name integer, optional, & intent( out) :: max_count integer, optional, & intent( out) :: get integer, optional, & intent( out) :: put type( trace_t), optional, & intent( inout) :: trace_v integer, optional, & intent( out) :: flag end subroutine semaphore_status end interface

The semaphore_status() routine returns the semaphore statistics of the semaphore indicated by the type semaphore_t semaphore variable s. The optional character variable name returns the semaphore's name. The optional integer variable max_count returns the semaphore's maximum count. The optional integer get returns the number of times a thread has called at the semaphore_get() indicating this semaphore. The optional integer put returns the number of times a thread has called at the semaphore_put() indicating this semaphore. The optional type trace_t variable trace_v and the optional integer variable flag provide tracing and feedback.

You might also like