【问题标题】:Using shared memory in a threads在线程中使用共享内存
【发布时间】:2014-07-14 21:17:54
【问题描述】:

您好,我想实现一个通过共享内存相互通信的客户端-服务器程序。在服务器端我有两个线程。一个写线程和一个读线程。写入线程将一些数据放入队列中,读取线程从中读取并将数据传递给客户端......

这是我的读者线程...问题是我的两个线程已成功创建,但它没有进入我在线程例程中指定的 while 循环...现在我的问题是:是否有可能在线程例程中使用共享内存时 线程被调用了吗?

    void *reader_thread(void * id)
    {
        .....

        shm_addr = shmat(shm_id,NULL,0);

        if(shm_addr==(void *)-1)
        {
                    perror("shmat error");
            exit(1);
        }

        while (1)
        {   
            printf("Here in thread reader!");
            sem_wait(&queue_t->full);
            sem_wait(&queue_t->mutex);
            if (queue_t->tail != queue_t->head)
            {
                   memmove(imgPath,queue_t->imgAddr[queue_t->head],strlen(queue_t->imgAddr[queue_t->head])-1);
                imgPath[strlen(queue_t->imgAddr[queue_t->head])-1] = '\0';
                queue_t->head = (queue_t->head + 1) % QUEUE_SIZE;
             }  
            sem_post(&queue_t->mutex);
            sem_post(&queue_t->empty);

            ...
                sem_wait(&shared->shm_sem);
                memset(shm_addr,0,SHMSZ);
                memcpy(shm_addr, &imgPath, sizeof(imgPath));    
                sem_post(&shared->shm_sem);  

        }   
        return 0;
    }

///////////////////////////////


int main(int argc , char *argv[])
{

    pthread_t writer_t, reader_t;

    queue_t = (struct Queue*) malloc(sizeof(Queue));
    queue_t->head = 0;
    queue_t->tail = 0;

    sem_init(&queue_t->empty, 0, QUEUE_SIZE);
    sem_init(&queue_t->full, 1, 0);
    sem_init(&queue_t->mutex, 1, 1);

    shared = (struct shared_mem*) malloc(sizeof(shared_mem));
    sem_init(&shared->shm_sem, 1, 1);   

    int shmid;
    key_t key; 
    char *shm_addr; 
    key=1234;

    //Create the segment and set permissions.
    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
    {
        perror("shmget error");
        if(errno==EEXIST)
                {
                        fprintf(stderr,"shared memory exist... ");
                        exit(1);
                }
    }
    fprintf(stdout,"shared mem created with id: %d\n",shmid);

    //Now we attach the segment to our data space.   
    if ((shm_addr = shmat(shmid, NULL, 0)) == (char *) -1)
    {
        perror("shmat error");
        exit(1);
    }

    // Zero out memory segment
    memset(shm_addr,0,SHMSZ);

    if( pthread_create( &reader_t , NULL , reader_thread , &shmid) < 0)
    {
        perror("could not create reader thread");
        return 1;
    }

    pthread_detach(reader_t);
    puts("reader thread assigned");

    if( pthread_create( &writer_t , NULL , writer_thread , NULL) < 0)
    {
        perror("could not create writer thread");
        return 1;
    }
    pthread_detach( writer_t);
        puts("writer thread assigned");

    //if(shmdt(shm_addr) != 0)
          //      fprintf(stderr, "Could not close memory segment.\n");
    shmctl(shmid,IPC_RMID,NULL);
    return 0;
}

【问题讨论】:

  • 有可能。你创建了 shm 吗?我只看到 shmat,服务器应该首先创建它,还有信号量
  • 是的,我已经在主函数中创建了它并将 shmid 传递给阅读器线程
  • 我是否认为在 main() 结束时您已经分离了 pthread 并破坏了共享内存区域......而 pthread 可能已经启动,也可能没有启动,可能启动也可能没有完成了吗?
  • 您的代码部分看起来不错。 shmget 命令可能是问题所在。你能发帖吗?如何从服务器获取 shm_id 到客户端?
  • 运行程序后可以在终端输入ipcs查看shm是否创建

标签: c multithreading shared-memory


【解决方案1】:

之前的问题已经解决了...我用 pthread_join 替换了 pthread_detach 并将 of 放在 main 的末尾。 但是现在我在客户端遇到了段错误:client get segmentation fault when attach to shared memory

【讨论】:

  • 我认为最好将您的解决方案作为自己的答案或通过对原始问题的更新来呈现。您的新问题最好作为另一个(单独的)问题发布,否则它不会作为问题对系统可见,并且没有人会注意到它......
猜你喜欢
  • 1970-01-01
  • 2012-11-29
  • 2014-04-19
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 2014-10-01
  • 2016-03-01
相关资源
最近更新 更多