[英]Semaphore deadlock when using unnamed semaphores in POSIX with shared memory
我有 2 个信号量。 我有一个共享的 memory 段。 我正在尝试同步进程,以便它们相互等待直到某个任务完成,但是当在其他进程中使用sem_post_util( sem_sync )时,它就会停止并且最后一个进程继续并退出。 为什么它不增加共享 memory 中的未命名信号量?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../utils.h"
#include <signal.h>
#define SHARED_MEM "/shmm115"
#define SEM "nammed_sem115"
#define SEM_SYNC "unnamed_sem115"
#define SIZE 256
#define n_proc 3
sem_t *sem, sem_sync;
void* addr;
void cleanup(){
sem_close( sem );
sem_close( &sem_sync );
munmap_util( addr, SIZE );
shm_unlink( SHARED_MEM );
}
int main(){
sem = sem_open( SEM, O_CREAT, 0666, 1 );/*opent the named semaphore we got*/
/*Open shared memory segment and put a shared variable inside of it*/
struct sigaction handler;
handler.sa_handler = cleanup;
sigaction( SIGINT, &handler, NULL);
int fd;
short count = 0;
fd = shm_open_util(SHARED_MEM, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IRWXU);
struct stat stat_buf;
fstat( fd, &stat_buf );
if( stat_buf.st_size == 0){/*if it is the first one to write to it*/
ftruncate_util(fd, SIZE);
addr = mmap_util(0, SIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
memcpy( addr, &count, sizeof(short) );
memcpy( addr+sizeof(short), &sem_sync, sizeof(sem_t) );
}
else
addr = mmap_util(0, SIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
close(fd);
sem_init_util( &sem_sync, 1, 0 );
sem_wait_util( sem );
printf("Reading count\n");
memcpy(&count, addr, sizeof(short));
printf("Incrementing and writing the count: %d\n", count);
count++;
memcpy(addr, &count, sizeof(short));
sem_post_util( sem );
if( count == n_proc ){
printf("%d unblocks all the processes\n", getpid());
for( int i = 0 ; i<n_proc-1; ++i )
sem_post_util( addr + sizeof(short) );
}
else{
printf("%d pauses\n", getpid());
sem_wait_util( &sem_sync );
}
printf("%d continues\n", getpid());
sem_close( sem );
sem_close( &sem_sync );
munmap_util( addr, SIZE );
shm_unlink( SHARED_MEM );
}
我用./a.out &./a.out &./a.out运行它
这是 output
[70] 19882
[71] 19883
Reading count
Incrementing and writing the count: 0
19882 pauses
Reading count
Incrementing and writing the count: 1
19883 pauses
Reading count
Incrementing and writing the count: 2
19884 unblocks all the processes
19884 continues
如您所见,只有最后一个进程在继续,进程 19882 和 19883 没有继续并挂在那里等待。 我究竟做错了什么?
我希望信号量与其中的一些其他数据一起位于共享的 memory 中。
您没有将源代码提供给 util.h,但您似乎正在通过sem_wait_util( &sem_sync );
,但通过sem_post_util( addr + sizeof(short) );
.
我原以为它会通过sem_post_util(&sem_sync);
发布。
对 POSIX 信号量副本的操作是未定义的。 您必须在由sem_init
或sem_open
初始化的 memory 上发布并等待。
因此,在普通进程 memory 中初始化的sem_sync
的memcpy
到共享 memory 不会使 memory 成为可行的。
相反,尝试这样的事情:
struct shared_items {
sem_t sem;
short count;
} *shmem;
....
*shmem = mmap(0, sizeof(*shared_bundle), ..., shm_opened_fd, 0);
if (i_am_the_first_to_open_this_shared_memory) {
sem_init(&shmem->sem, 1, 0);
}
....
sem_post(&shmem->sem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.