LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-15-2018, 07:50 AM   #1
fingon
LQ Newbie
 
Registered: Dec 2018
Posts: 3

Rep: Reputation: Disabled
Shared memory. Program with write - read communication. Sending multiple messages.


This program generate two messages in shared memory. What I want is messages to store different data. When I run it I get this:

Data written in message 1: This is message 1

Data written in message 2: This is message 1

But it shouldn't have text in str2 because the string "This is message 1" is only written in str.

The program prints in str2 the same message as str. How to fix this? I tried to use different keys, different ids but again the second message have the same data as the first. And if I write something in str2 it overwrites the first message str. I have a reader.c where I want to print the two different messages.


Code:
//writer.c
#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <stdio.h> 

int main() 
{ 
//MESSAGE1
    // ftok to generate unique key 
    key_t key = ftok("shmfile",65); 
  
    // shmget returns an identifier in shmid 
    int shmid = shmget(key,1024,0666|IPC_CREAT); 
  
    // shmat to attach to shared memory 
    char *str = (char*) shmat(shmid,(void*)0,0); 
  
  strcpy(str,"This is message 1");
  
    printf("Data written in message 1: %s\n",str); 
      

    //MESSAGE2
    
    // ftok to generate unique key 
    key_t key2 = ftok("shmfile2",77); 
  
    // shmget returns an identifier in shmid 
    int shmid2 = shmget(key2,1024,0666|IPC_CREAT); 
  
    // shmat to attach to shared memory 
    char *str2 = (char*) shmat(shmid2,(void*)0,0); 
     printf("Data written in message2: %s\n",str2); 
    
    
    
  
    return 0; 
}

Last edited by fingon; 12-15-2018 at 07:53 AM.
 
Old 12-15-2018, 08:24 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
You should debug more carefully. For a test, I added some more debug-print, and got this:

Code:
Data written in message 1 (key=-1, id=9469959): This is message 1
Data written in message2 (key2=-1, id2=9469959): This is message 1

Last edited by NevemTeve; 12-15-2018 at 08:53 AM.
 
1 members found this post helpful.
Old 12-15-2018, 09:00 AM   #3
fingon
LQ Newbie
 
Registered: Dec 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
You should debug more carefully. For a test, I added some more debug-print, and got this:

Code:
Data written in message 1 (key=-1, id=9469959): This is message 1
Data written in message2 (key2=-1, id2=9469959): This is message 1
Thanks for your effort. How come they are with the same ID?
 
Old 12-15-2018, 11:14 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
You forgot to check for errors after ftok
 
1 members found this post helpful.
Old 12-15-2018, 12:22 PM   #5
fingon
LQ Newbie
 
Registered: Dec 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
You forgot to check for errors after ftok
Thanks a lot. It was a minor and stupid mistake but I found that I have mistaken the name of the file in ftok. That's why it returns -1.
 
  


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
[SOLVED] Error in Communication between two process using shared memory srinietrx Programming 4 11-21-2014 08:13 AM
Shared memory communication program pratham29 Programming 6 12-03-2013 02:28 PM
Write, Read messages using Message Queues, Shared Memory or Unix Domain Sockets harig Programming 4 02-19-2013 08:58 AM
Fault in KDE processes communication: Could not read network communication list Magnus Johansson MEPIS 0 03-30-2008 12:50 PM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM

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

All times are GMT -5. The time now is 12:26 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
Open Source Consulting | Domain Registration