put an 2D array in shared memory


 
Thread Tools Search this Thread
Top Forums Programming put an 2D array in shared memory
# 1  
Old 12-03-2011
put an 2D array in shared memory

Hi,
I want to make 2 simple programs communicate each other with shared memory.
The programs will share an 2D array. In the first program (intarr.c) i create an x[3][4] array which is:
1 2 3 4
5 6 7 8
9 10 11 12
and i call the second program (intarr2.c) with execvp(); giving it the segment id as an argument. The intarr2.c just print the array to the terminal.
Code:
/*intarr.c*/
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>
main()
{
  int segid,i,j,y;
  int *x[3][4];
  segid=shmget(IPC_PRIVATE,512,S_IRUSR|S_IWUSR);
  x[3][4]=(int *)shmat(segid,NULL,0);
  y=1;
  for(i=0;i<3;i++)
  {
    for(j=0;j<4;j++)
    {
      *x[i][j]=y;
      y++;
    }
  }
  char str[100+1] = {'\0'};
  sprintf( str, "%d", segid );
  const char *argv[]={"./intarr2.out",str,(char *)NULL};
  execvp("./intarr2.out",argv);
  shmdt(x);
  shmctl(segid,IPC_RMID,NULL);
  return 0;
}

Code:
/*intarr2.c*/
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>

main(int argc,char *argv[])
{
  int *x[3][4];
  x[3][4]=(int *)shmat(atoi(argv[1]),NULL,0);
  int i,j;
  for(i=0;i<3;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",*x[i][j]);
    }
    printf("\n");
  }
}

Code:
/*Terminal*/
x@ubuntu:~/x$ gcc intarr2.c -o intarr2.out
x@ubuntu:~/x$ gcc intarr.c
x@ubuntu:~/x$ ./a.out
Segmentation fault

With this code i dont get any warning or error with gcc.
But the ./a.out gives me Segmentation fault.
Any ideas?Smilie
# 2  
Old 12-03-2011
Probably operator precedence, among other problems.

Code:
  int *x[3][4];

If with that you're trying to define "x" as a pointer to a 3x4 array, I think offhand what you're actually getting is a 3x4 array of integer pointers. I'd guess that's probably not what you want.

And this:

Code:
  x[3][4]=(int *)shmat(atoi(argv[1]),NULL,0);

That assigns the address of the shared memory segment to the [3][4] element of your array. That's almost certainly NOT what you want to do.

This definition might work, but I'm not going to dig through all the C operator precedence rules to find out:

Code:
int *(x[3][4]);

You can always use typedef's to make it easier, because then you can build your complex declaration step-by-step:
Code:
typedef int intFourArray[4];
typedef intfourarray intThreeByFourArray[3];
typedef intThreeByFourArray *intThreeByFourArrayPtr;

This User Gave Thanks to achenle For This Post:
# 3  
Old 12-03-2011
I google search it a little and i create this code which does the job :
Code:
/*intarr.c*/
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>
main()
{
  int rows=3;
  int columns=4;
  int *matrix;
  int segid,y,i,j;
  segid=shmget(IPC_PRIVATE,sizeof(int)*rows*columns,S_IRUSR|S_IWUSR);
  matrix = (int *)shmat(segid,NULL,0);
  y=1;
  for (i = 0; i < rows; i++)
  {
    for (j = 0; j < columns; j++)
    {
        matrix[i*columns + j] = y;
        y++;
    }
  }
  char strid[100+1] = {'\0'};
  sprintf( strid, "%d", segid );
  char strrow[100+1] = {'\0'};
  sprintf( strrow, "%d", rows );
  char strcolumn[100+1] = {'\0'};
  sprintf( strcolumn, "%d", columns );
  const char *argv[]={"./intarr2.out",strid,strrow,strcolumn,(char *)NULL};
  execvp("./intarr2.out",argv);
  shmdt(matrix);
  shmctl(segid,IPC_RMID,NULL);
  return 0;
}
/*intarr2.c*/
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>

main(int argc,char *argv[])
{
  int row;
  int column;
  row=atoi(argv[2]);
  column=atoi(argv[3]);
  int *matrix;
  matrix=(int *)shmat(atoi(argv[1]),NULL,0);
  int i,j;
  for(i=0;i<3;i++)
  {
    for(j=0;j<4;j++)
    {
      printf("%d\t",matrix[i*column+j]);
    }
    printf("\n");
  }
}

If there is a more correct way i'm willing to know ...
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

2. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

3. AIX

shared memory

1.How to know wich process is using the shared memory? 2.How to flush (release) the process from the shared memory? (1 Reply)
Discussion started by: pchangba
1 Replies

4. UNIX for Advanced & Expert Users

Shared Memory

Hi, Using ipcs we can see shared memory, etc.. details. How can I add/remove shared memory(command name)? Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

5. UNIX for Advanced & Expert Users

Writing into shared memory Array

Hi All, How do I write into shared memory blocks using multiple threads? For example, I have created 50 threads to write into shared memory and 50 threads to read from shared memory. I have already created a shared memory and I want to write into shared memory as follows: ... (0 Replies)
Discussion started by: kumars
0 Replies

6. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

7. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

8. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

9. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies

10. Programming

Shared memory

Dear Reader, Is is necessary to attach / dettach the shared memory segments for write operations , if more than one program is accessing same shared memory segments.. I have used semaphore mutex and still I'm getting segmentation fault when I write to the segment when other program is already... (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question