Skip to content

Instantly share code, notes, and snippets.

@Citymonstret
Created September 2, 2019 19:33
Show Gist options
  • Select an option

  • Save Citymonstret/9236925f067af878f281bea0e8b7e380 to your computer and use it in GitHub Desktop.

Select an option

Save Citymonstret/9236925f067af878f281bea0e8b7e380 to your computer and use it in GitHub Desktop.
int main()
{
// Oändlig slinga
for (;;)
{
}
}
#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