LinuxQuestions.org
Review your favorite Linux distribution.
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-29-2013, 02:51 PM   #1
pratham29
LQ Newbie
 
Registered: Oct 2013
Posts: 8

Rep: Reputation: Disabled
Shared memory communication program


Hello,
How to write a code for communication between two programs eg:A program "Read.c" takes input from user and other program "Write.c" prints the characters.
How to execute these in Ubuntu.
Thanks for your time and consideration.

Regards,
Pratham
 
Old 11-30-2013, 01:29 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That's what named pipes are good for.
 
Old 12-01-2013, 01:46 PM   #3
pratham29
LQ Newbie
 
Registered: Oct 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Shared memory example

Code:
//SHMServer.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
 #define MAXSIZE     27
 
void die(char *s)
{  perror(s);
   exit(1);}
 
int main()
{   char c;
    int shmid;
    key_t key;
    char *shm, *s;
    key = 5678;
    if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
    die("shmget");
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
    die("shmat");
    s = shm;                                       /* Put some things into the memory for the  other process to read.  */
    for (c = 'a'; c <= 'z'; c++)
    *s++ = c;
   
 /* Wait for process  changes the 1st character of  memory to '*', indicating that it has read what  we put there.  */
   
 while (*shm != '*')
        sleep(1);
        exit(0);
}
SHMClient.C
Code:
 
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE     27
 void die(char *s)
{   perror(s);
    exit(1);}
 
int main()
{
    int shmid;
    key_t key;
    char *shm, *s;
    key = 5678;
    if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
    die("shmget");
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
    die("shmat");
    /*Read what the server.c  put in the memory.
    for (s = shm; *s != '\0'; s++)
    putchar(*s);
    putchar('\n');

    /*Change the first character of the segment to '*', indicating we have read the segment.  */
   
 *shm = '*';
   exit(0);}
I am getting an error with the above code, as "command not found " when I enter alphabets in server.c program.
Kindly help me with this. Thanks for time and consideration,awaiting response.
Regards,
Pratham
 
Old 12-01-2013, 03:31 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I'm not sure how you were able to compile the client source code; you are missing the comment terminator on the following line:
Code:
/*Read what the server.c  put in the memory.
Btw, your server should detach from [using shmdt()], and remove [using shmctl()] the shared-memory segment, just before it completes.

And a few other miscellaneous comments...

1) Don't assume (from within the client) that a termination NULL character is at the end of your data stream (unless you specifically put it there).

2) You are missing the include file for <unistd.h> for the usage of sleep() in your server code.

3) Use proper indentation in your code.

Last edited by dwhitney67; 12-01-2013 at 03:34 PM.
 
Old 12-01-2013, 11:37 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You don't need shared memories for your problem, see my previous answer. But, you have to learn how to debug your program. Most likely it had terminated by the time you 'entered alphabets'
 
Old 12-02-2013, 01:48 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I'll quickly second the notion that ... "this is what named-pipes, or FIFOs or any one of several well-understood existing IPC-mechanisms in Unix/Linux are for."

(And, gosh, if you peek under the covers of their already fully-debugged implementations, you find things like shared memory segments being used ... by somebody else's code.)

"Never imagine that you can actually do better by doing things yourself. You usually can't. Most often, you're just wasting time and (somebody else's) money. Choose your battles."
 
Old 12-03-2013, 02:28 PM   #7
Spect73
Member
 
Registered: Aug 2013
Distribution: Slackware 14.1
Posts: 128

Rep: Reputation: Disabled
Quote:
Originally Posted by pratham29 View Post
Hello,
How to write a code for communication between two programs eg:A program "Read.c" takes input from user and other program "Write.c" prints the characters.
How to execute these in Ubuntu.
Thanks for your time and consideration.

Regards,
Pratham

"Advanced Programming in the UNIX Environment, 2nd Edition" by W. Richard Stevens and Stephen A. Rago. Best money I've ever spent on programming.

Coordially,
 
  


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
problem with shared memory in multi process program golden_boy615 Programming 6 12-22-2011 04:44 AM
a program based on shared memory chakki Linux - Newbie 2 06-16-2011 05:37 AM
Forking and Shared Memory program. the_satsuma_man Programming 5 03-10-2009 06:51 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 06:55 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