-
Notifications
You must be signed in to change notification settings - Fork 14
/
Task 11.c
38 lines (31 loc) · 1.28 KB
/
Task 11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
11. Да се напише програма на C, която изпълнява последователно подадените ѝ като параметри команди,
като реализира следната функционалност постъпково:
- main cmd1 ... cmdN Изпълнява всяка от командите в отделен дъщерен процес.
- ... при което се запазва броя на изпълнените команди, които са дали грешка и броя на завършилите успешно.
*/
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
int main(int argc, char **argv){
size_t successful = 0, failed = 0;
for(int i = 1; i< argc; ++i){
const char *cmd = argv[i];
const pid_t child_pid = fork();
if(child_pid == -1)
err(1, "failed to fork %s", cmd);
if(child_pid == 0)
if(execlp(cmd, cmd, (char*)NULL) == -1)
err(2,"error while execlp command %s", cmd);
int status;
if(wait(&status) == -1)
err(2,"could not wait for current child");
if(WIFEXITED(status)){
if(WEXITSTATUS(status)) ++failed;
else ++successful;
} else ++failed;
}
printf("successful: %zu, failed: %zu\n", successful, failed);
exit(0);
}