/* main program illustrating the UNIX fork() system call.
Compile using cc -o main main.c
*/
#include <stdio.h>
main() {
int fork(), childpid, value;
childpid = fork(); /* create a new process */
if(childpid == -1) {
}
else if(childpid == 0) { /* child process */
printf("child: my_process_id= %d,parent_process_id=%d\n", getpid
(), getppid
()); /* Transform the child process to another program called Hello */
/* Note the many variations of exec that exist! */
printf("Parent: About to run hello program \n"); if((value = execl("hello", "hello",0)) == -1) {
}
}
else { /* parent process */
printf("Parent: my_process_id=%d,my_child’s_process_id=%d",getpid
(), childpid
); } /* end if */
}
LyogICAgbWFpbiBwcm9ncmFtIGlsbHVzdHJhdGluZyB0aGUgVU5JWCBmb3JrKCkgc3lzdGVtIGNhbGwuIApDb21waWxlIHVzaW5nIGNjIC1vIG1haW4gbWFpbi5jCiovCiNpbmNsdWRlIDxzdGRpby5oPgptYWluKCkgewppbnQgZm9yaygpLCBjaGlsZHBpZCwgdmFsdWU7CgpjaGlsZHBpZCA9IGZvcmsoKTsgLyogY3JlYXRlIGEgbmV3IHByb2Nlc3MgKi8KCmlmKGNoaWxkcGlkID09IC0xKSB7CglwZXJyb3IoImNhbuKAmXQgZm9yay4gSGVscCEhIik7CglleGl0KC0xKTsKCX0gCgogZWxzZSBpZihjaGlsZHBpZCA9PSAwKSB7IC8qIGNoaWxkIHByb2Nlc3MgKi8KCXByaW50ZigiY2hpbGQ6IG15X3Byb2Nlc3NfaWQ9ICVkLHBhcmVudF9wcm9jZXNzX2lkPSVkXG4iLCBnZXRwaWQoKSwgZ2V0cHBpZCgpKTsKCS8qIFRyYW5zZm9ybSB0aGUgY2hpbGQgcHJvY2VzcyB0byBhbm90aGVyIHByb2dyYW0gY2FsbGVkIEhlbGxvICovCgkvKiBOb3RlIHRoZSBtYW55IHZhcmlhdGlvbnMgb2YgZXhlYyB0aGF0IGV4aXN0ISAqLwoJcHJpbnRmKCJQYXJlbnQ6IEFib3V0IHRvIHJ1biBoZWxsbyBwcm9ncmFtIFxuIik7CglpZigodmFsdWUgPSBleGV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