Sponsored Content
Full Discussion: Semaphore
Top Forums UNIX for Beginners Questions & Answers Semaphore Post 303015714 by Corona688 on Tuesday 10th of April 2018 02:51:44 PM
Old 04-10-2018
Quote:
Originally Posted by uniran
I didn't get it, which is the critical section?
Exactly what you think it is.
Quote:
How would a thread wake up in order to enter it if the post comes after this critical section?
You have it a bit backwards, I think - sem_wait is what's responsible for making threads sleep. If the resource is busy, sem_wait makes you sleep, if the resource is free, it does not.

The first thread to enter the critical section does not sleep and the semaphore is decremented from 1 to 0. The second and third threads cannot decrement the semaphore and are forced to wait instead.

The first thread, which didn't stop, keeps going and eventually runs sem_post, which wakes up the second thread, which runs and eventually runs sem_post, et cetera.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

semaphore

hi, is there any command where we can monitor semaphores? (1 Reply)
Discussion started by: yls177
1 Replies

2. UNIX for Dummies Questions & Answers

Semaphore

Hi, I'm new to UNIX. I need to know what's a semaphore Do reply. Thanks VJ (3 Replies)
Discussion started by: vjsony
3 Replies

3. Programming

Semaphore debugging

I'm running one multithreaded application, in that one of my thread is waiting infinitely in a semphore. Is there a way to determine, in which semaphore the particular thread is waiting and which thread(s) is holding the semaphore. (5 Replies)
Discussion started by: ptprabu
5 Replies

4. UNIX for Advanced & Expert Users

semaphore concept

Hi All, I am going through the semaphore concept and have a doubt regarding the same and hope to get a resolution here. I have a file which has a number of records. I want to write an application (in C) which will be able to do concurrent read/write on these records. Of what I have... (8 Replies)
Discussion started by: maverix
8 Replies

5. Shell Programming and Scripting

Semaphore

Hi, I am looking to use a semaphore for the first time in one of my scripts. I am just wondering if there are any simple examples or tutorials around? I am a beginner so the simpler the better :) Thanks -Jaken (2 Replies)
Discussion started by: Jaken
2 Replies

6. UNIX for Dummies Questions & Answers

semaphore

what is semaphore? can any body explain it in a more simple way than the manual ?? replies appreciated Regards raguram R (7 Replies)
Discussion started by: raguramtgr
7 Replies

7. Programming

Semaphore

In my server code there is a thread per client... The server call accept() and after that start the thread. So there is a thread for client that save in RAM the client's message, that will be send to other clients. Now in RAM I have created a shared memory in which thread read and write(save)... (2 Replies)
Discussion started by: italian_boy
2 Replies

8. Shell Programming and Scripting

semaphore

Control two exclusively shared resources(semaphore). The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the... (0 Replies)
Discussion started by: gokult
0 Replies

9. Programming

Semaphore

If I create a semaphore and then I fork a number of child processes then all the child process use that same semaphore. Since the process address spaces are different rfom each other then how all the child process are able to access the same semaphore? I understand that semaphore/mutex is at os... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

10. Solaris

Semaphore

Can anyone tell me abt the Semaphore concept and what is semaphore??? is semaphore id is associated in terms as in resources like semaphore id 1 indicates cpu share unit is given and semaphore id 2 will indicate abt the memore or semaphore id 3 will tell us the i/o components (1 Reply)
Discussion started by: aarjun07
1 Replies
sem_wait(3C)						   Standard C Library Functions 					      sem_wait(3C)

NAME
sem_wait, sem_trywait - acquire or wait for a semaphore SYNOPSIS
#include <semaphore.h> int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); DESCRIPTION
The sem_wait() function locks the semaphore referenced by sem by performing a semaphore lock operation on that semaphore. If the semaphore value is currently zero, then the calling thread will not return from the call to sem_wait() until it either locks the semaphore or the call is interrupted by a signal. The sem_trywait() function locks the semaphore referenced by sem only if the semaphore is currently not locked; that is, if the semaphore value is currently positive. Otherwise, it does not lock the semaphore. Upon successful return, the state of the semaphore is locked and remains locked until the sem_post(3C) function is executed and returns successfully. The sem_wait() function is interruptible by the delivery of a signal. RETURN VALUES
The sem_wait() and sem_trywait() functions return 0 if the calling process successfully performed the semaphore lock operation on the sem- aphore designated by sem. If the call was unsuccessful, the state of the semaphore is unchanged, and the function returns -1 and sets errno to indicate the error. ERRORS
The sem_wait() and sem_trywait() functions will fail if: EINVAL The sem function does not refer to a valid semaphore. ENOSYS The sem_wait() and sem_trywait() functions are not supported by the system. The sem_trywait() function will fail if: EAGAIN The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation. The sem_wait() and sem_trywait() functions may fail if: EDEADLK A deadlock condition was detected; that is, two separate processes are waiting for an available resource to be released via a semaphore "held" by the other process. EINTR A signal interrupted this function. USAGE
Realtime applications may encounter priority inversion when using semaphores. The problem occurs when a high priority thread "locks" (that is, waits on) a semaphore that is about to be "unlocked" (that is, posted) by a low priority thread, but the low priority thread is pre- empted by a medium priority thread. This scenario leads to priority inversion; a high priority thread is blocked by lower priority threads for an unlimited period of time. During system design, realtime programmers must take into account the possibility of this kind of priority inversion. They can deal with it in a number of ways, such as by having critical sections that are guarded by semaphores execute at a high priority, so that a thread cannot be preempted while executing in its critical section. EXAMPLES
Example 1 The customer waiting-line in a bank may be analogous to the synchronization scheme of a semaphore utilizing sem_wait() and sem_trywait(): #include <errno.h> #define TELLERS 10 sem_t bank_line; /* semaphore */ int banking_hours(), deposit_withdrawal; void *customer(), do_business(), skip_banking_today(); thread_t tid; ... sem_init(&bank_line,TRUE,TELLERS); /* 10 tellers available */ while(banking_hours()) thr_create(NULL, NULL, customer, (void *)deposit_withdrawal, THREAD_NEW_LWP, &tid); ... void * customer(deposit_withdrawal) void *deposit_withdrawal; { int this_customer, in_a_hurry = 50; this_customer = rand() % 100; if (this_customer == in_a_hurry) { if (sem_trywait(&bank_line) != 0) if (errno == EAGAIN) { /* no teller available */ skip_banking_today(this_customer); return; } /*else go immediately to available teller & decrement bank_line*/ } else sem_wait(&bank_line); /* wait for next teller, then proceed, and decrement bank_line */ do_business((int *)deposit_withdrawal); sem_getvalue(&bank_line,&num_tellers); sem_post(&bank_line); /* increment bank_line; this_customer's teller is now available */ } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ |Standard |See standards(5). | +-----------------------------+-----------------------------+ SEE ALSO
sem_post(3C), attributes(5), standards(5) SunOS 5.11 5 Feb 2008 sem_wait(3C)
All times are GMT -4. The time now is 03:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy