-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.c
More file actions
77 lines (62 loc) · 1.78 KB
/
Copy pathproc.c
File metadata and controls
77 lines (62 loc) · 1.78 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "proc.h"
#include "tsh.h"
#include "job.h"
#include "helper.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
void write_proc(char * name, pid_t pid, pid_t ppid, char * stat) {
char path[MAXLINE];
sprintf(path, "./proc/%d/status", pid);
FILE * fp = fopen(path, "w");
if(fp == NULL){
unix_error("fopen");
}
fprintf(fp, "Name: %s\n", name);
fprintf(fp, "Pid: %d\n", pid);
fprintf(fp, "PPid: %d\n", ppid);
fprintf(fp, "PGid: %d\n", pid);
fprintf(fp, "Sid: %d\n", shell_pid);
fprintf(fp, "STAT: %s\n", stat);
fprintf(fp, "Username: %s\n", username);
fclose(fp);
}
void change_proc_stat(pid_t pid, char * stat){
char path[MAXLINE];
sprintf(path, "./proc/%d/status", pid);
// check if the file exists (some processes in the process group may have terminated and their proc files have been removed)
if(access(path, F_OK) != 0) {
return;
}
FILE * fp = fopen(path, "r+");
if(fp == NULL){
unix_error("fopen");
}
char str[MAXLINE];
for(int cnt = 0; cnt < 5; cnt++){
fgets(str, MAXLINE, fp);
}
fprintf(fp, "STAT: %s\n", stat);
fprintf(fp, "Username: %s\n", username);
fclose(fp);
}
void add_proc(char * name, pid_t pid, pid_t ppid, char * stat){
char path[MAXLINE];
sprintf(path, "./proc/%d", pid);
if(mkdir(path, 0777)!=0){
unix_error("mkdir error");
}
write_proc(name, pid, ppid, stat);
}
void remove_proc(pid_t pid){
char path[MAXLINE];
sprintf(path, "./proc/%d/status", pid);
if(remove(path) != 0){
unix_error("remove error");
}
sprintf(path, "./proc/%d", pid);
if(rmdir(path)!=0){ // remove directory only if it's empty
unix_error("rmdir error");
}
}