1
int exe(int sec)
{
const char *buf;
int timeout_sec=sec;
int i=0,j=0;
sigset_t mask;
sigset_t orig_mask;
sigset_t pset;
siginfo_t siginfo;
struct timespec timeout;
pid_t pid;
int fd,status=-1;
int fl;
sig_init();
sigemptyset (&mask);
sigaddset (&mask, SIGCHLD);

if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
    perror ("sigprocmask");
    goto error;
}

if (( pid=fork())<0)
{
    perror ("fork");
    goto error;
}
else if (pid == 0)
{
    printf("In child pid=%d\n",getpid());
    if(execv(ptr[0],ptr)==-1)
    {
        printf("execv failed in %s: error: %s\n", __FUNCTION__, strerror(errno));
        _exit(1);
    }

    printf("Exiting child\n");
    _exit(0);

}
else
{
    timeout.tv_sec = timeout_sec;
    timeout.tv_nsec = 0;

do {
         if ((ss=sigtimedwait(&mask, &siginfo, &timeout)) < 0) {

            if (errno == EINTR) {
                // Interrupted by a signal other than SIGCHLD.
                printf("Continue\n");
                continue;
            }
            else if (errno == EAGAIN) {
               printf("%s %s failed in:%s KILLING\n",ptr[0],ptr[1],__FUNCTION__);
                kill (pid, SIGKILL);
            }
            else {
                perror ("sigtimedwait");
                goto error;
            }
        }

        break;
} while (1);
if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) {
        perror ("sigprocmask");
        goto error;
    }
  sleep(10);

  }
return 0;
error:
printf("%s failed\n",__FUNCTION__);
return -1;
}

Here I am trying to block the SIGCHLD and the in child process executing execv, for SIGCHLD status I have a handler for it(not mentioned here in code) which is working fine when sigtimedwait() returning error but when sigtimedwait() success the handler is not called, and if I am using waitpid directly in parent instead of handler then in both of cases it is woking fine. Please help me out. I just found one clue in man page of sigtimedwait i.e. "sigwaitinfo() removes the delivered signal from the calling process's list of pending signals and returns the signal number as its function result" Curious to know the reason and too much in need.Thanks :)

Sumit
  • 11
  • 2

0 Answers0