使用互斥锁但缺少数据的两个进程之间共享内存

时间:2012-12-12 12:51:55

标签: linux ipc mutex shared-memory

using example我创建了两个进程master和slave,来测试共享内存IPC。 Master创建共享内存并开始写入,并且在从属连接一段时间后,这是有效的,但onces slave连接它不接收/获取主写入共享内存的所有数据。

主代码如下所示:


    typedef struct custom_data_s {
      int   min;
      int   max;
      /* for shared */
      pthread_mutex_t ipc_mutex;
      pthread_cond_t ipc_condvar;
    } custom_data_t;

    int main(void) {
      int       fd = -1;
      custom_data_t *this_custom_data;
      pthread_mutexattr_t mutex_attr;
      pthread_condattr_t cond_attr;

      fd = shm_open("/A_CUSTOM_DATA", O_RDWR | O_CREAT | O_EXCL , (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
      if(fd == -1) {
        printf("ERROR fd %d %s\n",fd,strerror(errno));
      }

     if (ftruncate (fd,sizeof(custom_data_t)) == -1) {
        printf("ERROR trucate fd %d %s\n",fd,strerror(errno)); 
        exit(1);
      }

      this_custom_data = (custom_data_t *) mmap(NULL, sizeof(custom_data_t), PROT_READ | PROT_WRITE , MAP_SHARED ,fd ,0);
      if(this_custom_data ==(custom_data_t *) -1) {
        printf("ERROR mapping fd %d %s\n",fd,strerror(errno));
        exit(1);
      }
      close(fd);

      pthread_mutexattr_init(&mutex_attr);
      pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
      pthread_mutex_init(&this_custom_data->ipc_mutex, &mutex_attr);

      pthread_condattr_init(&cond_attr);
      pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
      pthread_cond_init(&this_custom_data->ipc_condvar, &cond_attr);

     for (fd=0; fd != 100000; fd++) {
        pthread_mutex_lock(&this_custom_data->ipc_mutex);
        this_custom_data->min = fd;
        this_custom_data->max = fd+5;
        pthread_cond_signal(&this_custom_data->ipc_condvar);
        pthread_mutex_unlock(&this_custom_data->ipc_mutex);
      }
      /* Clean up and exit  should check exit codes of all*/
      pthread_mutexattr_destroy(&mutex_attr); 
      pthread_condattr_destroy(&cond_attr);

      pthread_cond_destroy(&this_custom_data->ipc_condvar);
      pthread_mutex_destroy(&this_custom_data->ipc_mutex);

      if(0 != munmap(this_custom_data, sizeof(custom_data_t))) {
        printf("ERROR unmapping %s\n",strerror(errno));
        exit(1);
      }  
      if (0 != shm_unlink("/A_CUSTOM_DATA")){
        printf("ERROR unlinking %s\n",strerror(errno));
        exit(1);
      }
      return 0;
    }

例如,主设备开始将min和max写入共享内存从1到10000经过一段时间后,slave连接一旦slave连接它应该读取所​​有由master写入的数据但是如果在slave中连接的代码,它仍然不能读取所有数据数据,我做错了什么?是否应该有另一个奴隶设置的条件变量?我正在尝试学习共享内存,我认为我做错了或不了解互斥和共享内存。在slave中我正在等待条件变量设置,这里是slave的代码。


    typedef struct custom_data_s {
      int   min;
      int   max;
      /* for shared */
      pthread_mutex_t ipc_mutex;
      pthread_cond_t ipc_condvar;
    } custom_data_t;

    int main(void) {
      int       fd = -1;
      custom_data_t *this_custom_data_ptr;
      custom_data_t this_data;
      int prv_packet = 0;

      fd = shm_open("/A_CUSTOM_DATA", O_RDWR , (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
      if(fd == -1) {
        printf("ERROR fd %d %s\n",fd,strerror(errno));
      }

      if (ftruncate (fd,sizeof(custom_data_t)) == -1) {
        printf("ERROR trucate fd %d %s\n",fd,strerror(errno)); 
        exit(1);
      }

      this_custom_data_ptr = (custom_data_t *) mmap(NULL, sizeof(custom_data_t), PROT_READ | PROT_WRITE , MAP_SHARED ,fd ,0);
      if(this_custom_data_ptr ==(custom_data_t *) -1) {
        printf("ERROR mapping fd %d %s\n",fd,strerror(errno));
        exit(1);
      }
      close(fd);
    while (1) {
        pthread_mutex_lock(&this_custom_data_ptr->ipc_mutex);
        pthread_cond_wait(&this_custom_data_ptr->ipc_condvar, &this_custom_data_ptr->ipc_mutex);
        memcpy(&this_data, this_custom_data_ptr, sizeof(this_custom_data_ptr));
        if (prv_packet == 0){
          printf ("got first ");
          prv_packet = this_data.min;
        }
        if ((prv_packet +1) != this_data.min){
          printf ("error prv:%d this:%d\n", prv_packet, this_data.min);
        }
        pthread_mutex_unlock(&this_custom_data_ptr->ipc_mutex);
        prv_packet = this_data.min;
      }
      return 0;
    }

我做错了什么?如何进行同步,以便一旦连接从站,它将不会丢失任何数据,但如果未连接,则主站也不会被阻止。

0 个答案:

没有答案