/* Returns 1 if 'string' is a palindrom, 0 otherwise. */ int isPalindrome(char *string) { int result = 0; /* set to 1 if 'string' is a palindrome */ int i; int count = 0; /* Before finishing this function, you'll need to finish sll.c */ struct sll *halfword = sll_create(); struct sll *halfword2 = sll_create(); for (i = 0; i < (int)strlen(string)/2; i++) { /* "string[i]" will retrieve the next character */ sll_push(halfword, string[i]); printf("%c", sll_top(halfword)); } printf(" "); for (i = (int)strlen(string); i > (int)strlen(string)/2; i--) { sll_push(halfword2, string[i]); printf("%c", sll_top(halfword2)); } /* This loop will help you iterate over a string one character at time */ for (i = 0; i < (int)strlen(string)/2; i++) { if (sll_top(halfword) == sll_top(halfword2)) { sll_pop(halfword); sll_pop(halfword2); count++; } } printf("\n%d\n", count); if (count == (int)strlen(string)/2) { result = 1; } else { result = 0; } return result; }
/* * sleep_process_system_tick * @return: If not NULL returns the task that is needed to be scheduled next. * This function is called by scheduler to see if there is a task that is needed * to wake-up at a given system tick. */ static TASK *sleep_process_system_tick(void) { TASK *tcb = NULL; TASK *tcb_break = NULL; for (;;) { /* Check if we need to schedule a sleeping task. */ if ( (sleep_scheduler.ready_tasks.head != tcb_break) && (current_system_tick() >= sleep_scheduler.ready_tasks.head->tick_sleep) ) { /* Schedule this sleeping task. */ tcb = (TASK *)sll_pop(&sleep_scheduler.ready_tasks, OFFSETOF(TASK, next_sleep)); /* Task is already resumed. */ if (tcb->status != TASK_SUSPENDED) { /* Lets not get this task out of sleep any time soon. */ tcb->tick_sleep = MAX_WAIT; /* Put back this task on the scheduler list. */ /* We will remove it from this list when we will resume. */ sll_push(&sleep_scheduler.ready_tasks, tcb, OFFSETOF(TASK, next_sleep)); /* Save the task at which we will need to break the search. */ tcb_break = tcb; /* This is not the task we need. */ tcb = NULL; /* Check if we can resume the next task. */ continue; } else { /* Task is being resumed from sleep. */ tcb->status = TASK_RESUME_SLEEP; } } break; } /* Return the task that is needed to be scheduled. */ return (tcb); } /* sleep_process_system_tick */
/* * net_buffer_read * @fd: File descriptor. * @buffer: Pointer to a buffer pointer that will be updated with the available * buffer. * @size: Size of buffer, should be the size of a pointer. * @return: Number of bytes read. * This function will dequeue a networking buffer and return it's pointer in * the provided buffer. */ static int32_t net_buffer_read(void *fd, uint8_t *buffer, int32_t size) { NET_BUFFER_FS *net_buffer = (NET_BUFFER_FS *)fd; FS_BUFFER *fs_buffer; int32_t nbytes = sizeof(FS_BUFFER *); /* Unused parameter. */ UNUSED_PARAM(size); /* Caller already has the lock for net buffer data. */ /* Pull a networking buffer from the list. */ fs_buffer = sll_pop(&net_buffer->buffer_list, OFFSETOF(FS_BUFFER, next)); /* If there is no buffer on the list to process. */ if (fs_buffer == NULL) { /* We did not get any data. */ nbytes = 0; } else { /* Remove the buffer link. */ fs_buffer->next = NULL; /* Return the buffer to the caller. */ *((FS_BUFFER **)buffer) = fs_buffer; } /* If are not returning any data or the list is now empty. */ if ((fs_buffer == NULL) || (net_buffer->buffer_list.head == NULL)) { /* Tell the file system that there is no data on the file descriptor. */ fd_data_flushed(fd); } /* Return the number of bytes. */ return (nbytes); } /* net_buffer_read */
/* * scheduler_get_next_task * @return: Task's control block that will run next. * This function is called by system tick to get the next task that is needed to * run next. */ TASK *scheduler_get_next_task(void) { TASK *tcb = NULL; /* Resume any of the sleeping tasks. */ sleep_process_system_tick(); /* Get the task we need to run */ tcb = (TASK *)sll_pop(&sch_ready_task_list, OFFSETOF(TASK, next)); /* We should always have a task to execute. */ ASSERT(tcb == NULL); #ifdef TASK_STATS /* Increment the number of times this task was scheduled. */ tcb->scheduled ++; #endif /* TASK_STATS */ /* Return the task to run. */ return (tcb); } /* scheduler_get_next_task */