Sponsored Content
Top Forums Programming put an 2D array in shared memory Post 302579012 by giampoul on Saturday 3rd of December 2011 09:20:27 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
mlib_MatrixMulShift_S16_S16_Mod(3MLIB)			    mediaLib Library Functions			    mlib_MatrixMulShift_S16_S16_Mod(3MLIB)

NAME
mlib_MatrixMulShift_S16_S16_Mod, mlib_MatrixMulShift_S16_S16_Sat, mlib_MatrixMulShift_S16C_S16C_Mod, mlib_MatrixMulShift_S16C_S16C_Sat - matrix multiplication plus shifting SYNOPSIS
cc [ flag... ] file... -lmlib [ library... ] #include <mlib.h> mlib_status mlib_MatrixMulShift_S16_S16_Mod(mlib_s16 *z, const mlib_s16 *x, const mlib_s16 *y, mlib_s32 m, mlib_s32 l, mlib_s32 n, mlib_s32 shift); mlib_status mlib_MatrixMulShift_S16_S16_Sat(mlib_s16 *z, const mlib_s16 *x, const mlib_s16 *y, mlib_s32 m, mlib_s32 l, mlib_s32 n, mlib_s32 shift); mlib_status mlib_MatrixMulShift_S16C_S16C_Mod(mlib_s16 *z, const mlib_s16 *x, const mlib_s16 *y, mlib_s32 m, mlib_s32 l, mlib_s32 n, mlib_s32 shift); mlib_status mlib_MatrixMulShift_S16C_S16C_Sat(mlib_s16 *z, const mlib_s16 *x, const mlib_s16 *y, mlib_s32 m, mlib_s32 l, mlib_s32 n, mlib_s32 shift); DESCRIPTION
Each of these functions performs a multiplication of two matrices and shifts the result. For real data, the following equation is used: l-1 z[i*n + j] = {SUM (x[i*l + k] * y[k*n + j])} * 2**(-shift) k=0 where i = 0, 1, ..., (m - 1); j = 0, 1, ..., (n - 1). For complex data, the following equation is used: l-1 z[2*(i*n + j)] = {SUM (xR*yR - xI*yI)} * 2**(-shift) k=0 l-1 z[2*(i*n + j) + 1] = {SUM (xR*yI + xI*yR)} * 2**(-shift) k=0 where xR = x[2*(i*l + k)] xI = x[2*(i*l + k) + 1] yR = y[2*(k*n + j)] yI = y[2*(k*n + j) + 1] i = 0, 1, ..., (m - 1) j = 0, 1, ..., (n - 1) PARAMETERS
Each of the functions takes the following arguments: z Pointer to the first element of the result matrix, in row major order. x Pointer to the first element of the first matrix, in row major order. y Pointer to the first element of the second matrix, in row major order. m Number of rows in the first matrix. m > 0. l Number of columns in the first matrix, and the number of rows in the second matrix. l > 0. n Number of columns in the second matrix. n > 0. shift Right shifting factor. 1 <= shift <= 16. RETURN VALUES
Each of the functions returns MLIB_SUCCESS if successful. Otherwise it returns MLIB_FAILURE. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
mlib_MatrixMul_U8_U8_Mod(3MLIB), attributes(5) SunOS 5.10 10 Nov 2004 mlib_MatrixMulShift_S16_S16_Mod(3MLIB)
All times are GMT -4. The time now is 04:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy