I am trying to use the child process to count characters in a string and then make that count available to the parent process so that the parent process can print it out. After hours of videos and reading I still cant find a way to make something from the child process available to the parent process. Can anyone show an example of something similar to what I am asking.
Asked
Active
Viewed 260 times
0
-
1Why are you using a child process to process a string? Why not a thread in the parent process instead? In any case, does the child process exit after calculating the count? If so, then just return the count as its exit code. Otherwise, you can put the count into shared memory that the parent process can access. Or, you can communicate the count back to the parent via an IPC mechanism, such as a pipe. There are many options available. – Remy Lebeau Feb 08 '22 at 17:26
-
Yes, there are various ways to do that. Completely silly technique though. If that should be done asynchously, a thread would be way more efficient. – πάντα ῥεῖ Feb 08 '22 at 17:28
-
There are lots of ways. Shared memory, named pipes, etc. Or for Windows you could post a window message. – Jerry Jeremiah Feb 09 '22 at 22:35
1 Answers
0
I think it is important to help ( and it makes me revise ...)
There should be numerous SO posts that can be found like How to get return value from CHILD PROCESS?.
- It sounds odd to me to (re)discover that you may only retrieve only a byte from the return value of a process. After all you return only a status and 256 values are enough to express something.
- Return values are not intended to retrieve high amount of information.
Even intereting hints like this answer of Return value range of the main function stating that you might get more was not successful on my side.
- Return values are int. Size of std::string is size_t.
- Nasty teachers may well set the exercice with such huge string of size let's say 100009.
So an easier way for this exercice is to establish a pipe (bridge) between the parent and the child, redefine standard input and output and by the way just use the cin and cout streams.
#include <iostream>
#include <unistd.h>
enum { read_end = 0, write_end, PIPE_EDGE_NB};
int main(int argc, char* argv[])
{
int fd[PIPE_EDGE_NB];
std::string long_str(100009,'*');
int res = pipe(fd);
if (0>res) {
std::cout << "pipe failed\n" << std::flush;
return -1;
}
pid_t pid = fork();
if (0>pid) {
std::cout << "fork failed\n" << std::flush;
return -1;
}
switch(pid)
{
case 0: {//Child
auto sz = long_str.size();
std::cout << "Child says size is " << sz << "\n" << std::flush;
dup2(fd[write_end],STDOUT_FILENO);
close(fd[write_end]);
std::cout << sz << "\n" << std::flush;
}
break;
default:{
dup2(fd[read_end],STDIN_FILENO);
close(fd[read_end]);
size_t received_size;
try {
std::cin >> received_size;
}
catch(std::exception & ex) {
std::cout << ex.what() << "\n" << std::flush;
return -1;
}
std::cout << "Parent says size is " << received_size << "\n" << std::flush;
}
break;
}
return 0;
}
Child says size is 100009
Parent says size is 100009

NGI
- 852
- 1
- 12
- 31