Skip to content

Commit

Permalink
Finished number 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirrelus committed Jun 9, 2020
1 parent 42420ab commit ccb76bb
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions lab4/src/parallel_min_max.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
#include "find_min_max.h"
#include "utils.h"

pid_t group_pid[1000];
int k = 0;
void terminate (int param)
{
printf("Killing all child procesesses %d\n", k);
int i;
int ret;
for (i = 0 ;i < k; i++) {
printf("trying to kill %d\n", group_pid[i]);
kill(group_pid[i], SIGKILL);
ret = waitpid(group_pid[i], NULL, WNOHANG);
printf("kill %d\n", ret);
}
exit(0);
}

int main(int argc, char **argv) {
int seed = -1;
int array_size = -1;
Expand All @@ -29,7 +45,7 @@ int main(int argc, char **argv) {
{"array_size", required_argument, 0, 0},
{"pnum", required_argument, 0, 0},
{"by_files", no_argument, 0, 'f'},
{"timeout", optional_argument, 0, 0},
{"timeout", required_argument, 0, 0},
{0, 0, 0, 0}};

int option_index = 0;
Expand Down Expand Up @@ -63,21 +79,14 @@ int main(int argc, char **argv) {
case 2:
pnum = atoi(optarg);
// your code here
if (pnum < 1)
if (pnum < 1)
{
printf("at least 1 parallel process should be started\n");
return 1;
}
// error handling
break;
case 3:
/*timeout = atoi(optarg);
if(timeout == 0) with_files = true;
else if(timeout < 1)
{
printf("timeout is a positive number\n");
return 1;
}*/
with_files = true;
break;
case 4:
Expand All @@ -89,7 +98,7 @@ int main(int argc, char **argv) {
}
break;

defalut:
default:
printf("Index %d is out of options\n", option_index);
}
break;
Expand All @@ -116,7 +125,10 @@ int main(int argc, char **argv) {
return 1;
}

printf("timeout = %d\n", timeout);
// создатение указателя на функцию и обработчика сигнала
void (*funcptr)(int);
funcptr = signal (SIGALRM, terminate);
alarm(timeout);

int *array = malloc(sizeof(int) * array_size);
GenerateArray(array, array_size, seed);
Expand Down Expand Up @@ -172,6 +184,8 @@ int main(int argc, char **argv) {
}
return 0;
}
group_pid[k++]=child_pid;
sleep(1);

} else {
printf("Fork failed!\n");
Expand Down

0 comments on commit ccb76bb

Please sign in to comment.