semaphore

Programming in UNIX - Semaphores,shared memory in C

孤者浪人 提交于 2019-12-08 06:19:56
问题 I started a week ago understanding and working with semaphores and shared memory, and actually created this program, the problem is i cant find anything wrong with it i been looking at it for hours and everything seems correct.. The code compiles and i can create the build but when i execute it nothing happens ... #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <sys/fcntl.h> #include <semaphore.h> #define

Unexpected Output Running Semaphore

僤鯓⒐⒋嵵緔 提交于 2019-12-08 06:17:44
问题 This question was migrated from Unix & Linux Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . I'm trying to write a program that forks another process and stays in sync so that the first process doesn't start its (i)th iteration until the second process has finished its (i-1)th iteration. Is this possible using only two semaphores? This is what I have so far: #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <errno.h>

Semaphore with priority

谁说我不能喝 提交于 2019-12-08 05:55:29
问题 I know about the Semaphore class in the System.Threading namespace, but I don't see if it allows waiting threads to have different priorities levels. If two threads are waiting for an open slot, is there a way to allow the thread with the higher priority to have the first open slot available? 回答1: From the MSDN documentantion on the Semaphore class There is no guaranteed order, such as FIFO or LIFO, in which blocked threads enter the semaphore. But take a look at these two projects which both

Linux inter-process reentrant semaphore

拟墨画扇 提交于 2019-12-08 05:40:24
问题 I'm porting a Windows application to Linux and I have a synchronization problem. In Windows I'm using a system-level named mutex to sync access to a shared memory block. How do I emulate that in Linux? I've created a SystemV semaphore, using semget. The problem is that it is not reentrant, if I already hold it it will block, unlike on Windows. I could add a reference count to it, but then I would need to synchronize access to that, which means another (this time for the current process only)

Possible Stack Corruption

牧云@^-^@ 提交于 2019-12-07 17:45:42
问题 With reference to my previous question about GDB not pinpointing the SIGSEGV point, My thread code is as follows: void *runner(void *unused) { do { sem_wait(&x); ... if(/*condition 1 check*/) { sem_post(&x); sleep(5); sem_wait(&x); if(/*repeat condition 1 check; after atleast 5 seconds*/) { printf("LEAVING...\n"); sem_post(&x); // putting exit(0); here resolves the dilemma return(NULL); } } sem_post(&x); }while(1); } Main code: sem_t x; int main(void) { sem_init(&x,0,1); ... pthread_t thrId;

Modification to “Implementing an N process barrier using semaphores”

喜你入骨 提交于 2019-12-07 17:36:59
问题 Recently I see this problem which is pretty similar to First reader/writer problem. Implementing an N process barrier using semaphores I am trying to modify it to made sure that it can be reuse and work correctly. n = the number of threads count = 0 mutex = Semaphore(1) barrier = Semaphore(0) mutex.wait() count = count + 1 if (count == n){ barrier.signal()} mutex.signal() barrier.wait() mutex.wait() count=count-1 barrier.signal() if(count==0){ barrier.wait()} mutex.signal() Is this correct? I

Single producer/consumer circular buffer which only blocks consumer

▼魔方 西西 提交于 2019-12-07 17:18:43
问题 I'd like to implement a buffer with single producer and a single consumer, where only the consumer may be blocked. Important detail here is that the producer can drop the update if the queue is full. I've considered converting a wait-free implementation, but at first glance there seems to be no easy way to notify the consumer new data has arrived without losing notifications. So I settled on the very simple approach below, using a counting semaphore (some error handling details omitted for

What is the purpose of Semaphore.wait(timeout: .now())?

元气小坏坏 提交于 2019-12-07 13:40:15
问题 Looking at some Apple code sample, I found this: func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { // wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data. if metadataObjectsOverlayLayersDrawingSemaphore.wait(timeout: .now()) == .success { DispatchQueue.main.async { // Some processing... self.metadataObjectsOverlayLayersDrawingSemaphore

JAVA多线程--信号量(Semaphore)

独自空忆成欢 提交于 2019-12-07 09:46:46
简介 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确、合理的使用公共资源。 一个计数信号量。从概念上讲,信号量维护了一个许可集。如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可。每个 release() 添加一个许可,从而可能释放一个正在阻塞的获取者。但是,不使用实际的许可对象,Semaphore 只对可用许可的号码进行计数,并采取相应的行动。拿到信号量的线程可以进入代码,否则就等待。通过acquire()和release()获取和释放访问许可。 概念 Semaphore分为单值和多值两种,前者只能被一个线程获得,后者可以被若干个线程获得。 以一个停车场运作为例。为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这时如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入一辆,如果又离开两辆,则又可以放入两辆,如此往复。 在这个停车场系统中,车位是公共资源,每辆车好比一个线程,看门人起的就是信号量的作用。 更进一步,信号量的特性如下:信号量是一个非负整数(车位数),所有通过它的线程(车辆)都会将该整数减一(通过它当然是为了使用资源)

Difference with cout and printf while multithreading in c++

泪湿孤枕 提交于 2019-12-07 08:44:47
问题 Some background: I have a c++ program that is multithreaded using pthreads. The program is a hotel reservation system, with 10 guests (each their own thread), a check-in desk (1 thread) and a check-out desk (1 thread). There are only 5 rooms in the hotel that a guest can be in. I am using semaphores to enforce mutual exclusion and event ordering in this program. Question: here is my code (Just parts that are needed...) sem_init(&openRooms, 0, 5); sem_wait(&openRooms); //waits for there to be