Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main(int argc, char *argv[]) {
- if (argv[1] == NULL) {
- printf("Desired amount of child processes must be passed as argument! \n");
- exit(-1);
- }
- char *endp;
- int childQuantity = (int) strtol(argv[1], &endp, 0);
- if (*endp != 0) {
- printf("Conversion from value: %s of type string to value of type long failed! \n", argv[1]);
- }
- int returnStatus;
- pid_t pid;
- for (int i = 0; i < childQuantity; i++) {
- pid = fork();
- if (pid < 0) {
- printf("Fork failed its work! \n");
- }
- if (pid == 0) {
- sleep(1);
- }
- if (pid > 0) {
- waitpid(pid, &returnStatus, 0);
- if (returnStatus == 0) {
- printf("Child process: %d | Parent process: %d \n", getpid(), getppid());
- kill(pid, 2563);
- printf("The child process terminated normally. \n");
- exit(0);
- }
- if (returnStatus == 1) {
- printf("The child process terminated with an error!. \n");
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment