This program was working a second ago...I'm so frustrated. Anyways, The way it works is the parents enters arbitrary lines of text and then a signal is sent to the child. Once the child gets the signal, I want the child to start a counter of how many lines(signals) have been sent. The parent is continuously asked for lines of text, and the child is continuously waiting for lines (signals). I really have no idea how this broke.
The expected output is to have the child print the number of lines that have been sent after every signal received. My current output is nothing...I enter a line of text, nothing is displayed, then I'm asked if I want to enter another line.
code:
int main()
{
pid_t pid;
if((pid = fork()) == -1)
{
printf("Error on fork().\n");
exit(1);
}
if(pid == 0)
{
int i=0;
while(1)
{
signal(SIGUSR1, my_handler);
pause();
i++;
printf("%d Line(s) have been sent.", i);
}
}
else if(pid > 0)
{
char x[3];
x[0]='y';
int ch;
while(x[0]=='y'||x[0]=='Y')
{
char buff[256]={0};
printf("Enter a line: ");
fgets(buff, 256, stdin);
if (buff[254] == '\n' && buff[255] == '\0') { while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); }
kill(pid, SIGUSR1);
printf("Send another line?(y/n):\n");
fgets(x, 2, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
}
}
}
void my_handler(int signum)
{
if (signum == SIGUSR1)
{
}
}