共享内存
多个进程共享同一块内存区域,必然需要某种同步机制,互斥锁和信号量都可以。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
#define SEM_NAME "mysem"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int count = 0;
int main(int argc,char* argv[])
{
int i,nloop;
sem_t *mutex;
if(argc != 2)
{
printf("usage: incrl <#loops>");
exit(0);
}
nloop = atoi(argv[1]);
//创建有名信号量
mutex = sem_open(SEM_NAME,O_RDWR|O_CREAT|O_EXCL,FILE_MODE,1);
sem_unlink(SEM_NAME);
//将stdout设置为非缓冲的
setbuf(stdout,NULL);
//子进程开始执行增加1
if(fork() == 0)
{
for(i = 0;i<nloop;++i)
{
sem_wait(mutex);
printf("child: %d\n",count++);
sem_post(mutex);
}
exit(0);
}
//父进程执行增加1操作
for(i = 0;i<nloop;++i)
{
sem_wait(mutex);
printf("parent: %d\n",count++);
sem_post(mutex);
}
//等待子进程退出
wait(NULL);
exit(0);
}
gcc demo.c -o app -lphthread -std=c99
./app 10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#define SEM_NAME "mysem"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc,char* argv[])
{
int fd,i,nloop,zero = 0;
int *ptr;
sem_t *mutex;
if(argc != 3)
{
printf("usage: incrl <#loops>");
exit(0);
}
nloop = atoi(argv[2]);
//打开文件
fd = open(argv[1],O_RDWR|O_CREAT,FILE_MODE);
//向文件中写入0值
write(fd,&zero,sizeof(int));
//将文件映射到进程地址空间,返回被映射区的起始地址
ptr = mmap(NULL,sizeof(int),PROT_READ| PROT_WRITE,MAP_SHARED,fd,0);
if(ptr == MAP_FAILED)
{
perror("mmap() error");
exit(0);
}
close(fd);
mutex = sem_open(SEM_NAME,O_RDWR|O_CREAT|O_EXCL,FILE_MODE,1);
sem_unlink(SEM_NAME);
setbuf(stdout,NULL);
if(fork() == 0)
{
for(i = -0;i<nloop;++i)
{
sem_wait(mutex);
printf("child: %d\n",(*ptr)++);
sem_post(mutex);
}
exit(0);
}
for(i = 0;i<nloop;++i)
{
sem_wait(mutex);
printf("parent: %d\n",(*ptr)++);
sem_post(mutex);
}
wait(NULL);
exit(0);
}
gcc demo1.c -o app2 -lpthread -stc=c99
./app2 app 10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#define SEM_NAME "mysem"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
//共享内存结构
struct shared
{
sem_t mutex; //信号量
int count; //计数器
}shared;
int main(int argc,char* argv[])
{
int fd,i,nloop;
struct shared *ptr;
if(argc != 3)
{
printf("usage: incrl <#loops>");
exit(0);
}
nloop = atoi(argv[2]);
fd = open(argv[1],O_RDWR|O_CREAT,FILE_MODE);
write(fd,&shared,sizeof(struct shared));
ptr = mmap(NULL,sizeof(struct shared),PROT_READ| PROT_WRITE,MAP_SHARED,fd,0);
if(ptr == MAP_FAILED)
{
perror("mmap() error");
exit(0);
}
close(fd);
sem_init(&ptr->mutex,1,1);
setbuf(stdout,NULL);
if(fork() == 0)
{
for(i = -0;i<nloop;++i)
{
sem_wait(&ptr->mutex);
printf("child: %d\n",ptr->count++);
sem_post(&ptr->mutex);
}
exit(0);
}
for(i = 0;i<nloop;++i)
{
sem_wait(&ptr->mutex);
printf("parent: %d\n",ptr->count++);
sem_post(&ptr->mutex);
}
wait(NULL);
exit(0);
}
gcc demo1.c -o app2 -lpthread -stc=c99
./app2 app 10
以上可以使用匿名映射
// 方式一
int *ptr;
ptr = mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED | MAP_ANON,-1,0);
// 方式二
int *ptr;
fd = open("dev/zero",O_RDWR);
ptr = mmap(NULL,sizeof(int),PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
浙公网安备 33010602011771号