In Linux i am trying to create a C program that forks and create 2 childs (Child1 and Child2). Each child is performing a process which is to execute the file using execv command. This is a parent file that creates two files destination1.txt and destination2.txt. The codes for these files are inside Prcs_P1.c and Prcs_P2.c. They are the C files. The code compiles and runs but does not perform the operation of executing file. What am i doing wrong? What is the part i am missing?
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
pid child1, child2;
enter code here
child1 = fork();
errno = 0;
if (child1 < 0)
{
printf("Error! Forking can't be done\n");
}
else if (child1 == 0)
{
printf("Child one process activated! %d\n", getpid());
execv("Prcs_P1.c", NULL);
}
else
{
printf("Parent1 process activated! %d\n", getpid());
}
child2 = fork();
if (child2 < 0)
{
printf("Error! Forking can't be done\n");
}
else if (child2 == 0)
{
printf("Child two process activated! %d\n", getpid());
execv("Prcs_P2.c",NULL);
}
else
{
printf("Parent2 process activated! %d\n", getpid());
}
return 0;
}
The output is Parent1 process activated! 2614 Parent2 process activated! 2614 Child one process activated! 2615 Parent2 process activated! 2615 Child two process activated! 2617 Child two process activated! 2616