【发布时间】:2021-04-24 11:59:20
【问题描述】:
我有 2 个信号量。我有一个共享内存段。我正在尝试同步进程,以便它们相互等待,直到某个任务完成,但是当 sem_post_util(sem_sync) 在其他进程中使用时就会停止,最后一个进程继续并退出。为什么不增加共享内存中未命名的信号量?
#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
运行它这是输出
[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 没有继续并挂在那里等待。我做错了什么?
我希望信号量与其中的一些其他数据一起位于共享内存中。
【问题讨论】:
标签: c posix semaphore shared-memory systems-programming