-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path07_kill.c
61 lines (52 loc) · 1.52 KB
/
07_kill.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
/* Il programma crea un processo figlio, il quale stampa a video i processi
relativi all'utente 'user_name', ma solo alcune informazioni, dopodiche' il
figlio esce normalmente e il padre, dopo aver atteso la conclusione del figlio,
termina, eventualmente un processo */
int main(int argc, char *argv[]) {
pid_t pid, pid_kill;
int child_status;
char *user_name;
if ((user_name = getenv("USER")) == NULL) {
fprintf(stderr, "Err.(%s) getenv() failed\n", strerror(errno));
exit(EXIT_FAILURE);
}
char *arg_var[] = {
"ps",
"-U",
user_name,
"-o",
"pid,ppid,ruid,euid,args",
(char*)0
};
if ((pid = fork()) < 0) {
fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
exit(EXIT_FAILURE);
} else if (pid == 0) {
if (execv("/bin/ps", arg_var) < 0) {
fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
exit(EXIT_FAILURE);
}
} else {
if (waitpid(-1, &child_status, 0) < 0) {
fprintf(stderr, "Err.(%s) waitpid() failed\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("get PID to kill (0=quit): ");
scanf("%d", &pid_kill);
if (pid_kill == 0) {
printf("bye\n");
exit(0);
}
if (kill(pid_kill, SIGKILL) == -1) {
fprintf(stderr,"Err.(%s) fork() failed\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
return(EXIT_SUCCESS);
}