信号量概要:http://www.kernel.org/doc/man-pages/online/pages/man7/sem_overview.7.html
以下实例通过信号量使两个无亲缘关系的进程进行同步通信
HOST:
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#define MAP_SHARED_FILE "./test"//共享内存映射的文件
char SEM_NAME[]= "uart_test";//信号名称
typedef struct
{
char name[4];
int age;
}people;
int main(int argc, char** argv)
{
int fd,i;
people *p_map;
char temp;
sem_t *mutex;
fd = open(MAP_SHARED_FILE, O_CREAT | O_RDWR | O_TRUNC, 777);
if (fd < 0)
{
perror("Open ");
return 0;
}
/* 设置映射文件的大小,如果不设置的话第一次引用共享内存会产生SIGBUS错误 */
if (lseek(fd, sizeof(people)*10 - 1, SEEK_SET) == -1)
{
perror("lseek ");
return 0;
}
if (write(fd, "", 1) != 1)
{
perror("write ");
return 0;
}
//create & initialize existing semaphore
mutex = sem_open(SEM_NAME, O_CREAT, 0644, 0);
if(mutex == SEM_FAILED)
{
perror("Create semaphore ");
sem_unlink(SEM_NAME);
return 0;
}
p_map = (people *)mmap(NULL, sizeof(people)*10, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);//关闭文件描述符不会使内存映射I/O失效
temp = 'a';
for(i = 0; i < 10; i++)
{
memcpy((*(p_map+i)).name, &temp, 1);
(*(p_map+i)).age = 10 + i;
printf("Write %c\n", temp);
temp++;
sem_wait(mutex);//lock
}
sem_close(mutex);//close semaphore
sem_unlink(SEM_NAME);//remove semaphore from system
munmap(p_map, sizeof(people)*10);//解除映射区
return 0;
}
CLIENT:
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#define MAP_SHARED_FILE "./test" //共享内存映射的文件
char SEM_NAME[]= "uart_test";
typedef struct
{
char name[4];
int age;
}people;
int main(int argc, char** argv)
{
int fd,i;
people *p_map;
sem_t *mutex;
fd = open(MAP_SHARED_FILE, O_CREAT | O_RDWR, 777);
if (fd < 0)
{
perror("Open ");
return 0;
}
//create & initialize existing semaphore
mutex = sem_open(SEM_NAME, 0, 0644, 0);
if(mutex == SEM_FAILED)
{
perror("Execute semaphore ");
sem_close(mutex);
return 0;
}
p_map = (people*)mmap(NULL, sizeof(people)*10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
for(i = 0; i < 10; i++)
{
printf("name: %s age %d\n", (*(p_map+i)).name, (*(p_map+i)).age);
sem_post(mutex);//unlock
sleep(1);
}
sem_post(mutex);
sem_close(mutex);
munmap(p_map, sizeof(people)*10);
return 0;
}
编译时记得加 -lrt 参数!