1

I am writing a C program on Linux. At the same time I have an executable file A. I need to call A in the C program I am writing. but to run A I need to press any key, and I need to pass a key to the program I am writing after it calls A.

The approximate logic of A is as follows:

// read some config files

fprintf(stderr, "\n---------Hit Any Key To Start");
getc(stdin);

// the rest of the business

I refer to some information on the Internet and write the code as follows:

pid_t pid = fork();

int fd[2];
int read_end = 0;
int write_end = 1;

if(-1 == pipe(fd)) {
    // error warning
}
if(-1 == pid) {
    // error warning
}else if (0 == pid) {
    close(fd[write_end]);
    dup2(fd[read_end], STDIN_FILENO);

    // I am using execl to execute A. 
    // According to the logs, A executes to where it is waiting for input.
    execl(A ...)
}else {
    close(fd[read_end]);
    char key = 'y';

    // The two inputs are just me worrying that A didn't get the message so I input it a few more 
    // times, but it doesn't work
    write(fd[write_end], &key, sizeof(key));
    sleep(10);
    write(fd[write_end], &key, sizeof(key));

    // I need to terminate A after it executes for a while, so I wrote this code.
    sleep(75);
    kill(pid, SIGTERM);

    // Other things
    // ...
}

What I expect to happen is that I am starting my program with a bash script, then my program calls A and lets A run normally for a while and then exits.

I am not a native English speaker, so please let me know if my wording is wrong.

  • Can you not simply get the key before you execute the program and include the key in your `execl()` argument list? See [man 3 exec](https://man7.org/linux/man-pages/man3/exec.3.html) – David C. Rankin Aug 25 '23 at 02:59
  • @DavidC.Rankin I'm afraid not. In the best case, I cannot change the execution logic of A. If I pass new parameters, it means I need to change the source code of A program. – platanifolia Aug 25 '23 at 03:12
  • My thought being that `execl ("/path/to/A", ptr_to_key, (char*)NULL)` would call `A` with `ptr_to_key` as the argument for `A`. There is no need to change `A`, but if you can know `key` before you call `A`, you can just pass `key` (as a nul-terminated string) as the argument for `A` in the child-process without ever having to pipe between parent and child process. – David C. Rankin Aug 25 '23 at 03:18
  • Or are you saying `A` reads from `stdin` and doesn't take a program argument? In that case, then yes, you need to pipe input to `A` as you are attempting. – David C. Rankin Aug 25 '23 at 03:20
  • Send a newline with the key. Most of the programs want an enter. Are you writing program `yes`? – KamilCuk Aug 25 '23 at 04:12
  • @DavidC.Rankin Thanks for your guidance. In detail A accepts parameters, I have filled them in execl and ended with NULL. What is happening now is that after A accepts the parameters the run needs to start by pressing any key as `getc(stdin);` .The key here is just any trigger button. This is the problem I am trying to solve. – platanifolia Aug 25 '23 at 05:19
  • @KamilCuk I have tried `key = '\n'` , but it doesn't seem to work. I am indeed writing a program to use the executable A in the processing flow, but I can't change A... – platanifolia Aug 25 '23 at 06:08
  • `it doesn't seem to worl` "Doesn't work" is not an error description. What _happens_? What is the return value of `write()`? What does executable `A` do? How does it work? How to reproduce locally? Are you writing program `yes`, see `man yes`? Send a newline with the key. Consider creating a full [MCVE]. – KamilCuk Aug 25 '23 at 06:28

1 Answers1

1
pid_t pid = fork();

int fd[2];
int read_end = 0;
int write_end = 1;

if(-1 == pipe(fd)) {

You created the pipe after fork, so the pipe is local to the process. Create it before forking, so it is shared.

Overall, your whole program looks like timeout 75 bash -c 'yes | "$@"' -- A and doesn't look like requires a separate compiled C code.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Thank you very much for your help. I got it running using your alternative method. The previous reply to you was not standardized and did not contain enough information. I apologize for that. I'll try to find more detailed error messages. – platanifolia Aug 25 '23 at 08:45