Sponsored Content
Top Forums Programming sem_wait, sem_post confusion? Post 302582770 by vistastar on Sunday 18th of December 2011 05:45:09 AM
Old 12-18-2011
define another two semaphore:
Code:
sem_t zero, one;

a thread wait for zero and post one, while the other thread wait for one and post zero.
This User Gave Thanks to vistastar For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

2. Programming

C fork Confusion :-?

Hi, I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the... (2 Replies)
Discussion started by: ralpheno
2 Replies

3. Solaris

Printer confusion

I have printer old about 5 year , but I can not determine which driver to use output of ls -al /usr/share/lib/terminfo/h -rw-r--r-- 1 root bin 961 Jan 22 2005 h1000 -rw-r--r-- 1 root bin 1002 Jan 22 2005 h1420 -rw-r--r-- 1 root bin 1009... (2 Replies)
Discussion started by: solaris_user
2 Replies

4. Cybersecurity

LDAP; confusion

Hello, I hope all is well. Two issues that I am grappling with. One: Is this a true statement: (AIX, LDAP configured), even if authentication is configured with LDAP, the system would still need to be authenticated against local (/etc/passwd); incase of network failure? Two: I can log... (0 Replies)
Discussion started by: rsheikh
0 Replies

5. Shell Programming and Scripting

conditional confusion

Hell Unix.com Community: I am working on a personal project using yad v0.12.4 (zenity fork) and have hit a wall on how to show a progress bar while my function is processing. I have been all over the ABS Guide, googled 21 Linux-specific sites that I revere. I even asked on the yad-common... (4 Replies)
Discussion started by: Habitual
4 Replies

6. Programming

Undefined: sem_init, sem_post, sem_wait

Hi friends, I am using semaphores in my program, but when I compile the program, it gives the following error $ gcc sem.c -o sem -lpthread Undefined first referenced symbol in file sem_init ... (1 Reply)
Discussion started by: gabam
1 Replies

7. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

8. Homework & Coursework Questions

Semaphores sem_wait sem_post problem

hallo! if there are many processes running and I initialize the semaphore like this: my_sem = sem_open(SEM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 10); (the last argument is 10) and then i use sem_wait(my_sem); sleep(5); sem_post; Will 10 processes be able to access the... (1 Reply)
Discussion started by: whatevernever
1 Replies

9. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies
sem_wait(3RT)						    Realtime Library Functions						     sem_wait(3RT)

NAME
sem_wait, sem_trywait - acquire or wait for a semaphore SYNOPSIS
cc [ flag... ] file... -lrt [ library... ] #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(3RT) 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(): /* cc [ flag ... ] file ... -lrt -lthread [ library ... ] */ #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 |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sem_post(3RT), attributes(5), standards(5) SunOS 5.10 28 Jun 2002 sem_wait(3RT)
All times are GMT -4. The time now is 10:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy