Hello all,

I am encountering with a problem here and any help would be very much appreciated.

I was trying to program using fork(). The objective of this code is to
1. I am activating a process A - SubSuctionMotors.
2. Process A is going to stop with either of this condition
a) Switch is activated - swret=1
b) Time allowed has expired
3. Stop the process A.

*Process A is running a motor.

Now, let's see the code.

Code:
void usrl_handler( int sig_num )
{
	printf( "Time out(%d), signal received to terminate motor\n", getpid() );
	SubSuctionMotors (0);

} 

int SubFrameVertical ()
{

	int 	swret;
	int 	rc=0;
	int 	timeOut=0;
	pid_t ret;
	int role=-1; 
	    			
	
	ret = fork();
			
	if (ret>0) { 	/*Parent Context*/

	printf( "Parent: This is the parent process (pid %d)\n", getpid() );
	SubSuctionMotors (1);      /*Motor is activated and running.*/
	signal ( SIGUSR1, usrl_handler );
			
	role = 0; 
	
	swret=CheckSwitch(0);      /*Command to check if switch is activated.*/
	while(swret==0)
	{
	    swret=CheckSwitch(0);    /*while loop to continuously check if switch is activated, loop will exit if switch is activated (swret=1).*/
	}
        SubSuctionMotors (0);     /*Stop motor running.*/

	}

	else if (ret == 0) { 	/*Child Context*/
			
	printf( "Child: This is the child process (pid %d)\n", getpid() );
			
	role = 1; 
			
	sleep ( 2 );       /*Start counting time when motor start running, after 2 second, motor should be stopped. This is an additional condition that we add in case the switch did not being activated to protect our application.*/

	printf( "Child: Time Out, Sending SIGUSR1 to pid%d\n", getppid() );
	kill( getppid(), SIGUSR1 );
	}

	SubSuctionMotors (0);

       
        return rc; 									

}
We tried to compile the code and run it.
Now, our problem is
1. We purposely force the switch to be inactive to test the child process (timer), whether after 2 seconds the motor stop. It turns out to be successful in first few attempts. After some while, the program hang, nothing is executed anymore and the program is not exit or terminated. What can cause the program to hang?

2. We tried to stop motor by switch activation (if the switch is to be successfully activated, the time it takes will always be shorter than the timer allowed time). However, the program did not seems like noticing the switch has been activated, and it just stop the motor according to timer. Why it behave this way?

Thank you very much for your help.