LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2014, 03:48 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Error in Communication between two process using shared memory


Forgive me for not using quote, bold and code format due to technical limitation in my system.

I am learning shared memory.I written two file
In 1stfile I am creating segments and writing a string on segment

In second file I am trying to read that shared memory data and trying to print the string.

Error I am getting is segmentation fault while running 2nd file .
Please guide me in this


/* 1st program:shared.c*/

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


#define SHM_SIZE 2048 /* make it a 2K shared memory segment */

int main()
{
key_t key;
int shmid;
char *data;

if ((key = ftok("p.c", 'R')) == -1) {
perror("ftok");
exit(1);
}

if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT )) == -1) {
perror("shmget");
exit(1);
}
printf("shmid = %d\n", shmid);

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

printf("Enter A String:");
gets(data);
printf("\nString %s\n", data);
printf("Address %u\n",data);
printf("Integer val %u",data);


if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}






/* 2nd programro.c*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

int main()
{
key_t key;
char *data;
int shmid;

key = ftok("p.c",'T');
shmid = shmget(key, 2048, IPC_EXCL);
printf("shmid = %d\n", shmid);
printf("key = %ld\n", key);

data =(char *)shmat(shmid,0,0);
printf("Entered Shared Memory Data: %u",data);
printf("Entered Shared Memory Data: %s",data);


return 0;
}
 
Old 11-20-2014, 05:22 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Forgive me for not reading posts whose poster didn't use [code] and [/code] tags.
 
Old 11-20-2014, 09:42 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by srinietrx View Post
Forgive me for not using quote, bold and code format due to technical limitation in my system.
Your system doesn't have "[" and "]" keys on your keyboard?
 
Old 11-21-2014, 05:31 AM   #4
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
I got solved this error by hard coding the key directly in 1st argument of shmget.
Actually ftok was generating different key in different program.Hence segmentation fault

@Nevem Teve.I will stick to rules here after.

@psionl0. Normally I just highlight the text and select tag using mouse.
Thanks you for helping me out that we can do in keyboard.
 
Old 11-21-2014, 08:13 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start:

1. Use these compiler options: -g -W -Wall -Wextra -pedantic -Werror

2. At shmget (first program):
Code:
old: if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT )) == -1) {
new: if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0600)) == -1) {
3. At shmget (second program):
Code:
old: shmid = shmget(key, 2048, IPC_EXCL);
old: shmid = shmget(key, 2048, 0);
     if (shmid == -1) exit (8);

4. At ftok (second program):
Code:
old: key = ftok("p.c",'T');
new: key = ftok("p.c",'R');
 
  


Reply



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
Shared memory communication program pratham29 Programming 6 12-03-2013 02:28 PM
[SOLVED] Shared memory process for caching audio, can anyone help jonshouse Programming 0 04-07-2013 07:45 PM
[SOLVED] Free shared memory of dead process parrotfortytwo Linux - Newbie 8 11-03-2010 02:56 AM
Creating more than one shared memory by a process anoopmenons Linux - Embedded & Single-board computer 1 11-15-2009 12:11 PM
Creating more than one shared memory by a process anoopmenons Linux - Software 1 11-15-2009 10:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 AM.

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
Open Source Consulting | Domain Registration