On Fri, Jul 31, 2009 at 12:57 PM, Denis Kirjanov <kirjanov@xxxxxxxxx> wrote:
On Fri, Jul 31, 2009 at 8:43 AM, Andy Dalton<andy.dalton@xxxxxxxxx> wrote:
> Denis,
>
> You have a race condition. If the parent process gets scheduled first and
> executes the pause() before the child runs and sends the SIGUSR1 signal, you
> get the behavior you're expecting. However, if the child gets scheduled
> first and sends the signal before the parent has executed pause(), when the
> parent eventually gets scheduled it will execute the pause() and block
> waiting for some signal (after the signal send by the child has already been
> received and handled).
>
I have modified the code to avoid the race condition so that both of them terminate
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void trap1(int sig)
{
fprintf(stderr, "signal\n");
}
int main()
{
int stat;
signal(SIGUSR1, trap1);
int process_id = fork();
if (process_id) {
printf("I am parent\n",process_id);
pause();
wait(&stat);
exit(0);
}
else if (process_id == 0)
{
printf("I am child\n",process_id);
sleep(2);
}
kill(getppid(), SIGUSR1);
exit(0);
}
#include <stdlib.h>
#include <signal.h>
void trap1(int sig)
{
fprintf(stderr, "signal\n");
}
int main()
{
int stat;
signal(SIGUSR1, trap1);
int process_id = fork();
if (process_id) {
printf("I am parent\n",process_id);
pause();
wait(&stat);
exit(0);
}
else if (process_id == 0)
{
printf("I am child\n",process_id);
sleep(2);
}
kill(getppid(), SIGUSR1);
exit(0);
}
This is similar to the truth. Thanks.
> Andy
>
--
Regards,
Denis
--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ
--
Regards,
~Sid~