0

I have been working on semaphores and shared memory for a week now, and have some difficulties yet,so i tried to make this program which the children are supposed to write to a memory shared multidimensional integer array and the father is suppose to read that array from the memory that is shared.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h> 
#include <sys/shm.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <semaphore.h>
#include <sys/wait.h>

#define MAXCHILDS 1
#define MAX_SIZE 10
#define MAX_WRITES 100

typedef struct{
    int m[MAX_SIZE][MAX_SIZE];
}matrix;


/*fork variables*/
pid_t child[MAXCHILDS];
/*semphores variables */
sem_t *empty, *full, * mutex;
/*share memory id*/
int shmid;
/*shared memory array pointer */
matrix * sh_mem;




void init(){

     /*semaphores unlink and creation */    
     sem_unlink("EMPTY");
     empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,50);
     sem_unlink("FULL");
     full=sem_open("FULL",O_CREAT|O_EXCL,0700,0);
     sem_unlink("MUTEX");
     mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1);
    /*initialize shared memory */
    shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777);
    /*map shared memory*/
    sh_mem = (matrix*)shmat(shmid,NULL,0);
    if(sh_mem == (matrix*)(-1)){
        perror("shmat");
    }
}

void writer(int m[MAX_SIZE][MAX_SIZE],int n_child){
    int i,k;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            m[i][k] = 0;
            if(i==(k+1)){
                m[i][k] = 1;
            }
        }
    }

}
void reader(int m[MAX_SIZE][MAX_SIZE]){
    int i = 0;
    int k = 0;
    int sum = 0;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            printf("%d",m[k][i]);
        }
        sum++;
        printf("[i=]%d[k=]%d\n",i,k);
    }
    printf("%d",sum);

}

void terminate() {
  sem_close(empty);
  sem_close(full);
  sem_close(mutex);
  sem_unlink("EMPTY");
  sem_unlink("FULL");
  sem_unlink("MUTEX");
  shmctl(shmid, IPC_RMID, NULL);
}

int main(int argc, char **argv)
{
    int i,sum;
    init();

    for(i = 0;i<MAXCHILDS;i++){
        if((child[i]= fork()) < 0) // error occured
        {
            perror("Fork Failed");
            exit(1);
        }
        if((child[i] =fork())==0){
            writer(sh_mem->m,i);
            exit(0);
        }
    }

    /*father*/
    sleep(10);
    sum++;
    printf("%d\n",sum); 
    reader(sh_mem->m);
    wait(NULL);

    terminate();


    return 0;
}

I have two problems right now ... I need to save the array to a memory mapped file which i dont seem to understand and the output of the father is throwing is very strange ...His output is

0000100000[i=]3[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0100000000[i=]0[k=]10
0000000100[i=]6[k=]10
0010000000[i=]1[k=]10
0000000010[i=]7[k=]10
0001000000[i=]2[k=]10
0000000001[i=]8[k=]10
0000100000[i=]3[k=]10
0000000000[i=]9[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0000000100[i=]6[k=]10
0000000010[i=]7[k=]10
0000000001[i=]8[k=]10
0000000000[i=]9[k=]10

And it should be something like this:

0000100000[i=]0[k=]10
0000010000[i=]1[k=]10
0000001000[i=]2[k=]10
0100000000[i=]3[k=]10
0000000100[i=]4[k=]10
0010000000[i=]5[k=]10
0000000010[i=]6[k=]10
0001000000[i=]7[k=]10
0000000001[i=]8[k=]10
0000100000[i=]9[k=]10
2
  • A couple observations - first, you're creating a couple semaphores, but it doesn't appear that you're actually using them for anything, so your accesses to the shared memory appear to be completely unsynchronized; second, in writer(), you are referencing m[i][k], while in reader() you are referencing m[k][i]. Commented Nov 5, 2012 at 15:49
  • Duplicate: stackoverflow.com/questions/13224357/… Commented Mar 20, 2014 at 14:25

1 Answer 1

2

The code calls fork() in two places. Remove the second call.

Change:

    if((child[i] =fork())==0){

to:

    if(child[i] == 0){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.