简体   繁体   English

在线程中使用共享内存

[英]Using shared memory in a threads

Hi i want to implement a client-server program that communicates with each other via shared memory. 嗨,我想实现一个客户端服务器程序,该程序通过共享内存相互通信。 in the server side i have two threads. 在服务器端,我有两个线程。 a writer thread and a reader thread. 作家线程和读者线程。 the writer thread puts some data into the queue and the reader thread reads from it and passes the data to the client side... 编写器线程将一些数据放入队列,而读取器线程从队列中读取数据并将数据传递到客户端...

here is my reader thread ... the problem is that my two threads are created successfully but it does not get into the while loop i have specified in the thread routine... right now my question is: is it possible to work with shared memory in a thread routine when the thread is called? 这是我的阅读器线程...问题是我的两个线程创建成功,但是没有进入线程例程中指定的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;
}

the previous problem had resolved... i replaced pthread_detach with pthread_join and put of at the end of main. 先前的问题已解决...我用pthread_join替换了pthread_detach,并将其放在main的末尾。 but rigt now i encountered segfault at client Side: client get segmentation fault when attach to shared memory 但现在我在客户端遇到segfault: 附加到共享内存时 ,客户端出现分段错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM