Linux / how to check if subprocess is still alive

Discussion in 'C Programming' started by Sigfried Manner, Sep 1, 2003.

  1. Hello NG!

    I have following situation:

    c-prg | webprg

    "c-prg" is a small c-programm which reads data from an fastCgi-Server and
    writes them to stdout.
    The stdout is piped into webprg.
    "webprg" is an 4th-GL Application that reads Data from StdIn and writes Data
    to an fifo-pipe.
    "c-prg" reads data from the fifo-pipe and writes them back to the
    FastCGI-Server.

    Sometimes "webprg" stopps for different reasons.
    But "c-prg" is still alive and the Fast-CGI-Server has no reason to restart
    the process-chain.
    So, is there a way to determine if a subprocess is still alive.
    But keep in mind, that the subprocess (webprg) is not forked inside the
    parentprocess. It is just a pipe-chain.

    The following c-source is from "c-prg"


    Many thanks for any ideas in advance!
    Sig






    /*
    *
    * Produce a page containing all the inputs (fcgiapp version)
    *
    *
    * Copyright (c) 1996 Open Market, Inc.
    *
    * See the file "LICENSE.TERMS" for information on usage and redistribution
    * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    *
    */
    #ifndef lint
    static const char rcsid[] = "$Id: echo-x.c,v 1.1 2001/06/19 15:06:17 robs
    Exp $";
    #endif /* not lint */

    #include <stdio.h>
    #include "fcgi_config.h"

    #include <stdlib.h>

    #ifdef HAVE_UNISTD_H
    #include <unistd.h>
    #endif

    #ifdef _WIN32
    #include <process.h>
    #else
    extern char **environ;
    #endif

    #include "fcgiapp.h"

    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>

    static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
    {
    for( ; *envp != NULL; envp++) {
    printf("%s\n", *envp);
    }
    }

    int main ()
    {
    FCGX_Stream *in, *out, *err;
    FCGX_ParamArray envp;
    int count = 0;
    int in_count;

    int fd_fifo, pid;
    char buffer[4096];
    char fifo[30];
    mode_t mode = 0666;

    pid = getpid();

    sprintf(fifo, "/tmp/fifo_%d", pid);
    if((mkfifo(fifo, mode)) == -1) {
    FCGX_FPrintF(out,"Cannot create %s",fifo);
    return (-1);
    }
    printf("%s\n", fifo);


    while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
    char *contentLength = FCGX_GetParam("CONTENT_LENGTH", envp);
    int len = 0;

    ++count;

    if (contentLength != NULL)
    len = strtol(contentLength, NULL, 10);

    if (len <= 0) {
    }
    else {
    int i, ch;

    printf("POSTDATA=");
    for (i = 0; i < len; i++) {
    if ((ch = FCGX_GetChar(in)) < 0) {
    FCGX_FPrintF(out,
    "Error: Not enough bytes received on standard
    input<p>\n");
    break;
    }
    putchar(ch);
    }
    printf("\n");
    }

    PrintEnv(out, "Request environment", envp);
    PrintEnv(out, "Initial environment", environ);
    printf("<<EOF>>\n");
    fflush (stdout);

    fd_fifo=open(fifo, O_RDONLY);
    if (fd_fifo != -1) {
    while(1) {

    /* in_count = read(fd_fifo,&buffer,1);
    if (!in_count) {
    break;
    }
    FCGX_PutChar(buffer, out);
    */
    in_count = read(fd_fifo,&buffer,4096);
    if (!in_count) {
    break;
    }
    FCGX_PutStr(buffer,in_count,out);

    }
    close(fd_fifo);
    }
    else {
    FCGX_FPrintF(out, "Could not open fifo-pipe");
    }
    } /* while */

    unlink(fifo);
    return 0;
    }
    Sigfried Manner, Sep 1, 2003
    #1
    1. Advertising

  2. Sigfried Manner <> wrote in
    <3f535cef$0$34348$>:

    >Hello NG!
    >
    >I have following situation:
    >
    >c-prg | webprg

    <Warning: low topicality quotient>
    >
    >"c-prg" is a small c-programm which reads data from an fastCgi-Server and
    >writes them to stdout.

    <Severe warning: low topicality quotient>

    >The stdout is piped into webprg.

    <Error: topicality quotient critical>

    >"webprg" is an 4th-GL Application that reads Data from StdIn and writes Data
    >to an fifo-pipe.

    <Red alert: Topicality-core destabilisation in progress>

    >"c-prg" reads data from the fifo-pipe and writes them back to the
    >FastCGI-Server.

    <Danger: Topicality-core containment field failed>
    >
    >Sometimes "webprg" stopps for different reasons.
    >But "c-prg" is still alive and the Fast-CGI-Server has no reason to restart
    >the process-chain.

    <BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM>

    <SNIPped question and *nixish code>

    Please ask in an ng appropriate for your problem. c.l.c discusses the
    C language in its standardized (read: portable) flavours.

    Sorry.

    Irrwahn

    PS: While reading the FAQ for c.l.c, what did made you think it's the
    right place for your question?

    --
    If you don't care where you are, then you ain't lost.
    Irrwahn Grausewitz, Sep 1, 2003
    #2
    1. Advertising

  3. On Mon, 01 Sep 2003 17:16:24 +0200
    Irrwahn Grausewitz <> wrote:
    > Sigfried Manner <> wrote in
    > Please ask in an ng appropriate for your problem. c.l.c discusses the
    > C language in its standardized (read: portable) flavours.


    Actually, he did about five minutes later. At least he didn't cross-post, which
    would probably have resulted in extensive flaming and killfiling.

    > PS: While reading the FAQ for c.l.c, what did made you think it's the
    > right place for your question?


    Probably the part that said 'Please post endless moobs of nonstandard code so
    others can flame you without hesitation'. I think that's in the ten commandments
    of C as well somewhere...

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
    Pieter Droogendijk, Sep 1, 2003
    #3
  4. Irrwahn Grausewitz wrote:

    > Please ask in an ng appropriate for your problem. c.l.c discusses the
    > C language in its standardized (read: portable) flavours.


    Sorry and Thanks!

    Sig
    Sigfried Manner, Sep 1, 2003
    #4
  5. Pieter Droogendijk <> wrote in
    <>:
    >> PS: While reading the FAQ for c.l.c, what did made you think it's the
    >> right place for your question?

    >Probably the part that said 'Please post endless moobs of nonstandard code so
    >others can flame you without hesitation'. I think that's in the ten commandments
    >of C as well somewhere...

    Yup, right next to 'Write ++i=i++; 5 times a day and be proud of it.'.
    --
    It is not necessary to understand things
    in order to argue about them.
    Irrwahn Grausewitz, Sep 1, 2003
    #5
  6. Irrwahn Grausewitz wrote:

    > Pieter Droogendijk <> wrote in
    > <>:
    >>> PS: While reading the FAQ for c.l.c, what did made you think it's the
    >>> right place for your question?

    >>Probably the part that said 'Please post endless moobs of nonstandard code
    >>so others can flame you without hesitation'. I think that's in the ten
    >>commandments of C as well somewhere...

    > Yup, right next to 'Write ++i=i++; 5 times a day and be proud of it.'.


    To be honest, i am not a c-programmer.
    I?m developing business-applications since 15 years and (thank god!) i never
    had the need to use os-system-near languages ;-)

    Cheers!
    Sig
    Sigfried Manner, Sep 1, 2003
    #6
  7. Irrwahn Grausewitz <> scribbled the following:
    > Pieter Droogendijk <> wrote in
    > <>:
    >>> PS: While reading the FAQ for c.l.c, what did made you think it's the
    >>> right place for your question?

    >>Probably the part that said 'Please post endless moobs of nonstandard code so
    >>others can flame you without hesitation'. I think that's in the ten commandments
    >>of C as well somewhere...

    > Yup, right next to 'Write ++i=i++; 5 times a day and be proud of it.'.


    Be proud? Of code that won't even compile? Or are you thinking of C++?
    In C that should probably be "Write i=i++; 5 times a day and be proud of
    it".

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "That's no raisin - it's an ALIEN!"
    - Tourist in MTV's Oddities
    Joona I Palaste, Sep 1, 2003
    #7
  8. Joona I Palaste <> wrote in
    <bivtb5$mul$>:
    >> Yup, right next to 'Write ++i=i++; 5 times a day and be proud of it.'.

    >
    >Be proud? Of code that won't even compile? Or are you thinking of C++?
    >In C that should probably be "Write i=i++; 5 times a day and be proud of
    >it".

    Ah, you noticed the subtle humor. :)
    --
    It is not necessary to understand things
    in order to argue about them.
    Irrwahn Grausewitz, Sep 1, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG
    Replies:
    0
    Views:
    373
    clintonG
    Jul 3, 2004
  2. Michael Ulmann

    Check if session with know id is still alive...

    Michael Ulmann, Oct 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    586
    John Saunders
    Oct 4, 2004
  3. Andreas S
    Replies:
    2
    Views:
    325
    Gary Wright
    Feb 28, 2007
  4. Iñaki Baz Castillo
    Replies:
    12
    Views:
    192
    Eric Hodel
    Feb 9, 2011
  5. hisan
    Replies:
    1
    Views:
    1,302
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page