linux下多进程共享资源互斥访问实例
2016-12-22 17:31
309 查看
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <pthread.h>
pthread_mutex_t* g_mutex;
//创建共享的mutex
void init_mutex(void)
{
int ret;
//g_mutex一定要是进程间可以共享的,否则无法达到进程间互斥
g_mutex=(pthread_mutex_t*)mmap(NULL, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if( MAP_FAILED==g_mutex )
//使用匿名存储映射,只能在具有亲缘关系的进程间使用,
{ //若要在非亲缘关系进程间使用需要mmap文件映射结合SHARED //标志或者共享内存IPC
perror("mmap");
exit(1);
}
//设置attr的属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
//一定要设置为PTHREAD_PROCESS_SHARED
//具体可以参考http://blog.chinaunix.net/u/22935/showart_340408.html
ret=pthread_mutexattr_setpshared(&attr,PTHREAD_PROCESS_SHARED);
if( ret!=0 )
{
perror("init_mutex pthread_mutexattr_setpshared");
exit(1);
}
pthread_mutex_init(g_mutex, &attr);
}
int main(int argc, char *argv[])
{
init_mutex();
int ret;
char str1[]="this is child process/r/n";
char str2[]="this is father process/r/n";
int fd=open("tmp", O_RDWR|O_CREAT|O_TRUNC, 0666);
if( -1==fd )
{
perror("open");
exit(1);
}
pid_t pid;
pid=fork();
if( pid<0 )
{
perror("fork");
exit(1);
}
else if( 0==pid )
{
ret=pthread_mutex_lock(g_mutex);
if( ret!=0 )
{
perror("child pthread_mutex_lock");
}
sleep(10);//测试是否能够阻止父进程的写入
write(fd, str1, sizeof(str1));
ret=pthread_mutex_unlock(g_mutex);
if( ret!=0 )
{
perror("child pthread_mutex_unlock");
}
}
else
{
sleep(2);//保证子进程先执行
ret=pthread_mutex_lock(g_mutex);
if( ret!=0 )
{
perror("father pthread_mutex_lock");
}
write(fd, str2, sizeof(str2));
ret=pthread_mutex_unlock(g_mutex);
if( ret!=0 )
{
perror("father pthread_mutex_unlock");
}
}
wait(NULL);
munmap(g_mutex, sizeof(pthread_mutex_t));
}
运行后tmp文件内容为:
this is child process
this is father process
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <pthread.h>
pthread_mutex_t* g_mutex;
//创建共享的mutex
void init_mutex(void)
{
int ret;
//g_mutex一定要是进程间可以共享的,否则无法达到进程间互斥
g_mutex=(pthread_mutex_t*)mmap(NULL, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if( MAP_FAILED==g_mutex )
//使用匿名存储映射,只能在具有亲缘关系的进程间使用,
{ //若要在非亲缘关系进程间使用需要mmap文件映射结合SHARED //标志或者共享内存IPC
perror("mmap");
exit(1);
}
//设置attr的属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
//一定要设置为PTHREAD_PROCESS_SHARED
//具体可以参考http://blog.chinaunix.net/u/22935/showart_340408.html
ret=pthread_mutexattr_setpshared(&attr,PTHREAD_PROCESS_SHARED);
if( ret!=0 )
{
perror("init_mutex pthread_mutexattr_setpshared");
exit(1);
}
pthread_mutex_init(g_mutex, &attr);
}
int main(int argc, char *argv[])
{
init_mutex();
int ret;
char str1[]="this is child process/r/n";
char str2[]="this is father process/r/n";
int fd=open("tmp", O_RDWR|O_CREAT|O_TRUNC, 0666);
if( -1==fd )
{
perror("open");
exit(1);
}
pid_t pid;
pid=fork();
if( pid<0 )
{
perror("fork");
exit(1);
}
else if( 0==pid )
{
ret=pthread_mutex_lock(g_mutex);
if( ret!=0 )
{
perror("child pthread_mutex_lock");
}
sleep(10);//测试是否能够阻止父进程的写入
write(fd, str1, sizeof(str1));
ret=pthread_mutex_unlock(g_mutex);
if( ret!=0 )
{
perror("child pthread_mutex_unlock");
}
}
else
{
sleep(2);//保证子进程先执行
ret=pthread_mutex_lock(g_mutex);
if( ret!=0 )
{
perror("father pthread_mutex_lock");
}
write(fd, str2, sizeof(str2));
ret=pthread_mutex_unlock(g_mutex);
if( ret!=0 )
{
perror("father pthread_mutex_unlock");
}
}
wait(NULL);
munmap(g_mutex, sizeof(pthread_mutex_t));
}
运行后tmp文件内容为:
this is child process
this is father process
相关文章推荐
- Linux下的多进程间共享资源的互斥访问
- Linux下的多进程间共享资源的互斥访问
- Linux下的多进程间共享资源的互斥访问
- Linux下的多进程间共享资源的互斥访问
- 进程间 对共享资源的访问 互斥 机制
- Window访问Linux共享"您可能没有权限使用网络资源"问题解决
- Linux\Unix IPC进程通信实例分析(一):共享内存通信---系统V
- linux-多线程-互斥锁在多进程共享
- linux下使用线程锁互斥访问资源
- Linux下进程的同步互斥实例——生产者消费者
- 实现在WINDOWS中访问LINUX共享资源
- 使用临界区CRITICAL_SECTION结构对象保护多线程互斥地访问共享资源
- 进程间使用信号量协调对共享资源访问c++代码实现
- Linux下进程的同步互斥实例——生产者消费者
- 使用Mutex实现Windows下进程间互斥访问同一资源
- Linux\Unix IPC进程通信实例分析(一):共享内存通信---文件映射mmap方式
- linux-多线程-互斥锁在多进程共享
- Linux系统上使用smbclient访问Windows共享资源
- Linux下进程的同步互斥实例——生产者消费者
- 解析进程为何不能访问网络资源(作者Bingle)