예제 #1
0
파일: barber.c 프로젝트: Munksgaard/osm-g6
void barber_function(void *arg) {
    barber_data *barber = (barber_data *)arg;
    customer_data *customer;

    syscall_lock_acquire(&barber_lock);

    while (1) {
        printf("Barber #%d looking for new customers\n", barber->id);

        if (next_customer == NULL) {
            syscall_condition_signal(&sitting_cond, &barber_lock);
            syscall_condition_signal(&standing_cond, &barber_lock);

            printf("Barber #%d sleeping\n", barber->id);
            syscall_condition_wait(&barber_cond, &barber_lock);
            printf("Barber #%d got woken up!\n", barber->id);
        } 
        
        customer = next_customer;
        next_customer = NULL;
        printf("Barber #%d servicing customer #%d\n", barber->id, customer->id);
        syscall_lock_release(&barber_lock);
        simulatedwait(100000);
        syscall_lock_acquire(&barber_lock);
        
        printf("Barber #%d done servicing customer #%d\n", barber->id, customer->id);
        syscall_condition_signal(&customer->cond, &barber_lock);
    }
}
예제 #2
0
파일: barber.c 프로젝트: Munksgaard/osm-g6
void customer_function(void *arg) {
    customer_data *customer = (customer_data *)arg;

    syscall_lock_acquire(&barber_lock);

    printf("Customer #%d arrives\n", customer->id);

    if (sitting + standing >= MAX_WAITING) {
        printf("Barbershop full. Customer #%d leaving\n", customer->id);
        syscall_lock_release(&barber_lock);
        syscall_exit(0);
    }

    if (sitting >= MAX_SITTING) {
        printf("Chairs occupied. Customer #%d standing in line\n", customer->id);
        standing++;
        syscall_condition_wait(&standing_cond, &barber_lock);
        standing--;
    }

    if (sitting <= MAX_SITTING && next_customer != NULL) {
        printf("Customer #%d takes a seat\n", customer->id);
        sitting++;
        syscall_condition_wait(&sitting_cond, &barber_lock);
        sitting--;
    }

    printf("Customer #%d is being serviced\n", customer->id);

    next_customer = customer;
    syscall_condition_signal(&barber_cond, &barber_lock);
    syscall_condition_wait(&customer->cond, &barber_lock);
    syscall_lock_release(&barber_lock);
    syscall_exit(0);
}
예제 #3
0
파일: barber.c 프로젝트: Munksgaard/osm-g6
int main() {
    int i;
    syscall_lock_create(&barber_lock);
    syscall_lock_acquire(&barber_lock);

    syscall_condition_create(&barber_cond);
    syscall_condition_create(&standing_cond);
    syscall_condition_create(&sitting_cond);

    for(i=0; i<(BARBERS); i++) {
        barber[i].id = i;
        syscall_condition_create(&barber[i].cond);
        syscall_fork((void (*)(int))(&barber_function), (int)&barber[i]);
    }

    for(i=0; i<(CUSTOMERS); i++) {
        customers[i].id = i;
        syscall_condition_create(&customers[i].cond);
        syscall_fork((void (*)(int))(&customer_function), (int)&customers[i]);
    }

    syscall_lock_release(&barber_lock);
    syscall_exit(0);

    return 0;
}
예제 #4
0
int main() {
    syscall_lock_create(&baton_lock);
    syscall_lock_acquire(&baton_lock);

    // Setup data for threads
    thread_data data[THREADS];
    int i;
    for(i=0; i<(THREADS); i++) {
        data[i].id = i;
        data[i].baton = 0;
        data[i].countdown = 0;
        syscall_condition_create(&data[i].cond);
        data[i].next = &data[(i+1)%THREADS];
        syscall_fork((void (*)(int))(&thread_function), (int)&data[i]);
    }

    // Setup special data for the first thread
    data[0].countdown = ROUNDS;
    data[0].baton = 1;

    // Start first thread
    syscall_condition_signal(&data[0].cond, &baton_lock);
    syscall_lock_release(&baton_lock);

    syscall_exit(0);
    return 0;
}
예제 #5
0
void countup(int me)
{
    int me2 = me;
    int i, j;
    for (j = 0; j <= me; j++) {
        for (i=0; i < (me+1)*1000; i++); {
            syscall_lock_acquire(&print_lock); 
            printf("%d,%d: %d\n", me, me2, counter++);
            syscall_lock_release(&print_lock); 
        }
    }
    syscall_lock_acquire(&print_lock); 
    printf("%d,%d: STOPPING\n", me,me2);
    syscall_lock_release(&print_lock); 
    thread_status[me] = 1;
    syscall_exit(0);
}
예제 #6
0
void thread_function(void *arg){
    thread_data *data = (thread_data *)arg;
    while(1) {
      syscall_lock_acquire(&baton_lock);
        // Wait until baton is non-zero (meaning that the thread
        // has the baton)
        while(data->baton == 0) {
          syscall_condition_wait(&data->cond, &baton_lock);
        }

        // First thread should perform countdown
        if(data->id == 0) {
            printf("\n%d: New round\n", data->id);
            if(--data->countdown == 0) {
                data->baton = -1; // create a stop baton
            }
        }

        // Pass the baton on to the next thread
        printf("%d: passing the baton on to %d\n", data->id,
                data->next->id);
        data->next->baton = data->baton;

        // Quit if the baton was a stop baton
        if(data->baton < 0){
            printf("%d: I quit.\n",data->id);
            syscall_condition_signal(&data->next->cond,
                    &baton_lock);
            syscall_lock_release(&baton_lock);
            syscall_exit(0);
        }

        //Remove baton from self and signal the next thread
        data->baton = 0;
        syscall_condition_signal(&data->next->cond, &baton_lock);
        syscall_lock_release(&baton_lock);
    }
}
예제 #7
0
파일: syscall.c 프로젝트: bjornua/OSM
/**
 * Handle system calls. Interrupts are enabled when this function is
 * called.
 *
 * @param user_context The userland context (CPU registers as they
 * where when system call instruction was called in userland)
 */
