How to detect and communicate with another process under Linux? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a program 'P' and P is executed in terminal A. Let's call it process A. While process A is running, terminal B is opened and executes P as process B.
How can I make process A find process B and exchange data with each other? Someone told me to implement it with MPI but I haven't found any material telling me how.
I also appreciate that if anyone can tell me how to make these two process read and write the same variable (same address in memory). This solves my problem, too.

There are lots of options, but in most cases I think you'll find that named pipes/fifo will meet your needs.
See mkfifo, which creates a named pipe on the filesystem; that pipe can then be opened and accessed using standard open/read/write like a file for interprocess communications.

Related

Declaring a variable which has a value in c++ or c that does not get destroyed when the program terminates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I declare a variable or array that contain a value which does not get destroyed when the program terminates in c or c++?
When the process terminates, the kernel releases the resources owned by it. If you want to keep data/information obtained during the runtime of the process, you can use a database or the file system.
if you want to keep data after your process terminates, try storing it in a file.
check this for more:
file handling in C++

Launching another application through c++ code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would like to know if there is a way to launch an application through a c++ code? As if I was launching it through the command line (with giving parameters for example).
If it exists, please can you provide me with both the windows code as well as linux code (in case they differ).
You can use system calls, like:
exec()
fork()
You can find plenty of examples. I had also answered a question about fork() here.
For exec(), you could read this: Please explain exec() function and its family.
For Windows, you can use one of the spawn family of functions, like _wspawnl. For Linux, you can use one of the exec family of functions, in combination with fork, like execl.

Save Values on Exit C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been working on a C++ program which gets values and saves them for later use. The problem for me is saving the data on exit, and initializing it on opening. How can I do this?
You can use a lib like pugixml to easily write the data on a xml file and read it on program startup.
On linux, you can register a "program exiting" callback with the atexit function, this is the perfect place to put your xml creation code. There is probably something similar to the atexit function on windows :)
Edit:
Another alternative, like #molbdnilo said, is to leave the file writing code in the end of the main function, which would not require using global variables to hold the data that needs to be written.

UNIX c programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anybody tell me how you would do this question ?
Write short C program for a UNIX/Linux operating system that will do the following:
Fork a child process
The parent process prints out its own pid and its child’s pid
The parent exits properly so as not to orphan its child
The child process sets its own priority to 2
The child process prints out its own pid, its parent’s pid and its own priority
The child process checks if it is an orphan before finishing; if it is an orphan it prints a message to say “I am an orphan”.
You do not need to put any error checks in your program. You do not need to list all of the C library include files, i.e. the .h files.
Since stack overflow is by NO means a write code for me please website the only "answer" we can offer you is a suggestion on where to look. With that in mind look up pthreads. Beyond that I don't expect people to give you anything due dates are your problem not ours.

It is possible to get the PID from a given file descriptor( programmatically using C++)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have seen lot of tutorials and documentation on how to get the files descriptors from a given pid. Well, I want to do otherwise.
Thanks.
Of course not, that's like trying to get the PID that called main. Every process has a file descriptor 0 (stdin), 1, 2, etc., and they mean different things for each process.
A file descriptor, which is just a small integer, isn't meaningful unless you already know what process you are talking about.