I have two cpp files, and I want one of them to be able to tell me the value of some specific variables declared in the other C file. For example:
file1.cpp
int var1 = 10;
while (true) {std::cin >> var1;}
I want another cpp file, file2.cpp, when compiled, to be able to tell me the value of file1->var1.
Now, I realise that I can use files to store the value and read it but that seems like it's not the "right" way to do it, so what would the right way be (maybe storing it in a specific spot in RAM?)
Edit: Since some people asked, both the cpp files are going to be compiled separately. They are going to be independent executables.
CodePudding user response:
You can use what's called named pipe as solution to connect your program 1 with the program 2 . A named pipe, also called a FIFO for its behavior, can be used to connect two unrelated processes and exists independently of the processes; meaning it can exist even if no one is using it. A FIFO is created using the mkfifo() library function.
Example
writer.c
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", sizeof("Hi"));
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
reader.c
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
close(fd);
return 0;
}
Edit : sorry I noticed that you need it in C language, The code for C language you can find it here Create Named Pipe C Windows
CodePudding user response:
Q: How to pass information between two separate processes (written in c )?
A: Use an IPC:
https://en.wikipedia.org/wiki/Inter-process_communication
In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. Typically, applications can use IPC, categorized as clients and servers, where the client requests data and the server responds to client requests.
As the article discusses, there are many, many ways processes can communicate. Many are platform-specific. To name just a few:
Some questions you need to ask:
- Do I need to communicate between processes, or just share information between threads in the same process?
- Communicate between different processes on the same host, or will I need to communicate between hosts?
- Should the solution be portable to different OSs (e.g. work on Windows, Linux and MacOS)?
- Would I benefit from a 3rd party framework or library?
- Etc. etc.
There is no "right way"; there is no "one size fits all solution".
Please provide more details about your "use case" and your "constraints", so we can provide you a better-informed answer.