void syscall_handle(context_t *user_context)
{
    /* When a syscall is executed in userland, register a0 contains
     * the number of the syscall. Registers a1, a2 and a3 contain the
     * arguments of the syscall. The userland code expects that after
     * returning from the syscall instruction the return value of the
     * syscall is found in register v0. Before entering this function
     * the userland context has been saved to user_context and after
     * returning from this function the userland context will be
     * restored from user_context.
     */
    switch(user_context->cpu_regs[MIPS_REGISTER_A0]) {
    case SYSCALL_HALT:
        halt_kernel();
        break;
    case SYSCALL_EXIT:
        syscall_exit(user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_WRITE:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_write(user_context->cpu_regs[MIPS_REGISTER_A1],
                          (char*)user_context->cpu_regs[MIPS_REGISTER_A2],
                          (user_context->cpu_regs[MIPS_REGISTER_A3]));
        break;
    case SYSCALL_READ:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_read(user_context->cpu_regs[MIPS_REGISTER_A1],
                         (char*)user_context->cpu_regs[MIPS_REGISTER_A2],
                         (user_context->cpu_regs[MIPS_REGISTER_A3]));
        break;
    case SYSCALL_JOIN:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
          syscall_join(user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_EXEC:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_exec((char*)user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_FORK:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_fork((void (*)(int))user_context->cpu_regs[MIPS_REGISTER_A1],
                         user_context->cpu_regs[MIPS_REGISTER_A2]);
        break;
    case SYSCALL_LOCK_CREATE:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_lock_create((lock_t*) user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_LOCK_ACQUIRE:
        syscall_lock_acquire((lock_t*) user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_LOCK_RELEASE:
        syscall_lock_release((lock_t*) user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_CONDITION_CREATE:
        user_context->cpu_regs[MIPS_REGISTER_V0] =
            syscall_condition_create((cond_t*) user_context->cpu_regs[MIPS_REGISTER_A1]);
        break;
    case SYSCALL_CONDITION_WAIT:
        syscall_condition_wait((cond_t*) user_context->cpu_regs[MIPS_REGISTER_A1],
                (lock_t*) user_context->cpu_regs[MIPS_REGISTER_A2]);
        break;
    case SYSCALL_CONDITION_SIGNAL:
        syscall_condition_signal((cond_t*) user_context->cpu_regs[MIPS_REGISTER_A1],
                (lock_t*) user_context->cpu_regs[MIPS_REGISTER_A2]);
        break;
    case SYSCALL_CONDITION_BROADCAST:
        syscall_condition_broadcast((cond_t*) user_context->cpu_regs[MIPS_REGISTER_A1],
                (lock_t*) user_context->cpu_regs[MIPS_REGISTER_A2]);
        break;
    default:
        KERNEL_PANIC("Unhandled system call\n");
    }
    /* Move to next instruction after system call */
    user_context->pc += 4;
}