I'm trying to write a C code with three processes, each one with its own purpose:
- First process: it has to allow the user to write a source code in C in a new terminal window.
- Second process: it has to compile the source code the user has just written in C.
- Third process: it has to execute the compiled file.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#define PATH_LENGTH 1024
#define ARGS_LENGTH 5
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
pid_t pid2;
pid2 = fork();
if(pid2 == 0){
char path[PATH_LENGTH];
getcwd(path, PATH_LENGTH);
strcat(path, "/source.c");
char *args[ARGS_LENGTH] = {"gnome-terminal", "--", "nano"};
args[3] = path;
args[4] = NULL;
execvp(args[0], args);
}
else{
wait(NULL);
system("gcc source.c -o source");
}
}
else{
wait(NULL);
system("./source");
}
return 0;
}
I have a doubt about this code. As soon as I run it nano opens correctly on a new window but the parent process that opens nano doesn't wait for it to finish: it's like it ignores the wait(NULL).
It's not the first time I use the system call, exec*(), and the other times the parent process waited. The only thing that raises a doubt for me is the fact that I open a new terminal window.
P.S.
I am not asking for the solution to the problem. I am just curious and interested about the problem and especially what happens to that wait(NULL) of the parent process of the process that opens nano.
I have tried to use the system(), to open the new terminal window, function but the result is the same, in this case obviously.