Problem with signal method

This is a discussion on Problem with signal method within the C Programming forums, part of the General Programming Boards category; Hello all, I am encountering with a problem here and any help would be very much appreciated. I was trying ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Problem with signal method

    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.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    can you place the code of the checkSwitch function. Or better briefly explain the flow of that function

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    I think the program is not hung , after the the 2 seconds child process killed the parent ,
    that makes you to feel like the program is got exiting.

    When the child was sleeping the parent start the motor and come out of code and stop the motor.
    I think this and all happens in a milliseconds ,
    Is my understanding is right ? , else tell me what is the normal execution time of the
    SubSuctionMotors (0); function.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by Alexander jack View Post
    I think the program is not hung , after the the 2 seconds child process killed the parent ,
    that makes you to feel like the program is got exiting.

    When the child was sleeping the parent start the motor and come out of code and stop the motor.
    I think this and all happens in a milliseconds ,
    Is my understanding is right ? , else tell me what is the normal execution time of the
    SubSuctionMotors (0); function.
    The parent and child process is started simultaneously, isn't it?
    The SubSuctionMotor(1) run motors in one direction and continue running until there is a command of SubSuctionMotors(0) that asked the motor to stop.

    In normal case whereby the switch is detected to be activated, the whole process from motor start running until motor stopped, it takes about one second or two.

    We are putting the timer function so that in case the switch is not activated within 2 second, timer takes the authority to stop the motor.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    I just found out the solution to exit the child process as well as the parent process. It is all to be coded in handler function.

    This is my code. I hope my code is stable (not hiding any risk of crash or buffer overflow).

    Code:
    void usrl_handler( int sig_num )
    {
    	printf( "Time out(%d), signal received to terminate motor\n", getpid() );
    	SubSuctionMotors (0);
    	exit(0); 
    }
    
    int SubFrameVertical (int updown)
    {
    
    	int 	dir;
    	int 	swret;
    	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);
    		
    	signal ( SIGUSR1, usrl_handler );
    			
    	role = 0; 
    	if (returnresult >0) SendStatus();
            else printf("Connection Fail\n");
    
    	swret=CheckSwitch(0);
    	while(swret==0)
    	{
    	    swret=CheckSwitch(0);
    	}
            SubSuctionMotors (0);
    	
            }
    
    	else if (ret == 0) { 	/*Child Context*/
    			
    	printf( "Child: This is the child process (pid %d)\n", getpid() );
    			
    	role = 1; 
    			
    	sleep ( 2 );
    
    	printf( "Child: Time Out, Sending SIGUSR1 to pid%d\n", getppid() );
    	kill( getppid(), SIGUSR1 );
    	}
    
    	SubSuctionMotors (0);
    	
            if (returnresult >0) SendStatus();
            else printf("Connection Fail\n");
    
    	return rc;										
    
    }
    I have add in "exit(0)" in the usrl_handler. This settled my problem and get my sequence right.

    However, I think I still encountering with problem of running out of memory after executing this the child code for a few times (this means switch is never being activated and child process--time out take turn to stop motor). Someone suggested that I should just pause the program when time out happen and wait for instruction from user. I am currently studying this problem. Any help or idea will be very welcomed and appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  3. My signal problem
    By afreedboy in forum Linux Programming
    Replies: 2
    Last Post: 09-28-2004, 01:10 AM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21