ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
//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
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'
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."
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.