OS Project1 1
Hello World Writing a Hello World program #include header.h main ( int argc, char *argv[] ) { printf( Hello World!\n ); } Compile the Hello World program > gcc helloworld.c o helloworld 2
Header.h #ifndef HEADER_H_ #define HEADER_H_ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <wait.h> #include <signal.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <ctype.h> #include <sys/time.h> #include <pthread.h> #include <semaphore.h> #endif /*HEADER_H_*/ 3
Sample makefile Name your makefile makefile Sample makefile for the first problem: all: helloworld clear: rm *.o helloworld : helloworld.o gcc helloworld.o -o helloworld helloworld.o: helloworld.c header.h gcc -c helloworld.c 4
makefile Create a line to compile everything all: basic_server basic_client Create a line to remove all object files clear: rm *.o Create a line for each object file you wish to make basic_server.o: basic_server.cc header.h gcc -c basic_server.cc -o basic_server.o Create a line for each executable file you wish to make basic_server: basic_server.o gcc basic_server.o -o basic_server Compile executables: > make 5
Problem 1 Sample MyCopy 1 Open File Stream Object: FILE fopen(char *filename,char *mode) for read and write. r : Read only. w+ : Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed. int fclose(file *stream ) to close file stream object after use. Check errors 6
Problem 1 Sample MyCopy 2 Read and write to a file Read/Write a block of chars: size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); fread returns the number of full items actually read size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); fwrite returns the number of full items actually written Read an individual char: int fgetc( FILE *stream ); int fputc( int c, FILE *stream ); 7
Timing I Problem 1 Sample MyCopy 3 clock_t start, end; double elapsed; start = clock(); end = clock(); elapsed = ((double) (end - start)) / CLOCKS_PER_SEC * 1000; printf("time used: %f millisecond\n", elapsed); 8
Timing II Problem 1 Sample MyCopy 4 struct timeval starttime, endtime; struct timezone tz; struct tm *tm; gettimeofday(&starttime, &tz); gettimeofday(&endtime, &tz); long run_time_in_microseconds; run_time_in_microseconds = endtime.tv_usec - starttime.tv_usec; printf(" Time used: %d microseconds.\n",run_time_in_microseconds); 9
Problem 1 Use fork() Fork the child process: pid_t ForkPID; ForkPID = fork(); Write a quick switch statement: switch (ForkPID) { // -1, fork failure case -1: printf("error: Failed to fork.\n"); break; // 0, this is the child process case 0: break; // > 0, parent process and the PID is the child's PID default: } 10
Problem 1 Using exec() execve(char * filename,char * argv[ ],char * envp[ ]); execlp(char *filename, char *argv0, char *argv1,..., NULL); execvp(char *filename, char *argv [ ]); Run the above functions in a forked child: char* command_array[] = {./MyCopy,argv[1],argv[2],0}; execvp(command_array[0],command_array); The parent thread can wait for the forked thread to complete: while (wait(&status)!= 0) {} Or wait(&status); 11
Problem 2 Tips Part 1 Sample code run in the child process: if (execvp(command_array[0],command_array) == -1) { printf("error: running command: '%s'\n", line); exit(0);} For example: line = ls l command_array[0] = ls command_array[1] = -l command_array[2] = NULL 12
Problem 2 Tips Part 2 Parsing command int parseline(char *line, char *command_array[]) { char *p; int count=0; p = strtok(line, " "); while (p && strcmp(p," ")!=0) { command_array[count] = p; count++; p = strtok(null," "); } return count; } 13
Problem 3 Tips Part 1 Header: #include <pthread.h> Usage of pthread: // create a thread 't1' pthread_t t1; rc = pthread_create (&t1, &attr1(null), my_function, &my_arguments1); // join thread 1 and wait for completion void* status1; rc = pthread_join (t1, &status1); if (rc) { printf("error; return code from pthread_join(t1) is %d\n", rc); exit(-1); } 14
Problem 3 Tips Part 2 Header: #include <semaphore.h> sem_t: structure of semaphore sem_t sem; sem_init: initialize semaphore sem_init(&sem,0,1); // set up a binary semaphore to act as a mutex for the critical section "use of shovel", initialize it to 1 sem_destroy: destroy a semaphore sem_destroy(&sem); sem_wait(&sem) wait for/block on a semaphore decrease the value of the semaphore, and if the value is <0, it will BLOCK the thread sem_post(&sem) signal/post on a semaphore increase the value of the semaphore, and if the value becomes <= 0, the OS will unblock a blocked thread (a thread who called sem_wait and was blocked) 15
Problem 4 Cooks, Cashiers, and Customers are each modeled as a thread. Cashiers sleep until a customer is present. A Customer approaching a cashier can start the order process. A Customer cannot order until the cashier is ready. Once the order is placed, a cashier has to get a burger from the rack. If a burger is not available, a cashier must wait until one is made. The cook will always make burgers and place them on the rack. The cook will wait if the rack is full. There are NO synchronization constraints for a cashier presenting food to the customer. 16
Burger Buddies Problem 17
Prepare Knowledge: tips Simulating time passing sleep(rand() % NUM); Assigning ID id_cook[i] = i +1; create_thread (Cook, &id_cook[i]); void *Cook(void *args){ int id = *(int *)args; } 18
General Requirement Source file: BurgerBuddies.c Executable file: BBC gcc BurgerBuddies.c o BBC lpthread Run: BBC #Cooks #Cashiers #Customers #RackSize 19
Sample Output >./BBC 2 4 41 10 Cooks [2], Cashiers [4], Customers [41] Begin run. Cook [1] put a burger on the rack. Cook [1] put a burger on the rack. Cook [2] put a burger on the rack. Customer [10] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [19] come. Casher [2] accepts an order. Casher [2] take a burger to customor. Customer [7] come. Casher [3] accepts an order. Casher [3] take a burger to customor. Customer [17] come. Casher [2] accepts an order. Cook [1] put a burger on the rack. Casher [2] take a burger to customor. 20