1

Good day!

first time posting on stack overflow. Hope, I get this right.

For an exercise project, I would like my program to start vim or nano (doesn't matter for now) before proceeding.

Using the below code, vim starts. I.e. I can see the vim interface in the terminal window. But: As soon as I start typing, after one or two characters, vim crashes giving me the following output:

Vim: Error reading input, exiting...

Vim: Finished.

Here's my code snippet:

      pid_t childPid = fork();
      if (childPid) {
        // ... 
      }
      else {
       // open [filename.md] in vim
       char * vim = "/bin/vim";
       filePath = "/home/myusername/Documents/test.md";
       execl(vim, vim, filePath, NULL); 
      }

Not sure, if I have to block the input recognition of the parent process or similar.

Guess I went a bit too far for my level of knowledge. It would be great to have a solution anyway. I think knowing a bit of how to solve this, might help when I encounter this topic later on.

trekkerly
  • 31
  • 5
  • Does this answer your question? [How to call execl() in C with the proper arguments?](https://stackoverflow.com/questions/12596839/how-to-call-execl-in-c-with-the-proper-arguments) – pm100 Mar 02 '22 at 21:57
  • @pm100 That's only needed with `execv()`. `execl()` uses a variadic parameter list. – Barmar Mar 02 '22 at 22:18
  • How are you running your program? – Barmar Mar 02 '22 at 22:19
  • @Barmar From Terminal: I compiled it with gcc without using any flags. I run it directly from the Terminal using `./a.out`. The main function receives two arguments as follows: `( int argc, char * argv[ ] )`. Does this answer your query? – trekkerly Mar 03 '22 at 06:26
  • @pm100: I thought the parameters of my execl use wouldn't be the problem, since vim actually starts - but crashes due to i/o issues – trekkerly Mar 03 '22 at 06:30
  • Okay, It seems like I could solve the problem. My code example was an actual exerpt. That means the parent conditional branch in fact did not contain any further code. What should have been there is a wait statement. After adding wait(), it seems like all works fine. I will do some testing and flag my post as solved, if I won't encounter any problems regarding this later today. – trekkerly Mar 03 '22 at 06:48
  • That's the problem. When your main program exits, job control disconnects the child `vim` process from the foreground terminal process group. – Barmar Mar 03 '22 at 15:32

1 Answers1

0

I was able to fix this a little bit later. I had forgotten to make sure the parent process waits for the child process. After adding wait, everything worked as I expected.

trekkerly
  • 31
  • 5