Sponsored Content
Top Forums Programming Posix Semaphore Use - Releasing all resources? Post 302394130 by Loic Domaigne on Wednesday 10th of February 2010 01:38:30 PM
Old 02-10-2010
Hello Snowbarr,

Well, it's a tradition in POSIX threads to avoid semaphores whenever possible ... Corona688 idea to use condition variables (with a broadcast) is a good, see man pthread_cond_broadcast() and pthread_cond_wait() among others,

Another possibility, that may even better fit, could be using a barrier: see man pthread_barrier_init() and pthread_barrier_wait().

HTH,
Loïc
 

10 More Discussions You Might Find Interesting

1. Programming

Problem with releasing semaphore lock

Hi, I am trying to write stuff to a shared memory using a writer, and reading the corresponding stuff using a reader. I am facing problems while releasing the lock, as a result of which I am having segmentation faults. The code is as follows... /********** writer.c ***********/ ... (1 Reply)
Discussion started by: jacques83
1 Replies

2. Programming

replacing of the mutex with POSIX semaphore

Hi, does anybody know, if it is possible to replace mutex with POSIX semaphore? I'm gonna to rewrite old thread's program with processes and I don't want rewrite most of the code...So if I replace the threads with processes and mutexes with semaphores WILL IT WORK? Before, the critical... (1 Reply)
Discussion started by: michael26100
1 Replies

3. Solaris

Checking/ Releasing File Locks in Solaris

Hi, One of my prod jobs using Ab Initio (which is Solaris Based) returned this error: ========= Error from Reformat_3.000 on f7j21-01.xxxx3.com ========= Failed opening file for flow: Resource temporarily unavailable Flow = "Flow_11.000" connected to output port "out0" of "Reformat_3.000"... (0 Replies)
Discussion started by: teenu18
0 Replies

4. Linux

ext3 file system not releasing space

Hi all i am facing a problem with ext3 file system df -h is showing 19gb is used even if there is not a single file on the mount point /dev/mapper/vg01-archive 55G 19G 33G 36% /archive_log OS == Linux cdrsvr 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007... (1 Reply)
Discussion started by: ajays
1 Replies

5. Filesystems, Disks and Memory

Veritas HSM not releasing tapes

I have a volume managed by Veritas Storage Migrator 6.0 running on Solaris 10. It has one managed volume and one tape robot it shares with netpackup (separate volume pools). When I check the process tree I am not seeing any migration processes running, but when I check the panel on the tape robot... (0 Replies)
Discussion started by: ilikecows
0 Replies

6. Solaris

Releasing the swap space Solaris

Hi Guys ! I have found this problem many times that the swap space (/tmp space more precisely )of my Server becomes full specially when i run a complete back up of the server . once the /tmp space is full I am not able to run simple commands as i get error like : fork no space on... (6 Replies)
Discussion started by: Paarth
6 Replies

7. Solaris

Releasing the swap space Solaris

Hi Guys ! I have found this problem many times that the swap space (/tmp space more precisely )of my Server becomes full specially when i run a complete back up of the server . once the /tmp space is full I am not able to run simple commands as i get error like : fork no space on... (4 Replies)
Discussion started by: Paarth
4 Replies

8. AIX

Releasing a Port

Hey Guys, I need a help, After I upgraded my server to AIX 6.1.4 the port 80 is occupied by a Java process bash-3.00# netstat -Aan | grep "*.80" f1000700007d7bb0 tcp 0 0 *.80 *.* LISTEN bash-3.00# rmsock f1000700007d7bb0 tcp usage: rmsock Addr... (2 Replies)
Discussion started by: kkeng808
2 Replies

9. AIX

AIX 6.1: Releasing Memory and Page Space

Hi everyone, i have a question about the Memory Management in AIX 6.1. I have - 128 GB RAM and - 70 GB Page Space. The application i am running on this machine is doing some operations in perl. These are done only once a day and uses both memory and paging space. My problem... (1 Reply)
Discussion started by: Haichao
1 Replies

10. UNIX for Beginners Questions & Answers

PLINK from Windows not releasing job

Hi all, I would greatly appreciate some help with this. I may not even be on the right path but I think I am close. I have a Synology NAS that I am running rsync jobs on to offload to USB3. These work fine. I have backup software on our Windows Server 2008R2 that I have now realized can... (2 Replies)
Discussion started by: stefshuuj
2 Replies
semaphore(3C)						   Standard C Library Functions 					     semaphore(3C)

NAME
semaphore, sema_init, sema_destroy, sema_wait, sema_trywait, sema_post - semaphores SYNOPSIS
cc [ flag... ] file... -lthread -lc [ library... ] #include <synch.h> int sema_init(sema_t *sp, unsigned int count, int type, void * arg); int sema_destroy(sema_t *sp); int sema_wait(sema_t *sp); int sema_trywait(sema_t *sp); int sema_post(sema_t *sp); DESCRIPTION
A semaphore is a non-negative integer count and is generally used to coordinate access to resources. The initial semaphore count is set to the number of free resources, then threads slowly increment and decrement the count as resources are added and removed. If the semaphore count drops to 0, which means no available resources, threads attempting to decrement the semaphore will block until the count is greater than 0. Semaphores can synchronize threads in this process and other processes if they are allocated in writable memory and shared among the coop- erating processes (see mmap(2)), and have been initialized for this purpose. Semaphores must be initialized before use; semaphores pointed to by sp to count are initialized by sema_init(). The type argument can assign several different types of behavior to a semaphore. No current type uses arg, although it may be used in the future. The type argument may be one of the following: USYNC_PROCESS The semaphore can synchronize threads in this process and other processes. Initializing the semaphore should be done by only one process. A semaphore initialized with this type must be allocated in memory shared between processes, either in Sys V shared memory (see shmop(2)), or in memory mapped to a file (see mmap(2)). It is illegal to initialize the object this way and not allocate it in such shared memory. arg is ignored. USYNC_THREAD The semaphore can synchronize threads only in this process. The arg argument is ignored. USYNC_THREAD does not support multiple mappings to the same logical synch object. If you need to mmap() a synch object to different locations within the same address space, then the synch object should be initialized as a shared object USYNC_PROCESS for Solaris threads and PTHREAD_PROCESS_PRIVATE for POSIX threads. A semaphore must not be simultaneously initialized by multiple threads, nor re-initialized while in use by other threads. Default semaphore initialization (intra-process): sema_t sp; int count = 1; sema_init(&sp, count, NULL, NULL); or sema_init(&sp, count, USYNC_THREAD, NULL); Customized semaphore initialization (inter-process): sema_t sp; int count = 1; sema_init(&sp, count, USYNC_PROCESS, NULL); The sema_destroy() function destroys any state related to the semaphore pointed to by sp. The semaphore storage space is not released. The sema_wait() function blocks the calling thread until the semaphore count pointed to by sp is greater than 0, and then it atomically decrements the count. The sema_trywait() function atomically decrements the semaphore count pointed to by sp, if the count is greater than 0; otherwise, it returns an error. The sema_post() function atomically increments the semaphore count pointed to by sp. If there are any threads blocked on the semaphore, one will be unblocked. The semaphore functionality described on this man page is for the Solaris threads implementation. For the POSIX-conforming semaphore interface documentation, see sem_close(3C), sem_destroy(3C), sem_getvalue(3C), sem_init(3C), sem_open(3C), sem_post(3C), sem_unlink(3C), and sem_wait(3C). RETURN VALUES
Upon successful completion, 0 is returned; otherwise, a non-zero value indicates an error. ERRORS
These functions will fail if: EINVAL The sp argument does not refer to a valid semaphore. EFAULT Either the sp or arg argument points to an illegal address. The sema_wait() function will fail if: EINTR The wait was interrupted by a signal or fork(). The sema_trywait() function will fail if: EBUSY The semaphore pointed to by sp has a 0 count. The sema_post() function will fail if: EOVERFLOW The semaphore value pointed to by sp exceeds SEM_VALUE_MAX. EXAMPLES
Example 1 The customer waiting-line in a bank is analogous to the synchronization scheme of a semaphore using sema_wait() and sema_try- wait(): /* cc [ flag ... ] file ... -lthread [ library ... ] */ #include <errno.h> #define TELLERS 10 sema_t tellers; /* semaphore */ int banking_hours(), deposit_withdrawal; void*customer(), do_business(), skip_banking_today(); ... sema_init(&tellers, TELLERS, USYNC_THREAD, NULL); /* 10 tellers available */ while(banking_hours()) pthread_create(NULL, NULL, customer, deposit_withdrawal); ... void * customer(int deposit_withdrawal) { int this_customer, in_a_hurry = 50; this_customer = rand() % 100; if (this_customer == in_a_hurry) { if (sema_trywait(&tellers) != 0) if (errno == EBUSY){ /* no teller available */ skip_banking_today(this_customer); return; } /* else go immediately to available teller and decrement tellers */ } else sema_wait(&tellers); /* wait for next teller, then proceed, and decrement tellers */ do_business(deposit_withdrawal); sema_post(&tellers); /* increment tellers; this_customer's teller is now available */ } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
mmap(2), shmop(2), sem_close(3C), sem_destroy(3C), sem_getvalue(3C), sem_init(3C), sem_open(3C), sem_post(3C), sem_unlink(3C), sem_wait(3C), attributes(5), standards(5) NOTES
These functions are also available by way of: #include <thread.h> By default, there is no defined order of unblocking for multiple threads waiting for a semaphore. SunOS 5.11 5 Feb 2008 semaphore(3C)
All times are GMT -4. The time now is 04:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy