In a C program, I have a menu, with some options to choose from, represented by characters. There's an option that forks the process, and runs a function (we can say it runs in the background). This function that runs in the background, under some conditions, can ask the user to enter data.
My problem is: when the main process is asking for data (or option) and the child process is asking for data too, I can't send the data properly.
Do you have any idea on how to deal with this?
I'll add a bit of the structure of the code (I can't post it all, because is around 600 lines of code):
int main(int argc, char *argv[])
{
// Some initialization here
while(1)
{
scanf("\n%c", &opt);
switch(opt)
{
// Some options here
case 'r':
send_command(PM_RUN, fiforfd, fifowfd, pid);
break;
// Some more options here
}
}
}
void send_command(unsigned char command, int fiforfd, int fifowfd, int pid)
{
// Some operations here
if(command == PM_RUN)
{
time = microtime();
childpid = fork();
if(childpid == -1)
{
// Error here
}
else if(childpid == 0)
{
// Here is the child
// Some operations and conditions here
// In some condition, appears this (expected a short in hexadecimal)
for(i = 0; i < 7; ++i)
scanf("%hx\n",&data[i]));
// More operations
exit(0);
}
}
}