Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <n>\n", argv[0]);
- return 1;
- }
- int n = atoi(argv[1]);
- for (int i = 0; i < n; i++) {
- pid_t child_pid = fork();
- if (child_pid == -1) {
- perror("fork");
- exit(1);
- } else if (child_pid == 0) {
- // Child process
- printf("Child process %d created\n", getpid());
- sleep(5); // Sleep for 5 seconds
- exit(0);
- }
- }
- // Parent process
- sleep(5 * n); // Sleep for 5 seconds per fork
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement