5

Create a zombie process

  1. /*
  2.  * Creates a zombie process.
  3.  *
  4.  *   fork
  5.  *   getpid
  6.  *   getppid
  7.  *   sleep
  8.  */
  9.  
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #if defined( LINUX )
  17. #include <string.h>
  18. #endif
  19.  
  20. #define SLEEPTIME               2               /* in secs */
  21.  
  22. int main( int argc, char **argv ) {
  23.     int pid;
  24.  
  25.     fprintf( stdout, "%i running...\n", getpid() );
  26.  
  27.     pid = fork();
  28.  
  29.     if ( pid == -1 ) { /* error */
  30.         fprintf( stderr, "%s\n", strerror( errno ) );
  31.         exit( 1 );
  32.     }
  33.  
  34.     if ( pid == 0 ) { /* child */
  35.         fprintf( stdout, "%i with parent %i...\n", getpid(), getppid() );
  36.         fprintf( stdout, "%i sleeping for %i secs...\n", getpid(), SLEEPTIME );
  37.         sleep( SLEEPTIME );
  38.         fprintf( stdout, "%i with parent %i...\n", getpid(), getppid() );
  39.         exit( 0 );
  40.     }
  41.  
  42.     /* don't wait for child (zombie) */
  43.     fprintf( stdout, "%i bye\n", getpid() );
  44.  
  45.     exit( 0 );
  46. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].