1

In this work, I'm supposed to learn multithreading and inter"process" communication, (which I've learned from Tanenbaum's book). I have two processes, which I'm trying to share information between. I'm just beginning to crawl in systems programming, so, my code is missing deallocation of data etc, which I'll fix later on.

I am guessing but not sure what my problem is; in the server code, I've built a struct region, which contains 2 other struct pointers. I share struct pointers between processes. But, I create struct arrays inside the server and point those shared pointers to there. I get segmentation error because the problem is that other processes are not allowed to read the array. If my point is correct, I need to share arrays as well. But I can't share since they are dynamically allocated. I'm not allowed to use sys/shm.h header.

first the server code

#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);
}

Now, the client code;

#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);
}
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
b.g.
  • 847
  • 8
  • 14

0 Answers0