Hi, New to C. Cannot find an answer to the question, please have patience I an new and trying.
I have to spawn child processes from the parent, using execl. I need to pass string and int from parent to child, and child will print to screen. I think I am able to get child to read whatever I send, just stuck on how to send it.
Below is only parts of my logic I am having trouble with, not full program. If more of the program of full one is needed to understand please let me know.
Please if anyone can help it would be appreciated.
Code:
int status;
//Declare pipe arrays.
int pfd[2];
int main()
{
//Declare descriptors to write to in pipes
int pipeFileDescript1;
char pipeFileDescript2[10];
//Set up pipes before fork
//1st pipe to get PID information to display, check started correctly
if (pipe(pfd) < 0)
{
perror("pipe");
//Exit negative 1 to show exit with incorrect operation.
exit(-1);
}
//Set string for Write end of pipes.
sprintf(pipeFileDescript2, "%d", pfd1[WRITE]);
for (int count = 0; count < NUMBER_OF_CHILDREN; count++)
{
pid_t pid = fork();
//Functions for Child process
if (pid == 0)
{
//First Child created to display all communications
if (count == 0)
{
//Write child process ID to display
char *str = ("Drone Simulator Started with PID %i \n", pid);
write(pfd[WRITE], str, strlen(str));
//Close write side of pipe between Display and parent/Start up.
close(pfd[WRITE]);
execl("../Display/Display.exe", "Display", (char *)NULL);
}
//Close pipes to parent as not needed..
close(pfd[READ]);
close(pfd1[WRITE]);
close(pfd1[READ]);
wait(NULL);
}
for (int count = 0; count < NUMBER_OF_CHILDREN; count++)
{ int status;
//Get status of the Child processors. and print results.
pid_t pid = wait(&status);
pipeFileDescript1 = ("Child process with PID %i terminated with status %d", pid, status);
write(pfd[WRITE], &pipeFileDescript1, sizeof(pipeFileDescript1));
close(pfd[WRITE]);
}
return 0;
}