Sekhar
Sekhar

Reputation: 1

system() API getting stuck occasionally

I have a code where I am running the system API to check the pid of another process in a while(1) loop. There is a sleep of 1 sec in the loop. Also I am checking if the call is getting executed every second or not in this way,

#include<stdio.h>
#include <time.h>
#include <unistd.h>
#include<stdlib.h>
#include<math.h>
#include<signal.h>

int main()
{
    struct timespec now;
    unsigned long long prev;
    int diff = 0;
    if( clock_gettime( CLOCK_REALTIME, &now) == -1 ) {
            perror( "clock gettime" );
            exit( EXIT_FAILURE );
    }
    prev = now.tv_sec;
    while(1) {
        int ret =  system("/sbin/pidof vim > /dev/null");
        if( clock_gettime( CLOCK_REALTIME, &now) == -1 ) {
            perror( "clock gettime" );
            exit( EXIT_FAILURE );
        }
        if(prev != 0 ) {
            diff = abs(now.tv_sec - prev);
            if( diff > 1)
                printf("Diff %d prev = %lu now = %lu\n",diff,prev,now.tv_sec);
        }
        prev = now.tv_sec;
        sleep(1);
    }
    return 0;
}

Ideally I expect the print should not be seen at all , but many times I am seeing a diff of 2 seconds. If I comment the system() API, then I dont see the print.I even ran a strace with timing option and dont see any increase in timing when the issue is seen.

Are there any known issues where system() gets stuck for a while?

Sample output

Diff 2 prev = 1640101799 now = 1640101801
Diff 2 prev = 1640101911 now = 1640101913

Upvotes: 0

Views: 111

Answers (0)

Related Questions