LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2011, 05:45 AM   #1
chakki
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Rep: Reputation: 0
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);
}
 
Old 06-16-2011, 05:59 AM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
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.
 
Old 06-16-2011, 06:37 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by chakki View Post
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]?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a shared memory and some shared objects in linux? jeremy28 Programming 6 06-11-2010 06:21 AM
Forking and Shared Memory program. the_satsuma_man Programming 5 03-10-2009 07:51 PM
Linux shared memory segment access problem and x86 Virtual Memory layout. regmee Linux - Kernel 1 08-23-2008 01:11 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 05:05 AM


All times are GMT -5. The time now is 07:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration