I would like to use fork from my main program to make a process instance of other program I wrote. Here is example of what I am trying to do:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char *ar[] = {"./myfile",NULL};
switch (fork()) {
case -1:
printf("Problem.\n");
break;
case 0:
printf("Everything ok...\n");
execv("./myfile",ar);
printf("err\n");
exit(1);
default:
sleep(1);
}
return 0;
}
This is my main program. Program myfile looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <unistd.h>
int main(int argc, char *argv[]){
printf("Hi!\n");
return 0;
}
This is output I am expecting:
Everything ok...
Hi!
But I am only getting:
Everything ok...
What I am doing wrong? execv takes two parameters, but the second one in my case is empty (or NULL?). I tried to add line
char *argv[] = NULL;
to my main program, but I got error because I can't do that in C.