Chinaunix首页 | 论坛 | 博客
  • 博客访问: 114550
  • 博文数量: 22
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 290
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-08 20:21
文章分类
文章存档

2011年(9)

2009年(1)

2008年(12)

我的朋友

分类: C/C++

2008-09-19 10:18:45

#include
#include
#include
#include
#include
#include

void fun_kill( int sig )
{
    printf( "process with PID %d got a signal!\n", getpid() );
    fflush( stdout );
    char *pre = "ps awx -o \"\%p \%P\"|grep -w ";
    char *post = " | awk \'{ print $1  }\'|xargs kill -9";
    
    char *command = malloc( 1024*sizeof (char) );
    char *c_pid = malloc( 8*sizeof (char) );
    memset ( command, 0, 1024*sizeof(char) );
    memset ( c_pid, 0, 8*sizeof(char) );
    char *a,*b;
    char c;
   
    //get PID string * NOTE: here the string is in a wrong order
    int len = 0;
    int tmp = getpid();
    for ( len = 0; tmp; tmp /= 10   )
    {
        *( c_pid + len ) = (tmp%10)+'0';
        len++;
    }
    printf ( "%s has %d char\n", c_pid,len );
    fflush( stdout );

    // deal the order
    a = c_pid;
    b = c_pid+len-1;
    printf ("c_pid = %d\n",c_pid);
    printf ("a = %d\n",a);
    printf ("b = %d\n",b);
    fflush( stdout );


    while ( a < b )
    {
        c = *a;
        *a =*b;
        *b = c;
        a += sizeof(char);
        b -= sizeof(char);
    }
/*   
    int i = 0;
    while ( i    {
        printf ("%c", *(c_pid+i) );
    }
*/   

    printf ( "command1:%s\n", command );
    fflush( stdout );


    strcat ( command, pre );
    strcat ( command, c_pid );
    strcat ( command, post );
    printf ( "command2:%s\n", command );
    fflush( stdout );

//    system( command );

}



int main( void )
{
    int pid = 0;

    if( pid = fork() )
    {
//        printf ( "in father, pid= %d\nin father, child pid = %d\n", getpid(), pid );
        fflush( stdout );
        sleep ( 5 );
        printf ( "kill ( pid, SIG ); \n");
        fflush( stdout );
        kill ( pid, SIGUSR1 );
        sleep ( 10 );
        printf ( "the father is still alive!\n", pid );
        fflush( stdout );
    }
    else
    {
        signal ( SIGUSR1, fun_kill );
        if( pid = fork() )
        {
            int i = 10;
            while ( i-- )
            {   
//                printf ( "in child, pid= %d\nin child, ppid= %d\n", getpid(), getppid() );
                fflush( stdout );
                sleep ( 1 );
            }
        }
        else
        {
            int i = 10;
            while ( i-- )
            {   
//                printf ( "in grandsun , pid= %d\nin grandsun , ppid= %d\n", getpid(), getppid() );
                fflush( stdout );
                sleep ( 1 );
            }
        }
    }
   
    return 0;
}

 
阅读(1359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~