【求助】linux不同进程使用共享内存及互斥锁

下面是一个 在 Linux不同进程间,使用共享内存的例子,但是程序总不能按预期运行。特求助高手,帮忙看看问题在哪里?

进程A:

#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pthread.h>

//本进程后启动 shm_open +mmap 方式
#define SHARE_MEMORY_SIZE 80
struct sharedmemory
{
	pthread_mutex_t mutex;
	pthread_cond_t isReadCond;
	pthread_cond_t isWriteCond;
	int condition;
	char data[SHARE_MEMORY_SIZE];
};
#define SHAREDMEMORYFILE "data.d"

int main()
{
	int fd;
	int i=0;
	int sharedSize;
	struct sharedmemory *ptr;
	sharedSize = sizeof(struct sharedmemory);
	printf("share memory total size %d,real size:%d\n",sharedSize,SHARE_MEMORY_SIZE);
	fd=shm_open(SHAREDMEMORYFILE,O_RDWR,0777);
	ptr=mmap(NULL,sharedSize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	close(fd);
	if(ptr==MAP_FAILED)
	{
		printf("open shared failed \n");
		exit(0);
	}
	while(1)
	{
		
		pthread_mutex_lock(&ptr->mutex);
		while(ptr->condition == 0)
		{
			printf(">>>>>>>>>>process B  waiting for read condition become to 1\n");
			pthread_cond_wait(&ptr->isReadCond,&ptr->mutex);
		}
		printf(">>>>>>>>>>process B Read Data is:%s\n",ptr->data);
		/*ptr->condition =0;
		printf(">>>>>>>>>>process B send signal for processA write data!\n");
		pthread_cond_signal(&ptr->isWriteCond);
		*/
		ptr->condition =0;
		pthread_mutex_unlock(&ptr->mutex);
               
		
		ptr->condition =0;
		printf(">>>>>>>>>>process B Modify Cond:%d\n",ptr->condition);
		printf(">>>>>>>>>>process B send signal for processA write data!\n");
		pthread_cond_signal(&ptr->isWriteCond);
		

		if(strcmp(ptr->data,"exit")==0)
		{
			break;
		}
		i++;
	}
	shm_unlink(SHAREDMEMORYFILE);
	exit(0);
	return 0;
}

进程B:

#include <sys/mman.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <pthread.h>


//本进程首先启动 shm_open +mmap 方式
#define SHARE_MEMORY_SIZE 80
#define SHAREDMEMORYFILE "data.d"

 struct sharedmemory
 {
    pthread_mutex_t mutex;
    pthread_cond_t isReadCond;
    pthread_cond_t isWriteCond;
    int condition;
    char data[SHARE_MEMORY_SIZE];
 };

 
   pthread_mutexattr_t mutexAttr;
  
   pthread_condattr_t condAttr;
 void InitProcessLock(struct sharedmemory *ptr)
 {
	 if(ptr ==NULL)
		 return;
    pthread_mutexattr_init(&mutexAttr);
    pthread_mutexattr_setpshared(&mutexAttr,PTHREAD_PROCESS_SHARED);
    pthread_condattr_init(&condAttr);
    pthread_condattr_setpshared(&condAttr,PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(&ptr->mutex,&mutexAttr);
    pthread_cond_init(&ptr->isReadCond,&condAttr);
    pthread_cond_init(&ptr->isWriteCond,&condAttr);
	
 }
 
 
 int main()
 {
    int fd;
    int i=0;
    int memorySize;
    
    struct sharedmemory *ptr;
    memorySize=sizeof(struct sharedmemory);
    printf("share memory total size %d,real size:%d\n",memorySize,SHARE_MEMORY_SIZE);
	
    fd=shm_open(SHAREDMEMORYFILE,O_CREAT|O_TRUNC|O_RDWR,0777);
    if(-1==fd)
    {
        printf("Shared shm_open\n");
        exit(0);
    }
    if(ftruncate(fd,memorySize))
    {
        printf("Shared ftruncate\n");
        exit(0);
    }
    ptr=mmap(NULL,memorySize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if(ptr==MAP_FAILED)
    {
        printf("Shared MAP_FAILED\n");
        exit(0);
    }
    close(fd);
	
	
	//when ptr->condition ==0 we can write data to mem 
    ptr->condition=0;
    strcpy(ptr->data,"");
    while(1)
    {
	printf("we are begin loop %d\n",i);
        pthread_mutex_lock(&ptr->mutex);
        while(ptr->condition != 0)
        {
            printf("++++++++++process A waiting for write condition become to true! %d\n",ptr->condition);
            pthread_cond_wait(&ptr->isWriteCond,&ptr->mutex);
        }
        gets(ptr->data);
		//改变条件
	ptr->condition=1;
	//pthread_cond_signal(&ptr->isReadCond);
        pthread_mutex_unlock(&ptr->mutex);
        
        printf("++++++++++parent change Condition value:%d\n",++ptr->condition);
	pthread_cond_signal(&ptr->isReadCond);
       
        if(strcmp(ptr->data,"exit")==0)
        {
	    printf("+++++++++++parent Current %d loop is over!\n\n\n",i);
            break;
        }
        i++;
    }
    shm_unlink(SHAREDMEMORYFILE);
    exit(0);
    return 0;
 }

程序应该先启动进程B,后启动进程A。B进程向共享内存中写数据,A进程负责读数据,启动进程A后,就会发现A的输出是不太正常的。AB进程也不能交替读写。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值