LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   a program based on shared memory (https://www.linuxquestions.org/questions/linux-newbie-8/a-program-based-on-shared-memory-886676/)

chakki 06-16-2011 04:45 AM

a program based on shared memory
 
Please check these programs.....I got an error as segmentation fault...Coudn't find the mistake....
It is a program to just exchange a set of strings via shared memory....
//////////////////////////////
1st program
//////////////////////////////
shmclient2.c
/////////////////////////////


#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>

#define SIZE 100

int main()
{
char *shm[50],*s[50],n[10][10];
char msg[10]="end",change[10]="read";
key_t key;
char c;
int shmid,i;

key=7858;

/*locate the shared memory with key=7856.Here vomit the IPC_CREAT */

if((shmid=shmget(key,SIZE,0666))<0)
{
perror("shmget");
exit(1);
}

/*attach to our data space*/

if((shm[0]=shmat(shmid,0,0))==(char *)-1)
{
perror("shmat");
exit(1);
}

/*read data from shared memory*/

//for(i=0,s[0]=shm[0];s[i]!="end";i++)
i=0;
s[0]=shm[0];
while(strcmp(s[i],msg)!=0)
{
printf("%s\n",s[i]);
}


/*change the first character indicating that this reads data*/

strcpy(shm[0],change);
exit(0);
}
///////////////////////////
2nd program
///////////////////////////
shmserver2.c
///////////////////////////

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>

#define SIZE 100

int main()
{
char *shm[50],*s[50],n[50][50];
char msg[10]="end",change[10]="read";
key_t key;
char c;
int shmid,i,num;

key=7858;

/*create shared memory segment*/

if((shmid=shmget(key,SIZE,IPC_CREAT|0666))<0)
{
perror("shmget");
exit(1);
}

/*attach that to our data space*/

if((shm[0]=shmat(shmid,0,0))==(char *)-1)
{
perror("shmat");
exit(1);
}

/*put data on the shared memory*/

s[0]=shm[0];
printf("Enter 3 strings\n");
i=0;
do
{
printf("\nEnter string %d",i+1);
scanf("%s",n[i]);
strcpy(s[i],n[i]);
i++;
}while(i<3);
strcpy(s[i],msg);

while(strcmp(s[i],msg)!=0)
{
printf("\ntest :%s",s[i]);
}


/*wait until other process changes the first string in the memory indicating that it reads what we put*/

while(strcmp(shm[0],change)!=0)
sleep(1);
exit(0);
}

arizonagroovejet 06-16-2011 04:59 AM

Use CODE tags when posting code.
I don't think this belongs in Newbie. I mean it's not a post about being new to Linux and having some difficulty is it? If I were you I'd use the Report button to ask a mod to move it to the programming forum where it's more likely to be seen by people who are able to help you.

johnsfine 06-16-2011 05:37 AM

Quote:

Originally Posted by chakki (Post 4387338)
Please check these programs.....I got an error as segmentation fault...Coudn't find the mistake....

I think your problem is in the use of the array s[] in the second program:
Quote:

char *shm[50],*s[50],n[50][50];
s[] starts as an array of 50 uninitialized char*

Quote:

s[0]=shm[0];
Then s[0] points to the shared memory, while the rest of s[] is 49 uninitialized char*
Quote:

strcpy(s[i],n[i]);
i++;
The first time through, you copy the input to shared memory. Second time through, you copy it via uninitialized pointer, so likely seg fault.
Quote:

strcpy(s[i],msg);
I don't understand what you intend to accomplish with s[]. Why does it exist at all. Why aren't you copying directly to shm[0]?


All times are GMT -5. The time now is 06:45 AM.