- 论坛徽章:
- 0
|
这是通过SHELL工作
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <unistd.h>
- #include <string.h>
-
- 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<len )
- {
- 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;
- }
-
复制代码
[ 本帖最后由 net_robber 于 2007-3-13 10:07 编辑 ] |
|