Professional Web Applications Themes

Making a process sleep instead of running constantly - UNIX Programming

Hi everyone, I am working on a program where i need to make the process sleep after executing certain commands and spawning child processes. It goes into a while(1) loop where it waits for its child processes to terminate. The problem that i am facing now is that the program is always running and it never goes to sleep. It takes up the entire CPU (99.9%) resources. Is there anything special that i need to follow to make the process sleep inspite of the while(1) loop. Thanks for the help Karthik...

  1. #1

    Default Making a process sleep instead of running constantly

    Hi everyone,

    I am working on a program where i need to make the process sleep after
    executing certain commands and spawning child processes. It goes into a
    while(1) loop where it waits for its child processes to terminate.

    The problem that i am facing now is that the program is always running
    and it never goes to sleep. It takes up the entire CPU (99.9%) resources.

    Is there anything special that i need to follow to make the process
    sleep inspite of the while(1) loop.

    Thanks for the help

    Karthik

    Karthik Guest

  2. #2

    Default Re: Making a process sleep instead of running constantly

    Karthik <com> writes:
     

    You're looking for wait, waitpid or wait4.

    --
    Måns Rullgård
    se
    Måns Guest

  3. #3

    Default Re: Making a process sleep instead of running constantly

    In comp.os.linux.development.system Karthik <com> wrote: 
     
     
     

    What you do in the while loop? If you call wait(2) or waitpid(2) the
    process *will* sleep until the child exits (or you recive a signal,
    but that shouldn't drive the CPU usage ip to 99.9%). Are you sure that
    what you're seeing is the parent process and not the child process?

    BTW, comp.os.linux.development.system isn't the right group for these
    kinds of questions, so I removed it from the followup list.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ fu-berlin.de
    \__________________________ http://www.physik.fu-berlin.de/~toerring
    Jens.Toerring@physik.fu-berlin.de Guest

  4. #4

    Default Re: Making a process sleep instead of running constantly



    Karthik wrote:
     

    As other posters have noted...

    If you are doing NOTHING in the while(1) loop and expecting
    a SIGCHLD to interrupt you, you WILL be using up all available
    CPU doing nothing.

    Use any of the wait(2) family of calls to wait for a specific
    process or for any process to terminate.


    --
    Ñ
    "It is impossible to make anything foolproof because fools are so
    ingenious" - A. Bloch

    Nick Guest

  5. #5

    Default Re: Making a process sleep instead of running constantly

    Karthik wrote: 

    It sounds mostly like what you need is one of the
    wait calls.

    If you just want to wait for a signal. You could
    do this:

    while(1) pause();

    Any other use of pause will usually have race
    conditions and should be avoided. Instead using
    sigsuspend is a possibility. Incorrect usage of
    sigsuspend will of course also lead to race
    conditions, so you need to know what you are
    doing.

    --
    Kasper Dupont -- der bruger for meget tid paa usenet.
    For sending spam use mailto:au.dk
    /* Would you like fries with that? */
    Kasper Guest

  6. #6

    Default Re: Making a process sleep instead of running constantly

    fu-berlin.de wrote: 
    >>I am pasting my code snippet:[/ref]
    >

    >

    >

    >

    >

    >

    >

    >

    >
    >
    > Ok, for a start lets skip the weird sigjmp() stuff. As long as
    > the child process isn't dead waitpid() will return with a value
    > of 0 immediately (since you're using the WNOHANG flag) because
    > there's no dead child. So the inner loop won't be run, instead
    > you get back to the start (that 'for_ever' thingy, which I guess
    > is the 'while(1)' you are talking about). And, of course, that
    > will eat all the CPU time the process can get because you don't
    > give it a chance to sleep. If you want the parent to sleep until
    > the child dies drop the WNOHANG flag. I don't know what this
    > inner while() loop is meant for, why do you need to check if
    > another child died immediately after the first on?
    >
    > But then there is this really weird sigsetjmp() stuff. For what
    > reason do you need the sigsetjmp() and siglongjmp() stuff? All
    > it gets you is that you jump back from the signal handler to a
    > place _before_ the the loop, thereby restarting it (probably with
    > the effect, when there's no other child, of keeping your program
    > running forever, just heating the CPU). I have really not the
    > slightest idea what this is supposed to be good for. You don't
    > need to "jump" out of the signal handler, when the signal handler
    > is done flow of control will go back to where the program was at
    > the moment the signal arrived, only that waitpid() will suddenly
    > return with the PID of the dead child process.
    >
    > So, please explain what that is all about, I have no idea what
    > that is supposed to achieve.
    > Regards, Jens[/ref]

    I finally solved the problem. I did'nt pay very good attention to the
    details in the man page properly and as a result was tied up with this
    problem.

    The code which solved it is :


    ----code----

    while(1) {
    sigjmpval = sigsetjmp(jmpbuf, 1);
    jumpok = 1;

    pid = wait(&status);
    while(pid > 0) {
    log_msg("PID is : %d",pid);
    pid = waitpid(-1, &status, WNOHANG);
    }
    }

    ----code----

    Coming back to the discussion of why I am doing this is :

    my program is supposed to be like a daemon program spawning processes on
    request (more like a server) and then when the child processes exit out,
    it has to collect them back. I have most of my functionality implemented
    outside the while(1) loop and basically the while(1) loop is present
    there to collect any dead child process. I am not doing anything spl in
    this while(1) loop and thats the reason i have my SIGCHILD handler jump
    to here.

    I tried some other ways like while(1) pause(); to constantly run this
    program and implement the code to collect the child processes inside the
    SIGCHILD handler, but due to my mis-understanding as stated before the
    program was running at 99.9% CPU time. Maybe now i will modify the code
    and avoid the sigsetjmp() stuff and implement the SIGCHILD handler properly.

    Thanks everyone for u'r help

    Karthik

    Karthik Guest

Similar Threads

  1. Running XP as a sub-process.
    By bill in forum Ubuntu
    Replies: 37
    Last Post: October 31st, 03:26 PM
  2. Verity won't stay running - constantly running
    By DaveF67 in forum Coldfusion Server Administration
    Replies: 0
    Last Post: September 26th, 12:59 PM
  3. running .exe in PHP, but not as a process
    By Darren Gates in forum PHP Development
    Replies: 4
    Last Post: December 24th, 12:51 PM
  4. Two Worker Process running at once
    By Adam in forum ASP.NET General
    Replies: 5
    Last Post: July 29th, 07:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139