Skip to content

Instantly share code, notes, and snippets.

@cwetanow
Last active June 13, 2018 08:45
Show Gist options
  • Save cwetanow/e2385068084509e2aab3a5a573050471 to your computer and use it in GitHub Desktop.
Save cwetanow/e2385068084509e2aab3a5a573050471 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
// TODO:
// - parse max number
// - parse commands
// execute commands
// int main(int argc, char **argv)
int main()
{
// if (argc < 2)
// {
// write(2, "Invalid parameters\n", 20);
// exit(1);
// }
const char *a[4];
a[0] = "sleep 2 ; echo second";
a[1] = "sleep 4 ; echo fourth | sed s:four:4:g";
a[2] = "echo f1rst";
a[3] = "sleep 1 ; echo third";
int tasks = 4;
int maxParallel = 2;
int currentExecutingTasks = 0;
int current = 0;
int status = 0;
int pid2[tasks];
while (current < tasks)
{
if (currentExecutingTasks < maxParallel)
{
pid2[current] = fork();
if (pid2[current] == 0)
{
system(a[current]);
currentExecutingTasks--;
exit(0);
}
else
{
currentExecutingTasks++;
current++;
}
}
else
{
wait(NULL);
currentExecutingTasks--;
}
}
while ((wait(&status)) > 0)
{
};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment