semaphore

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that i...

POSIX Semaphores on Mac OS X: sem_timedwait alternative

I am trying to port a project (from linux) that uses Semaphores to Mac OS X however some of the posix semaphores are not implemented on Mac OS X The one that I hit in this port is sem_timedwait() I don't know much about semaphores but from the man pages sem_wait() seems to be close to sem_timedwait and it is implemented From the man p...

Semaphore queues

I'm extending the functionality of a semaphore. I ran into a roadblock when I realized I don't know the implementation of an actual semaphore and to make sure my code ran correctly, I needed to know this. I know a semaphore works by blocking threads that are waiting on it when they call sem_wait() and another thread currently has it lo...

Question about semaphore

Given the following code, can you figure out what caused "You input 7 characters" showed up 3 times especially the last time? #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void *thread_function(void *arg); sem_t bin_sem; #define WORK_SIZE 1024 char work_area[W...

sem_post() fail with valid semaphore

I have a queue with a semaphore. At certain point all the calls to sem_post() always return 'Invalid argument' error although the semaphore itself is valid The semaphore is a private member of C++ object which is never deleted, it also can be inspected in gdb. I added sem_getvalue() just before the sem_post() - the value reads OK and th...

Problem using pthread to utilize multiple cores

I'm doing a simple ray tracer in C++ using SDL for graphics and pthread for threading. And I have a problem making my program utilizing two cores, the threads work, they just don't drive both cores to 100%. To interface SDL I write directly to it's memory, SDL_Surface.pixels, so I assume that it can't be SDL locking me. My thread functi...

Why am I getting a segmentation fault with this code?

I am getting a segmentation fault while running this code. I can't work out why this is happening - can anyone see a possible reason? (I have already got and initialized the semaphore's shared memory.) My code: #include<stdlib.h> #include<sys/types.h> #include<sys/shm.h> #include<sys/ipc.h> #include<stdio.h> #include<...

What is the consensus number for semaphores?

(I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)? ...

RE: Posix and System V IPC

Which IPC among message queues, shared memory and semaphores is easiest to convert to network IPC and which is the hardest. Would it be easier to convert System V shared memory to network IPC or Posix shared memory to network IPC ...

Looking for good analogy/examples for monitor verses semaphore.

A monitor is supposed to solve problems with semaphores in concurrent environments. I'm looking for a good analogy using a monitor verses semaphore. Please use information for the analogy: 4 tasks (TaskA, TaskB, TaskC, TaskD) 1 variable varX Each Task wants to manipulate varX based on some event. ...

Re: Shared Memory and Semaphores

Is an IPC mechanism using shared memory and semaphores for synchronization simplex like pipes or duplex like message queues? ...

Where do you place POSIX semaphores when using POSIX shared memory?

I'm trying to build a client server application using POSIX shared memory and POSIX semaphores. Do I have to place the semaphores inside the shared memory segment or can the semaphores just be global variables? I wish to adhere to POSIX convention. ...

Odd/Incorrect Semaphore Behavior on OS X

Hi, I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results... #include <iostream> #include <fcntl.h> #include <stdio.h> #include <semaphore.h> int main() { sem_t* test; test = sem_open("test", O_CREAT, 0, 1); int val...

why are some posix shared memory segments and posix semaphores not visible to ipcs

I built a client server application using posix shared memory and posix unnamed semaphores with pshared=1. The semaphores are placed inside the shared memory. The program runs fine, but when I type ipcs -m or ipcs -s, I do not see any shared memory segments or semaphores that I created. Why is it so? /* Server main function for implemen...

Run one instance from the application

I have a windows application (C#) and i need to configure it to run one instance from the application at the time , It means that one user clicked the .exe file and the application is run and the user didn't close the first instance of the application that is being run and need to run a next instance so it should appear the first instanc...

Interprocess Mutex In Perl

I have a Perl CGI program that executes under mod_perl. Within the program, I would like to prevent a resource from accessing by multiple processes at the same time. # Semaphore Initialization Code # 10023 is unique id, and this id will be same across different apache process. # 1, Only one semaphore being created. # 0722, as all proces...

semget fails with "Permission denied"

Hi I have 2 processes P1 and P2. P1 is running as root, and is creating a semaphore with the following call: semget (key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT); and am trying to get the handle to the same semaphore in another process P2, which is running under the normal user's context. In this proce...

How do I terminate a thread that is waiting for a semaphore operation

I am writing a program that uses shared memory and semaphores for ipc. There is one main server process that creates the shared memory and semaphores. Any number of client processes can attach to the shared memory and read and write to it when allowed. The semaphores provide the blocking mechanism to control reads and writes. Everything ...

How to wait/block until a semaphore value reaches 0 in windows

Using the semop() function on unix, it's possible to provide a sembuf struct with sem_op =0. Essentially this means that the calling process will wait/block until the semaphore's value becomes zero. Is there an equivalent way to achieve this in windows? The specific use case I'm trying to implement is to wait until the number of reade...

Deadlock on a single java semaphore?

In one of my recent answers, I gave a theoretical semaphore example of limiting access to memory resources: public static byte[] createArray(int size) throws InterruptedException { semaphore.acquire(size); return new byte[size]; } public static void releaseArray(byte[] array) { semaphore.release(array.length); } I think th...