程序代写代做代考 #include

#include
#include

#include “private.h”
#include “uthread.h”

/* Size of the stack for a thread (in bytes) */
#define UTHREAD_STACK_SIZE 32768

void uthread_ctx_switch(uthread_ctx_t *prev, uthread_ctx_t *next)
{
/*
* swapcontext() saves the current context in structure pointer by @prev
* and actives the context pointed by @next
*/
if (swapcontext(prev, next)) {
perror(“swapcontext”);
exit(1);
}
}

void *uthread_ctx_alloc_stack(void)
{
return malloc(UTHREAD_STACK_SIZE);
}

void uthread_ctx_destroy_stack(void *top_of_stack)
{
free(top_of_stack);
}

/*
* uthread_ctx_bootstrap – Thread context bootstrap function
* @func: Function to be executed by the new thread
* @arg: Argument to be passed to the thread
*/
static void uthread_ctx_bootstrap(uthread_func_t func, void *arg)
{
/*
* Enable interrupts right after being elected to run for the first time
*/
preempt_enable();

/* Execute thread and when done, exit */
func(arg);
uthread_exit();
}

int uthread_ctx_init(uthread_ctx_t *uctx, void *top_of_stack,
uthread_func_t func, void *arg)
{
/*
* Initialize the passed context @uctx to the currently active context
*/
if (getcontext(uctx))
return -1;

/*
* Change context @uctx’s stack to the specified stack
*/
uctx->uc_stack.ss_sp = top_of_stack;
uctx->uc_stack.ss_size = UTHREAD_STACK_SIZE;

/*
* Finish setting up context @uctx:
* – the context will jump to function uthread_ctx_bootstrap() when
* scheduled for the first time
* – when called, function uthread_ctx_bootstrap() will receive two
* arguments: @func and @arg
*/
makecontext(uctx, (void (*)(void)) uthread_ctx_bootstrap,
2, func, arg);

return 0;
}

#include
#include
#include
#include
#include
#include
#include

#include “private.h”
#include “uthread.h”

/*
* Frequency of preemption
* 100Hz is 100 times per second
*/
#define HZ 100

void preempt_disable(void)
{
/* TODO Phase 4 */
}

void preempt_enable(void)
{
/* TODO Phase 4 */
}

void preempt_start(void)
{
/* TODO Phase 4 */
}

void preempt_stop(void)
{
/* TODO Phase 4 */
}

#include
#include
#include

#include “queue.h”

struct queue {
/* TODO Phase 1 */
};

queue_t queue_create(void)
{
/* TODO Phase 1 */
}

int queue_destroy(queue_t queue)
{
/* TODO Phase 1 */
}

int queue_enqueue(queue_t queue, void *data)
{
/* TODO Phase 1 */
}

int queue_dequeue(queue_t queue, void **data)
{
/* TODO Phase 1 */
}

int queue_delete(queue_t queue, void *data)
{
/* TODO Phase 1 */
}

int queue_iterate(queue_t queue, queue_func_t func)
{
/* TODO Phase 1 */
}

int queue_length(queue_t queue)
{
/* TODO Phase 1 */
}

#include
#include

#include “queue.h”
#include “sem.h”
#include “private.h”

struct semaphore {
/* TODO Phase 3 */
};

sem_t sem_create(size_t count)
{
/* TODO Phase 3 */
}

int sem_destroy(sem_t sem)
{
/* TODO Phase 3 */
}

int sem_down(sem_t sem)
{
/* TODO Phase 3 */
}

int sem_up(sem_t sem)
{
/* TODO Phase 3 */
}

#include
#include
#include
#include
#include
#include
#include

#include “private.h”
#include “uthread.h”

struct uthread_tcb {
/* TODO Phase 2 */
};

struct uthread_tcb *uthread_current(void)
{
/* TODO Phase 2 */
}

void uthread_yield(void)
{
/* TODO Phase 2 */
}

void uthread_exit(void)
{
/* TODO Phase 2 */
}

int uthread_create(uthread_func_t func, void *arg)
{
/* TODO Phase 2 */
}

int uthread_start(uthread_func_t func, void *arg)
{
/* TODO Phase 2 */
}

void uthread_block(void)
{
/* TODO Phase 2/3 */
}

void uthread_unblock(struct uthread_tcb *uthread)
{
/* TODO Phase 2/3 */
}