160
Créer un processus zombie
- /*
- * Creates a zombie process.
- *
- * fork
- * getpid
- * getppid
- * sleep
- */
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #if defined( LINUX )
- #include <string.h>
- #endif
- #define SLEEPTIME 2 /* in secs */
- int main( int argc, char **argv ) {
- int pid;
- fprintf( stdout, "%i running...\n", getpid() );
- pid = fork();
- if ( pid == -1 ) { /* error */
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- if ( pid == 0 ) { /* child */
- fprintf( stdout, "%i with parent %i...\n", getpid(), getppid() );
- fprintf( stdout, "%i sleeping for %i secs...\n", getpid(), SLEEPTIME );
- sleep( SLEEPTIME );
- fprintf( stdout, "%i with parent %i...\n", getpid(), getppid() );
- exit( 0 );
- }
- /* don't wait for child (zombie) */
- fprintf( stdout, "%i bye\n", getpid() );
- exit( 0 );
- }
Commentaires