IPC共享结构数组

时间:2017-04-15 20:11:18

标签: c arrays unix struct

在这项工作中,我应该学习多线程和内部过程"沟通,(我从Tanenbaum的书中学到的)。 我有两个流程,我试图在两者之间共享信息。 我刚刚开始在系统编程中爬行,所以,我的代码缺少数据的重新分配等,我稍后会修复它。

我在猜,但不确定我的问题是什么;在服务器代码中,我构建了一个结构区域,其中包含2个其他结构指针。我在进程之间共享结构指针。 但是,我在服务器内部创建了struct数组,并将那些共享指针指向那里。我得到分段错误,因为问题是不允许其他进程读取数组。 如果我的观点是正确的,我也需要共享数组。但我无法分享,因为它们是动态分配的。我不允许使用sys / shm.h标头。

首先是服务器代码

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

#define NUMBER_OF_THREADS 5
/*https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux*/
void (*originalQuitHandler) (); // to exit safely by deallocating the system resources by ctrl+c
void quitHandler(); // this function is going to be called by the ctrl+c      signal given by the system.
void *print_hello_world(void * tid)
{
    /* This function prints the thread’s identifier and then exits. */
    printf("Hello World. Greetings from thread %ld\n", (long)tid);
    pthread_exit(NULL);
}
#define MAX_LEN 10
struct region {        /* Defines "structure" of shared memory */
    int len;
    struct client_message *ptr_client_message;
    struct server_message *ptr_server_message;
};
struct client_message {
    pthread_t client_id;
    int question;
};
struct server_message {
    pthread_t client_id;
    pid_t server_id;
    int answer;
};

int main(int argc, char* argv[])
{
    struct region *rptr;
    int fd;
    fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    if (fd == -1)
        printf("error creating shm\n");

    if (ftruncate(fd, sizeof(struct region)) == -1)
        printf("error creating ftruncate\n");
    /* Map shared memory object */

    rptr = mmap(NULL, sizeof(struct region),
                PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (rptr == MAP_FAILED)
        printf("error in mmap\n");

    rptr->len = MAX_LEN;
    struct client_message clientMessageArray[rptr->len];
    rptr->ptr_client_message = clientMessageArray;
    struct server_message serverMessageArray[rptr->len];
    rptr->ptr_server_message = serverMessageArray;

    /* The main program creates 10 threads and then exits. */
    pthread_t threads[NUMBER_OF_THREADS];
    originalQuitHandler = signal (SIGINT, quitHandler);
    int status, i, j=0;
    while (1)
    {
        for (i = 0; i < NUMBER_OF_THREADS; i++) {
            printf("Main here. Now. Creating thread %d\n", i);
            status = pthread_create(&threads[i], NULL, print_hello_world, (void * )i);
            sleep (1);
            (rptr->ptr_client_message[(++j)%MAX_LEN]).question = 30;
            printf("rptr len is %d and question of client %d is: %d\n", rptr->len, j%MAX_LEN, (rptr->ptr_client_message[(j)%MAX_LEN]).question);
            if (status != 0) {
                printf("Oops. pthread create returned error code %d\n", status);
                //exit(-1);
            }
        }
    }
    exit(0);
}
void quitHandler()
{
    printf("stopped by ctrl + c \n");
    exit(1);
}

现在,客户端代码;

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
/*https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux*/
void (*originalQuitHandler) ();
// to exit safely by deallocating the system resources by ctrl+c
void quitHandler(); // this function is going to be called by the ctrl+c signal given by the system.
struct region {        /* Defines "structure" of shared memory */
    int len;
    struct client_message *ptr_client_message;
    struct server_message *ptr_server_message;
};
struct client_message {
    pthread_t client_id;
    int question;
};
struct server_message {
    pthread_t client_id;
    pid_t server_id;
    int answer;
};

int main(int argc, char* argv[])
{
    struct region *rptr;
    int fd;
    fd = shm_open("/myregion", O_RDWR, S_IRUSR | S_IWUSR);
    if (fd == -1)
        printf("error creating shm\n");

    /* Map shared memory object */

    rptr = mmap(NULL, sizeof(fd),
           PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (rptr == MAP_FAILED)
        printf("error in mmap\n");

    /* The main program creates 10 threads and then exits. */
    originalQuitHandler = signal (SIGINT, quitHandler);
    int j=0;
    while(1)
    {
        printf("%d \n",rptr->len);
//Segmentation error occurs in the next line
        printf("rptr len is %d and question of client %d is: %d\n", rptr->len, j%rptr->len, (rptr->ptr_client_message[(++j)%rptr->len]).question);
        sleep (1);
    }
    exit(0);
}
void quitHandler()
{
    printf("stopped by ctrl + c \n");
    exit(1);
}

0 个答案:

没有答案