-
Notifications
You must be signed in to change notification settings - Fork 4
/
crt.c
151 lines (123 loc) · 4.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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) {
//printf("You're in c_child_die\n");
exit(0);
}
// ***CRT UART SIMULATION***
int main(int argc, char * argv[]) {
//printf("You're in crt main\n");
// if parent tells us to terminate, then clean up first
sigset(SIGINT, c_child_die);
// delay after being forked from parent
usleep(1000000);
// 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 = c_mmap_ptr;
buf_index = 0;
// link the flag to the end of the buffer and set it
out_mem_p->oc_flag = &out_mem_p->outdata[c_bufsize];
*out_mem_p->oc_flag = 0;
//c is our temp character to read from the buffer
char c;
usleep(100000);
// regular running, infinite loop - exit when parent signals us
while(1)
{
//printf("crt 1, flag = %d\n", *out_mem_p->oc_flag);
while(*out_mem_p->oc_flag == 0)
{
//wait for the iprocess to load the buffer (if applicable)
usleep(100000);
//printf("crt 2.5\n");
// signal the crt i-process periodically to start doing it's thing
kill(parent_pid,SIGUSR2);
}
//printf("crt 2\n");
// 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)
{
//printf("crt 3\n");
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("crt 5\n");
//send a signal to parent to start handler to start crt_iproc
// kill(parent_pid, SIGUSR2);
}
// increment the buffer
buf_index++;
}
}
*/
// while there is something in the buffer
while(out_mem_p->outdata[buf_index] != '\0')
{
printf("%c", out_mem_p->outdata[buf_index]);
buf_index++;
fflush(stdout);
}
// when we reach the null character reset the flag and array start point
*out_mem_p->oc_flag = 0;
buf_index = 0;
}
}
// if the shared pointer is initialized as NULL
else{
printf("CRT shared memory pointer initialization failed\n");
}
}