// ********************************************************************** // ********************************************************************** // signal semaphore // // if task blocked by semaphore, then clear semaphore and wakeup task // else signal semaphore // void semSignal(Semaphore* s) { int i; // assert there is a semaphore and it is a legal type assert("semSignal Error" && s && ((s->type == 0) || (s->type == 1))); // check semaphore type if (s->type == 0) { // binary semaphore // look through tasks for one suspended on this semaphore for (i=0; i<MAX_TASKS; i++) // look for suspended task { if (tcb[i].event == s) { s->state = 0; // clear semaphore tcb[i].event = 0; // clear event pointer tcb[i].state = S_READY; // unblock task if(DeQ(s->q,i) >= 0) { enQ(rq,i,tcb[i].priority); } if (!superMode) swapTask(); return; } } // nothing waiting on semaphore, go ahead and just signal s->state = 1; // nothing waiting, signal if (!superMode) swapTask(); return; } else { // counting semaphore if (++s->state > 0) { return; } else { int nextTask = DeQ(s->q,-1); enQ(rq, nextTask, tcb[nextTask].priority); tcb[nextTask].state = S_READY; tcb[nextTask].event = 0; return; } } } // end semSignal
int main () { Q *q = createQ(10); enQ(q, 0x0, 10); enQ(q, 0x0, 20); deQ(q); deQ(q); enQ(q, 0x0, 30); enQ(q, 0x0, 40); enQ(q, 0x0, 50); struct qNode *n = deQ(q); if (n != NULL) printf("Dequeued item is %d", n->key); return 0; }
static void exitTask(int taskId) { Semaphore* sem = semaphoreList; Semaphore** semLink = &semaphoreList; assert("exitTaskError" && tcb[taskId].name); // 1. find task in system queue // 2. if blocked, unblock (handle semaphore) // 3. set state to exit // ?? add code here while(sem = *semLink) { if (tcb[taskId].event == sem) { // printf("\nHello World"); DeQ(sem->q,taskId); enQ(rq,taskId,tcb[taskId].priority); // printQ(rq); } // move to next semaphore semLink = (Semaphore**)&sem->semLink; } // DeQ(rq,taskId); tcb[taskId].state = S_EXIT; // EXIT task state return; } // end exitTask
void dispatch() /* dispatch determines which process is to run next and transfers control to that process. */ { PCBptr oldrun = running; if ((running->state == DISPATCH) && (running->pin != 1)) enQ(running, &Ready); /* get next process */ running = deQ(&Ready); if (running == 0) running = &(PTable[0]); /* wait process */ /* set CPU timer */ SPT_(26); /* save old stack ptr and load new one */ if (running->state == NEW) { running->state = DISPATCH; CSWITCH1(&oldrun->top, running->regs); } else CSWITCH(&oldrun->top, &running->top); } /* end dispatch */
int ki_wait (struct k_t *sem, int timeout) { // used by msg system DI (); if (0 < sem->cnt1) { // lucky that we do not need to wait ? sem->cnt1--; // Salute to Dijkstra return (0); } if (timeout == -1) { // no luck, dont want to wait so bye bye return (-2); } // from here we want to wait pRun->cnt2 = timeout; // if 0 then wait forever if (timeout) pRun->cnt3 = (int) sem; // nasty keep ref to semaphor // so we can be removed if timeout occurs sem->cnt1--; // Salute to Dijkstra enQ (sem, deQ (pRun)); ki_task_shift (); // call enables NOT interrupt on return return ((char) (pRun->cnt2)); // 0: ok, -1: timeout }
int k_wait (struct k_t *sem, int timeout) { // copy of ki_wait just with EI()'s before leaving DI (); if (0 < sem->cnt1) { // lucky that we do not need to wait ? sem->cnt1--; // Salute to Dijkstra EI (); return (0); } if (timeout == -1) { // no luck, dont want to wait so bye EI (); return (-2); } // from here we have to wait pRun->cnt2 = timeout; // if 0 then wait forever if (timeout) pRun->cnt3 = (int) sem; // nasty keep ref to semaphore, // so we can be removed if timeout occurs sem->cnt1--; // Salute to Dijkstra enQ (sem, deQ (pRun)); ki_task_shift (); // call enables interrupt on return EI (); return (char) (pRun->cnt2); // 0: ok, -1: timeout }
int ki_wait (struct k_t *sem, int timeout) { DI (); if (0 < sem->cnt1) { sem->cnt1--; // Salute to Dijkstra k_sem_unclip (sem->nr); return (1); // ok: 1 bq we are not suspended } if (timeout < 0) // no luck, dont want to wait so bye bye { return (-2); } // from here we want to wait pRun->cnt2 = timeout; // 0 == wait forever if (timeout) { // so we can be removed if timeout occurs pRun->cnt3 = (int) sem; // nasty keep ref to semaphore in task stomach } sem->cnt1--; // Salute to Dijkstra k_sem_unclip (sem->nr); enQ (sem, deQ (pRun)); ki_task_shift (); // back again - have semaphore received signal or timeout ? pRun->cnt3 = 0; // reset ref to timer semaphore return ((char) (pRun->cnt2)); // 0: ok , -1: timeout }
int main() { int r, i; /* 32 entries */ queInit(&Q, 32); srand(time(NULL)); printf("Rand #: \n"); for(i=0; i<25; i++) { r = rand()%32; enQ(&Q, r); printf("%d st: %d en: %d\n", r, (Q)->start, (Q)->end); } printf("\nQue after inserts: \n"); walk(&Q); for(i=0; i<5; i++) deQ(&Q); printf("\nQue after DeQ: \n"); walk(&Q); printf("st: %d en: %d\n\n", (Q)->start, (Q)->end); return 0; }
// create task int createTask(char* name, // task name int (*task)(int, char**), // task address int priority, // task priority int argc, // task argument count char* argv[]) // task argument pointers { int tid; // find an open tcb entry slot for (tid = 0; tid < MAX_TASKS; tid++) { if (tcb[tid].name == 0) { char buf[8]; // create task semaphore if (taskSems[tid]) deleteSemaphore(&taskSems[tid]); sprintf(buf, "task%d", tid); taskSems[tid] = createSemaphore(buf, 0, 0); taskSems[tid]->taskNum = 0; // assign to shell // copy task name tcb[tid].name = (char*)malloc(strlen(name)+1); strcpy(tcb[tid].name, name); // set task address and other parameters tcb[tid].task = task; // task address tcb[tid].state = S_NEW; // NEW task state tcb[tid].priority = priority; // task priority tcb[tid].parent = curTask; // parent tcb[tid].argc = argc; // argument count // ?? malloc new argv parameters tcb[tid].argv = clone_argv(argv,argc); // argument pointers tcb[tid].event = 0; // suspend semaphore tcb[tid].RPT = tcb[tid].RPT = LC3_RPT + ((tid) ? ((tid-1)<<6) : 0); // root page table (project 5) tcb[tid].cdir = CDIR; // inherit parent cDir (project 6) // define task signals createTaskSigHandlers(tid); // Each task must have its own stack and stack pointer. tcb[tid].stack = malloc(STACK_SIZE * sizeof(int)); tcb[tid].time = 0; // ?? may require inserting task into "ready" queue enQ(rq,tid, tcb[tid].priority); if (tid) swapTask(); // do context switch (if not cli) return tid; // return tcb index (curTask) } } // tcb full! return -1; } // end createTask
// ********************************************************************** // ********************************************************************** // wait on semaphore // // if semaphore is signaled, return immediately // else block task // int semWait(Semaphore* s) { assert("semWait Error" && s); // assert semaphore assert("semWait Error" && ((s->type == 0) || (s->type == 1))); // assert legal type assert("semWait Error" && !superMode); // assert user mode // check semaphore type if (s->type == 0) { // binary semaphore // if state is zero, then block task if (s->state == 0) { tcb[curTask].event = s; // block task tcb[curTask].state = S_BLOCKED; DeQ(rq,curTask); enQ(s->q, curTask,tcb[curTask].priority); swapTask(); // reschedule the tasks return 1; } // state is non-zero (semaphore already signaled) s->state = 0; // reset state, and don't block return 0; } else { s->state--; if (s->state >= 0) return; tcb[curTask].event = s; // block task tcb[curTask].state = S_BLOCKED; DeQ(rq,curTask); enQ(s->q, curTask,tcb[curTask].priority); swapTask(); // reschedule the tasks return 1; } } // end semWait
int preflow(int n) { Edge edge; int i = 0; for (edge = g_node[0].adj_list; edge < g_node[1].adj_list; edge++) { g_node[0].excess -= edge->capacity; edge->endpoint->excess += (edge->capacity); edge->mateedge->capacity += (edge->capacity); edge->capacity = 0; if (edge->endpoint != sink && edge->endpoint->inQ == 0) { edge->endpoint->inQ = (i % n) + 1; enQ(threadEnv + i % n, edge->endpoint); i++; } } }
int k_prio_wait (struct k_t *sem, int timeout, char prio) { int retval; return -666; // no rdy for use // copy of ki_wait just with EI()'s before leaving DI (); if (0 < sem->cnt1) { // lucky that we do not need to wait ? sem->cnt1--; // Salute to Dijkstra // set prio pRun->prio = prio; // no need bq we are alrdy in front prio_enQ (pAQ, deQ (pRun)); EI (); return (0); } if (timeout == -1) { // no luck, dont want to wait so bye EI (); return (-2); } // from here we have to wait pRun->cnt2 = timeout; // if 0 then wait forever if (timeout) { pRun->cnt3 = (int) sem; // nasty keep ref to semaphore, } // so we can be removed if timeout occurs sem->cnt1--; // Salute to Dijkstra enQ (sem, deQ (pRun)); ki_task_shift (); // call enables interrupt on return pRun->cnt3 = 0; // reset ref to timer semaphore retval = pRun->cnt2; if (retval == 0) { // set prio pRun->prio = prio; // no need prio_enQ (pAQ, deQ (pRun)); } EI (); return retval; // 0: ok, -1: timeout }
static void exitTask(int taskId) { assert("exitTaskError" && tcb[taskId].name); // 1. find task in system queue // 2. if blocked, unblock (handle semaphore) // 3. set state to exit // ?? add code here if (tcb[taskId].state == S_BLOCKED) { Semaphore* s = tcb[taskId].event; enQ(rq, deQ(s->q, taskId), tcb[taskId].priority); tcb[curTask].event = 0; s->state++; } tcb[taskId].state = S_EXIT; // EXIT task state return; } // end exitTask
static int do_read(struct net_device *dev, void *cbuf, int cbuflen, void *dbuf, int dbuflen) { int i = getmbox(); int ret; if(i) { qels[i].cbuf = (unsigned char *) cbuf; qels[i].cbuflen = cbuflen; qels[i].dbuf = (unsigned char *) dbuf; qels[i].dbuflen = dbuflen; qels[i].QWrite = 0; qels[i].mailbox = i; /* this should be initted rather */ enQ(&qels[i]); idle(dev); ret = mailbox[i]; mboxinuse[i]=0; return ret; } printk("ltpc: could not allocate mbox\n"); return -1; }
int main(int argc, char *argv[]) { bool rFlag = false; // Flag to tell if -r is specified int height = 3; // HEIGHT of the Rubik's square int width = 3; // WIDTH of the Rubik's square int maxlength; // MAXLENGTH of a series of moves char *initial = NULL; // The INITIAL string char *goal = NULL; // The GOAL string /* INPUT ERROR CHECKING */ // Check for correct number of args if (argc < 4 || argc > 7) { DIE("RubikSq: RubikSq [-r] [HEIGHT WIDTH] MAXLENGTH INITIAL GOAL"); } if (argc == 5 || argc == 7) { // Check to make sure the first arg is "-r" if (strcmp(argv[1], "-r") != 0) { DIE("RubikSq: Invalid [-r] Flag"); } rFlag = true; } if (argc == 6 || argc == 7) { char *check = argv[argc-5]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check); i++) { if (!isdigit(check[i])) { DIE("RubikSq: Invalid HEIGHT"); } } char *end = NULL; // End pointer for strtol height = strtol(argv[argc-5], &end, 10); if (*end != '\0') { DIE("RubikSq: Invalid HEIGHT"); } char *check2 = argv[argc-4]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check2); i++) { if (!isdigit(check2[i])) { DIE("RubikSq: Invalid WIDTH"); } } char *end2 = NULL; // End pointer for strtol width = strtol(argv[argc-4], &end2, 10); if (*end2 != '\0') { DIE("RubikSq: Invalid WIDTH"); } } char *check3 = argv[argc-3]; // Used to check for non-numeric chars in the args for (int i = 0; i < strlen(check3); i++) { if (!isdigit(check3[i])) { DIE("RubikSq: Invalid MAXLENGTH"); } } char *end3 = NULL; // End pointer for strtol maxlength = strtol(argv[argc-3], &end3, 10); if (*end3 != '\0') { DIE("RubikSq: Invalid MAXLENGTH"); } // Check for non-alpha chars in the args initial = argv[argc-2]; for (int i = 0; i < strlen(initial); i++) { if (!isalpha(initial[i])) { DIE("RubikSq: Invalid INITIAL"); } } // Check for non-alpha chars in the args goal = argv[argc-1]; for (int i = 0; i < strlen(goal); i++) { if (!isalpha(goal[i])) { DIE("RubikSq: Invalid GOAL"); } } if (maxlength < 0) { DIE("RubikSq: MAXLENGTH < 0"); } if (height < 2 || height > 5 || width < 2 || width > 5) { DIE("RubikSq: HEIGHT/WIDTH must be between 2 and 5, inclusive"); } if (strlen(initial) != strlen(goal)){ DIE("RubikSq: Length of INITIAL does not equal length of GOAL"); } if ((height*width) != strlen(initial)) { DIE("RubikSq: HEIGHT*WIDTH does not match length of INITIAL/GOAL"); } // If GOAL and INITIAL are the same, print and exit if (strcmp(initial, goal) == 0) { printf("%s\n", initial); exit(0); } // Make an alphabetized copy of INITIAL char *initialSort = malloc(strlen(initial)+1); // An alphabetized version of INITIAL strcpy(initialSort, initial); sortAlphabetical(initialSort); // Make an alphabetized copy of GOAL char *goalSort = malloc(strlen(goal)+1); // An alphabetized version of GOAL strcpy(goalSort, goal); sortAlphabetical(goalSort); if (strcmp(initialSort, goalSort) != 0) { DIE("RubikSq: INITIAL and GOAL do not contain the same letters"); } // Make sure all letters are between A and L, inclusive if (initialSort[0] < 'A' || initialSort[strlen(initialSort)-1] > 'L' || goalSort[0] < 'A' || goalSort[strlen(goalSort)-1] > 'L') { DIE("RubikSq: INITIAL/GOAL contain invalid letters"); } /* ALGORITHM */ Trie dict; // The dictionary trie for storing past positions createT(&dict); addT(&dict, goal, NULL, 0, 0); Queue moves; // The queue of positions to analyze createQ(&moves); enQ(&moves, goal); // Main loop while (!isEmptyQ(&moves)) { char *pos = NULL; // The current position P deQ(&moves, &pos); int len = getLengthT(&dict, pos, 0); // The length of P in the dictionary if (len < maxlength) { int total; // The total number of possible moves // If -r is specified, there are more possible moves if (rFlag) { total = (width-1)*height + (height-1)*width; } else { total = width + height; } char *primes[total]; // The array of possible positions P' char *copy; // Used to find all P' to put in the array // If the -r flag is specified, we can move right or down multiple times if (rFlag) { for (int i = 1; i <= height; i++) { copy = pos; for (int j = 1; j < width; j++){ copy = moveRight(i, width, copy); primes[(i-1)*(width-1)+j-1] = copy; } } for (int i = 1; i <= width; i++) { copy = pos; for (int j = 1; j < height; j++){ copy = moveDown(i, width, height, copy); primes[height*(width-1)+(i-1)*(height-1)+j-1] = copy; } } } // Otherwise, we can only make one move else { for (int i = 1; i <= height; i++) { primes[i-1] = moveRight(i, width, pos); } for (int i = 1; i <= width; i++) { primes[i+height-1] = moveDown(i, width, height, pos); } } // P' checking for loop for (int i = 0; i < total; i++) { // If P' is INITIAL, print the moves and exit if (strcmp(primes[i], initial) == 0) { printf("%s\n", primes[i]); printf("%s\n", pos); while (getFromT(&dict, pos, 0) != NULL) { pos = getFromT(&dict, pos, 0); printf("%s\n", pos); } // Free the sorted INITIAL and GOAL free(initialSort); free(goalSort); exit(0); } // Else if P' is not in the dictionary, add it to the dict and queue else if (!isMemberT(&dict, primes[i], 0)) { addT(&dict, primes[i], pos, len+1, 0); enQ(&moves, primes[i]); } // Else free the storage else { free(primes[i]); } } } } // Free the sorted INITIAL and GOAL free(initialSort); free(goalSort); return 0; }
void* scan_nodes(void* threadid) { int i, j, k; long tid; unsigned int min_nbr_height; Edge nbr_edge, min_height_edge; long d; long local_e; long local_c; int max_flow; int isInQ; int height_before_lift; int node_push_times = 0, node_lift_times = 0; int op_times = 0; tid = (long)threadid; Node* cur_node; ThreadEnv* thisThread; thisThread = threadEnv + tid; #if 0 if (tid == 0) global_relabel(tid); #endif pthread_barrier_wait(&start_barrier); while (!flow_done()) { cur_node = thisThread->Q[0]; while (cur_node != NoNode) { #ifdef GR if (op_times > gr_threshold) { op_times = 0; if (pthread_mutex_trylock(&gr_mutex) == 0) { global_relabel(tid); pthread_mutex_unlock(&gr_mutex); } } #endif while (cur_node->excess > 0) { #ifdef DEBUG fprintf(stderr, "%d h=%d, e=%ld\n", cur_node - g_node, cur_node->height, cur_node->excess); fflush(stderr); #endif min_nbr_height = UINT_MAX; for (nbr_edge = cur_node->adj_list; nbr_edge < (cur_node + 1)->adj_list; nbr_edge++) { if (nbr_edge->capacity > 0 && (nbr_edge->endpoint)->height < min_nbr_height) { min_nbr_height = (nbr_edge->endpoint)->height; min_height_edge = nbr_edge; } } #ifdef DEBUG fprintf(stderr, "work on %d\n", cur_node - g_node); fflush(stderr); #endif if (cur_node->height > min_nbr_height) { local_e = cur_node->excess; local_c = min_height_edge->capacity; d = MIN(local_e, local_c); if (min_height_edge->endpoint->wave == cur_node->wave && cur_node->height > min_height_edge->endpoint->height) { node_push_times++; op_times++; atomic_add(d, &(min_height_edge->mateedge->capacity)); atomic_sub(d, &(min_height_edge->capacity)); atomic_add(d, &((min_height_edge->endpoint)->excess)); atomic_sub(d, &(cur_node->excess)); #if defined(PUSH) || defined(DEBUG) fprintf(stderr, "[%ld] %ld(%ld) -> %ld -> %ld(%ld) \n", tid, cur_node - g_node, cur_node->excess, d, min_height_edge->endpoint - g_node, (min_height_edge->endpoint)->excess); fflush(stderr); #endif // add min_nbr to local queue isInQ = cmpxchg(&(min_height_edge->endpoint->inQ), 0, tid + 1); if (isInQ == 0) enQ(thisThread, min_height_edge->endpoint); } } else { // if we cannot push to any nodes, then we must be able to lift node_lift_times++; op_times++; pthread_mutex_lock(&(node_mutex[cur_node - g_node])); if (cur_node->height < min_nbr_height + 1) cur_node->height = min_nbr_height + 1; pthread_mutex_unlock(&(node_mutex[cur_node - g_node])); #if defined(LIFT) || defined(DEBUG) fprintf(stderr, "%ld ^ %d, ref %ld(%d)\n", cur_node - g_node, cur_node->height, min_height_edge->endpoint - g_node, min_height_edge->endpoint->height); fflush(stderr); #endif } } // while( g_node[i].excess > 0 ) set0(&(cur_node->inQ)); if (cur_node->excess > 0) { isInQ = cmpxchg(&(cur_node->inQ), 0, tid + 1); if (isInQ == 0) { reenQ(thisThread, cur_node); } else { deQ(thisThread); } } else { deQ(thisThread); } #ifdef HELP if (thisThread->request < MAX_THRD) send_work(tid); #endif cur_node = thisThread->Q[thisThread->head]; } // while (i != -1) #ifdef HELP // Q is empty, find something to do; request_work(tid); #else break; #endif } // while(!flow_done()) atomic_add(node_push_times, &(totalPushes)); atomic_add(node_lift_times, &(totalLifts)); } // scan_node
void enQw(rbq *pQ, uint16_t data) { // IRQMASK_SAVE; enQ(pQ, data & 0xFF); /* LOW byte */ enQ(pQ, data >> 8); /* HIGH byte */ // IRQMASK_RESTORE; }
int main(void) { PriorityQueue* q = newPriorityQueue(); assert("PriorityQueue creation error" && q->head == NULL); enQ(q, 10, 100); assert("enQ error" && q->head != NULL); assert("enQ error" && q->head->data == 10); assert("enQ error" && q->head->priority == 100); enQ(q, 11, 102); enQ(q, 8, 105); enQ(q, 2, 103); enQ(q, 90, 101); print(q); printf("\n"); // Pop int data, priority; data = pop(q); assert("Pop error" && data == 8); data = pop(q); assert("Pop error" && data == 2); data = pop(q); assert("Pop error" && data == 11); data = pop(q); assert("Pop error" && data == 90); data = pop(q); assert("Pop error" && data == 10); data = pop(q); assert("Pop error" && data == -1); print(q); printf("\n"); // Test deQ enQ(q, 10, 100); enQ(q, 11, 102); enQ(q, 8, 105); enQ(q, 2, 103); enQ(q, 90, 101); enQ(q, 20, 98); print(q); printf("\n"); deQ(q, 2); assert("DeQ error" && q->head != NULL); data = pop(q); assert("Pop error after deQ" && data == 8); data = pop(q); assert("Pop error after deQ" && data == 11); deQ(q, 90); assert("DeQ error" && q->head != NULL); data = pop(q); assert("Pop error after deQ" && data == 10); data = pop(q); assert("Pop error after deQ" && data == 20); data = pop(q); assert("Pop error after deQ" && data == -1); assert("Pop error after deQ" && q->head == NULL); deQ(q, 90); assert("DeQ error" && q->head == NULL); print(q); printf("\n"); enQ(q, 10, 100); enQ(q, 11, 102); enQ(q, 8, 105); enQ(q, 2, 103); enQ(q, 90, 101); enQ(q, 20, 98); deQ(q, 90); assert("DeQ error" && q->head != NULL); data = pop(q); assert("Pop error" && data == 8); data = pop(q); assert("Pop error" && data == 2); data = pop(q); assert("Pop error" && data == 11); data = pop(q); assert("Pop error" && data == 10); data = pop(q); assert("Pop error" && data == 20); data = pop(q); assert("Pop error" && data == -1); print(q); printf("\n"); enQ(q, 20, 98); assert("enQ error" && q->head != NULL); data = pop(q); assert("Pop error" && data == 20); assert("pop error" && q->head == NULL); enQ(q, 20, 98); assert("enQ error" && q->head != NULL); data = pop(q); assert("Pop error" && data == 20); assert("pop error" && q->head == NULL); enQ(q, 20, 98); assert("enQ error" && q->head != NULL); data = pop(q); assert("Pop error" && data == 20); assert("pop error" && q->head == NULL); print(q); printf("\n"); // Pop and reQ enQ(q, 0, 98); enQ(q, 1, 100); enQ(q, 2, 100); enQ(q, 3, 100); enQ(q, 4, 100); print(q); printf("\n"); data = pop(q); assert("Pop error" && data == 1); enQ(q, 1, 100); data = pop(q); assert("Pop error" && data == 2); enQ(q, 2, 100); data = pop(q); assert("Pop error" && data == 3); enQ(q, 3, 100); data = pop(q); assert("Pop error" && data == 4); enQ(q, 4, 100); data = pop(q); assert("Pop error" && data == 1); enQ(q, 1, 100); data = pop(q); assert("Pop error" && data == 2); enQ(q, 2, 100); data = pop(q); assert("Pop error" && data == 3); enQ(q, 3, 100); print(q); printf("\n"); deletePriorityQueue(q); q = 0; return 0; }
int main() { /* test */ int i,j,k,l,m,n; int exit = 1; int dir = 0; graph* gr=NULL; myQueue * q = NULL; while(exit) { printf("\nTEST MENU\n"); printf("*********\n"); printf("1.Create and print a graph\n"); printf("2.DFS\n"); printf("5.Exit\n"); scanf("%d",&m); switch(m) { case 1: printf("Enter no of nodes\n"); scanf("%d",&k); printf("Enter 0:UnDirected 1:Directed\n"); scanf("%d",&dir); gr=(graph*)createGraph(k); // addVertices(gr,0,1,dir,1); addVertices(gr,0,4,dir,4); addVertices(gr,1,2,dir,2); addVertices(gr,1,3,dir,3); addVertices(gr,1,4,dir,4); addVertices(gr,2,3,dir,3); addVertices(gr,3,4,dir,4); printGraph(gr); break; case 2: graph_DFS(gr,0); break; case 3: q = createQ(10); enQ(q,1); enQ(q,3); enQ(q,5); deQ(q); enQ(q,7); enQ(q,9); enQ(q,11); deQ(q); deQ(q); deQ(q); break; case 5: exit = 0; break; } } return 0; }
int create_(char *pname, unsigned int *psw, void *ustack, int narg, void *argptr) /* create creates a new PCB containing the information to make the program "runnable" in the OS. It locates an unused slot in the Process Table, initializes it, and attaches it to the end of the process queue. It expects as parameters: ptr to the process name, address of the initial psw of the process, the address of a memory area to be used a the process's user stack (if 0 then allocate the space for it), the number of parameters to be passed as child processes, and the address of an array containing the parameters to be passed to the child. */ { int pos = 0; /* position in the table */ int i; /* loop index for registers */ P_(PTSEM); /* locate empty slot in process table */ while (PTable[pos].state != UNUSED && pos < PTSIZE) pos++; if (pos == PTSIZE) return -1; /* table full */ else { pinct++; /* assign type */ memcpy(PTable[pos].type, "PCB ", 4); /* assign pin */ PTable[pos].pin = pinct; /* assign name */ memcpy(PTable[pos].name, pname, 8); /* assign state of NEW */ PTable[pos].state = NEW; /* allocate stack space */ PTable[pos].kernal = getmem_(0x1000); if (ustack == 0) PTable[pos].user = getmem_(0x1000); else PTable[pos].user = ustack; /* copy contents of parameter array on to user stack */ memcpy(PTable[pos].user, argptr, narg*4); /* initialize PSW */ PTable[pos].psw[0] = psw[0]; PTable[pos].psw[1] = psw[1]; /* initialize registers */ for (i=0; i<16; i++) PTable[pos].regs[i] = 0xFAFAFAFA; PTable[pos].regs[11] = psw[1]; PTable[pos].regs[12] = (unsigned int)PTable[pos].user; PTable[pos].regs[13] = (int)KILL; /* initialize semact and owned semaphore array */ PTable[pos].semact = 0; for (i=0; i<MAXSEMAS; i++) PTable[pos].owns[i] = NULL; /* initialize OKToSend and OKToRecieve semaphores */ PTable[pos].OKToSend = getsem_(0, &(PTable[pos])); PTable[pos].OKToReceive = getsem_(0, &PTable[pos]); /* initialize msglen and SenderPIN to 0 */ PTable[pos].msglen = 0; PTable[pos].SenderPIN = 0; /* place the new process on the ready Q */ enQ(&(PTable[pos]), &Ready); return pinct; } } /* end create_() */
static void idle(struct net_device *dev) { unsigned long flags; int state; /* FIXME This is initialized to shut the warning up, but I need to * think this through again. */ struct xmitQel *q = NULL; int oops; int i; int base = dev->base_addr; spin_lock_irqsave(&txqueue_lock, flags); if(QInIdle) { spin_unlock_irqrestore(&txqueue_lock, flags); return; } QInIdle = 1; spin_unlock_irqrestore(&txqueue_lock, flags); /* this tri-states the IRQ line */ (void) inb_p(base+6); oops = 100; loop: if (0>oops--) { printk("idle: looped too many times\n"); goto done; } state = inb_p(base+6); if (state != inb_p(base+6)) goto loop; switch(state) { case 0xfc: /* incoming command */ if (debug & DEBUG_LOWER) printk("idle: fc\n"); handlefc(dev); break; case 0xfd: /* incoming data */ if(debug & DEBUG_LOWER) printk("idle: fd\n"); handlefd(dev); break; case 0xf9: /* result ready */ if (debug & DEBUG_LOWER) printk("idle: f9\n"); if(!mboxinuse[0]) { mboxinuse[0] = 1; qels[0].cbuf = rescbuf; qels[0].cbuflen = 2; qels[0].dbuf = resdbuf; qels[0].dbuflen = 2; qels[0].QWrite = 0; qels[0].mailbox = 0; enQ(&qels[0]); } inb_p(dev->base_addr+1); inb_p(dev->base_addr+0); if( wait_timeout(dev,0xf9) ) printk("timed out idle f9\n"); break; case 0xf8: /* ?? */ if (xmQhd) { inb_p(dev->base_addr+1); inb_p(dev->base_addr+0); if(wait_timeout(dev,0xf8) ) printk("timed out idle f8\n"); } else { goto done; } break; case 0xfa: /* waiting for command */ if(debug & DEBUG_LOWER) printk("idle: fa\n"); if (xmQhd) { q=deQ(); memcpy(ltdmacbuf,q->cbuf,q->cbuflen); ltdmacbuf[1] = q->mailbox; if (debug>1) { int n; printk("ltpc: sent command "); n = q->cbuflen; if (n>100) n=100; for(i=0;i<n;i++) printk("%02x ",ltdmacbuf[i]); printk("\n"); } handlecommand(dev); if(0xfa==inb_p(base+6)) { /* we timed out, so return */ goto done; } } else { /* we don't seem to have a command */ if (!mboxinuse[0]) { mboxinuse[0] = 1; qels[0].cbuf = rescbuf; qels[0].cbuflen = 2; qels[0].dbuf = resdbuf; qels[0].dbuflen = 2; qels[0].QWrite = 0; qels[0].mailbox = 0; enQ(&qels[0]); } else { printk("trouble: response command already queued\n"); goto done; } } break; case 0Xfb: /* data transfer ready */ if(debug & DEBUG_LOWER) printk("idle: fb\n"); if(q->QWrite) { memcpy(ltdmabuf,q->dbuf,q->dbuflen); handlewrite(dev); } else { handleread(dev); /* non-zero mailbox numbers are for commmands, 0 is for GETRESULT requests */ if(q->mailbox) { memcpy(q->dbuf,ltdmabuf,q->dbuflen); } else { /* this was a result */ mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1]; mboxinuse[0]=0; } } break; } goto loop; done: QInIdle=0; /* now set the interrupts back as appropriate */ /* the first read takes it out of tri-state (but still high) */ /* the second resets it */ /* note that after this point, any read of base+6 will trigger an interrupt */ if (dev->irq) { inb_p(base+7); inb_p(base+7); } return; }
// ********************************************************************** // ********************************************************************** // create task int createTask(char* name, // task name int (*task)(int, char**), // task address int priority, // task priority int argc, // task argument count char* argv[]) // task argument pointers { int tid; // find an open tcb entry slot for (tid = 0; tid < MAX_TASKS; tid++) { if (tcb[tid].name == 0) { char buf[8]; // create task semaphore if (taskSems[tid]) deleteSemaphore(&taskSems[tid]); sprintf(buf, "task%d", tid); taskSems[tid] = createSemaphore(buf, 0, 0); taskSems[tid]->taskNum = tid; // assign to shell // copy task name tcb[tid].name = (char*)malloc(strlen(name)+1); strcpy(tcb[tid].name, name); // set task address and other parameters tcb[tid].task = task; // task address tcb[tid].state = S_NEW; // NEW task state tcb[tid].priority = priority; // task priority tcb[tid].parent = curTask; // parent tcb[tid].argc = argc; // argument count // malloc new argv parameters char** temp_argv = (char**)malloc(sizeof(char*) * (argc + 1)); temp_argv[argc] = NULL; for (int i = 0; i < argc; i++) { // printf("index: %d\tvalue: %s\n", i, argv[i]); temp_argv[i] = (char*)malloc(sizeof(char) * (strlen(argv[i]) + 1)); strcpy(temp_argv[i], argv[i]); } tcb[tid].argv = temp_argv; // argument pointers tcb[tid].event = 0; // suspend semaphore tcb[tid].RPT = 0x2400 + (tid ? (tid + 1) << 6 : 0); // root page table (project 5) tcb[tid].cdir = CDIR; // inherit parent cDir (project 6) // define task signals createTaskSigHandlers(tid); // Each task must have its own stack and stack pointer. tcb[tid].stack = malloc(STACK_SIZE * sizeof(int)); // ?? may require inserting task into "ready" queue enQ(rq, tid, tcb[tid].priority); // printf("In createTask()\n"); // printQ(rq); // printf("\nFinished creating task `%s`\n", tcb[tid].name); if (tid) swapTask(); // do context switch (if not cli) return tid; // return tcb index (curTask) } } // tcb full! return -1; } // end createTask