1

I was wondering if it was possible to make a program that executes another program, the program that is being executed returns a value that the first program catches, like if they were communicating, an example of this: Program 1 executes Program 2, then Program 2 returns/sends to Program 1 whatever it has to send and then closes

Is this possible in c/c++ ?

thokuest
  • 5,800
  • 24
  • 36
Nico Cano
  • 37
  • 3
  • How can I do it? – Nico Cano May 21 '23 at 00:45
  • *the program that is being executed returns a value that the first program catches* -- You need to be clear as to what you mean by "returns a value". If it means that program 1 outputs information, then that can be done using pipes, where you are piping the output of one program that will be used as the input to another program. – PaulMcKenzie May 21 '23 at 00:47
  • 1
    C and C++ are two separate languages, despite the fact that you can _sometimes_ compile a C program with a C++ compiler. Pick one. – Chris May 21 '23 at 01:19
  • @PaulMcKenzie what I mean is that program 1 executes program 2, then program two returns/sends to program 1 whatever data it has to send and program 2 closes – Nico Cano May 21 '23 at 01:31
  • Read up on `fork()`, `exec()` & `pipe()` and IPC in general. – Jesper Juhl May 21 '23 at 02:14
  • Since this is tagged windows,`CreateProcess()` is what's needed, not `fork()`/`exec()`... – Shawn May 21 '23 at 02:37
  • Since C/C++ is not a language, the answer is "No, it can't be done in C/C++". You may be able to do this in C **or** C++. Please read up on sockets, searching terms like "C++ sockets example". – Thomas Matthews May 21 '23 at 19:05

2 Answers2

3

The simplest approach is to use pipes (i.e. popen, pclose). Here is a detailed example. The following code provides a simple example.

Sample Code

#include <stdio.h>
#include <iostream>

int main(int argc, const char *argv[]) {
    FILE *pipe = popen("ls", "r");

    char c;
    while ((c = fgetc(pipe)) != EOF)
        putchar(c);

    pclose(pipe);

    return 0;
}

Output

CMakeCache.txt
CMakeFiles
CPackConfig.cmake
CPackSourceConfig.cmake
Makefile
...

You may also want to look at this answer.

RandomBits
  • 4,194
  • 1
  • 17
  • 30
  • Thanks, I'll be trying it, do you have an article or video about the subject? – Nico Cano May 21 '23 at 01:40
  • 5
    Why when I was a kid we didn't have videos. We couldn't even look it up in a book because other than your trusty copy of K&R you were making the stuff up as you went along. We'd code 72 hours straight for chips you kids today wouldn't even consider worth eating in a bag of Doritos. Why lemme tell you.... 40 miles of snow....uphill.... both ways.... – user4581301 May 21 '23 at 02:12
  • @user4581301 Ok b – Nico Cano May 21 '23 at 02:34
1

Every execution of a program happens inside a process. Your code can spawn another process, which will become its child process, and can wait on it to complete execution and get back the return value. I would suggest reading more on processes. How you achieve this is using the fork() system call. It makes two copies of the process and returns different values in both copies so you can distinguish which process is which. It returns 0 in the child process and process id of the child process in the parent process. Then in the child process you can use one of the system calls from the exec family like execlp(all the functions in exec family differ in the arguments you provide to them, I would suggest reading manpages) to execute your second program. In the parent process, you can wait on the child process to finish execution using the wait() system call and then get back it's return value in a variable. The below code snippet should be helpful

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
    pid_t childPid = fork();

if (childPid == -1) {
    // Handle fork error
} else if (childPid == 0) {
    // Code for the child process
    execlp("program", "program", arg1, arg2, ..., (char*) NULL);
    //Program here is the path to the program you want to run
} else {
    // Code for the parent process
    int status;
    wait(&status);
    if (WIFEXITED(status)) {
        int returnValue = WEXITSTATUS(status);
        // Use the returnValue as needed
    } else {
        // Child process exited abnormally
    }
}

return 0;

}

  • Thanks, I'll be trying it, do you have an article or video about the subject? – Nico Cano May 21 '23 at 01:40
  • Writing an article or recording a video is a multiple-hours-long task that nobody is likely to do as part of a StackOverflow answer; it's just too much work. Finding an existing article or video on the topic, OTOH, is reasonable, but that is more a job for Google than for StackOverflow. You could probably use some keywords from this answer as your search terms. – Jeremy Friesner May 21 '23 at 04:42