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

  • SEARCH
  • Home
  • 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 8744373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:49:51+00:00 2026-06-13T11:49:51+00:00

Assuming this program is running in background. Assuming headers are included e.g signal.h… void

  • 0

Assuming this program is running in background.

Assuming headers are included e.g signal.h…

void SignalHandler(int sig);
int fd;
int main(int argc, char *argv[]){


signal(SIGINT,SignalHandler);
signal(SIGHUP,SignalHandler);
signal(SIGTERM,SignalHandler);

setsid();
close(0);
close(1);
close(2);


fd = open("/myPIPE", O_RDWR);
if(fd<0){
perror("open() error");
exit(1);
}

while (1) {
        err = read(fd, strbuf, 256);
        if(err)
            syslog(LOG_INFO,"%s",strbuf);   

        }
close(fd);

}

void SignalHandler(int sig){

if(fd!=-1)
close(fd);
exit(0);

}

Assuming that this code is already running. and has program name of testsignal. When i run it again in the terminal ./testsignal & the process just keeps on adding up.. the currently running process should exit and the new process should replace the old one. so only process should be running. I need help on this. Thanks

  • 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-06-13T11:49:52+00:00Added an answer on June 13, 2026 at 11:49 am

    This code is a simplification of yours in that it doesn’t use syslog(). It also doesn’t close standard input, standard output and standard error, so the daemon still has a way to log information to standard output (specifically). This makes it easier for me to test.

    The flprintf() function is used to ensure that all outputs to standard output are flushed; it reduces the number of boring 4-line blocks of code in the program. The program also logs more of its activity, which also makes it easier to see what’s going on. (In part that’s because I managed to overlook the commented-out close() system calls.) I make everything that doesn’t have to be visible outside of this source file static; that means only main() is not static. This code also ensures that the string read is null terminated, so long strings followed by short ones don’t produce messages that are a mish-mash of the short one plus the remains of the longer ones.

    #include <unistd.h>
    #include <stdio.h>
    #include <signal.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <stdarg.h>
    
    static void flprintf(const char *fmt, ...);
    static void SignalHandler(int sig);
    static int fd = -1;
    
    int main(int argc, char **argv)
    {
        int nbytes;
        char strbuf[257];
    
        if (argc > 1)
        {
            int pid = atoi(argv[1]);
            if (kill(pid, SIGINT) != 0)
                flprintf("Failed to deliver SIGINT to PID %d\n", pid);
            else
                flprintf("SIGINT successfully delivered to PID %d\n", pid);
        }
    
        flprintf("Starting - %d\n", (int)getpid());
    
        signal(SIGINT, SignalHandler);
        signal(SIGHUP, SignalHandler);
        signal(SIGTERM, SignalHandler);
    
        setsid();
        //close(0);
        //close(1);
        //close(2);
    
        fd = open("./myPIPE", O_RDWR);
        if (fd < 0)
        {
            perror("open() error");
            exit(1);
        }
    
        flprintf("Starting read loop\n");
    
        while ((nbytes = read(fd, strbuf, sizeof(strbuf)-1)) > 0)
        {
            strbuf[nbytes] = '\0';
            flprintf("<<%s>>\n", strbuf);
        }
    
        flprintf("Read EOF - %d exiting\n", (int)getpid());
    
        close(fd);
        return(0);
    }
    
    void SignalHandler(int sig)
    {
        flprintf("Received signal %d - %d exiting\n", sig, (int)getpid());
        if (fd != -1)
            close(fd);
        exit(0);
    }
    
    static void flprintf(const char *fmt, ...)
    {
        va_list args;
        va_start(args, fmt);
        vprintf(fmt, args);
        va_end(args);
        fflush(stdout);
    }
    

    I tested on Mac OS X 10.7.5 (GCC 4.7.1, not that it matters much in this case). The only behaviour I found surprising once it was working was that the program didn’t get EOF when the process writing a message to the FIFO closed. However, I think that’s explained by the O_RDWR on the open() call; there’s still a process (this one) with the FIFO open. Recompile with O_RDONLY and the process exits after the first time something writes to the pipe…sanity rules. What you’ve done is fine as a way of achieving a FIFO that never reports EOF. (It also explains why the process didn’t block on the open, waiting for a process to open the FIFO.)

    Sample output:

    $ ./sig >> sig.out &
    [1] 47598
    $ cat sig.out
    Starting - 47598
    Starting read loop
    $ echo "Testing, 1, 2, 3" > myPIPE
    $ cat sig.out
    Starting - 47598
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    $ echo "Another test - 1, 2, 3" > myPIPE
    $ echo "Mini test - 1, 2" > myPIPE
    $ cat sig.out
    Starting - 47598
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    <<Another test - 1, 2, 3
    >>
    <<Mini test - 1, 2
    >>
    $ ./sig 47598 >> sig.out &
    [2] 47610
    $ cat sig.out
    Starting - 47598
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    <<Another test - 1, 2, 3
    >>
    <<Mini test - 1, 2
    >>
    Received signal 2 - 47598 exiting
    SIGINT successfully delivered to PID 47598
    Starting - 47610
    Starting read loop
    [1]-  Done                    ./sig >> sig.out
    $ echo "Testing, 1, 2, 3" > myPIPE
    $ cat sig.out
    Starting - 47598
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    <<Another test - 1, 2, 3
    >>
    <<Mini test - 1, 2
    >>
    Received signal 2 - 47598 exiting
    SIGINT successfully delivered to PID 47598
    Starting - 47610
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    $ kill 47610
    $ cat sig.out
    Starting - 47598
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    <<Another test - 1, 2, 3
    >>
    <<Mini test - 1, 2
    >>
    Received signal 2 - 47598 exiting
    SIGINT successfully delivered to PID 47598
    Starting - 47610
    Starting read loop
    <<Testing, 1, 2, 3
    >>
    Received signal 15 - 47610 exiting
    [2]+  Done                    ./sig 47598 >> sig.out
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program in C: int main(int argc, char *argv[]) { int i=300;
Consider the following sample program in which a loop is running; int main() {
I am missing this DLL reference in c:\program files\SQL Server\90\Tools\Binn. I'm assuming that this
This issue is related to running a Java program (jar) dependent on thirdparty jar
I just recently asked this question: https://stackoverflow.com/questions/6359367/running-a-bash-program-every-day-at-the-same-time The solution of using crontab -e to
Assuming we are not concerned about running time of the program (which is practically
I have seen this on a program that I am tinkering: static const void
Just created an acc on SO to ask this :) Assuming this simplified example:
I've recently taken over a project from another consulting firm. I'm assuming this can
Are there any way to access or set iphone's alarm? Im assuming if this

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.