Solved

matrix addition using shared memory

Posted on 2010-11-23
4
1,063 Views
Last Modified: 2012-05-10
I want to add 2 matrices using shared memory and parallel processes, in linux, but my code doesnt run correctly .
Where is the problem?
#include <stdio.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <stdlib.h>



int main ()

{



int idA,idB,idC, child_id;

int *A,*B, *C;//the 3 arrays

int* child_memory;

int *pA, *pB, *pC;

const int size = 4096;

int i;

//allocate shared memory for an array of 5 integers

idA = shmget(IPC_PRIVATE, 5*sizeof(int), S_IRUSR | S_IWUSR);

idB = shmget(IPC_PRIVATE, 5*sizeof(int), S_IRUSR | S_IWUSR);

idC = shmget(IPC_PRIVATE, 5*sizeof(int), S_IRUSR | S_IWUSR);



//attach shared memory to parent

A = (int *) shmat(idA, NULL, 0);

B = (int *) shmat(idB, NULL, 0);

C = (int *) shmat(idC, NULL, 0);

for(i=0; i<5; i++){

         A[i]=1;

         B[i]=2;

         C[i]=0;

         }

for (i=0; i<5; i++){

    printf("A[%d]=%d B[%d]=%d  C[%d]=%d\n",i,A[i],i,B[i],i,C[i]);

}

printf("\n"); 

for(i=0;i<5; i++){

   child_id=fork();

   if (child_id==0){

       A = (int*) shmat(idA, NULL, 0);

       B = (int*) shmat(idB, NULL, 0);

       C = (int*) shmat(idC, NULL, 0);             

       C[i] = A[i] + B[i];

       

   }

   else if (child_id>0){

    wait(0);

        

   }

   else {perror("fork");exit(1);}

}   

printf("C[%d] = %d ", i,C[i]);       



shmdt(A);shmdt(B);shmdt(C);



shmctl(idA, IPC_RMID, NULL);

shmctl(idB, IPC_RMID, NULL);

shmctl(idC, IPC_RMID, NULL);

return 0;

}

Open in new window

0
Comment
Question by:dimi67
  • 2
  • 2
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 34196336
Could you elaborate a bit on how it doesn't run correctly ? What do you observe ? What did you expect instead ?
0
 

Author Comment

by:dimi67
ID: 34197385
.if you run the code you will see that t values of array C don't change at all...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34197828
>> printf("C[%d] = %d ", i,C[ i]);  

At this point, i = 5. That'll show an out-of-range value from the array. You probably intended to put this in a loop for i from 0 to 4.

You probably also want to end the children (ie. exit) once they've done their job.

And you probably also want to let the parent wait until the children have done their job.
0
 

Author Closing Comment

by:dimi67
ID: 34198875
ok men, I fixed it with exit(0) at the end of the child...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Migrating an SQL 2008 database to Oracle 12c 3 88
Sendmail STARTTLS error 37 79
Assigning default value to structure in C for mutithread application 17 55
outlook office 365 8 82
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now