Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:20:46+00:00 2026-05-15T10:20:46+00:00

In this code C i launch a program from the command line and when

  • 0

In this code C i launch a program from the command line and when it is closed from a signal different from SIGTERM (signal for normal end) my code should relaunch the initial program passed from the command line. But it is not so, in fact my code never relaunchs program saying that it is properly terminated.In practice my condition”if(WTERMSIG(status)!=SIGTERM)” works bad, WHY????? :'(

This is my code:

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char*argv[])
{   
    pid_t pid;
    int* status=(int*)malloc(sizeof(int));
    int term;
    if(argc<2)
    {
        printf("Error: Too few arguments\n");
        exit(EXIT_FAILURE);
    }

    while(1)
    {
    pid=fork();

    if(pid!=0) /*father*/
    {
        waitpid(pid,status,0);
        //term=WIFSIGNALED(status);
        if(WIFSIGNALED(status))
        {
            if(WTERMSIG(status)!=SIGTERM)
            {
                printf("The program %d ended abnormally:\nRelaunching...\n",pid);
                sleep(1);
            }
            else
            printf("The program %d is properly terminated...\n",pid);
            break;

        }
        else
        {
            printf("Can not read the reason for termination\n");
        }

    }
    else    /*child*/
    {
        execvp(argv[1],argv+1);
        exit(EXIT_SUCCESS);
    }
    }

    return 1;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:20:47+00:00Added an answer on May 15, 2026 at 10:20 am

    The WIFSIGNALED() and WTERMSIG() macros both expect plain ints, not pointers to ints. This means that in your code, where status is a pointer to an int, you need to use *status when calling the macros, to pass them the value of the integer.

    That said: why are you calling malloc() to allocate room for a single int, anyway? Just use a normal variable, and &status if you need a pointer to it.

    Also, you should return EXIT_SUCCESS from main() on successful completion of your program, not 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It am trying to launch this code that would normally run from a batch
I am trying to launch a program from another program. Here is the code
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
This code produces a different output in Python 2 and Python 3 . class
I am developing a GUI to launch an external long-term running background program. This
I wrote a small C++ program in VS2k8. When I launch it from windows
I'm trying to launch another program install in my system from my Access application.
I've found a need to launch a WPF application from inside a c# program.
i have a command line executable program that accepts a configuration file as it's
I'm writing a simple browser-based front end that should be able to launch a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.