forked from rhit-csse332/userlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csse332_lib.c
111 lines (94 loc) · 2.01 KB
/
csse332_lib.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "csse332_lib.h"
int
register_process_pid(pid_t pid)
{
int fd;
fd = open(PROCFS_PATH, O_RDWR);
if (fd < 0) {
fprintf(stderr, "\t***FAIL***: Could not open procfs entry: %s\n",
strerror(errno));
return fd;
}
char buff[16];
int llen = sprintf(buff, "%c, %d\n", REGISTER_CHAR, pid);
buff[llen] = '\0';
llen = write(fd, buff, strlen(buff));
printf("Registering process with pid = %d\n", getpid());
if (llen != strlen(buff)) {
fprintf(stderr, "\t*** FAIL ***: Incorrect write to procfs: %s",
strerror(errno));
close(fd);
return llen;
}
close(fd);
return 0;
}
int withdraw_process_pid(pid_t pid)
{
int fd;
fd = open(PROCFS_PATH, O_RDWR);
if (fd < 0) {
fprintf(stderr, "\t***FAIL***: Could not open procfs entry: %s\n",
strerror(errno));
return fd;
}
char buff[16];
int llen = sprintf(buff, "%c, %d\n", WITHDRAW_CHAR, pid);
buff[llen] = '\0';
llen = write(fd, buff, strlen(buff));
printf("Withdrawing process with pid = %d\n", pid);
if (llen != strlen(buff)) {
fprintf(stderr, "\t*** FAIL ***: Incorrect write to procfs: %s",
strerror(errno));
close(fd);
return llen;
}
close(fd);
return 0;
}
int
yield_process_pid(pid_t pid)
{
int fd;
fd = open(PROCFS_PATH, O_RDWR);
if (fd < 0) {
fprintf(stderr, "\t***FAIL***: Could not open procfs entry: %s\n",
strerror(errno));
return fd;
}
char buff[16];
int llen = sprintf(buff, "%c, %d\n", YIELD_CHAR, pid);
buff[llen] = '\0';
llen = write(fd, buff, strlen(buff));
printf("Process with pid = %d yielding\n", pid);
if (llen != strlen(buff)) {
fprintf(stderr, "\t*** FAIL ***: Incorrect write to procfs: %s",
strerror(errno));
close(fd);
return llen;
}
close(fd);
return 0;
}
int
register_process(void)
{
return register_process_pid(getpid());
}
int
withdraw_process(void)
{
return withdraw_process_pid(getpid());
}
int
yield(void)
{
return yield_process_pid(getpid());
}