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); }
int main(void) { usr_sem_t *wait0, *wait1, *read_write_lock; char line[BUFFER_SIZE]; /* Open the semaphores. */ wait0 = syscall_sem_open("wait0", -1); wait1 = syscall_sem_open("wait1", -1); read_write_lock = syscall_sem_open("rwlock", -1); /* Do work before barrier. */ syscall_sem_p(read_write_lock); readline(line, BUFFER_SIZE); printf("prog1: You wrote: %s\n", line); syscall_sem_v(read_write_lock); /* Wait for the other process. */ syscall_sem_v(wait1); /* DIFFERENT THAN IN prog0 */ syscall_sem_p(wait0); /* DIFFERENT THAN IN prog0 */ /* Do work after barrier. */ puts("prog1: done.\n"); syscall_exit(0); return 0; }
void exit(int status) { pthread_mutex_lock(&exit_mutex); if (_exit_func) (*_exit_func)(); _fini(); syscall_exit(status); __builtin_unreachable(); }
int main(void) { char *name; //int count; //heap_init(); puts("Malloc test!\n"); /* while (1) { name = (char*)malloc(BUFFER_SIZE); name = "hej"; printf("%s \n", name); //free(name); } */ name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = (char*)malloc(BUFFER_SIZE); name = name; puts("Now I shall exit!\n"); syscall_exit(2); return 0; }
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; }
int main() { int fibs[19]; int limit = 19; int how_many; do { syscall_print_string( "How many Fibonacci numbers to generate? (2 <= x <= 19) " ); how_many = syscall_read_int(); } while ( how_many > limit || how_many < 0 ); int next_value = 1; fibs[0] = next_value; fibs[1] = next_value; int index_mark = 0; for ( int repetitions = how_many - 2 ; repetitions > 0 ; repetitions-- ) { int f_n_minus_2 = fibs[index_mark]; int f_n_minus_1 = fibs[index_mark + 1]; int f_n = f_n_minus_2 + f_n_minus_1; fibs[index_mark + 2] = f_n; index_mark++; } print(fibs, how_many); syscall_exit(); }
int syscall_open(const char *file) { if(!is_valid_ptr(file)) syscall_exit(-1); // 0. Try to open it. struct file *fp = filesys_open(file); if (!fp) return -1; // 1. Get First Empty FD int fd = Get_First_Empty_FD(&(thread_current()->FDs)); if (fd == -1) // XXX Reached MAX_FD (* TEST: [multi-oom]) return -1; // 2. Make FD struct fd_list *new_fd = (struct fd_list *)calloc(1, sizeof(struct fd_list)); if(!new_fd) return -1; // 3. Assign That FD as got the first empty one. new_fd->fd = fd; new_fd->file = fp; // 4. Insert Ordered this FD at FDs List. list_insert_ordered( &(thread_current()->FDs), &(new_fd->elem), FD_List_Less_Func, NULL); return fd; }
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; }
int main() { char* data[10]; int j; for (j = 0; j < 50; j++) { int i; /* Allocate and insert in different places. */ for (i = 0; i < 10; i++) { data[i] = (char*) malloc(15000); data[i][i * 1003] = '!'; } /* Check that it's still there. */ for (i = 0; i < 10; i++) { if (data[i][i * 1003] != '!') { return 1; } } /* Free it all, but not in order. If this is not done, Buenos runs out of memory. */ free(data[0]); free(data[3]); free(data[1]); free(data[5]); free(data[9]); free(data[2]); free(data[7]); free(data[6]); free(data[8]); free(data[4]); } syscall_exit(0); return 0; }
int syscall_read(int fd, void *buffer, unsigned size){ /* Reads size bytes from the file open as fd into buffer. * * Returns the number of bytes actually read (0 at end of file), * or -1 if the file could not be read (due to a condition * other than end of file). * * FD 0 reads from the keyboard using input_getc(). (ref:man29-31) */ int ret = -1; unsigned i; if(!is_valid_ptr(buffer)) syscall_exit(-1); if(fd == 0){ // STDIN 0 for(i = 0; i < size; i++){ uint8_t buf = input_getc(); memset((buffer + i * sizeof(uint8_t)), buf, sizeof(uint8_t)); } ret = i; }else{ // 0. Find fd. struct fd_list *found = Search_FD(&(thread_current()->FDs), fd); if(!found) return -1; lock_acquire(&(found->file->inode->lock)); ret = file_read(found->file, buffer, size); lock_release(&(found->file->inode->lock)); } return ret; }
int run_command(char* cmdline) { char* argv[BUFFER_SIZE]; int argc = tokenize(cmdline, argv); if (argc == 0) { return 0; } if (strcmp(argv[0], "wait") == 0) { return cmd_wait(argc, argv); } else if (strcmp(argv[0], "ls") == 0) { return cmd_ls(argc, argv); } else if (strcmp(argv[0], "cp") == 0) { return cmd_cp(argc, argv); } else if (strcmp(argv[0], "show") == 0) { return cmd_show(argc, argv); } else if (strcmp(argv[0], "rm") == 0) { return cmd_rm(argc, argv); } else if (strcmp(argv[0], "cmp") == 0) { return cmd_cmp(argc, argv); } else if (strcmp(argv[0], "echo") == 0) { return cmd_echo(argc, argv); } else if (strcmp(argv[0], "exit") == 0) { syscall_exit(0); return 1; // not reached } else { int k = strlen(argv[argc-1]); if (argv[argc-1][k-1] == '&') { argv[argc-1][k-1] = '\0'; return background_run(cmdline); } else { return cmd_run(cmdline); } } }
void do_syscall(TrapFrame *tf) { int id = tf->eax; switch(id) { case SYS_fork: syscall_fork(tf); break; case SYS_exec: syscall_exec(tf); break; case SYS_exit: syscall_exit(tf); break; case SYS_getpid: syscall_getpid(tf); break; case SYS_waitpid: syscall_waitpid(tf); break; case SYS_puts1: printk((char*)(tf->ebx)); printk(" %d\n", current->pid); break; case SYS_puts: syscall_puts(tf); break; case SYS_read_line: syscall_read_line(tf); break; case SYS_sleep: syscall_sleep(tf); break; case SYS_open: syscall_open(tf); break; case SYS_read: syscall_read(tf); break; case SYS_write: syscall_write(tf); break; case SYS_create: syscall_create(tf); break; case SYS_close: syscall_close(tf); break; case SYS_delete: syscall_delete(tf); break; case SYS_lseek: syscall_lseek(tf); break; case SYS_dup: syscall_dup(tf); break; case SYS_dup2: syscall_dup2(tf); break; case SYS_mkdir: syscall_mkdir(tf); break; case SYS_rmdir: syscall_rmdir(tf); break; case SYS_lsdir: syscall_lsdir(tf); break; case SYS_chdir: syscall_chdir(tf); break; //default: panic("Unknown system call type"); } }
int main(void) { char *name; int count; heap_init(); /* Or malloc() won't work. */ puts("Hello, World!\n\n"); while (1) { name = (char*)malloc(BUFFER_SIZE); printf("Please enter your name (max %d chars): ", BUFFER_SIZE); count = readline_static(name, BUFFER_SIZE); if (count == 0) { break; } name[count] = 0; /* Chomp off newline */ printf("And hello to you, %s!\n", name); free(name); } puts("Now I shall exit!\n"); syscall_exit(2); return 0; }
static int check_file_ptr(const char* file) { void *valid; if(file == NULL) return 0; if(file == "") return 0; if(!is_user_vaddr(file)) return 0; valid = pagedir_get_page(thread_current()->pagedir, (const void*)file); if(valid == NULL) syscall_exit(-1); return 1; }
static void main_loop() { char* line, cmd, arg1; char buffer[81]; char command[81]; char args[16][81]; while (running) { line = (char*) readline(buffer); memset(command, 0, sizeof(command)); memset(args[0], 0, sizeof(args[0])); token(line, 1, command); token(line, 2, args[0]); if (strlen(command) > 1) { puts("The command is "); puts(command); puts("\n"); } if (strlen(args[0]) > 1) { puts("The first parameter is "); puts(args[0]); puts("\n"); } if (command && strncmp(command, "EXIT", strlen(command))) { puts("Exit command\n"); running = 0; } if (command && args[0] && strncmp(command, "EXEC", strlen(command))) { if (!syscall_fork()) { if (!exec(&args[0])) { puts("Error.\n"); syscall_exit(); } } } } puts("Exiting...\n"); syscall_exit(); }
static void syscall_seek (int fd, unsigned position) { struct file_with_lock fwl = fwl_from_fd (fm, fd); if (fwl.lock == NULL || fwl.mode == FM_MODE_MMAP) { syscall_exit (-1); return; } lock_acquire (fwl.lock); file_seek (fwl.fp, position); lock_release (fwl.lock); }
void syscall_init (void) { intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); fm = init_file_map (); if (fm == NULL) { syscall_exit (-1); return; } lock_init (&filesys_lock); lock_init (&cleanup_lock); }
static unsigned syscall_tell (int fd) { struct file_with_lock fwl = fwl_from_fd (fm, fd); if (fwl.lock == NULL || fwl.mode == FM_MODE_MMAP) { syscall_exit (-1); return 1; } lock_acquire (fwl.lock); unsigned retval = file_tell (fwl.fp); lock_release (fwl.lock); return retval; }
int main(void) { int i; char str[3]; syscall_write(1, "Validprogram1: Caling 'vallidprogram2'\n",39); syscall_exec(syscall_exec(validprog)); syscall_write(1,"That is it! Goodbye\n",20); syscall_exit(4); return 0; }
void sploit_shutdown(void) { ssl_shutdown(); net_shutdown(); str_shutdown(); io_shutdown(); dlink_shutdown(); queue_shutdown(); log_shutdown(); timer_shutdown(); mem_shutdown(); syscall_exit(1); }
/* -------------------------------------------------------------------------- * * Clean things up. * * -------------------------------------------------------------------------- */ void servauth_shutdown(void) { log(servauth_log, L_status, "Shutting down servauth..."); syscall_exit(0); connect_shutdown(); io_shutdown(); queue_shutdown(); dlink_shutdown(); mem_shutdown(); log_shutdown(); timer_shutdown(); log_source_unregister(servauth_log); }
pid_t syscall_exec(const char *file){ /* Runs the executable whose name is given in cmd_line, * passing any given arguments, and returns the new process's * program id (pid). * * Must return pid -1, which otherwise should not be a valid pid, * if the program cannot load or run for any reason. * * Thus, the parent process cannot return from the exec * until it knows whether the child process successfully loaded * its executable. (ref:man29-30) */ if(!is_valid_ptr(file)) syscall_exit(-1); return process_execute(file); }
int syscall_write(int fd, const void *buffer, unsigned size){ /* Writes size bytes from buffer to the open file fd. * * Returns the number of bytes actually written, which may be less than * size if some bytes coudl not be written. * * Writing pas end-of-file would normally extend the file, * but file growth is not implemented by the basic file system. * * The expected behaviour is to write as many bytes as possible * up to end-of-file and return the actual number written, * or 0 if no bytes could be written at all. * * Fd 1 writes to the console. * * Your code to write to the console should write all of buffer * in one call to putbuf(), at least as long as size is not bigger than * a few hundred bytes. (It is reasonable to break up larger buffers.) * * Otherwise, lines of text output by different processes may end up * interleaved on the console, confusing both human readers * and our grading scripts. * (ref:man29-31) */ int ret = -1; unsigned i = 0; if(!is_valid_ptr(buffer)) syscall_exit(-1); if(fd == 0){ // STDIN 0 ret = -1; }else if(fd == 1){ // STDOUT 1 for(i = 0; (i < size) && *(char *)(buffer + i * sizeof(char)); i++){} putbuf(buffer, i); ret = i; }else{ // 0. Find fd. struct fd_list *found = Search_FD(&(thread_current()->FDs), fd); if(!found) return -1; lock_acquire(&(found->file->inode->lock)); ret = file_write(found->file, buffer, size); lock_release(&(found->file->inode->lock)); } return ret; }
/* * get system call */ static void syscall_handler (struct intr_frame *f) { int *esp = (int *)syscall_user_to_kernel_vaddr(f->esp); switch(*esp) { case SYS_WRITE: syscall_write(f); break; case SYS_EXIT: syscall_exit(f); break; case SYS_HALT: syscall_halt(f); break; case SYS_EXEC: syscall_exec(f); break; case SYS_CREATE: syscall_create(f); break; case SYS_REMOVE: syscall_remove(f); break; case SYS_OPEN: syscall_open(f); break; case SYS_FILESIZE: syscall_filesize(f); break; case SYS_READ: syscall_read(f); break; case SYS_SEEK: syscall_seek(f); break; case SYS_TELL: syscall_tell(f); break; case SYS_CLOSE: syscall_close(f); break; case SYS_WAIT: syscall_wait(f); break; } }
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); }
/** * 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_READ: user_context->cpu_regs[MIPS_REGISTER_V0] = syscall_read(user_context->cpu_regs[MIPS_REGISTER_A1], (void *)user_context->cpu_regs[MIPS_REGISTER_A2], user_context->cpu_regs[MIPS_REGISTER_A3]); break; case SYSCALL_WRITE: user_context->cpu_regs[MIPS_REGISTER_V0] = syscall_write(user_context->cpu_regs[MIPS_REGISTER_A1], (const void *)user_context->cpu_regs[MIPS_REGISTER_A2], user_context->cpu_regs[MIPS_REGISTER_A3]); break; case SYSCALL_EXEC: user_context->cpu_regs[MIPS_REGISTER_V0] = syscall_exec((const char *)user_context->cpu_regs[MIPS_REGISTER_A1]); break; case SYSCALL_EXIT: syscall_exit(user_context->cpu_regs[MIPS_REGISTER_A1]); break; case SYSCALL_JOIN: user_context->cpu_regs[MIPS_REGISTER_V0] = syscall_join(user_context->cpu_regs[MIPS_REGISTER_A1]); break; default: KERNEL_PANIC("Unhandled system call\n"); } /* Move to next instruction after system call */ user_context->pc += 4; }
int main(void) { int i; char str[3]; syscall_write(1, "Hello, I am validprog, I will count to ten!\n",44); str[1] = '\n'; str[2] = '\0'; /* syscall_join(syscall_exec(validprog));*/ for (i=0; i <= 10; i++) { str[0] = (char)i+36; syscall_write(1,str,2); } syscall_write(1,"That is it! Goodbye\n",20); syscall_exit(4); return 0; }
/** * 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 (A0) { case SYSCALL_HALT: halt_kernel(); break; case SYSCALL_READ: V0 = tty_read((int) A1, (void*) A2, (int) A3); break; case SYSCALL_WRITE: V0 = tty_write((int) A1, (void*) A2, (int) A3); break; case SYSCALL_EXEC: V0 = syscall_exec((char*) A1); break; case SYSCALL_EXIT: syscall_exit((int) A1); break; case SYSCALL_JOIN: V0 = syscall_join((process_id_t) A1); break; case SYSCALL_MEMLIMIT: V0 = (int)process_memlimit((void *) A1); break; default: KERNEL_PANIC("Unhandled system call\n"); } /* Move to next instruction after system call */ user_context->pc += 4; }
int main(void) { int t0, t1; int i, a=0; t0 = syscall_getclock(); //create a big loop, so there will be a some thread switch for(i =0; i< 100000; i++){ a = a + 2; } a = 0; for(i =0; i< 100000; i++){ a = a + 2; } puts("Done t5\n"); t1 = syscall_getclock(); syscall_exit(t1-t0); return t1-t0; }
void w00t(char *argv0) { /* char **argv; char **envp; int argc;*/ int ret; /* argv = &argv0; envp = argv; while(*envp) envp++; argc = (size_t)(envp - argv); envp++; */ ret = main(0, NULL, NULL/*argc, argv, envp*/); syscall_exit(ret); }