linux 信号量

信号量概要: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 参数!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值