int main (int argc, char * argv[]) { int parent_pid, fid; caddr_t mmap_ptr; inputbuf * in_mem_p; char c; // if parent tells us to terminate, then clean up first sigset(SIGINT,in_die); // get id of process to signal when we have input // and the file id of the memory mapped file // i.e. process input arguments sscanf(argv[1], "%d", &parent_pid ); sscanf(argv[2], "%d", &fid ); // get the file id // attach to shared memory so we can pass input to // keyboard interrupt handler mmap_ptr = mmap((caddr_t) 0, /* Memory Location, 0 lets O/S choose */ bufsize,/* How many bytes to mmap */ PROT_READ | PROT_WRITE, /* Read and write permissions */ MAP_SHARED, /* Accessible by another process */ fid, /* which file is associated with mmap */ (off_t) 0); /* Offset in page frame */ if (mmap_ptr == MAP_FAILED){ printf("Child memory map has failed, KB is aborting!\n"); in_die(0); } in_mem_p = (inputbuf *) mmap_ptr; // now we have a shared memory pointer // read keyboard buf_index = 0; in_mem_p->ok_flag = 0; do { //local_irq_disable(); //printf(">>"); c = getchar(); //local_irq_enable(); if ( c != '\n') { if( buf_index < MAXCHAR-1 ) { in_mem_p->indata[buf_index++] = c; } } else { in_mem_p->indata[buf_index] = '\0'; in_mem_p->ok_flag = 1; //set ready status bit kill(parent_pid,SIGUSR1); //send a signal to parent buf_index = 0; // for now, just restart while( in_mem_p->ok_flag == 1) usleep(100000); } } while(1); //an infinite loop - exit when parent signals us } // keyboard
int main (int argc, char * argv[]) { int parent_pid, fid; caddr_t mmap_ptr; outputbuf * in_mem_p; char c; // if parent tells us to terminate, then clean up first sigset(SIGINT,in_die); // get id of process to signal when we have input // and the file id of the memory mapped file // i.e. process input arguments sscanf(argv[1], "%d", &parent_pid ); sscanf(argv[2], "%d", &fid ); // get the file id // attach to shared memory so we can pass input to // keyboard interrupt handler mmap_ptr = mmap((caddr_t) 0, /* Memory Location, 0 lets O/S choose */ bufsize,/* How many bytes to mmap */ PROT_READ | PROT_WRITE, /* Read and write permissions */ MAP_SHARED, /* Accessible by another process */ fid, /* which file is associated with mmap */ (off_t) 0); /* Offset in page frame */ if (mmap_ptr == MAP_FAILED){ printf("Child memory map has failed, CRT is aborting!\n"); in_die(0); } in_mem_p = (outputbuf *) mmap_ptr; // now we have a shared memory pointer // read keyboard buf_index = 0; in_mem_p->ok_flag = WAITING_TO_BE_WRITTEN; do { while(in_mem_p->ok_flag == WAITING_TO_BE_WRITTEN) { usleep(100000); } fflush(stdout); //printf("====================CRT OUTPUT: %s===================\n",in_mem_p->outdata); printf("%s\n", in_mem_p->outdata); fflush(stdout); in_mem_p->ok_flag = WAITING_TO_BE_WRITTEN; kill(parent_pid,SIGUSR2); }while(1); //an infinite loop - exit when parent signals us } // CRT
int main (int argc, char * argv[]) { /*Set up some Variables*/ int parent_PCB_id = argc; int fid; caddr_t mmap_ptr; inputbuf *in_mem_p; char c[MAX_CHAR]; // If parent tells us to terminate, then clean up first sigset(SIGINT,in_die); //Gets id of parents (to signal) and the file id of mmap. sscanf(argv[1], "%d", &parent_PCB_id); sscanf(argv[2], "%d", &kfid ); kmmap_ptr = mmap((caddr_t) 0, bufsize, PROT_READ | PROT_WRITE, MAP_SHARED, kfid, (off_t) 0); if (kmmap_ptr == MAP_FAILED){ printf("Child memory map has failed, KB is aborting! Error <%s> ...\n", strerror(errno)); in_die(0); } in_mem_p = (inputbuf *) kmmap_ptr; buf_index = 0; in_mem_p->flag = MEM_EMPTY; // Set the flag to “Buffer Empty” do { fgets(c, MAX_CHAR, stdin); // This only returns when <cr> is pressed strcpy(in_mem_p->data, c); // Read in data from buffer in_mem_p->flag = MEM_FULL; // Set the flag kill(parent_PCB_id ,SIGUSR1); // Signal the parent buf_index = 0; while(in_mem_p->flag == MEM_FULL){ usleep(100000); // Sleep until the RTX has read the data } } while(1); // Loops Forever }