Difference when ending a process with CTRL+C vs. kill

80 Views Asked by At

Environment:

  • LINUX-Controller <---> Electricity meter1 & Electricity meter2
  • communication via RS485 MODBUS-RTU (without terminating resistor)
  • baud rate: 9600
  • both meters communicate via the same path /dev/ttyS4
  • Process: modbus_rs485 (running in screen)

Problem description:

  • at process start (in SCREEN), both meters always supply actual values
  • after a few days, the 2nd meter only delivers the same value (meter1 supply actual values)

Question:

If I kill the process by code (fork -> setsid -> kill -> start), the meter still returns the same values. BUT If I stop the process remotely and then restart it, the meter2 returns actual values.

What happens differently in the background?

small note:

  • same modbus pointer (for slave1 and slave2)
  • no errors
  • even via modbus debug i got the echo
  • close, free, flush, reconnect not helps

the process also has a signal handler if(signal == SIGINT || signal == SIGTERM) exit(EXIT_SUCCESS);

          switch(fork())
            {
            case -1: // Fork() has failed
                perror ("fork");
                break;
            case 0: // This is processed by the child
                if(setsid() < 0)
                    exit(EXIT_FAILURE);

                system("screen -X -S modbus_rs485 kill");
                sleep(3);
                system("screen -dmS modbus_rs485 sh -c \"while :; do /opt/modbus_rs485/bin/modbus_rs485; echo 'modbus_rs485 stopped (\\$?: \\\"'\\$?'\\\", \\$!: \\\"'\\$!'\\\") --> restart via surrounding shell cmd' | logger -s; sleep 2; done\"\n");
                exit(0);
                break;
            default:      // This is processed by the parent
                sleep(10);
                break;
            }

it does not help

but killing via ctrl + c and then start with ./modbus_rs485/..... helps (then it returns actual values)

0

There are 0 best solutions below