在线程中使用共享内存

时间:2014-07-14 21:17:54

标签: c multithreading shared-memory

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

这是我的读者线程...问题是我的两个线程已成功创建,但它没有进入我在线程例程中指定的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;
}

1 个答案:

答案 0 :(得分:0)

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