S
shellcode
the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
pid_t forkval;
int rv, exitcode;
forkval=fork();
switch(forkval)
{
case -1:
perror("fork");
exit(1);
case 0:
printf("CHILD: This is the child process!!\n");
printf("CHILD: My PID is: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: Enter my exit code: ");
scanf("%d", &exitcode);
printf("CHILD: Exiting with exitcode.. %d\n", exitcode);
exit(exitcode);
default:
printf("PARENT: This is the parent process!!\n");
printf("PARENT: My PID is: %d\n", getpid());
printf("PARENT: My child's PID is: %d\n", forkval);
printf("PARENT: Waiting for child to exit....\n");
wait(&rv);
printf("PARENT: Child died!! Exit code: %d\n",
WEXITSTATUS(rv));
printf("PARENT: Exiting....\n");
break;
}
return 0;
}
------fork.c------
the compilation: gcc -Wall -o fork fork.c
the results:
$ ./fork
CHILD: This is the child process!!
CHILD: My PID is: 1557
CHILD: My parent's PID is: 1556
CHILD: Enter my exit code: PARENT: This is the parent process!!
PARENT: My PID is: 1556
PARENT: My child's PID is: 1557
PARENT: Waiting for child to exit....
im a bit confused at this point. i've looked over the code and the man
pages and i really think i did it right, yet for some reason the CHILD
gets executed before the parent and i'm never given a chance to enter
the exitcode. thank's for help.
------fork.c------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
pid_t forkval;
int rv, exitcode;
forkval=fork();
switch(forkval)
{
case -1:
perror("fork");
exit(1);
case 0:
printf("CHILD: This is the child process!!\n");
printf("CHILD: My PID is: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: Enter my exit code: ");
scanf("%d", &exitcode);
printf("CHILD: Exiting with exitcode.. %d\n", exitcode);
exit(exitcode);
default:
printf("PARENT: This is the parent process!!\n");
printf("PARENT: My PID is: %d\n", getpid());
printf("PARENT: My child's PID is: %d\n", forkval);
printf("PARENT: Waiting for child to exit....\n");
wait(&rv);
printf("PARENT: Child died!! Exit code: %d\n",
WEXITSTATUS(rv));
printf("PARENT: Exiting....\n");
break;
}
return 0;
}
------fork.c------
the compilation: gcc -Wall -o fork fork.c
the results:
$ ./fork
CHILD: This is the child process!!
CHILD: My PID is: 1557
CHILD: My parent's PID is: 1556
CHILD: Enter my exit code: PARENT: This is the parent process!!
PARENT: My PID is: 1556
PARENT: My child's PID is: 1557
PARENT: Waiting for child to exit....
im a bit confused at this point. i've looked over the code and the man
pages and i really think i did it right, yet for some reason the CHILD
gets executed before the parent and i'm never given a chance to enter
the exitcode. thank's for help.