forked from ShariKing/FuzzyBunnies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrt.c
124 lines (96 loc) · 3.8 KB
/
crt.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
112
113
114
115
116
117
118
119
120
121
122
123
// crt_child
//
// Send characters to be displayed on screen
//
//+++++++++++++++++++++++
// modifed to use the POSIX-style of obtaining shared memory
// by P. Dasiewicz, June 5, 2007
//+++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
// #include <setjmp.h>
#include <signal.h>
// #include <string.h>
// #include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
// #include <sys/types.h>
// #include <sys/ipc.h>
// #include <sys/shm.h>
// #include <errno.h>
#include "kbcrt.h"
// ***FUNCTION TO CLEAN UP CHILD PROCESSES***
void c_child_die(int signal) {
exit(0);
}
// ***CRT UART SIMULATION***
int main(int argc, char * argv[]) {
// delay after being forked from parent
usleep(1000000);
// if parent tells us to terminate, then clean up first
sigset(SIGINT, c_child_die);
// get id of process to signal when we have output and the file id of the memory mapped file
sscanf(argv[1], "%d", &parent_pid);
sscanf(argv[2], "%d", &c_fid); // get the file id
// attach to shared memory so we can pass output to crt interrupt handler
c_mmap_ptr = mmap((caddr_t)0, // Memory Location, 0 lets O/S choose
c_bufsize + 1, // How many bytes to mmap -> +1 is the flag
PROT_READ | PROT_WRITE, // Read and write permissions
MAP_SHARED, // Accessible by another process
c_fid, // which file is associated with mmap
(off_t) 0); // Offset in page frame
// if the pointer to the memory sucks
if (c_mmap_ptr == MAP_FAILED) {
printf("Child memory map has failed, CRT is aborting!\n");
c_child_die(0);
}
// create a shared memory pointer
out_mem_p = (struct outbuf *) malloc(sizeof (struct outbuf));
// ensuring the pointer is not NULL
if (out_mem_p){
// initialize stuff
out_mem_p->outdata = (char *) c_mmap_ptr;
buf_index = 0;
//link the flag to the end of the buffer and set the flag
out_mem_p->oc_flag = &out_mem_p->outdata[c_bufsize + 1];
*out_mem_p->oc_flag = 0; //0 is empty
//c is our temp character to read from the buffer
char c;
// regular running, infinite loop - exit when parent signals us
do {
while(*out_mem_p->oc_flag == 0){
// signal the crt i-process periodically to start doing it's thing
kill(parent_pid,SIGUSR2);
//wait for the iprocess to load the buffer (if applicable)
usleep(100000);
}
// reset the buffer index to read from the beginning
buf_index = 0;
// while there is something in the buffer
while(*out_mem_p->oc_flag != 0){
if (buf_index < MAXCHAR - 1) {
c = out_mem_p->outdata[buf_index];
// while we're not at the end of the output (NULL character)
if (c != '\0')
printf("%c", c);
// when we reach the null character
else {
// reset the flag and array start point
*out_mem_p->oc_flag = 0;
buf_index = 0;
printf("\n");
//send a signal to parent to start handler to start crt_iproc
// kill(parent_pid, SIGUSR2);
}
// increment the buffer
buf_index++;
}
}
} while (1);
}
// if the shared pointer is initialized as NULL
else{
printf("CRT shared memory pointer initialization failed");
}
}