void PrintDriver(){ int i, code; char *p; msg_t msg; msg.recipient = 2; printing_semaphore = SemGet(0); outportb(LPT1_BASE+LPT_CONTROL, PC_SLCTIN); code = inportb(LPT1_BASE+LPT_STATUS); for(i=0; i<50; i++)IO_DELAY(); outportb(LPT1_BASE+LPT_CONTROL, PC_INIT|PC_SLCTIN|PC_IRQEN); Sleep(1); for(;;){ MsgRcv(&msg); cons_printf("PrintDriver (PID %d) now prints...\n",GetPid()); p = msg.data; while(*p){ SemWait(printing_semaphore); outportb(LPT1_BASE+LPT_DATA, *p); code = inportb(LPT1_BASE+LPT_CONTROL); outportb(LPT1_BASE+LPT_CONTROL, code|PC_STROBE); for(i=0; i<50; i++)IO_DELAY(); outportb(LPT1_BASE+LPT_CONTROL, code); p++; } } }
void PrintDriver() { int i, code; char *p; msg_t temp_msg; printing_semaphore = SemGet(0); //request semaphore printing_semaphore, limit 0. // reset printer (check printer power, cable, and paper), it will jitter outportb(LPT1_BASE+LPT_CONTROL, PC_SLCTIN); // CONTROL reg, SeLeCT INterrupt code = inportb(LPT1_BASE+LPT_STATUS); // read STATUS for(i = 0; i < 50; i++) //loop 50 times of IO_DELAY(); { IO_DELAY(); } outportb(LPT1_BASE+LPT_CONTROL, PC_INIT|PC_SLCTIN|PC_IRQEN); // IRQ ENable Sleep(1); //Sleep for a second, needs time resetting while(1)//forever loop: { temp_msg.recipient = running_pid; MsgRcv(&temp_msg);//receive a message, get if msg to print ******* p = temp_msg.data; cons_printf("PrintDriver (PID %d) now prints...\n", GetPid()); //a notification msg (match how demo runs) //set p to point to start of character string in message while (*p != '\0') //"what p points to" is not null/empty/(char)0, { outportb(LPT1_BASE+LPT_DATA, *p); // write char to DATA reg code = inportb(LPT1_BASE+LPT_CONTROL); // read CONTROL reg outportb(LPT1_BASE+LPT_CONTROL, code|PC_STROBE); // write CONTROL, STROBE added for(i = 0; i < 50; i++) //do 50 times of IO_DELAY { IO_DELAY(); } outportb(LPT1_BASE+LPT_CONTROL, code); // send back original CONTROL SemWait(printing_semaphore);//semaphore-wait on the printing semaphore p++; //move p to next character to print } } }
int main() { int *array = (int *)ShmAllocate(10*sizeof(int)); array[0]=1; int x = Fork(); int id = SemGet(SEMKEY); if(x == 0) { SemOp(id, -1); PrintChar('C'); array[0]=10; SemOp(id, 1); } else { Join(x); SemOp(id, -1); PrintChar('P'); PrintInt(array[0]); SemOp(id, 1); } }
int main() { array = (int*)ShmAllocate(sizeof(int)*(SIZE+3)); // queue[SIZE], head, tail, count int x, i, j, seminit = 1, y; int pid[NUM_DEQUEUER+NUM_ENQUEUER]; for (i=0; i<SIZE; i++) array[i] = -1; array[SIZE] = 0; array[SIZE+1] = 0; array[SIZE+2] = 0; semid = SemGet(SEM_KEY1); SemCtl(semid, SYNCH_SET, &seminit); stdoutsemid = SemGet(SEM_KEY2); SemCtl(stdoutsemid, SYNCH_SET, &seminit); notFullid = CondGet(COND_KEY1); notEmptyid = CondGet(COND_KEY2); int temp; temp = -11; SemCtl(semid,SYNCH_GET,&temp); PrintInt(temp); for (i=0; i<NUM_DEQUEUER; i++) { x = Fork(); if (x == 0) { for (j=0; j<NUM_DEQUEUE_OP; j++) { x = Dequeue (i, &y); SemOp(stdoutsemid, -1); PrintString("Dequeuer "); PrintInt(i); PrintString(": Got "); PrintInt(x); PrintString(" from slot "); PrintInt(y); PrintChar('\n'); SemOp(stdoutsemid, 1); } Exit(DEQUEUE_EXIT_CODE); } pid[i] = x; } for (i=0; i<NUM_ENQUEUER; i++) { x = Fork(); if (x == 0) { x = i*NUM_ENQUEUE_OP; for (j=0; j<NUM_ENQUEUE_OP; j++) { y = Enqueue (x+j, i); SemOp(stdoutsemid, -1); PrintString("Enqueuer "); PrintInt(i); PrintString(": Inserted "); PrintInt(x+j); PrintString(" in slot "); PrintInt(y); PrintChar('\n'); SemOp(stdoutsemid, 1); } Exit(ENQUEUE_EXIT_CODE); } pid[i+NUM_DEQUEUER] = x; } for (i=0; i<NUM_DEQUEUER+NUM_ENQUEUER; i++) { x = Join(pid[i]); SemOp(stdoutsemid, -1); PrintString("Parent joined with "); PrintInt(pid[i]); PrintString(" having exit code "); PrintInt(x); PrintChar('\n'); SemOp(stdoutsemid, 1); } SemCtl(semid, SYNCH_REMOVE, 0); SemCtl(stdoutsemid, SYNCH_REMOVE, 0); CondRemove(notFullid); CondRemove(notEmptyid); return 0; }