#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
main()
{
int pid ;
printf("I'am the original process with PID %d and PPID %d.\n", getpid(), getppid()) ;
pid = fork ( ) ; /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero,so I must be the parent*/ {
printf("I'am the parent with PID %d and PPID %d.\n", getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
} else /* pid is zero, so I must be the child */ {
printf("\nI'm the child with PID %d and PPID %d before sleeping.\n", getpid(), getppid()) ;
sleep(4); /* make sure that the parent terminates first */
printf("\nI'm the child with PID %d and PPID %d after waking up.\n", getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+ICAKI2luY2x1ZGUgPHVuaXN0ZC5oPiAgCiNpbmNsdWRlIDxzdGRsaWIuaD4gIAojaW5jbHVkZSA8c3lzL3dhaXQuaD4gIAptYWluKCkgIAp7IAoJaW50IHBpZCA7ICAKCXByaW50ZigiSSdhbSB0aGUgb3JpZ2luYWwgcHJvY2VzcyB3aXRoIFBJRCAlZCBhbmQgUFBJRCAlZC5cbiIsICBnZXRwaWQoKSwgZ2V0cHBpZCgpKSA7ICAKCXBpZCA9IGZvcmsgKCApIDsgLyogRHVwbGljYXRlLiBDaGlsZCBhbmQgcGFyZW50IGNvbnRpbnVlIGZyb20gaGVyZSAqLyAgCglpZiAoIHBpZCAhPSAwICkgLyogcGlkIGlzIG5vbi16ZXJvLHNvIEkgbXVzdCBiZSB0aGUgcGFyZW50Ki8gIHsgCgkJcHJpbnRmKCJJJ2FtIHRoZSBwYXJlbnQgd2l0aCBQSUQgJWQgYW5kIFBQSUQgJWQuXG4iLCAgZ2V0cGlkKCksIGdldHBwaWQoKSkgOyAgCgkJcHJpbnRmKCJNeSBjaGlsZCdzIFBJRCBpcyAlZFxuIiwgcGlkICkgOyAgCgl9IGVsc2UgLyogcGlkIGlzIHplcm8sIHNvIEkgbXVzd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