On Tue, Aug 19, 2003 at 02:20:49PM +0430, Behdad Esfahbod wrote:

> Hi,
> 
> How to stop a process from a shell script (or C code)?  I mean 
> like pushing Ctrl+Z.  When I try kill -STOP pid, the process 
> terminates.  'strace bash' didn't helped.  It makes lots of 
> sigaction calls that I can't understand what they are.

Hi Behdad, 

Take a look at this program: 

#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#define __USE_GNU
#include <signal.h>

void sighandler(int signum)
{
        printf("caught %d\n", signum); 
}

int main(void)
{
        int i; 
        sighandler_t ret; 
        printf("setting sighandlers: \n"); 

        for (i = 1; i < SIGRTMIN; ++i) {
                if ((ret = signal(i, &sighandler)) == SIG_ERR) { 
                        /* some signals can't be caught */ 
                        if (i == SIGKILL || i == SIGSTOP) 
                                continue; 
                        
                        printf("signal(%d) failed\n", i); 
                        exit(EXIT_FAILURE); 
                } 
        } 

        printf("sighandlers set.\n");   

        while(1)
                getchar(); 

        return 0; 
}

Compile and run it, you'll see that when you hit C-z, the shell
actually sends SIGTSTP (20), not SIGSTOP(19). 

> Moreover, how to start such a stopped process?  'kill -CONT pid' 
> does not work on a process stopped by (the same) bash. BTW, 
> strace showed that bash itself do call a kill -CONT.

kill -CONT should work, I don't know why it doesn't. Perhaps it does,
but you are confusing the shell's job control facilities, which still
show it as stopped? it should be pretty easy to check. 
-- 
Muli Ben-Yehuda
http://www.mulix.org

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to