I have this program that "simulates" a parking lot. I have been searching a lot and I couldn't find anything that really fits on my problem. But if you know a similar question that could answer mine, send me the link! :)
This is my main, with the second child process already created (I don't think I need to put comments here, it's only doing a fork(), then inside the parent it does a second fork(), creating another child with the same parent. Right?):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char spots[25];
main()
{
int descritor, descritor2, //gets the fork's pid
pipe1[2], // comunicacao pai -> filho
pipe2[2]; // comunicacao filho -> pai
char msg[10] = "";
if (pipe(pipe1)<0 || pipe(pipe2) <0)
{
printf("Erro na chamada PIPE");
exit(0);
}
if ((descritor = fork()) <0)
{
printf("Erro na chamada FORK");
exit(0);
}
else if (descritor > 0)
{
descritor2 = fork();
if(descritor2 == 0)
{
strcpy(msg,"CHILD 2");
fprintf(stdout, msg);
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
close(pipe1[0]);
close(pipe2[1]);
exit(0);
}
else if (descritor2 > 0)
{
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0], pipe1[1]);
close(pipe1[1]);
close(pipe2[0]);
exit(0);
}
else if (descritor2 < 0)
{
printf("Erro na chamada FORK");
exit(0);
}
}
else
{
strcpy(msg,"CHILD 1");
fprintf(stdout, msg);
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
close(pipe1[0]);
close(pipe2[1]);
exit(0);
}
}
I have the parent process which is calling the client function below:
client (int readfd, int writefd)
// readfd, leitura do pipe2[0]
// writefd; escrita no pipe1[1]
{
char buff[2];
int i, pos = 0;
system("clear");
populating(); //this is a function I call only this time to populate my global char array with the spots 1-25
printing(pos); //this one I use to update and to print the spots
while(1)
{
printf("\nDigite sua vaga -> "); //type the spot
gets(buff);
write(writefd, buff, 10); //sends to server (child)
read(readfd,buff,10); //reads from server (child)
pos = atoi(buff); //parses to int, because I think I can't write() read() int data, and I need to use this data (pos) to update my spots
if(spots[pos] == 0) //check if it's occupied
{
sleep(1);
system("clear");
printing(0); //passes the value 0 to update spots[0] which contains nothing
printf("\nEsta vaga esta ocupada!\n");
}
else
{
printf("Vaga %s reservada.\n",buff);
sleep(1);
system("clear");
printing(pos); //the function printing is only a loop that gets the value typed earlir and do spots[pos] = 0. then print the spots updated
}
}
}
Then I have my server function:
server(int readfd, int writefd)
// readfd, leitura do pipe1[0]
// writefd; escrita no pipe2[1]
{
char buff[2];
int n, fd, pos;
while(1)
{
read(readfd,buff,10); //read the spot
pos = atoi(buff);
if(pos > 25 || pos < 1)//checks if it's in range
{
while(pos > 25 || pos < 1)
{
printf("\nVaga indisponivel. Digite novamente\n");
printf("-> ");
gets(buff);
pos = atoi(buff);
}
}
write(writefd, buff, 10); //sends the spot back to the parent
}
}
So, based on this, I have a few questions and would appreciate if someone could help with them.
I want to make the two child process "fight" for the cpu, but I noticed that I'm getting stuck with only one child. Is that because of the "while(1)"? If so, how could I do a loop to keep the program running then? Or the problem is that I'm using two pipes for 3 processes? (pipe1 I read from one side and write from the other, and vice-versa).
I also noticed that the messages "CHILD 1" or "CHILD 2" only prints when I type an invalid spot... Why is that?
So, summing all this up, I want to keep the two child processes running and getting the spot typed on father "concurrently", running one child process a few times, then the other, and so on. And seeing if it's on range. I would also want to see if the spot == 0, but this doesn't work on the child process, I don't know why, so I left this on the parent.
It's working with no errors at all, but the problem is that I think there's only one child running per execution... I want to make both of them run.
If you need additional information in order to understand my situation or something else, ask me!