Created
September 2, 2019 19:33
-
-
Save Citymonstret/9236925f067af878f281bea0e8b7e380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int main() | |
| { | |
| // Oändlig slinga | |
| for (;;) | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <wait.h> | |
| #include <stdlib.h> | |
| int main() | |
| { | |
| int processCount; | |
| int version; | |
| printf("How many processes to start?\n"); | |
| scanf("%d", &processCount); | |
| printf("What version to run? (1 for first version, 2 for second version with random termination time)\n"); | |
| scanf("%d", &version); | |
| printf("Launching %d processes\n", processCount); | |
| for (int i = 0; i < processCount; i++) | |
| { | |
| const pid_t pid = fork(); | |
| if (pid < 0) | |
| { | |
| fprintf(stderr, "Failed to fork process.\n"); | |
| return 1; | |
| } else if (pid == 0) | |
| { | |
| execlp("./CPU", "CPU", NULL); | |
| return 0; | |
| } | |
| } | |
| printf("Sleeping for two minutes.\n"); | |
| sleep(120); | |
| if (version == 2) { | |
| printf("Waiting for children.\n"); | |
| for (int i = 0; i < processCount; i++) | |
| { | |
| pid_t child = wait(NULL); | |
| printf("Child with PID %d died.\n", child); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment