下面是一个 在 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进程也不能交替读写。