Need some help with shared memory and fork system call

This is a discussion on Need some help with shared memory and fork system call within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Need some help with shared memory and fork system call

    Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the multi-processes will be created using fork. Each process will calculate a partition of the final Matrix

    Now, I'm not sure whether I should write all the matrices to the shared memory or just a single integer to keep track of what partition to calculate. I'm also not sure if I'm using fork correctly, as my print commands print out twice each instead of once.

    Code:
    #include <stdio.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    
    #define DIM 4
    #define NUM_OF_FORK_CALLS 2
    #define NUM_OF_PROC 4
    
    int main()
    {
        pid_t pid;
        int segment_id;
        int *partition;
        *partition = 0;
        int i;
        int j;
        int matrixA[DIM][DIM];
        int matrixB[DIM][DIM];
        int matrixC[DIM][DIM];
    
        //allocating a shared memory segment
        segment_id = shmget(IPC_PRIVATE, sizeof(int)*(DIM*DIM)*3, IPC_CREAT|0666);
    
        //attach the shared memory segment to the partition variable
        partition = (int *) shmat(segment_id, NULL,0);
    
        //fill the matrices
        for(i = 0; i < DIM; i++)
        {
    	for(j = 0; j < DIM; j++)
    	{
    		matrixA[i][j] = i + j;
    		matrixB[i][j] = i + 3;
    	}
        }
    
        for(i = 0; i < NUM_OF_FORK_CALLS; i++)
        {
       	pid = fork();
        }
    
    
        //error occurred
        if(pid < 0)
        {
            fprintf(stderr, "Fork Failed");
            return 1;
        }
    
        //child process
        else if(pid==0)
        {
    
            int start = (*partition * DIM)/NUM_OF_PROC;
            /*The start position will tell which row to start calculating */
    
            int end = ((*partition+1) * DIM)/NUM_OF_PROC;
            /*The end position wil tell which row to stop calculating*/
    
            int i;
            int j;
            int k;
    
             //These for loops will calculate a partition of C
            for(i = start; i<end; i++)
            {
                for(j = 0; j < DIM; j++)
                {
                    matrixC[i][j] = 0;
    
                    for(k = 0; k < DIM; k++)
                    {
                         matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
                    }
                 }
            }
    
        }
    
        //parent process
        else
        {
    
            wait(NULL);
    
            int i;
            int j;
    
            //print matrix A
            for(i = 0; i < DIM; i++)
            {
                for(j = 0; j<DIM; j++) 
                {
                    printf("%d ",matrixA[i][j]);  
                }
    
                printf("\n");  
            }
    
            printf("-----------\n\n");
    
            //print matrix B
            for(i = 0; i < DIM; i++)
            {
                for(j = 0; j<DIM; j++)  
                {
                    printf("%d ",matrixB[i][j]);  
                }
    
                printf("\n");  
            }
    
             printf("-----------\n\n");
    
            //print matrix C
            for(i = 0; i < DIM; i++)
            {
                for(j = 0; j<DIM; j++) 
                {
                    printf("%d ",matrixC[i][j]);  
                }
    
                printf("\n"); 
            }
             printf("-----------\n\n");
    
        }
    
        return 0;
    }
    Any help would be appreciated, thanks in advance

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing shared memory via forked processes
    By rklockow in forum C Programming
    Replies: 7
    Last Post: 06-30-2010, 05:44 PM
  2. BSD mmap for shared memory - am I right?
    By sean in forum Linux Programming
    Replies: 21
    Last Post: 03-09-2009, 01:57 PM
  3. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 03:45 PM
  4. Memory freeing after exec call?
    By Sethiorth in forum C Programming
    Replies: 4
    Last Post: 10-02-2004, 03:59 PM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21