├── Endsems ├── 180010027 │ ├── CS 314 Operating Systems Lab- Exam.pdf │ ├── README.md │ ├── a.out │ ├── check.awk │ ├── master-worker │ ├── master-worker.c │ ├── output │ └── test-master-worker.sh ├── 180010027.zip ├── Resources │ ├── Lab Codes │ │ ├── 180010027.zip │ │ └── 180010027_lab5.zip │ ├── master-worker │ │ ├── check.awk │ │ ├── master-worker │ │ ├── master-worker-skeleton.c │ │ ├── master-worker.c │ │ ├── output │ │ └── test-master-worker.sh │ ├── pthreads-sync-code │ │ ├── a.out │ │ ├── first.cpp │ │ ├── first1.cpp │ │ ├── master-worker │ │ │ ├── check.awk │ │ │ ├── master-worker-skeleton.c │ │ │ └── test-master-worker.sh │ │ ├── nlocks.c │ │ ├── reader-writer.c │ │ ├── rwlock │ │ │ ├── rwlock-reader-pref.cpp │ │ │ ├── rwlock-writer-pref.cpp │ │ │ ├── rwlock.h │ │ │ ├── test-reader-pref.cpp │ │ │ ├── test-rwlock.sh │ │ │ └── test-writer-pref.cpp │ │ ├── second.cpp │ │ ├── second1.cpp │ │ ├── threads-with-mutex.c │ │ ├── threads.c │ │ └── zemaphore │ │ │ ├── test-toggle.c │ │ │ ├── test-zem.c │ │ │ ├── test-zem.sh │ │ │ ├── zemaphore.c │ │ │ └── zemaphore.h │ ├── pthreads-sync.pdf │ ├── test.cpp │ └── threads-api.pdf └── threads-api.pdf ├── LICENSE ├── Lab-1 ├── 180010027_lab1.pdf ├── 180010027_lab1.tex └── Setting up Minix3.pdf ├── Lab-2 ├── 180010027.c ├── 180010027_Report.pdf ├── 180010027_lab2_part1.zip ├── 180010027_lab2_part2.zip ├── Laboratory 2.pdf ├── Makefile ├── forkexit.c ├── images │ ├── PID.png │ ├── gmake_hello.png │ ├── log.png │ └── reboot.png └── run.sh ├── Lab-3 ├── 180010027.pdf ├── 180010027_lab3.zip ├── Laboratory 3.pdf ├── byte-unixbench-mod.zip ├── byte-unixbench-mod │ ├── LICENSE.txt │ ├── README.md │ └── UnixBench │ │ ├── Makefile │ │ ├── README │ │ ├── Run │ │ ├── USAGE │ │ ├── WRITING_TESTS │ │ ├── pgms │ │ ├── gfx-x11 │ │ ├── index.base │ │ ├── multi.sh │ │ ├── tst.sh │ │ └── unixbench.logo │ │ ├── src │ │ ├── arith.c │ │ ├── big.c │ │ ├── context1.c │ │ ├── dhry.h │ │ ├── dhry_1.c │ │ ├── dhry_2.c │ │ ├── dummy.c │ │ ├── execl.c │ │ ├── fstime.c │ │ ├── hanoi.c │ │ ├── looper.c │ │ ├── pipe.c │ │ ├── spawn.c │ │ ├── syscall.c │ │ ├── time-polling.c │ │ ├── timeit.c │ │ ├── ubgears.c │ │ └── whets.c │ │ ├── testdir │ │ ├── cctest.c │ │ ├── dc.dat │ │ ├── large.txt │ │ └── sort.src │ │ └── workload_mix │ │ ├── arithoh.sh │ │ ├── fstime.sh │ │ ├── pipe.sh │ │ ├── spawn.sh │ │ ├── syscall.sh │ │ ├── workload_mix.sh │ │ ├── workload_mix1.sh │ │ ├── workload_mix2.sh │ │ ├── workload_mix3.sh │ │ └── workload_mix4.sh ├── images │ ├── part1 │ │ ├── 1.png │ │ ├── gmake.png │ │ ├── log.png │ │ └── swap.png │ ├── wmix1 │ │ ├── wmix1-1.png │ │ ├── wmix1-2.png │ │ ├── wmix1-3.png │ │ └── wmix1-4.png │ ├── wmix2 │ │ ├── wmix2-1.png │ │ └── wmix2-2.png │ ├── wmix3 │ │ ├── wmix3-1.png │ │ ├── wmix3-2.png │ │ └── wmix3-3.png │ └── wmix4 │ │ ├── wmix4-1.png │ │ ├── wmix4-2.png │ │ ├── wmix4-3.png │ │ └── wmix4-4.png ├── run.sh └── schedule.c ├── Lab-4 ├── 180010027_Report.pdf ├── 180010027_lab4.zip ├── Laboratory 4(1).pdf ├── Links.txt ├── RR.png ├── byte-unixbench-mod.zip ├── byte-unixbench-mod │ ├── LICENSE.txt │ ├── README.md │ └── UnixBench │ │ ├── Makefile │ │ ├── README │ │ ├── Run │ │ ├── USAGE │ │ ├── WRITING_TESTS │ │ ├── pgms │ │ ├── gfx-x11 │ │ ├── index.base │ │ ├── multi.sh │ │ ├── tst.sh │ │ └── unixbench.logo │ │ ├── src │ │ ├── arith.c │ │ ├── big.c │ │ ├── context1.c │ │ ├── dhry.h │ │ ├── dhry_1.c │ │ ├── dhry_2.c │ │ ├── dummy.c │ │ ├── execl.c │ │ ├── fstime.c │ │ ├── hanoi.c │ │ ├── looper.c │ │ ├── pipe.c │ │ ├── spawn.c │ │ ├── syscall.c │ │ ├── time-polling.c │ │ ├── timeit.c │ │ ├── ubgears.c │ │ └── whets.c │ │ ├── testdir │ │ ├── cctest.c │ │ ├── dc.dat │ │ ├── large.txt │ │ └── sort.src │ │ └── workload_mix │ │ ├── arithoh.sh │ │ ├── fstime.sh │ │ ├── pipe.sh │ │ ├── spawn.sh │ │ ├── syscall.sh │ │ ├── workload_mix.sh │ │ ├── workload_mix1.sh │ │ ├── workload_mix2.sh │ │ ├── workload_mix3.sh │ │ └── workload_mix4.sh ├── run1.sh ├── run2.sh ├── schedule.c └── system.c ├── Lab-5 ├── 180010027_lab5.zip ├── 180010027_lab5 │ ├── 180010027_Report.pdf │ ├── Lab-5.pptx │ ├── Laboratory 5(1).pdf │ ├── Makefile │ ├── a.out │ ├── input.ppm │ ├── part1.cpp │ ├── part2_1a.cpp │ ├── part2_1b.cpp │ ├── part2_2.cpp │ └── part2_3.cpp ├── Images │ ├── Butterfly │ │ ├── input.png │ │ ├── input.ppm │ │ ├── output.png │ │ ├── output_part1.ppm │ │ ├── output_part2_1a.ppm │ │ ├── output_part2_1b.ppm │ │ ├── output_part2_2.ppm │ │ └── output_part2_3.ppm │ └── Eagle │ │ ├── input.png │ │ ├── input.ppm │ │ ├── output_part1.png │ │ ├── output_part1.ppm │ │ ├── output_part2_1a.ppm │ │ ├── output_part2_1b.ppm │ │ ├── output_part2_2.ppm │ │ └── output_part2_3.ppm ├── Lab-5.pptx ├── Laboratory 5(1).pdf ├── Makefile ├── Report │ └── Report.pdf ├── part1.cpp ├── part2_1a.cpp ├── part2_1b.cpp ├── part2_2.cpp └── part2_3.cpp ├── Lab-6 ├── 180010027_Report.pdf ├── 180010027_lab6.zip ├── Lab-6 │ ├── link.c │ ├── open.c │ ├── read.c │ └── run.sh ├── Laboratory 6(1).pdf ├── gerofi-minix-vfs.pdf ├── images │ ├── create.png │ ├── delete.png │ ├── nano.png │ ├── read.png │ └── write.png └── minix.pdf ├── Lab-7 ├── 180010027 │ ├── Laboratory 7.pdf │ ├── Report.pdf │ ├── const.h │ ├── fslib.c │ ├── mfs │ │ ├── read.c │ │ └── write.c │ ├── run.sh │ └── vfs │ │ ├── link.c │ │ ├── open.c │ │ └── read.c ├── 180010027_lab7.zip ├── Images.zip ├── Images │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png ├── Laboratory 7.pdf ├── gerofi-minix-vfs.pdf └── minix.pdf ├── Lab-8 ├── 180010027_lab8.zip └── 180010027_lab8 │ ├── clock │ ├── clock.c │ ├── clock.dat │ ├── clock.p │ ├── clock.png │ ├── lru │ ├── lru.c │ ├── lru.dat │ ├── lru.p │ ├── lru.png │ ├── optimal │ ├── optimal.c │ ├── optimal.dat │ ├── optimal.p │ ├── optimal.png │ ├── random │ ├── random.c │ ├── random.dat │ ├── random.p │ ├── random.png │ ├── run.sh │ └── swap-lab.pdf └── README.md /Endsems/180010027.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/180010027.zip -------------------------------------------------------------------------------- /Endsems/180010027/CS 314 Operating Systems Lab- Exam.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/180010027/CS 314 Operating Systems Lab- Exam.pdf -------------------------------------------------------------------------------- /Endsems/180010027/README.md: -------------------------------------------------------------------------------- 1 | ### README.txt - Rishit Saiya 180010027 2 | 3 | I have used `include ` library in `master-worker.c`. So while compiling the code, the following command was used: 4 | 5 | ``` 6 | g++ master-worker.c -lpthread 7 | ``` 8 | 9 | #### Testing Phase: 10 | 11 | The program needs 4 Command Line Arguments - 12 | 13 | ``` 14 | ./runme.sh 1000 800 12 10 15 | ``` 16 | 17 | 18 | Note: With the help of bits library, all the test cases are passed and Synchronization Primitives are correctly implemented. 19 | -------------------------------------------------------------------------------- /Endsems/180010027/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/180010027/a.out -------------------------------------------------------------------------------- /Endsems/180010027/check.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | for (i = 0; i < MAX; i++) { 3 | prod[i] = 0 4 | cons[i] = 0 5 | } 6 | } 7 | 8 | { 9 | if ($1 == "Produced" && $2 < MAX) { 10 | prod[$2]++; 11 | } 12 | else if ($1 == "Consumed" && $2 < MAX) { 13 | cons[$2]++; 14 | } 15 | 16 | } 17 | 18 | END { 19 | count=0; 20 | for (i = 0; i < MAX; i++) { 21 | if (prod[i] == 1 && cons[i] == 1) { 22 | count++; 23 | } 24 | else { 25 | # printf("%d\t%d\t%d\n", i, prod[i], cons[i]); 26 | } 27 | } 28 | if (count == MAX) { 29 | printf("OK. All test cases passed!\n"); 30 | } 31 | else { 32 | printf("Please check your code. %d/%d test cases failed!\n", MAX - count, MAX); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Endsems/180010027/master-worker: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/180010027/master-worker -------------------------------------------------------------------------------- /Endsems/180010027/master-worker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include //for fork() 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | 18 | using namespace std; 19 | 20 | // Condition Variable to denote eat and produce variables 21 | pthread_cond_t eat; 22 | pthread_cond_t produce; 23 | 24 | int item_to_produce, curr_buf_size; 25 | int item_to_consume; 26 | int total_items, max_buf_size, num_workers, num_masters; 27 | pthread_mutex_t M1 = PTHREAD_MUTEX_INITIALIZER; 28 | pthread_mutex_t M2 = PTHREAD_MUTEX_INITIALIZER; 29 | int *buffer; 30 | 31 | void print_produced(int num, int master) { 32 | 33 | printf("Produced %d by master %d\n", num, master); 34 | } 35 | 36 | void print_consumed(int num, int worker) { 37 | 38 | printf("Consumed %d by worker %d\n", num, worker); 39 | 40 | } 41 | 42 | 43 | //produce items and place in buffer 44 | //modify code below to synchronize correctly 45 | void *generate_requests_loop(void *data) { 46 | int thread_id = *((int *)data); 47 | 48 | while(1) { 49 | 50 | if(item_to_produce >= total_items) { 51 | break; 52 | } 53 | 54 | // Apply Lock - Critical Section 55 | pthread_mutex_lock(&M1); 56 | 57 | // Spinlock to avoid other threads from entering critical sections 58 | while(curr_buf_size == max_buf_size){ 59 | //Wait for thread to complete 60 | pthread_cond_wait(&eat, &M1); 61 | } 62 | 63 | buffer[curr_buf_size++] = item_to_produce; 64 | print_produced(item_to_produce, thread_id); 65 | item_to_produce++; 66 | 67 | // Apply Unlock - Critical Section 68 | pthread_mutex_unlock(&M1); 69 | 70 | //Produce Signal to wake other signals 71 | pthread_cond_signal(&produce); 72 | } 73 | return 0; 74 | } 75 | 76 | //write function to be run by worker threads 77 | //ensure that the workers call the function print_consumed when they consume an item 78 | void *generate_eat_loop(void *data) { 79 | int thread_id = *((int *)data); 80 | 81 | while(1){ 82 | 83 | if(item_to_consume >= total_items) { 84 | break; 85 | } 86 | 87 | // Apply Lock - Critical Section 88 | pthread_mutex_lock(&M2); 89 | 90 | // Spinlock to avoid other threads from entering critical sections 91 | while(curr_buf_size == 0) { 92 | //Wait for a thread to place in 93 | pthread_cond_wait(&produce,&M2); 94 | } 95 | 96 | curr_buf_size--; 97 | print_consumed(item_to_consume, thread_id); 98 | 99 | item_to_consume++; 100 | 101 | // Apply Unlock - Critical Section 102 | pthread_mutex_unlock(&M2); 103 | 104 | //Signal Done eating 105 | pthread_cond_signal(&eat); 106 | } 107 | return 0; 108 | } 109 | 110 | int main(int argc, char *argv[]) { 111 | int i; // For loops 112 | 113 | // Pthreads conditions initialized in main() 114 | pthread_cond_init(&eat,NULL); 115 | pthread_cond_init(&produce,NULL); 116 | 117 | int *master_thread_id; // Created array of pointers for masters 118 | int *worker_thread_id; // Created array of pointers for workers 119 | 120 | pthread_t *master_thread; 121 | pthread_t *worker_thread; // Created threads and declared for workers 122 | 123 | item_to_produce = 0; 124 | item_to_consume = 0; // Variable for Item created to eat 125 | curr_buf_size = 0; // Variable for Buffer Size 126 | 127 | if (argc < 5) { 128 | printf("./master-worker #total_items #max_buf_size #num_workers #masters e.g. ./exe 10000 1000 4 3\n"); 129 | exit(1); 130 | } 131 | else { 132 | total_items = atoi(argv[1]); 133 | max_buf_size = atoi(argv[2]); 134 | num_workers = atoi(argv[3]); 135 | num_masters = atoi(argv[4]); 136 | } 137 | 138 | buffer = (int *)malloc (sizeof(int) * max_buf_size); 139 | 140 | //create master producer threads 141 | master_thread_id = (int *)malloc(sizeof(int) * num_masters); 142 | master_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_masters); 143 | for (i = 0; i < num_masters; i++) 144 | master_thread_id[i] = i; 145 | 146 | for (i = 0; i < num_masters; i++) 147 | pthread_create(&master_thread[i], NULL, generate_requests_loop, (void *)&master_thread_id[i]); 148 | 149 | //create worker consumer threads 150 | 151 | //Using worker_thread_id similar to master_thread_id made above 152 | worker_thread_id = (int *)malloc(sizeof(int) * num_workers); 153 | worker_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_workers); 154 | for (i = 0; i < num_workers; i++) 155 | worker_thread_id[i] = i; 156 | 157 | // Created threads for workers 158 | for (i = 0; i < num_workers; i++) 159 | pthread_create(&worker_thread[i], NULL, generate_eat_loop, (void *)&worker_thread_id[i]); 160 | 161 | 162 | //wait for all threads to complete - master 163 | for (i = 0; i < num_masters; i++){ 164 | pthread_join(master_thread[i], NULL); 165 | printf("master %d joined\n", i); 166 | } 167 | 168 | //wait for all threads to complete - worker 169 | for (i = 0; i < num_workers; i++) { 170 | pthread_join(worker_thread[i], NULL); 171 | printf("worker %d joined\n", i); 172 | } 173 | 174 | /*----Deallocating Buffers---------------------*/ 175 | free(buffer); 176 | free(master_thread); 177 | free(worker_thread); 178 | free(master_thread_id); 179 | free(worker_thread_id); 180 | return 0; 181 | } 182 | -------------------------------------------------------------------------------- /Endsems/180010027/test-master-worker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # script takes 4 arguments that are given to the master worker program 3 | 4 | g++ -o master-worker master-worker.c -lpthread 5 | ./master-worker $1 $2 $3 $4 > output 6 | awk -f check.awk MAX=$1 output 7 | -------------------------------------------------------------------------------- /Endsems/Resources/Lab Codes/180010027.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/Lab Codes/180010027.zip -------------------------------------------------------------------------------- /Endsems/Resources/Lab Codes/180010027_lab5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/Lab Codes/180010027_lab5.zip -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/check.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | for (i = 0; i < MAX; i++) { 3 | prod[i] = 0 4 | cons[i] = 0 5 | } 6 | } 7 | 8 | { 9 | if ($1 == "Produced" && $2 < MAX) { 10 | prod[$2]++; 11 | } 12 | else if ($1 == "Consumed" && $2 < MAX) { 13 | cons[$2]++; 14 | } 15 | 16 | } 17 | 18 | END { 19 | count=0; 20 | for (i = 0; i < MAX; i++) { 21 | if (prod[i] == 1 && cons[i] == 1) { 22 | count++; 23 | } 24 | else { 25 | # printf("%d\t%d\t%d\n", i, prod[i], cons[i]); 26 | } 27 | } 28 | if (count == MAX) { 29 | printf("OK. All test cases passed!\n"); 30 | } 31 | else { 32 | printf("Please check your code. %d/%d test cases failed!\n", MAX - count, MAX); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/master-worker: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/master-worker/master-worker -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/master-worker-skeleton.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int item_to_produce, item_to_eat, curr_buf_size; 11 | int total_items, max_buf_size, num_workers, num_masters; 12 | 13 | int *buffer; 14 | 15 | void print_produced(int num, int master) { 16 | 17 | printf("Produced %d by master %d\n", num, master); 18 | } 19 | 20 | void print_consumed(int num, int worker) { 21 | 22 | printf("Consumed %d by worker %d\n", num, worker); 23 | 24 | } 25 | 26 | 27 | //produce items and place in buffer 28 | //modify code below to synchronize correctly 29 | void *generate_requests_loop(void *data) 30 | { 31 | int thread_id = *((int *)data); 32 | 33 | while(1) 34 | { 35 | 36 | if(item_to_produce >= total_items) { 37 | break; 38 | } 39 | 40 | buffer[curr_buf_size++] = item_to_produce; 41 | print_produced(item_to_produce, thread_id); 42 | item_to_produce++; 43 | } 44 | return 0; 45 | } 46 | 47 | //write function to be run by worker threads 48 | void *generate_eat_loop(void *data) 49 | { 50 | int thread_id = *((int *)data); 51 | 52 | while(1) 53 | { 54 | 55 | if(item_to_eat >= total_items) { 56 | break; 57 | } 58 | 59 | buffer[curr_buf_size++] = item_to_eat; 60 | print_consumed(item_to_eat, thread_id); 61 | item_to_eat--; 62 | } 63 | return 0; 64 | } 65 | //ensure that the workers call the function print_consumed when they consume an item 66 | 67 | int main(int argc, char *argv[]) 68 | { 69 | int *master_thread_id; 70 | int *worker_thread_id; 71 | pthread_t *master_thread; 72 | item_to_produce = 0; 73 | curr_buf_size = 0; 74 | 75 | int i; 76 | 77 | if (argc < 5) { 78 | printf("./master-worker #total_items #max_buf_size #num_workers #masters e.g. ./exe 10000 1000 4 3\n"); 79 | exit(1); 80 | } 81 | else { 82 | num_masters = atoi(argv[4]); 83 | num_workers = atoi(argv[3]); 84 | total_items = atoi(argv[1]); 85 | max_buf_size = atoi(argv[2]); 86 | } 87 | 88 | 89 | buffer = (int *)malloc (sizeof(int) * max_buf_size); 90 | 91 | //create master producer threads 92 | master_thread_id = (int *)malloc(sizeof(int) * num_masters); 93 | master_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_masters); 94 | for (i = 0; i < num_masters; i++) 95 | master_thread_id[i] = i; 96 | 97 | for (i = 0; i < num_masters; i++) 98 | pthread_create(&master_thread[i], NULL, generate_requests_loop, (void *)&master_thread_id[i]); 99 | 100 | //create worker consumer threads 101 | worker_thread_id = (int *)malloc(sizeof(int) * num_workers); 102 | worker_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_workers); 103 | for (i = 0; i < num_workers; i++) 104 | worker_thread_id[i] = i; 105 | 106 | for (i = 0; i < num_workers; i++) 107 | pthread_create(&worker_thread[i], NULL, generate_requests_loop, (void *)&worker_thread_id[i]); 108 | 109 | 110 | //wait for all threads to complete 111 | for (i = 0; i < num_masters; i++) 112 | { 113 | pthread_join(master_thread[i], NULL); 114 | printf("master %d joined\n", i); 115 | } 116 | 117 | for (i = 0; i < num_workers; i++) 118 | { 119 | pthread_join(worker_thread[i], NULL); 120 | printf("worker %d joined\n", i); 121 | } 122 | /*----Deallocating Buffers---------------------*/ 123 | free(buffer); 124 | free(master_thread_id); 125 | free(master_thread); 126 | free(worker_thread); 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/master-worker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | 20 | int item_to_produce, item_to_eat, curr_buf_size; 21 | int total_items, max_buf_size, num_workers, num_masters; 22 | pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; 23 | pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; 24 | pthread_cond_t eat,produce; 25 | int *buffer; 26 | 27 | void print_produced(int num, int master) { 28 | 29 | printf("Produced %d by master %d\n", num, master); 30 | } 31 | 32 | void print_consumed(int num, int worker) { 33 | 34 | printf("Consumed %d by worker %d\n", num, worker); 35 | 36 | } 37 | 38 | 39 | //produce items and place in buffer 40 | //modify code below to synchronize correctly 41 | void *generate_requests_loop(void *data) 42 | { 43 | int thread_id = *((int *)data); 44 | 45 | while(1) 46 | { 47 | 48 | if(item_to_produce >= total_items) { 49 | break; 50 | } 51 | pthread_mutex_lock(&mutex1); 52 | while(curr_buf_size==max_buf_size) 53 | { 54 | //Wait someone to eat 55 | pthread_cond_wait(&eat,&mutex1); 56 | } 57 | 58 | buffer[curr_buf_size++] = item_to_produce; 59 | print_produced(item_to_produce, thread_id); 60 | item_to_produce++; 61 | //Signal Produced 62 | 63 | pthread_mutex_unlock(&mutex1); 64 | pthread_cond_signal(&produce); 65 | } 66 | return 0; 67 | } 68 | 69 | //write function to be run by worker threads 70 | void *generate_eat_loop(void *data) 71 | { 72 | int thread_id = *((int *)data); 73 | 74 | while(1) 75 | { 76 | 77 | if(item_to_eat >= total_items) { 78 | break; 79 | } 80 | pthread_mutex_lock(&mutex2); 81 | while(curr_buf_size==0) 82 | { 83 | //Wait for someone to fill 84 | pthread_cond_wait(&produce,&mutex2); 85 | } 86 | curr_buf_size--; 87 | print_consumed(item_to_eat, thread_id); 88 | item_to_eat++; 89 | //Signal Done eating 90 | 91 | pthread_mutex_unlock(&mutex2); 92 | pthread_cond_signal(&eat); 93 | } 94 | return 0; 95 | } 96 | //ensure that the workers call the function print_consumed when they consume an item 97 | 98 | int main(int argc, char *argv[]) 99 | { 100 | int *master_thread_id; 101 | int *worker_thread_id; 102 | pthread_t *master_thread; 103 | pthread_t *worker_thread; 104 | item_to_produce = 0; 105 | item_to_eat =0; 106 | curr_buf_size = 0; 107 | pthread_cond_init(&eat,NULL); 108 | pthread_cond_init(&produce,NULL); 109 | int i; 110 | 111 | if (argc < 5) { 112 | printf("./master-worker #total_items #max_buf_size #num_workers #masters e.g. ./exe 10000 1000 4 3\n"); 113 | exit(1); 114 | } 115 | else { 116 | num_masters = atoi(argv[4]); 117 | num_workers = atoi(argv[3]); 118 | total_items = atoi(argv[1]); 119 | max_buf_size = atoi(argv[2]); 120 | } 121 | 122 | 123 | buffer = (int *)malloc (sizeof(int) * max_buf_size); 124 | 125 | //create master producer threads 126 | master_thread_id = (int *)malloc(sizeof(int) * num_masters); 127 | master_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_masters); 128 | for (i = 0; i < num_masters; i++) 129 | master_thread_id[i] = i; 130 | 131 | for (i = 0; i < num_masters; i++) 132 | pthread_create(&master_thread[i], NULL, generate_requests_loop, (void *)&master_thread_id[i]); 133 | 134 | //create worker consumer threads 135 | worker_thread_id = (int *)malloc(sizeof(int) * num_workers); 136 | worker_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_workers); 137 | for (i = 0; i < num_workers; i++) 138 | worker_thread_id[i] = i; 139 | 140 | for (i = 0; i < num_workers; i++) 141 | pthread_create(&worker_thread[i], NULL, generate_eat_loop, (void *)&worker_thread_id[i]); 142 | 143 | 144 | //wait for all threads to complete 145 | for (i = 0; i < num_masters; i++) 146 | { 147 | pthread_join(master_thread[i], NULL); 148 | printf("master %d joined\n", i); 149 | } 150 | 151 | for (i = 0; i < num_workers; i++) 152 | { 153 | pthread_join(worker_thread[i], NULL); 154 | printf("worker %d joined\n", i); 155 | } 156 | /*----Deallocating Buffers---------------------*/ 157 | free(buffer); 158 | free(master_thread_id); 159 | free(worker_thread_id); 160 | free(master_thread); 161 | free(worker_thread); 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/output: -------------------------------------------------------------------------------- 1 | Produced 0 by master 0 2 | Produced 1 by master 0 3 | Produced 2 by master 4 4 | Produced 3 by master 1 5 | Produced 4 by master 2 6 | Produced 5 by master 4 7 | Produced 6 by master 4 8 | Produced 7 by master 4 9 | Produced 8 by master 4 10 | Produced 9 by master 4 11 | Produced 10 by master 4 12 | Produced 11 by master 4 13 | Produced 12 by master 4 14 | Produced 13 by master 4 15 | Produced 14 by master 4 16 | Produced 15 by master 4 17 | Produced 16 by master 4 18 | Produced 17 by master 4 19 | Produced 18 by master 2 20 | Produced 19 by master 1 21 | Produced 20 by master 1 22 | Produced 21 by master 1 23 | Produced 22 by master 1 24 | Produced 23 by master 1 25 | Produced 24 by master 1 26 | Produced 25 by master 2 27 | Produced 26 by master 2 28 | Produced 27 by master 1 29 | Produced 28 by master 2 30 | Produced 29 by master 2 31 | Produced 30 by master 2 32 | Produced 31 by master 2 33 | Produced 32 by master 2 34 | Produced 33 by master 4 35 | Consumed 0 by worker 0 36 | Produced 34 by master 2 37 | Consumed 1 by worker 0 38 | Consumed 2 by worker 0 39 | Produced 35 by master 4 40 | Consumed 3 by worker 0 41 | Produced 36 by master 3 42 | Consumed 4 by worker 0 43 | Consumed 5 by worker 0 44 | Produced 37 by master 1 45 | Consumed 6 by worker 0 46 | Consumed 7 by worker 0 47 | Consumed 8 by worker 0 48 | Consumed 9 by worker 0 49 | Consumed 10 by worker 0 50 | Produced 38 by master 0 51 | Consumed 11 by worker 0 52 | Produced 39 by master 0 53 | Consumed 12 by worker 0 54 | Produced 40 by master 0 55 | Produced 41 by master 0 56 | Produced 42 by master 0 57 | Produced 43 by master 0 58 | Produced 44 by master 0 59 | Produced 45 by master 0 60 | Produced 46 by master 0 61 | Produced 47 by master 0 62 | Produced 48 by master 0 63 | Produced 49 by master 0 64 | Produced 50 by master 0 65 | Produced 51 by master 0 66 | Produced 52 by master 0 67 | Produced 53 by master 0 68 | Produced 54 by master 0 69 | Produced 55 by master 0 70 | Produced 56 by master 0 71 | Consumed 13 by worker 0 72 | Consumed 14 by worker 0 73 | Consumed 15 by worker 0 74 | Consumed 16 by worker 0 75 | Consumed 17 by worker 0 76 | Consumed 18 by worker 0 77 | Consumed 19 by worker 0 78 | Produced 57 by master 0 79 | Produced 58 by master 0 80 | Consumed 20 by worker 0 81 | Produced 59 by master 0 82 | Produced 60 by master 0 83 | Consumed 21 by worker 0 84 | Produced 61 by master 0 85 | Produced 62 by master 0 86 | Produced 63 by master 0 87 | Produced 64 by master 0 88 | Consumed 22 by worker 0 89 | Produced 65 by master 0 90 | Consumed 23 by worker 0 91 | Produced 66 by master 0 92 | Produced 67 by master 0 93 | Produced 68 by master 0 94 | Produced 69 by master 0 95 | Produced 70 by master 0 96 | Produced 71 by master 0 97 | Produced 72 by master 0 98 | Produced 73 by master 0 99 | Produced 74 by master 0 100 | Consumed 24 by worker 0 101 | Produced 75 by master 0 102 | Produced 76 by master 0 103 | Produced 77 by master 0 104 | Produced 78 by master 0 105 | Produced 79 by master 0 106 | Produced 80 by master 0 107 | Produced 81 by master 0 108 | Produced 82 by master 0 109 | Produced 83 by master 0 110 | Produced 84 by master 0 111 | Consumed 25 by worker 0 112 | Produced 85 by master 0 113 | Produced 86 by master 0 114 | Produced 87 by master 0 115 | Produced 88 by master 0 116 | Produced 89 by master 0 117 | Produced 90 by master 0 118 | Produced 91 by master 0 119 | Produced 92 by master 0 120 | Produced 93 by master 0 121 | Produced 94 by master 0 122 | Produced 95 by master 0 123 | Produced 96 by master 0 124 | Produced 97 by master 0 125 | Produced 98 by master 0 126 | Produced 99 by master 0 127 | Consumed 26 by worker 0 128 | Consumed 27 by worker 0 129 | Consumed 28 by worker 0 130 | Consumed 29 by worker 1 131 | Consumed 30 by worker 2 132 | Consumed 31 by worker 2 133 | Consumed 32 by worker 2 134 | Consumed 33 by worker 2 135 | Consumed 34 by worker 2 136 | Consumed 35 by worker 2 137 | Consumed 36 by worker 2 138 | Consumed 37 by worker 2 139 | Consumed 38 by worker 1 140 | Consumed 39 by worker 2 141 | Consumed 40 by worker 4 142 | Consumed 41 by worker 2 143 | Consumed 42 by worker 2 144 | Consumed 43 by worker 2 145 | Consumed 44 by worker 2 146 | Consumed 45 by worker 2 147 | Consumed 46 by worker 2 148 | Consumed 47 by worker 2 149 | Consumed 48 by worker 2 150 | Consumed 49 by worker 2 151 | Consumed 50 by worker 2 152 | Consumed 51 by worker 3 153 | Consumed 52 by worker 4 154 | Consumed 53 by worker 4 155 | Consumed 54 by worker 4 156 | Consumed 55 by worker 4 157 | Consumed 56 by worker 4 158 | Consumed 57 by worker 4 159 | Consumed 58 by worker 4 160 | Consumed 59 by worker 4 161 | Consumed 60 by worker 4 162 | Consumed 61 by worker 4 163 | Consumed 62 by worker 4 164 | Consumed 63 by worker 4 165 | Consumed 64 by worker 4 166 | Consumed 65 by worker 4 167 | Consumed 66 by worker 4 168 | Consumed 67 by worker 4 169 | Consumed 68 by worker 4 170 | Consumed 69 by worker 4 171 | Consumed 70 by worker 4 172 | Consumed 71 by worker 4 173 | Consumed 72 by worker 4 174 | Consumed 73 by worker 4 175 | Consumed 74 by worker 4 176 | Consumed 75 by worker 4 177 | Consumed 76 by worker 4 178 | Consumed 77 by worker 4 179 | Consumed 78 by worker 4 180 | Consumed 79 by worker 4 181 | Consumed 80 by worker 4 182 | Consumed 81 by worker 4 183 | Consumed 82 by worker 4 184 | Consumed 83 by worker 4 185 | Consumed 84 by worker 4 186 | Consumed 85 by worker 4 187 | Consumed 86 by worker 4 188 | Consumed 87 by worker 4 189 | Consumed 88 by worker 4 190 | Consumed 89 by worker 4 191 | Consumed 90 by worker 4 192 | Consumed 91 by worker 4 193 | Consumed 92 by worker 4 194 | Consumed 93 by worker 4 195 | Consumed 94 by worker 4 196 | Consumed 95 by worker 4 197 | Consumed 96 by worker 4 198 | Consumed 97 by worker 4 199 | Consumed 98 by worker 4 200 | Consumed 99 by worker 4 201 | master 0 joined 202 | Consumed 100 by worker 1 203 | Produced 100 by master 4 204 | Produced 101 by master 2 205 | Consumed 101 by worker 3 206 | Produced 102 by master 3 207 | Consumed 102 by worker 0 208 | Produced 103 by master 1 209 | Consumed 103 by worker 2 210 | master 1 joined 211 | master 2 joined 212 | master 3 joined 213 | master 4 joined 214 | worker 0 joined 215 | worker 1 joined 216 | worker 2 joined 217 | worker 3 joined 218 | worker 4 joined 219 | -------------------------------------------------------------------------------- /Endsems/Resources/master-worker/test-master-worker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # script takes 4 arguments that are given to the master worker program 3 | 4 | g++ -o master-worker master-worker.c -lpthread 5 | ./master-worker $1 $2 $3 $4 > output 6 | awk -f check.awk MAX=$1 output 7 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/pthreads-sync-code/a.out -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/first.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | int counter = 0; 20 | 21 | void increment(){ 22 | for (int i = 0; i < 10000; i++) 23 | { 24 | counter++; 25 | } 26 | 27 | } 28 | 29 | int main(){ 30 | thread t1(increment); 31 | thread t2(increment); 32 | thread t3(increment); 33 | thread t4(increment); 34 | thread t5(increment); 35 | thread t6(increment); 36 | thread t7(increment); 37 | thread t8(increment); 38 | thread t9(increment); 39 | thread t10(increment); 40 | 41 | t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); t6.join(); t7.join(); t8.join(); t9.join(); t10.join(); 42 | 43 | printf("%d", counter); 44 | } -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/first1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | int counter = 0; 20 | pthread_mutex_t mutex1; 21 | 22 | void increment(){ 23 | pthread_mutex_lock(&mutex1); // Lock 24 | for (int i = 0; i < 10000; i++){ 25 | counter++; 26 | } 27 | pthread_mutex_unlock(&mutex1); // unLock 28 | } 29 | 30 | int main(){ 31 | pthread_mutex_init(&mutex1, NULL); 32 | thread t1(increment); 33 | thread t2(increment); 34 | thread t3(increment); 35 | thread t4(increment); 36 | thread t5(increment); 37 | thread t6(increment); 38 | thread t7(increment); 39 | thread t8(increment); 40 | thread t9(increment); 41 | thread t10(increment); 42 | 43 | t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); t6.join(); t7.join(); t8.join(); t9.join(); t10.join(); 44 | 45 | printf("%d\n", counter); 46 | } -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/master-worker/check.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | for (i = 0; i < MAX; i++) { 3 | prod[i] = 0 4 | cons[i] = 0 5 | } 6 | } 7 | 8 | { 9 | if ($1 == "Produced" && $2 < MAX) { 10 | prod[$2]++; 11 | } 12 | else if ($1 == "Consumed" && $2 < MAX) { 13 | cons[$2]++; 14 | } 15 | 16 | } 17 | 18 | END { 19 | count=0; 20 | for (i = 0; i < MAX; i++) { 21 | if (prod[i] == 1 && cons[i] == 1) { 22 | count++; 23 | } 24 | else { 25 | # printf("%d\t%d\t%d\n", i, prod[i], cons[i]); 26 | } 27 | } 28 | if (count == MAX) { 29 | printf("OK. All test cases passed!\n"); 30 | } 31 | else { 32 | printf("Please check your code. %d/%d test cases failed!\n", MAX - count, MAX); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/master-worker/master-worker-skeleton.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int item_to_produce, curr_buf_size; 11 | int total_items, max_buf_size, num_workers, num_masters; 12 | 13 | int *buffer; 14 | 15 | void print_produced(int num, int master) { 16 | 17 | printf("Produced %d by master %d\n", num, master); 18 | } 19 | 20 | void print_consumed(int num, int worker) { 21 | 22 | printf("Consumed %d by worker %d\n", num, worker); 23 | 24 | } 25 | 26 | 27 | //produce items and place in buffer 28 | //modify code below to synchronize correctly 29 | void *generate_requests_loop(void *data) 30 | { 31 | int thread_id = *((int *)data); 32 | 33 | while(1) 34 | { 35 | 36 | if(item_to_produce >= total_items) { 37 | break; 38 | } 39 | 40 | buffer[curr_buf_size++] = item_to_produce; 41 | print_produced(item_to_produce, thread_id); 42 | item_to_produce++; 43 | } 44 | return 0; 45 | } 46 | 47 | //write function to be run by worker threads 48 | //ensure that the workers call the function print_consumed when they consume an item 49 | 50 | int main(int argc, char *argv[]) 51 | { 52 | int *master_thread_id; 53 | pthread_t *master_thread; 54 | item_to_produce = 0; 55 | curr_buf_size = 0; 56 | 57 | int i; 58 | 59 | if (argc < 5) { 60 | printf("./master-worker #total_items #max_buf_size #num_workers #masters e.g. ./exe 10000 1000 4 3\n"); 61 | exit(1); 62 | } 63 | else { 64 | num_masters = atoi(argv[4]); 65 | num_workers = atoi(argv[3]); 66 | total_items = atoi(argv[1]); 67 | max_buf_size = atoi(argv[2]); 68 | } 69 | 70 | 71 | buffer = (int *)malloc (sizeof(int) * max_buf_size); 72 | 73 | //create master producer threads 74 | master_thread_id = (int *)malloc(sizeof(int) * num_masters); 75 | master_thread = (pthread_t *)malloc(sizeof(pthread_t) * num_masters); 76 | for (i = 0; i < num_masters; i++) 77 | master_thread_id[i] = i; 78 | 79 | for (i = 0; i < num_masters; i++) 80 | pthread_create(&master_thread[i], NULL, generate_requests_loop, (void *)&master_thread_id[i]); 81 | 82 | //create worker consumer threads 83 | 84 | 85 | //wait for all threads to complete 86 | for (i = 0; i < num_masters; i++) 87 | { 88 | pthread_join(master_thread[i], NULL); 89 | printf("master %d joined\n", i); 90 | } 91 | 92 | /*----Deallocating Buffers---------------------*/ 93 | free(buffer); 94 | free(master_thread_id); 95 | free(master_thread); 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/master-worker/test-master-worker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # script takes 4 arguments that are given to the master worker program 3 | 4 | gcc -o master-worker master-worker.c -lpthread 5 | ./master-worker $1 $2 $3 $4 > output 6 | awk -f check.awk MAX=$1 output 7 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/nlocks.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | pthread_mutex_t locks[10]; 5 | int shared_counter[10]; 6 | 7 | void *myThread(void *id) 8 | { 9 | for(int i=0; i<1000; i++){ 10 | pthread_mutex_lock(&locks[*(int *)id]); 11 | shared_counter[*(int *)id]++; 12 | pthread_mutex_unlock(&locks[*(int *)id]); 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | pthread_t tid[10]; 19 | int index[10]; 20 | for(int i=0;i<10;i++){ 21 | pthread_mutex_init(&locks[i],NULL); 22 | shared_counter[i] = 0; 23 | index[i] = i; 24 | } 25 | 26 | 27 | for(int i=0;i<10; i++){ 28 | pthread_create(&tid[i], NULL, myThread, (void *)&index[i]); 29 | } 30 | 31 | for(int j=0; j<1000; j++) 32 | { 33 | for(int i=0;i<10;i++){ 34 | pthread_mutex_lock(&locks[i]); 35 | shared_counter[i]++; 36 | pthread_mutex_unlock(&locks[i]); 37 | } 38 | } 39 | 40 | for(int i=0;i<10;i++){ 41 | pthread_join(tid[i], NULL); 42 | pthread_mutex_destroy(&locks[i]); 43 | } 44 | 45 | //pthread_mutex_destroy() 46 | for(int i=0;i<10;i++){ 47 | printf("Counter[%d]: ", i); 48 | printf("%d\n", shared_counter[i]); 49 | } 50 | 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/reader-writer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | struct read_write_lock 10 | { 11 | // If required, use this strucure to create 12 | // reader-writer lock related variables 13 | pthread_mutex_t lock; 14 | pthread_cond_t read_cond; 15 | pthread_cond_t write_cond; 16 | //pthread_cond_t pref_cond; 17 | int nreaders; 18 | int nwriters; 19 | int nwaiting; 20 | 21 | 22 | }rwlock; 23 | 24 | struct thread_args 25 | { 26 | int t_delay; 27 | int t_id; 28 | }; 29 | 30 | long int data = 0; // Shared data variable 31 | 32 | void InitalizeReadWriteLock(struct read_write_lock * rw) 33 | { 34 | pthread_mutex_init(&rw->lock, NULL); 35 | pthread_cond_init(&rw->read_cond, NULL); 36 | pthread_cond_init(&rw->write_cond, NULL); 37 | rw->nreaders = 0; 38 | rw->nwriters = 0; 39 | rw->nwaiting = 0; 40 | 41 | } 42 | 43 | 44 | // The pthread based reader lock 45 | void ReaderLock(struct read_write_lock * rw) 46 | { 47 | //Wait on the writer cond 48 | pthread_mutex_lock(&rw->lock); 49 | while(rw->nwriters > 0) 50 | pthread_cond_wait(&rw->read_cond,&rw->lock); 51 | while(rw->nwaiting > 0) 52 | pthread_cond_wait(&rw->read_cond,&rw->lock); 53 | rw->nreaders++; 54 | pthread_mutex_unlock(&rw->lock); 55 | } 56 | 57 | // The pthread based reader unlock 58 | void ReaderUnlock(struct read_write_lock * rw) 59 | { 60 | pthread_mutex_lock(&rw->lock); 61 | rw->nreaders--; 62 | //if(rw->nwaiting > 0) 63 | // pthread_cond_signal(rw->write_cond); 64 | if(rw->nreaders == 0) 65 | pthread_cond_signal(&rw->write_cond); 66 | pthread_mutex_unlock(&rw->lock); 67 | } 68 | 69 | // The pthread based writer lock 70 | void WriterLock(struct read_write_lock * rw) 71 | { 72 | pthread_mutex_lock(&rw->lock); 73 | rw->nwaiting++; 74 | while(rw->nreaders > 0 || rw->nwriters > 0){ 75 | 76 | pthread_cond_wait(&rw->write_cond,&rw->lock); 77 | } 78 | rw->nwaiting--; 79 | rw->nwriters ++; 80 | pthread_mutex_unlock(&rw->lock); 81 | 82 | } 83 | 84 | // The pthread based writer unlock 85 | void WriterUnlock(struct read_write_lock * rw) 86 | { 87 | pthread_mutex_lock(&rw->lock); 88 | rw->nwriters--; 89 | if(rw->nwaiting > 0) 90 | pthread_cond_signal(&rw->write_cond); 91 | else 92 | pthread_cond_broadcast(&rw->read_cond); 93 | pthread_mutex_unlock(&rw->lock); 94 | } 95 | 96 | // Call this function to delay the execution by 'delay' ms 97 | void delay(int delay) 98 | { 99 | struct timespec wait; 100 | 101 | if(delay <= 0) 102 | return; 103 | 104 | wait.tv_sec = delay / 1000; 105 | wait.tv_nsec = (delay % 1000) * 1000 * 1000; 106 | nanosleep(&wait, NULL); 107 | } 108 | 109 | // The pthread reader start function 110 | void *ReaderFunction(void *args) 111 | { 112 | // Delay the execution by arrival time specified in the input 113 | struct thread_args * targs = (struct thread_args *) args; 114 | delay(targs->t_delay); 115 | 116 | // .... 117 | 118 | // Get appropriate lock 119 | // Display thread ID and value of the shared data variable 120 | // 121 | // Add a dummy delay of 1 ms before lock release 122 | // .... 123 | 124 | ReaderLock(&rwlock); 125 | printf("Reader %d, data: %ld\n",targs->t_id,data); 126 | delay(1); 127 | ReaderUnlock(&rwlock); 128 | } 129 | 130 | // The pthread writer start function 131 | void *WriterFunction(void *args) 132 | { 133 | // Delay the execution by arrival time specified in the input 134 | struct thread_args * targs = (struct thread_args *) args; 135 | delay(targs->t_delay); 136 | 137 | // .... 138 | // 139 | // Get appropriate lock 140 | // Increment the shared data variable 141 | // Display thread ID and value of the shared data variable 142 | // 143 | // Add a dummy delay of 1 ms before lock release 144 | // .... 145 | WriterLock(&rwlock); 146 | data++; 147 | printf("Writer %d, data: %ld\n",targs->t_id,data); 148 | delay(1); 149 | WriterUnlock(&rwlock); 150 | 151 | } 152 | 153 | int main(int argc, char *argv[]) 154 | { 155 | pthread_t *threads; 156 | //struct argument_t *arg; 157 | 158 | int reader_number = 0; 159 | int writer_number = 0; 160 | long int thread_number = 0; 161 | long int total_readers = 0; 162 | long int total_writers = 0; 163 | long int total_threads = 0; 164 | 165 | int count = 0; // Number of 3 tuples in the inputs. Assume maximum 10 tuples 166 | int rws[10]; 167 | int nthread[10]; 168 | int delay[10]; 169 | 170 | // Verifying number of arguments 171 | if(argc<4 || (argc-1)%3!=0) 172 | { 173 | printf("reader-writer ...\n"); 174 | printf("Any number of readers/writers can be added with repetitions of above mentioned 3 tuple \n"); 175 | exit(1); 176 | } 177 | 178 | // Reading inputs 179 | for(int i=0; i<(argc-1)/3; i++) 180 | { 181 | char rw[2]; 182 | strcpy(rw, argv[(i*3)+1]); 183 | 184 | if(strcmp(rw, "r") == 0 || strcmp(rw, "w") == 0) 185 | { 186 | if(strcmp(rw, "r") == 0) 187 | rws[i] = 1; // rws[i] = 1, for reader 188 | else 189 | rws[i] = 2; // rws[i] = 2, for writer 190 | 191 | nthread[i] = atol(argv[(i*3)+2]); 192 | delay[i] = atol(argv[(i*3)+3]); 193 | 194 | count ++; // Number of tuples 195 | total_threads += nthread[i]; // Total number of threads 196 | if(rws[i] == 1){ 197 | total_readers = total_readers + nthread[i]; 198 | } 199 | else total_writers = total_writers + nthread[i]; 200 | } 201 | else 202 | { 203 | printf("reader-writer ...\n"); 204 | printf("Any number of readers/writers can be added with repetitions of above mentioned 3 tuple \n"); 205 | exit(1); 206 | } 207 | } 208 | 209 | // Create reader/writer threads based on the input and read and write. 210 | pthread_t reader_tid[total_readers]; 211 | pthread_t writer_tid[total_writers]; 212 | struct thread_args reader_threads[total_readers]; 213 | struct thread_args writer_threads[total_writers]; 214 | InitalizeReadWriteLock(&rwlock); 215 | 216 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | struct read_write_lock 10 | { 11 | 12 | }; 13 | 14 | void InitalizeReadWriteLock(struct read_write_lock * rw); 15 | void ReaderLock(struct read_write_lock * rw); 16 | void ReaderUnlock(struct read_write_lock * rw); 17 | void WriterLock(struct read_write_lock * rw); 18 | void WriterUnlock(struct read_write_lock * rw); 19 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/rwlock/test-reader-pref.cpp: -------------------------------------------------------------------------------- 1 | #include "rwlock.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | long index; 8 | long *readerAcquireTime; 9 | long *readerReleaseTime; 10 | long *writerAcquireTime; 11 | long *writerReleaseTime; 12 | 13 | struct read_write_lock rwlock; 14 | pthread_spinlock_t spinlock; 15 | 16 | void *Reader(void* arg) 17 | { 18 | int threadNUmber = *((int *)arg); 19 | 20 | // Occupying the Lock 21 | ReaderLock(&rwlock); 22 | 23 | pthread_spin_lock(&spinlock); 24 | readerAcquireTime[threadNUmber] = index; 25 | index++; 26 | pthread_spin_unlock(&spinlock); 27 | 28 | // printf("Reader: %d has acquired the lock\n", threadNUmber); 29 | usleep(10000); 30 | 31 | pthread_spin_lock(&spinlock); 32 | readerReleaseTime[threadNUmber] = index; 33 | index++; 34 | pthread_spin_unlock(&spinlock); 35 | 36 | // Releasing the Lock 37 | ReaderUnlock(&rwlock); 38 | // printf("Reader: %d has released the lock\n",threadNUmber); 39 | } 40 | 41 | void *Writer(void* arg) 42 | { 43 | int threadNUmber = *((int *)arg); 44 | 45 | // Occupying the Lock 46 | WriterLock(&rwlock); 47 | 48 | pthread_spin_lock(&spinlock); 49 | writerAcquireTime[threadNUmber] = index; 50 | index++; 51 | pthread_spin_unlock(&spinlock); 52 | 53 | // printf("Writer: %d has acquired the lock\n",threadNUmber); 54 | usleep(10000); 55 | 56 | pthread_spin_lock(&spinlock); 57 | writerReleaseTime[threadNUmber] = index; 58 | index++; 59 | pthread_spin_unlock(&spinlock); 60 | 61 | // Releasing the Lock 62 | WriterUnlock(&rwlock); 63 | // printf("Writer: %d has released the lock\n",threadNUmber); 64 | } 65 | 66 | int main(int argc, char *argv[]) 67 | { 68 | 69 | int *threadNUmber; 70 | pthread_t *threads; 71 | 72 | InitalizeReadWriteLock(&rwlock); 73 | pthread_spin_init(&spinlock, 0); 74 | 75 | int read_num_threads; 76 | int write_num_threads; 77 | 78 | read_num_threads = atoi(argv[1]); 79 | write_num_threads = atoi(argv[2]); 80 | 81 | index = 0; 82 | readerAcquireTime = new long[read_num_threads*2]; 83 | readerReleaseTime = new long[read_num_threads*2]; 84 | writerAcquireTime = new long[write_num_threads]; 85 | writerReleaseTime = new long[write_num_threads]; 86 | 87 | int num_threads = 2*read_num_threads + write_num_threads; 88 | 89 | threads = (pthread_t*) malloc(num_threads * (sizeof(pthread_t))); 90 | 91 | int count = 0; 92 | for(int i=0; i 0) && (*max_reader_acquire_time > *min_reader_release_time)){ 160 | printf("Reader should not wait to acquire lock\n"); 161 | exit(0); 162 | } 163 | 164 | // All readers get lock before any writer 165 | if ((read_num_threads > 0) && (write_num_threads > 0) && (*min_writer_acquire_time < *max_reader_release_time)){ 166 | printf("All readers get lock before any writer\n"); 167 | exit(0); 168 | } 169 | 170 | // check if writer exited immediately 171 | for (int i = 0; i < write_num_threads; i++) 172 | if ((writerReleaseTime[i] - writerAcquireTime[i]) != 1){ 173 | printf("No reader/ writer is allowed when a writer holds lock\n"); 174 | exit(0); 175 | } 176 | 177 | // cout << *max_reader_acquire_time << endl; 178 | // cout << *min_reader_release_time << endl; 179 | // cout << *max_reader_release_time << endl; 180 | // cout << *min_writer_acquire_time << endl; 181 | printf("PASSED\n"); 182 | } -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/rwlock/test-rwlock.sh: -------------------------------------------------------------------------------- 1 | correct=0 2 | total=0 3 | 4 | echo "TESTSET: Running Testcases with reader preference" 5 | g++ test-reader-pref.cpp rwlock-reader-pref.cpp -o rwlock-reader-pref -lpthread 6 | 7 | echo "CASE1: Reader Preference with 5 reader and 1 writer" 8 | total=$((total+1)) 9 | out=`./rwlock-reader-pref 5 1` 10 | echo $out 11 | if [ "$out" = "PASSED" ]; then 12 | correct=$((correct+1)) 13 | fi 14 | 15 | echo "CASE2: Reader Preference with 5 reader and 3 writer" 16 | total=$((total+1)) 17 | out=`./rwlock-reader-pref 5 3` 18 | echo $out 19 | if [ "$out" = "PASSED" ]; then 20 | correct=$((correct+1)) 21 | fi 22 | 23 | echo "CASE3: Reader Preference with 5 reader and 5 writer" 24 | total=$((total+1)) 25 | out=`./rwlock-reader-pref 5 5` 26 | echo $out 27 | if [ "$out" = "PASSED" ]; then 28 | correct=$((correct+1)) 29 | fi 30 | 31 | echo "TESTSET: Running Testcases with writer preference" 32 | g++ test-writer-pref.cpp rwlock-writer-pref.cpp -o rwlock-writer-pref -lpthread 33 | 34 | echo "CASE1: Writer Preference with 5 reader and 1 writer" 35 | total=$((total+1)) 36 | out=`./rwlock-writer-pref 5 1` 37 | echo $out 38 | if [ "$out" = "PASSED" ]; then 39 | correct=$((correct+1)) 40 | fi 41 | 42 | 43 | echo "CASE2: Writer Preference with 5 reader and 3 writer" 44 | total=$((total+1)) 45 | out=`./rwlock-writer-pref 5 3` 46 | echo $out 47 | if [ "$out" = "PASSED" ]; then 48 | correct=$((correct+1)) 49 | fi 50 | 51 | echo "CASE3: Writer Preference with 5 reader and 5 writer" 52 | total=$((total+1)) 53 | out=`./rwlock-writer-pref 5 5` 54 | echo $out 55 | if [ "$out" = "PASSED" ]; then 56 | correct=$((correct+1)) 57 | fi 58 | 59 | echo "Test Cases Passed: $correct" 60 | echo "Test Cases Total: $total" 61 | 62 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/second.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | int counter = 0; 20 | pthread_mutex_t mutex1; 21 | 22 | void *increment(void* Args){ 23 | int* i = (int*) Args; 24 | printf("%d\n", i[0]); 25 | return NULL; 26 | } 27 | 28 | int main(){ 29 | int numberOfThreads = 5; 30 | 31 | for(int j = 0; j < 2; j++){ 32 | pthread_t *threads; 33 | threads = (pthread_t*) malloc(numberOfThreads * (sizeof(pthread_t))); 34 | 35 | for(int i = 0; i < numberOfThreads; i++){ 36 | pthread_create(threads+i, NULL, increment, (void*)&i); 37 | } 38 | for(int k = 0; k < numberOfThreads; k++){ 39 | pthread_join(threads[k], NULL); 40 | } 41 | } 42 | 43 | printf("%d\n", counter); 44 | } -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/second1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | volatile int C = 0; 20 | int num_threads = 5; 21 | pthread_mutex_t mutexx = PTHREAD_MUTEX_INITIALIZER; 22 | pthread_cond_t* cond = NULL; 23 | 24 | void *incr(void* Args) 25 | { 26 | int i = *(int*) Args; 27 | while (1) 28 | { 29 | pthread_mutex_lock(&mutexx); 30 | if(i != C) 31 | { 32 | pthread_cond_wait(&cond[i],&mutexx); 33 | } 34 | printf("%d ",i+1); 35 | 36 | if(C < num_threads-1) 37 | { 38 | C++; 39 | } 40 | else 41 | { 42 | C=0; 43 | } 44 | pthread_cond_signal(&cond[C]); 45 | pthread_mutex_unlock(&mutexx); 46 | } 47 | return NULL; 48 | } 49 | 50 | int main() 51 | { 52 | pthread_t *threads; 53 | volatile int i; 54 | threads = (pthread_t*) malloc(num_threads * (sizeof(pthread_t))); 55 | cond = (pthread_cond_t*) malloc(num_threads * (sizeof(pthread_cond_t))); 56 | 57 | for (int i = 0; i < num_threads; i++) { 58 | if (pthread_cond_init(&cond[i], NULL) != 0) { 59 | perror("pthread_cond_init() error"); 60 | exit(1); 61 | } 62 | } 63 | 64 | for (i = 0; i < num_threads; ++i) 65 | { 66 | int p = pthread_create(threads+i, NULL, incr, (void*)&i); 67 | cout<<"PTHREAD "< 2 | #include 3 | 4 | #define NTHREADS 100 5 | 6 | long int counter = 0; 7 | pthread_mutex_t pt = PTHREAD_MUTEX_INITIALIZER; 8 | 9 | // The thread starter function 10 | void *myThread() 11 | { 12 | for(int i=0; i<1000; i++){ 13 | pthread_mutex_lock(&pt); 14 | counter++; 15 | pthread_mutex_unlock(&pt); 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | // Create space for pthread variables 22 | pthread_t tid[NTHREADS]; 23 | 24 | for(int i=0; i 2 | #include 3 | 4 | #define NTHREADS 100 5 | 6 | long int counter = 0; 7 | 8 | // The thread starter function 9 | void *myThread() 10 | { 11 | for(int i=0; i<1000; i++) 12 | counter++; 13 | } 14 | 15 | int main() 16 | { 17 | // Create space for pthread variables 18 | pthread_t tid[NTHREADS]; 19 | 20 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define NUM_THREADS 3 11 | #define NUM_ITER 10 12 | 13 | void *justprint(void *data) 14 | { 15 | int thread_id = *((int *)data); 16 | 17 | for(int i=0; i < NUM_ITER; i++) 18 | { 19 | printf("This is thread %d\n", thread_id); 20 | } 21 | return 0; 22 | } 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | 27 | pthread_t mythreads[NUM_THREADS]; 28 | int mythread_id[NUM_THREADS]; 29 | 30 | 31 | for(int i =0; i < NUM_THREADS; i++) 32 | { 33 | mythread_id[i] = i; 34 | pthread_create(&mythreads[i], NULL, justprint, (void *)&mythread_id[i]); 35 | } 36 | 37 | for(int i =0; i < NUM_THREADS; i++) 38 | { 39 | pthread_join(mythreads[i], NULL); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/zemaphore/test-zem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "zemaphore.h" 11 | 12 | zem_t child; 13 | zem_t parent; 14 | 15 | void *justprint(void *data) 16 | { 17 | int thread_id = *((int *)data); 18 | zem_down(&child); 19 | printf("This is thread %d\n", thread_id); 20 | zem_up(&parent); 21 | return 0; 22 | } 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | 27 | pthread_t t1, t2; 28 | int t1id = 1, t2id = 2; 29 | 30 | zem_init(&child, 0); 31 | zem_init(&parent, 0); 32 | 33 | pthread_create(&t1, NULL, justprint, &t1id); 34 | pthread_create(&t2, NULL, justprint, &t2id); 35 | 36 | sleep(1); 37 | 38 | 39 | //in spite of sleep, this should print first 40 | printf("This is main thread. This should print first\n"); 41 | 42 | zem_up(&child); 43 | zem_down(&parent); 44 | printf("One thread has printed\n"); 45 | 46 | zem_up(&child); 47 | zem_down(&parent); 48 | printf("Second thread has printed\n"); 49 | 50 | pthread_join(t1, NULL); 51 | pthread_join(t2, NULL); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/zemaphore/test-zem.sh: -------------------------------------------------------------------------------- 1 | echo "Running test-zem.c" 2 | g++ test-zem.c zemaphore.c -o test-zem -lpthread 3 | ./test-zem 4 | echo 5 | echo "Running test-toggle.c" 6 | g++ test-toggle.c zemaphore.c -o test-toggle -lpthread 7 | ./test-toggle 8 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/zemaphore/zemaphore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "zemaphore.h" 9 | 10 | void zem_init(zem_t *s, int value) { 11 | 12 | } 13 | 14 | void zem_down(zem_t *s) { 15 | 16 | } 17 | 18 | void zem_up(zem_t *s) { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync-code/zemaphore/zemaphore.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct zemaphore { 4 | } zem_t; 5 | 6 | void zem_init(zem_t *, int); 7 | void zem_up(zem_t *); 8 | void zem_down(zem_t *); 9 | -------------------------------------------------------------------------------- /Endsems/Resources/pthreads-sync.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/pthreads-sync.pdf -------------------------------------------------------------------------------- /Endsems/Resources/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include //for fork() 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | sem_t s; 20 | int Sum = 0; 21 | 22 | void p1(key_t key, int create_pipe[2], int pid) 23 | { 24 | if(pid>0) 25 | { 26 | return; 27 | } 28 | int shmid = shmget(key, sizeof(int)*2, 0666 | IPC_CREAT); 29 | int *x; 30 | x = (int *)shmat(shmid, NULL, 0); 31 | int to_give[1]; 32 | to_give[0]= x[0]*100; 33 | write(create_pipe[1], to_give, sizeof(int)); 34 | } 35 | 36 | void p2(key_t key, int create_pipe[2], int pid) 37 | { 38 | if(pid>0) 39 | { 40 | return; 41 | } 42 | int shmid = shmget(key, sizeof(int)*2, 0666 | IPC_CREAT); 43 | int *x; 44 | x = (int *)shmat(shmid, NULL, 0); 45 | int to_take[1]; 46 | read(create_pipe[0], to_take, sizeof(int)); 47 | x[1] = 3*to_take[0]; 48 | } 49 | 50 | int main() 51 | { 52 | sem_init(&s, 0, 1); 53 | key_t key = ftok("shmfile",65); 54 | int shmid = shmget(key, sizeof(int)*2, 0666 | IPC_CREAT); 55 | int *x; 56 | x = (int*)shmat(shmid, NULL, 0); 57 | x[0] = 5; 58 | printf("%d\n",x[0]); 59 | 60 | int create_pipe[2]; 61 | int data; 62 | data = pipe(create_pipe); 63 | if (data == -1) 64 | { 65 | perror("pipe"); 66 | } 67 | 68 | p2(key, create_pipe, fork()); 69 | p1(key, create_pipe, fork()); 70 | 71 | 72 | wait(NULL); 73 | wait(NULL); 74 | 75 | printf("%d-%d\n",x[0],x[1]); 76 | shmdt(x); 77 | shmctl(shmid,IPC_RMID,NULL); 78 | } -------------------------------------------------------------------------------- /Endsems/Resources/threads-api.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/Resources/threads-api.pdf -------------------------------------------------------------------------------- /Endsems/threads-api.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Endsems/threads-api.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Rishit Saiya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Lab-1/180010027_lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-1/180010027_lab1.pdf -------------------------------------------------------------------------------- /Lab-1/Setting up Minix3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-1/Setting up Minix3.pdf -------------------------------------------------------------------------------- /Lab-2/180010027.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void printFork(char str[], int iter, int l){ 7 | if(iter <= l){ 8 | int idFork = fork(); 9 | if(idFork > 0){ // returned to parent/caller 10 | printf("%c %d\n", str[iter], idFork); 11 | sleep(rand()%4 + 1); 12 | printFork(str, iter+1, l); 13 | } 14 | } 15 | return; 16 | } 17 | 18 | int main(){ 19 | char strHW[] = "Hello World"; 20 | setbuf(stdout, NULL); 21 | printFork(strHW, 0, 10); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Lab-2/180010027_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/180010027_Report.pdf -------------------------------------------------------------------------------- /Lab-2/180010027_lab2_part1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/180010027_lab2_part1.zip -------------------------------------------------------------------------------- /Lab-2/180010027_lab2_part2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/180010027_lab2_part2.zip -------------------------------------------------------------------------------- /Lab-2/Laboratory 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/Laboratory 2.pdf -------------------------------------------------------------------------------- /Lab-2/Makefile: -------------------------------------------------------------------------------- 1 | hello: 180010027.c 2 | @clang 180010027.c && ./a.out -------------------------------------------------------------------------------- /Lab-2/images/PID.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/images/PID.png -------------------------------------------------------------------------------- /Lab-2/images/gmake_hello.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/images/gmake_hello.png -------------------------------------------------------------------------------- /Lab-2/images/log.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/images/log.png -------------------------------------------------------------------------------- /Lab-2/images/reboot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-2/images/reboot.png -------------------------------------------------------------------------------- /Lab-2/run.sh: -------------------------------------------------------------------------------- 1 | cp forkexit.c /usr/src/minix/servers/pm/forkexit.c 2 | cd /usr/src && make build MKUPDATE=yes >log.txt 2>log.txt -------------------------------------------------------------------------------- /Lab-3/180010027.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/180010027.pdf -------------------------------------------------------------------------------- /Lab-3/180010027_lab3.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/180010027_lab3.zip -------------------------------------------------------------------------------- /Lab-3/Laboratory 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/Laboratory 3.pdf -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/byte-unixbench-mod.zip -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/README.md: -------------------------------------------------------------------------------- 1 | ## mod at IIT Dharwad 2 | This is a modified version of the Unixbench suite. The original benchmarks were designed as "fixed duration" benchmarks: they each repeat a certain pattern over and over until a timer expires. The modification was done to make some of the benchmarks (namely, arith, fstime, pipe, spawn, syscall) as "fixed work": they do a certain fixed amount of work, regardless of the time it takes to do that work. 3 | 4 | # byte-unixbench 5 | 6 | **UnixBench** is the original BYTE UNIX benchmark suite, updated and revised by many people over the years. 7 | 8 | The purpose of UnixBench is to provide a basic indicator of the performance of a Unix-like system; hence, multiple 9 | tests are used to test various aspects of the system's performance. These test results are then compared to the 10 | scores from a baseline system to produce an index value, which is generally easier to handle than the raw scores. 11 | The entire set of index values is then combined to make an overall index for the system. 12 | 13 | Some very simple graphics tests are included to measure the 2D and 3D graphics performance of the system. 14 | 15 | Multi-CPU systems are handled. If your system has multiple CPUs, the default behaviour is to run the selected tests 16 | twice -- once with one copy of each test program running at a time, and once with N copies, where N is the number of 17 | CPUs. This is designed to allow you to assess: 18 | 19 | * the performance of your system when running a single task 20 | * the performance of your system when running multiple tasks 21 | * the gain from your system's implementation of parallel processing 22 | 23 | Do be aware that this is a system benchmark, not a CPU, RAM or disk benchmark. The results will depend not only on 24 | your hardware, but on your operating system, libraries, and even compiler. 25 | 26 | ## History 27 | 28 | **UnixBench** was first started in 1983 at Monash University, as a simple synthetic benchmarking application. It 29 | was then taken and expanded by **Byte Magazine**. Linux mods by Jon Tombs, and original authors Ben Smith, 30 | Rick Grehan, and Tom Yager. The tests compare Unix systems by comparing their results to a set of scores set 31 | by running the code on a benchmark system, which is a SPARCstation 20-61 (rated at 10.0). 32 | 33 | David C. Niemi maintained the program for quite some time, and made some major modifications and updates, 34 | and produced **UnixBench 4**. He later gave the program to Ian Smith to maintain. Ian subsequently made 35 | some major changes and revised it from version 4 to version 5. 36 | 37 | Thanks to Ian Smith for managing the release up to 5.1.3. As of the next release (5.2), [Anthony F. Voellm](https://github.com/voellm) is going to help maintain the code base. The releases will happen once there are enough pull requests to warrant a new release. 38 | 39 | The general process will be the following: 40 | 41 | * Open a bug announcing that a new release will happen. 42 | * Everything on the `dev` branch will be run. 43 | * Code will move from the `dev` branch into `main` and be tagged. Bug fix releases with increment the subversion and major functionality changes will increase the major version. 44 | 45 | ## Included Tests 46 | 47 | UnixBench consists of a number of individual tests that are targeted at specific areas. Here is a summary of what 48 | each test does: 49 | 50 | ### Dhrystone 51 | 52 | Developed by Reinhold Weicker in 1984. This benchmark is used to measure and compare the performance of computers. The test focuses on string handling, as there are no floating point operations. It is heavily influenced by hardware and software design, compiler and linker options, code optimization, cache memory, wait states, and integer data types. 53 | 54 | ### Whetstone 55 | 56 | This test measures the speed and efficiency of floating-point operations. This test contains several modules that are meant to represent a mix of operations typically performed in scientific applications. A wide variety of C functions including `sin`, `cos`, `sqrt`, `exp`, and `log` are used as well as integer and floating-point math operations, array accesses, conditional branches, and procedure calls. This test measure both integer and floating-point arithmetic. 57 | 58 | ### `execl` Throughput 59 | 60 | This test measures the number of `execl` calls that can be performed per second. `execl` is part of the exec family of functions that replaces the current process image with a new process image. It and many other similar commands are front ends for the function `execve()`. 61 | 62 | ### File Copy 63 | 64 | This measures the rate at which data can be transferred from one file to another, using various buffer sizes. The file read, write and copy tests capture the number of characters that can be written, read and copied in a specified time (default is 10 seconds). 65 | 66 | ### Pipe Throughput 67 | 68 | A pipe is the simplest form of communication between processes. Pipe throughput is the number of times (per second) a process can write 512 bytes to a pipe and read them back. The pipe throughput test has no real counterpart in real-world programming. 69 | 70 | ### Pipe-based Context Switching 71 | 72 | This test measures the number of times two processes can exchange an increasing integer through a pipe. The pipe-based context switching test is more like a real-world application. The test program spawns a child process with which it carries on a bi-directional pipe conversation. 73 | 74 | ### Process Creation 75 | 76 | This test measure the number of times a process can fork and reap a child that immediately exits. Process creation refers to actually creating process control blocks and memory allocations for new processes, so this applies directly to memory bandwidth. Typically, this benchmark would be used to compare various implementations of operating system process creation calls. 77 | 78 | ### Shell Scripts 79 | 80 | The shells scripts test measures the number of times per minute a process can start and reap a set of one, two, four and eight concurrent copies of a shell scripts where the shell script applies a series of transformation to a data file. 81 | 82 | ### System Call Overhead 83 | 84 | This estimates the cost of entering and leaving the operating system kernel, i.e., the overhead for performing a system call. It consists of a simple program repeatedly calling the `getpid` (which returns the process id of the calling process) system call. The time to execute such calls is used to estimate the cost of entering and exiting the kernel. 85 | 86 | ### Graphical Tests 87 | 88 | Both 2D and 3D graphical tests are provided; at the moment, the 3D suite in particular is very limited, consisting of the `ubgears` program. These tests are intended to provide a very rough idea of the system's 2D and 3D graphics performance. Bear in mind, of course, that the reported performance will depend not only on hardware, but on whether your system has appropriate drivers for it. 89 | 90 | # License 91 | 92 | This project is released under the [GPL v2](LICENSE.txt) license. 93 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/WRITING_TESTS: -------------------------------------------------------------------------------- 1 | Writing a Test 2 | ============== 3 | 4 | Writing a test program is pretty easy. Basically, a test is configured via 5 | a monster array in the Run script, which specifics (among other things) the 6 | program to execute and the parameters to pass it. 7 | 8 | The test itself is simply a program which is given the optional parameters 9 | on the command line, and produces logging data on stdout and its results on 10 | stderr. 11 | 12 | 13 | ============================================================================ 14 | 15 | Test Configuration 16 | ================== 17 | 18 | In Run, all tests are named in the "$testList" array. This names the 19 | individual tests, and also sets up aliases for groups of tests, eg. "index". 20 | 21 | The test specifications are in the "$testParams" array. This contains the 22 | details of each individual test as a hash. The fields in the hash are: 23 | 24 | * "logmsg": the full name to display for this test. 25 | * "cat": the category this test belongs to; must be configured 26 | in $testCats. 27 | * "prog": the name of the program to execute; defaults to the name of 28 | the benchmark. 29 | * "repeat": number of passes to run; either 'short' (the default), 30 | 'long', or 'single'. For 'short' and 'long', the actual numbers of 31 | passes are given by $shortIterCount and $longIterCount, which are 32 | configured at the top of the script or by the "-i" flag. 'single' 33 | means just run one pass; this should be used for test which do their 34 | own multi-pass handling internally. 35 | * "stdout": non-0 to add the test's stdout to the log file; defaults to 1. 36 | Set to 0 for tests that are too wordy. 37 | * "stdin": name of a file to send to the program's stdin; default null. 38 | * "options": options to be put on the program's command line; default null. 39 | 40 | 41 | ============================================================================ 42 | 43 | Output Format 44 | ============= 45 | 46 | The results on stderr take the form of a line header and fields, separated 47 | by "|" characters. A result line can be one of: 48 | 49 | COUNT|score|timebase|label 50 | TIME|seconds 51 | ERROR|message 52 | 53 | Any other text on stderr is treated as if it were: 54 | 55 | ERROR|text 56 | 57 | Any output to stdout is placed in a log file, and can be used for debugging. 58 | 59 | COUNT 60 | ----- 61 | 62 | The COUNT line is the line used to report a test score. 63 | 64 | * "score" is the result, typically the number of loops performed during 65 | the run 66 | * "timebase" is the time base used for the final report to the user. A 67 | value of 1 reports the score as is; a value of 60, for example, divides 68 | the time taken by 60 to get loops per minute. Atimebase of zero indicates 69 | that the score is already a rate, ie. a count of things per second. 70 | * "label" is the label to use for the score; like "lps" (loops per 71 | second), etc. 72 | 73 | TIME 74 | ---- 75 | 76 | The TIME line is optionally used to report the time taken. The Run script 77 | normally measures this, but if your test has signifant overhead outside the 78 | actual test loop, you should use TIME to report the time taken for the actual 79 | test. The argument is the time in seconds in floating-point. 80 | 81 | ERROR 82 | ----- 83 | 84 | The argument is an error message; this will abort the benchmarking run and 85 | display the message. 86 | 87 | Any output to stderr which is not a formatted line will be treated as an 88 | error message, so use of ERROR is optional. 89 | 90 | 91 | ============================================================================ 92 | 93 | Test Examples 94 | ============= 95 | 96 | Iteration Count 97 | --------------- 98 | 99 | The simplest thing is to count the number of loops executed in a given time; 100 | see eg. arith.c. The utlilty functions in timeit.c can be used to implement 101 | the fixed time interval, which is generally passed in on the command line. 102 | 103 | The result is reported simply as the number of iterations completed: 104 | 105 | fprintf(stderr,"COUNT|%lu|1|lps\n", iterations); 106 | 107 | The bnenchmark framework will measure the time taken itself. If the test 108 | code has significant overhead (eg. a "pump-priming" pass), then you should 109 | explicitly report the time taken for the test by adding a line like this: 110 | 111 | fprintf(stderr, "TIME|%.1f\n", seconds); 112 | 113 | If you want results reported as loops per minute, then set timebase to 60: 114 | 115 | fprintf(stderr,"COUNT|%lu|60|lpm\n", iterations); 116 | 117 | Note that this only affects the final report; all times passed to or 118 | from the test are still in seconds. 119 | 120 | Rate 121 | ---- 122 | 123 | The other technique is to calculate the rate (things per second) in the test, 124 | and report that directly. To do this, just set timebase to 0: 125 | 126 | fprintf(stderr, "COUNT|%ld|0|KBps\n", kbytes_per_sec); 127 | 128 | Again, you can use TIME to explicitly report the time taken: 129 | 130 | fprintf(stderr, "TIME|%.1f\n", end - start); 131 | 132 | but this isn't so important since you've already calculated the rate. 133 | 134 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/pgms/index.base: -------------------------------------------------------------------------------- 1 | # Baseline benchmark scores, used for calculating index results. 2 | 3 | # Scores from "George", a SPARCstation 20-61. 4 | dhry2reg|10|lps|116700|116700|2 5 | whetstone-double|10|MWIPS|55.0|55.0|2 6 | execl|20|lps|43.0|43.0|1 7 | fstime|20|KBps|3960|3960|1 8 | fsbuffer|20|KBps|1655|1655|1 9 | fsdisk|20|KBps|5800|5800|1 10 | pipe|10|lps|12440|12440|2 11 | context1|10|lps|4000|4000|2 12 | spawn|20|lps|126|126|1 13 | shell8|60|lpm|6|6|1 14 | syscall|10|lps|15000|15000|2 15 | 16 | # The shell1 test was added to the index in 5.0, and this baseline score 17 | # was extrapolated to roughly match George's performance. 18 | shell1|60|lpm|42.4|42.4|1 19 | 20 | # The 2D baseline scores were derived from a test run on an HP Compaq nc8430 21 | # with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly 22 | # common modern adaptor with 3D. The baseline scores here are then 23 | # 1/66.6 of the values from that run, to bring them roughly in line with 24 | # George. (The HP has an index score of 666.6 single-process.) 25 | 2d-rects|3|score|15|15|1 26 | #2d-lines|3|score|15|15|1 27 | #2d-circle|3|score|15|15|1 28 | 2d-ellipse|3|score|15|15|1 29 | #2d-shapes|3|score|15|15|1 30 | 2d-aashapes|3|score|15|15|1 31 | #2d-polys|3|score|15|15|1 32 | 2d-text|3|score|15|15|1 33 | 2d-blit|3|score|15|15|1 34 | 2d-window|3|score|15|15|1 35 | 36 | # The gears test score is derived from a test run on an HP Compaq nc8430 37 | # with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly 38 | # common modern adaptor with 3D. The baseline scores here are then 39 | # 1/66.6 of the values from that run, to bring them roughly in line with 40 | # George. 41 | ubgears|20|fps|33.4|33.4|3 42 | 43 | # The grep and sysexec tests were added in 5.1.1; they are not index tests, 44 | # but these baseline scores were added for convenience. 45 | grep|30|lpm|1|1|3 46 | sysexec|10|lps|25|25|10 47 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/pgms/multi.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ############################################################################### 3 | # The BYTE UNIX Benchmarks - Release 3 4 | # Module: multi.sh SID: 3.4 5/15/91 19:30:24 5 | # 6 | ############################################################################### 7 | # Bug reports, patches, comments, suggestions should be sent to: 8 | # 9 | # Ben Smith or Rick Grehan at BYTE Magazine 10 | # ben@bytepb.UUCP rick_g@bytepb.UUCP 11 | # 12 | ############################################################################### 13 | # Modification Log: 14 | # 15 | ############################################################################### 16 | ID="@(#)multi.sh:3.4 -- 5/15/91 19:30:24"; 17 | instance=1 18 | while [ $instance -le $1 ]; do 19 | /bin/sh "$UB_BINDIR/tst.sh" & 20 | instance=`expr $instance + 1` 21 | done 22 | wait 23 | 24 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/pgms/tst.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ############################################################################### 3 | # The BYTE UNIX Benchmarks - Release 3 4 | # Module: tst.sh SID: 3.4 5/15/91 19:30:24 5 | # 6 | ############################################################################### 7 | # Bug reports, patches, comments, suggestions should be sent to: 8 | # 9 | # Ben Smith or Rick Grehan at BYTE Magazine 10 | # ben@bytepb.UUCP rick_g@bytepb.UUCP 11 | # 12 | ############################################################################### 13 | # Modification Log: 14 | # 15 | ############################################################################### 16 | ID="@(#)tst.sh:3.4 -- 5/15/91 19:30:24"; 17 | sort >sort.$$ od.$$ 19 | grep the sort.$$ | tee grep.$$ | wc > wc.$$ 20 | rm sort.$$ grep.$$ od.$$ wc.$$ 21 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/pgms/unixbench.logo: -------------------------------------------------------------------------------- 1 | 2 | # # # # # # # ##### ###### # # #### # # 3 | # # ## # # # # # # # ## # # # # # 4 | # # # # # # ## ##### ##### # # # # ###### 5 | # # # # # # ## # # # # # # # # # 6 | # # # ## # # # # # # # ## # # # # 7 | #### # # # # # ##### ###### # # #### # # 8 | 9 | Version 5.1.3 Based on the Byte Magazine Unix Benchmark 10 | 11 | Multi-CPU version Version 5 revisions by Ian Smith, 12 | Sunnyvale, CA, USA 13 | January 13, 2011 johantheghost at yahoo period com 14 | 15 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/arith.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: arith.c SID: 3.3 5/15/91 19:30:19 5 | * 6 | ******************************************************************************* 7 | * Bug reports, patches, comments, suggestions should be sent to: 8 | * 9 | * Ben Smith, Rick Grehan or Tom Yager 10 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 11 | * 12 | ******************************************************************************* 13 | * Modification Log: 14 | * May 12, 1989 - modified empty loops to avoid nullifying by optimizing 15 | * compilers 16 | * August 28, 1990 - changed timing relationship--now returns total number 17 | * of iterations (ty) 18 | * November 9, 1990 - made changes suggested by Keith Cantrell 19 | * (digi!kcantrel) to defeat optimization 20 | * to non-existence 21 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 22 | * Andy Kahn 23 | * 24 | ******************************************************************************/ 25 | 26 | char SCCSid[] = "@(#) @(#)arith.c:3.3 -- 5/15/91 19:30:19"; 27 | /* 28 | * arithmetic test 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "timeit.c" 35 | 36 | int dumb_stuff(int); 37 | 38 | volatile unsigned long iter; 39 | 40 | /* this function is called when the alarm expires */ 41 | void report() 42 | { 43 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 44 | exit(0); 45 | } 46 | 47 | int main(argc, argv) 48 | int argc; 49 | char *argv[]; 50 | { 51 | int duration; 52 | int result = 0; 53 | /* 54 | if (argc != 2) { 55 | printf("Usage: %s duration\n", argv[0]); 56 | exit(1); 57 | } 58 | 59 | duration = atoi(argv[1]); 60 | */ 61 | /* set up alarm call */ 62 | iter = 0; /* init iteration count */ 63 | /* wake_me(duration, report); 64 | */ 65 | /* this loop will be interrupted by the alarm call */ 66 | while (iter <= 100000000l) 67 | { 68 | /* in switching to time-based (instead of iteration-based), 69 | the following statement was added. It should not skew 70 | the timings too much--there was an increment and test 71 | in the "while" expression above. The only difference is 72 | that now we're incrementing a long instead of an int. (ty) */ 73 | ++iter; 74 | /* the loop calls a function to insure that something is done 75 | the results of the function are fed back in (just so they 76 | they won't be thrown away. A loop with 77 | unused assignments may get optimized out of existence */ 78 | result = dumb_stuff(result); 79 | } 80 | } 81 | 82 | 83 | /************************** dumb_stuff *******************/ 84 | int dumb_stuff(i) 85 | int i; 86 | { 87 | #ifndef arithoh 88 | datum x, y, z; 89 | z = 0; 90 | #endif 91 | /* 92 | * 101 93 | * sum i*i/(i*i-1) 94 | * i=2 95 | */ 96 | /* notice that the i value is always reset by the loop */ 97 | for (i=2; i<=101; i++) 98 | { 99 | #ifndef arithoh 100 | x = i; 101 | y = x*x; 102 | z += y/(y-1); 103 | } 104 | return(x+y+z); 105 | #else 106 | } 107 | return(0); 108 | #endif 109 | } 110 | 111 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/context1.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: context1.c SID: 3.3 5/15/91 19:30:18 5 | * 6 | ******************************************************************************* 7 | * Bug reports, patches, comments, suggestions should be sent to: 8 | * 9 | * Ben Smith, Rick Grehan or Tom Yager 10 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 11 | * 12 | ******************************************************************************* 13 | * Modification Log: 14 | * $Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta $ 15 | * August 28, 1990 - changed timing routines--now returns total number of 16 | * iterations in specified time period 17 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 18 | * Andy Kahn 19 | * 20 | ******************************************************************************/ 21 | char SCCSid[] = "@(#) @(#)context1.c:3.3 -- 5/15/91 19:30:18"; 22 | /* 23 | * Context switching via synchronized unbuffered pipe i/o 24 | * 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include "timeit.c" 31 | 32 | unsigned long iter; 33 | 34 | void report() 35 | { 36 | fprintf(stderr, "COUNT|%lu|1|lps\n", iter); 37 | exit(0); 38 | } 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int duration; 45 | unsigned long check; 46 | int p1[2], p2[2]; 47 | ssize_t ret; 48 | 49 | if (argc != 2) { 50 | fprintf(stderr, "Usage: context duration\n"); 51 | exit(1); 52 | } 53 | 54 | duration = atoi(argv[1]); 55 | 56 | /* set up alarm call */ 57 | iter = 0; 58 | wake_me(duration, report); 59 | signal(SIGPIPE, SIG_IGN); 60 | 61 | if (pipe(p1) || pipe(p2)) { 62 | perror("pipe create failed"); 63 | exit(1); 64 | } 65 | 66 | if (fork()) { /* parent process */ 67 | /* master, write p1 & read p2 */ 68 | close(p1[0]); close(p2[1]); 69 | while (1) { 70 | if ((ret = write(p1[1], (char *)&iter, sizeof(iter))) != sizeof(iter)) { 71 | if ((ret == -1) && (errno == EPIPE)) { 72 | alarm(0); 73 | report(); /* does not return */ 74 | } 75 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 76 | perror("master write failed"); 77 | exit(1); 78 | } 79 | if ((ret = read(p2[0], (char *)&check, sizeof(check))) != sizeof(check)) { 80 | if ((ret == 0)) { /* end-of-stream */ 81 | alarm(0); 82 | report(); /* does not return */ 83 | } 84 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 85 | perror("master read failed"); 86 | exit(1); 87 | } 88 | if (check != iter) { 89 | fprintf(stderr, "Master sync error: expect %lu, got %lu\n", 90 | iter, check); 91 | exit(2); 92 | } 93 | iter++; 94 | } 95 | } 96 | else { /* child process */ 97 | /* slave, read p1 & write p2 */ 98 | close(p1[1]); close(p2[0]); 99 | while (1) { 100 | if ((ret = read(p1[0], (char *)&check, sizeof(check))) != sizeof(check)) { 101 | if ((ret == 0)) { /* end-of-stream */ 102 | alarm(0); 103 | report(); /* does not return */ 104 | } 105 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 106 | perror("slave read failed"); 107 | exit(1); 108 | } 109 | if (check != iter) { 110 | fprintf(stderr, "Slave sync error: expect %lu, got %lu\n", 111 | iter, check); 112 | exit(2); 113 | } 114 | if ((ret = write(p2[1], (char *)&iter, sizeof(iter))) != sizeof(check)) { 115 | if ((ret == -1) && (errno == EPIPE)) { 116 | alarm(0); 117 | report(); /* does not return */ 118 | } 119 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 120 | perror("slave write failed"); 121 | exit(1); 122 | } 123 | iter++; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/dhry_2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: dhry_2.c SID: 3.4 5/15/91 19:30:22 4 | * 5 | ***************************************************************************** 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ***************************************************************************** 12 | * Modification Log: 13 | * 10/22/97 - code cleanup to remove ANSI C compiler warnings 14 | * Andy Kahn 15 | * 16 | * Adapted from: 17 | * 18 | * "DHRYSTONE" Benchmark Program 19 | * ----------------------------- 20 | * 21 | * **** WARNING **** See warning in n.dhry_1.c 22 | * 23 | * Version: C, Version 2.1 24 | * 25 | * File: dhry_2.c (part 3 of 3) 26 | * 27 | * Date: May 25, 1988 28 | * 29 | * Author: Reinhold P. Weicker 30 | * 31 | ****************************************************************************/ 32 | /* SCCSid is defined in dhry_1.c */ 33 | 34 | #include 35 | #include "dhry.h" 36 | 37 | #ifndef REG 38 | #define REG 39 | /* REG becomes defined as empty */ 40 | /* i.e. no register variables */ 41 | #endif 42 | 43 | extern int Int_Glob; 44 | extern char Ch_1_Glob; 45 | 46 | void Proc_6(Enumeration, Enumeration *); 47 | void Proc_7(One_Fifty, One_Fifty, One_Fifty *); 48 | void Proc_8(Arr_1_Dim, Arr_2_Dim, int, int); 49 | Enumeration Func_1(Capital_Letter, Capital_Letter); 50 | Boolean Func_2(Str_30, Str_30); 51 | Boolean Func_3(Enumeration); 52 | 53 | void Proc_6 (Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par) 54 | /* executed once */ 55 | /* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */ 56 | { 57 | *Enum_Ref_Par = Enum_Val_Par; 58 | if (! Func_3 (Enum_Val_Par)) 59 | /* then, not executed */ 60 | *Enum_Ref_Par = Ident_4; 61 | switch (Enum_Val_Par) 62 | { 63 | case Ident_1: 64 | *Enum_Ref_Par = Ident_1; 65 | break; 66 | case Ident_2: 67 | if (Int_Glob > 100) 68 | /* then */ 69 | *Enum_Ref_Par = Ident_1; 70 | else *Enum_Ref_Par = Ident_4; 71 | break; 72 | case Ident_3: /* executed */ 73 | *Enum_Ref_Par = Ident_2; 74 | break; 75 | case Ident_4: break; 76 | case Ident_5: 77 | *Enum_Ref_Par = Ident_3; 78 | break; 79 | } /* switch */ 80 | } /* Proc_6 */ 81 | 82 | void Proc_7 (Int_1_Par_Val, Int_2_Par_Val, Int_Par_Ref) 83 | One_Fifty Int_1_Par_Val; 84 | One_Fifty Int_2_Par_Val; 85 | One_Fifty *Int_Par_Ref; 86 | /**********************************************/ 87 | /* executed three times */ 88 | /* first call: Int_1_Par_Val == 2, Int_2_Par_Val == 3, */ 89 | /* Int_Par_Ref becomes 7 */ 90 | /* second call: Int_1_Par_Val == 10, Int_2_Par_Val == 5, */ 91 | /* Int_Par_Ref becomes 17 */ 92 | /* third call: Int_1_Par_Val == 6, Int_2_Par_Val == 10, */ 93 | /* Int_Par_Ref becomes 18 */ 94 | { 95 | One_Fifty Int_Loc; 96 | 97 | Int_Loc = Int_1_Par_Val + 2; 98 | *Int_Par_Ref = Int_2_Par_Val + Int_Loc; 99 | } /* Proc_7 */ 100 | 101 | 102 | void Proc_8 (Arr_1_Par_Ref, Arr_2_Par_Ref, Int_1_Par_Val, Int_2_Par_Val) 103 | /*********************************************************************/ 104 | /* executed once */ 105 | /* Int_Par_Val_1 == 3 */ 106 | /* Int_Par_Val_2 == 7 */ 107 | Arr_1_Dim Arr_1_Par_Ref; 108 | Arr_2_Dim Arr_2_Par_Ref; 109 | int Int_1_Par_Val; 110 | int Int_2_Par_Val; 111 | { 112 | REG One_Fifty Int_Index; 113 | REG One_Fifty Int_Loc; 114 | 115 | Int_Loc = Int_1_Par_Val + 5; 116 | Arr_1_Par_Ref [Int_Loc] = Int_2_Par_Val; 117 | Arr_1_Par_Ref [Int_Loc+1] = Arr_1_Par_Ref [Int_Loc]; 118 | Arr_1_Par_Ref [Int_Loc+30] = Int_Loc; 119 | for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index) 120 | Arr_2_Par_Ref [Int_Loc] [Int_Index] = Int_Loc; 121 | Arr_2_Par_Ref [Int_Loc] [Int_Loc-1] += 1; 122 | Arr_2_Par_Ref [Int_Loc+20] [Int_Loc] = Arr_1_Par_Ref [Int_Loc]; 123 | Int_Glob = 5; 124 | } /* Proc_8 */ 125 | 126 | 127 | Enumeration Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val) 128 | /*************************************************/ 129 | /* executed three times */ 130 | /* first call: Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R' */ 131 | /* second call: Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C' */ 132 | /* third call: Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C' */ 133 | { 134 | Capital_Letter Ch_1_Loc; 135 | Capital_Letter Ch_2_Loc; 136 | 137 | Ch_1_Loc = Ch_1_Par_Val; 138 | Ch_2_Loc = Ch_1_Loc; 139 | if (Ch_2_Loc != Ch_2_Par_Val) 140 | /* then, executed */ 141 | return (Ident_1); 142 | else /* not executed */ 143 | { 144 | Ch_1_Glob = Ch_1_Loc; 145 | return (Ident_2); 146 | } 147 | } /* Func_1 */ 148 | 149 | 150 | 151 | Boolean Func_2 (Str_1_Par_Ref, Str_2_Par_Ref) 152 | /*************************************************/ 153 | /* executed once */ 154 | /* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */ 155 | /* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */ 156 | 157 | Str_30 Str_1_Par_Ref; 158 | Str_30 Str_2_Par_Ref; 159 | { 160 | REG One_Thirty Int_Loc; 161 | Capital_Letter Ch_Loc; 162 | 163 | Ch_Loc = 'A'; 164 | Int_Loc = 2; 165 | while (Int_Loc <= 2) /* loop body executed once */ 166 | if (Func_1 (Str_1_Par_Ref[Int_Loc], 167 | Str_2_Par_Ref[Int_Loc+1]) == Ident_1) 168 | /* then, executed */ 169 | { 170 | Ch_Loc = 'A'; 171 | Int_Loc += 1; 172 | } /* if, while */ 173 | if (Ch_Loc >= 'W' && Ch_Loc < 'Z') 174 | /* then, not executed */ 175 | Int_Loc = 7; 176 | if (Ch_Loc == 'R') 177 | /* then, not executed */ 178 | return (true); 179 | else /* executed */ 180 | { 181 | if (strcmp (Str_1_Par_Ref, Str_2_Par_Ref) > 0) 182 | /* then, not executed */ 183 | { 184 | Int_Loc += 7; 185 | Int_Glob = Int_Loc; 186 | return (true); 187 | } 188 | else /* executed */ 189 | return (false); 190 | } /* if Ch_Loc */ 191 | } /* Func_2 */ 192 | 193 | 194 | Boolean Func_3 (Enum_Par_Val) 195 | /***************************/ 196 | /* executed once */ 197 | /* Enum_Par_Val == Ident_3 */ 198 | Enumeration Enum_Par_Val; 199 | { 200 | Enumeration Enum_Loc; 201 | 202 | Enum_Loc = Enum_Par_Val; 203 | if (Enum_Loc == Ident_3) 204 | /* then, executed */ 205 | return (true); 206 | else /* not executed */ 207 | return (false); 208 | } /* Func_3 */ 209 | 210 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/execl.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: execl.c SID: 3.3 5/15/91 19:30:19 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: execl.c,v 3.5 87/06/22 15:37:08 kjmcdonell Beta $ 14 | * August 28, 1990 - Modified timing routines 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | /* 20 | * Execing 21 | * 22 | */ 23 | char SCCSid[] = "@(#) @(#)execl.c:3.3 -- 5/15/91 19:30:19"; 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | char bss[8*1024]; /* something worthwhile */ 31 | 32 | #define main dummy 33 | 34 | #include "big.c" /* some real code */ 35 | 36 | #undef main 37 | 38 | /* added by BYTE */ 39 | char *getenv(); 40 | 41 | 42 | int main(argc, argv) /* the real program */ 43 | int argc; 44 | char *argv[]; 45 | { 46 | unsigned long iter = 0; 47 | char *ptr; 48 | char *fullpath; 49 | int duration; 50 | char count_str[12], start_str[24], path_str[256], *dur_str; 51 | time_t start_time, this_time; 52 | 53 | #ifdef DEBUG 54 | int count; 55 | for(count = 0; count < argc; ++ count) 56 | printf("%s ",argv[count]); 57 | printf("\n"); 58 | #endif 59 | if (argc < 2) 60 | { 61 | fprintf(stderr, "Usage: %s duration\n", argv[0]); 62 | exit(1); 63 | } 64 | 65 | 66 | duration = atoi(argv[1]); 67 | if (duration > 0) 68 | /* the first invocation */ 69 | { 70 | dur_str = argv[1]; 71 | if((ptr = getenv("UB_BINDIR")) != NULL) 72 | sprintf(path_str,"%s/execl",ptr); 73 | fullpath=path_str; 74 | time(&start_time); 75 | } 76 | else /* one of those execl'd invocations */ 77 | { 78 | /* real duration follow the phoney null duration */ 79 | duration = atoi(argv[2]); 80 | dur_str = argv[2]; 81 | iter = (unsigned long)atoi(argv[3]); /* where are we now ? */ 82 | sscanf(argv[4], "%lu", (unsigned long *) &start_time); 83 | fullpath = argv[0]; 84 | } 85 | 86 | sprintf(count_str, "%lu", ++iter); /* increment the execl counter */ 87 | sprintf(start_str, "%lu", (unsigned long) start_time); 88 | time(&this_time); 89 | if (this_time - start_time >= duration) { /* time has run out */ 90 | fprintf(stderr, "COUNT|%lu|1|lps\n", iter); 91 | exit(0); 92 | } 93 | execl(fullpath, fullpath, "0", dur_str, count_str, start_str, (void *) 0); 94 | fprintf(stderr, "Exec failed at iteration %lu\n", iter); 95 | perror("Reason"); 96 | exit(1); 97 | } 98 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/hanoi.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: hanoi.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $ 14 | * August 28, 1990 - Modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)hanoi.c:3.3 -- 5/15/91 19:30:20"; 20 | 21 | #define other(i,j) (6-(i+j)) 22 | 23 | #include 24 | #include 25 | #include "timeit.c" 26 | 27 | void mov(int n, int f, int t); 28 | 29 | unsigned long iter = 0; 30 | int num[4]; 31 | long cnt; 32 | 33 | void report() 34 | { 35 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 36 | exit(0); 37 | } 38 | 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int disk=10, /* default number of disks */ 45 | duration; 46 | 47 | if (argc < 2) { 48 | fprintf(stderr,"Usage: %s duration [disks]\n", argv[0]); 49 | exit(1); 50 | } 51 | duration = atoi(argv[1]); 52 | if(argc > 2) disk = atoi(argv[2]); 53 | num[1] = disk; 54 | 55 | wake_me(duration, report); 56 | 57 | while(1) { 58 | mov(disk,1,3); 59 | iter++; 60 | } 61 | 62 | exit(0); 63 | } 64 | 65 | void mov(int n, int f, int t) 66 | { 67 | int o; 68 | if(n == 1) { 69 | num[f]--; 70 | num[t]++; 71 | return; 72 | } 73 | o = other(f,t); 74 | mov(n-1,f,o); 75 | mov(1,f,t); 76 | mov(n-1,o,t); 77 | } 78 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/looper.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 1 3 | * Module: looper.c SID: 1.4 5/15/91 19:30:22 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith or Tom Yager at BYTE Magazine 9 | * ben@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * 14 | * February 25, 1991 -- created (Ben S.) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)looper.c:1.4 -- 5/15/91 19:30:22"; 20 | /* 21 | * Shell Process creation 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | char *cmd_argv[28]; 32 | int cmd_argc; 33 | 34 | void report(void) 35 | { 36 | fprintf(stderr,"COUNT|%lu|60|lpm\n", iter); 37 | exit(0); 38 | } 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int slave, count, duration; 45 | int status; 46 | 47 | if (argc < 2) 48 | { 49 | fprintf(stderr,"Usage: %s duration command [args..]\n", argv[0]); 50 | fprintf(stderr," duration in seconds\n"); 51 | exit(1); 52 | } 53 | 54 | if((duration = atoi(argv[1])) < 1) 55 | { 56 | fprintf(stderr,"Usage: %s duration command [arg..]\n", argv[0]); 57 | fprintf(stderr," duration in seconds\n"); 58 | exit(1); 59 | } 60 | 61 | /* get command */ 62 | cmd_argc=argc-2; 63 | for( count=2;count < argc; ++count) 64 | cmd_argv[count-2]=argv[count]; 65 | #ifdef DEBUG 66 | printf("<<%s>>",cmd_argv[0]); 67 | for(count=1;count < cmd_argc; ++count) 68 | printf(" <%s>", cmd_argv[count]); 69 | putchar('\n'); 70 | exit(0); 71 | #endif 72 | 73 | iter = 0; 74 | wake_me(duration, report); 75 | 76 | while (1) 77 | { 78 | if ((slave = fork()) == 0) 79 | { /* execute command */ 80 | execvp(cmd_argv[0],cmd_argv); 81 | exit(99); 82 | } 83 | else if (slave < 0) 84 | { 85 | /* woops ... */ 86 | fprintf(stderr,"Fork failed at iteration %lu\n", iter); 87 | perror("Reason"); 88 | exit(2); 89 | } 90 | else 91 | /* master */ 92 | wait(&status); 93 | if (status == 99 << 8) 94 | { 95 | fprintf(stderr, "Command \"%s\" didn't exec\n", cmd_argv[0]); 96 | exit(2); 97 | } 98 | else if (status != 0) 99 | { 100 | fprintf(stderr,"Bad wait status: 0x%x\n", status); 101 | exit(2); 102 | } 103 | iter++; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/pipe.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: pipe.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: pipe.c,v 3.5 87/06/22 14:32:36 kjmcdonell Beta $ 14 | * August 29, 1990 - modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)pipe.c:3.3 -- 5/15/91 19:30:20"; 20 | /* 21 | * pipe -- test single process pipe throughput (no context switching) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | 32 | void report() 33 | { 34 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 35 | exit(0); 36 | } 37 | 38 | int main(argc, argv) 39 | int argc; 40 | char *argv[]; 41 | { 42 | char buf[512]; 43 | int pvec[2], duration; 44 | 45 | /* if (argc != 2) { 46 | fprintf(stderr,"Usage: %s duration\n", argv[0]); 47 | exit(1); 48 | } 49 | 50 | duration = atoi(argv[1]); 51 | */ 52 | pipe(pvec); 53 | 54 | // wake_me(duration, report); 55 | iter = 0; 56 | 57 | while (iter <= 1000000l) { 58 | if (write(pvec[1], buf, sizeof(buf)) != sizeof(buf)) { 59 | if ((errno != EINTR) && (errno != 0)) 60 | fprintf(stderr,"write failed, error %d\n", errno); 61 | } 62 | if (read(pvec[0], buf, sizeof(buf)) != sizeof(buf)) { 63 | if ((errno != EINTR) && (errno != 0)) 64 | fprintf(stderr,"read failed, error %d\n", errno); 65 | } 66 | iter++; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/spawn.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: spawn.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yagerat BYTE Magazine 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: spawn.c,v 3.4 87/06/22 14:32:48 kjmcdonell Beta $ 14 | * August 29, 1990 - Modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)spawn.c:3.3 -- 5/15/91 19:30:20"; 20 | /* 21 | * Process creation 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | 32 | void report() 33 | { 34 | fprintf(stderr,"COUNT|%lu|1|lps\n", iter); 35 | exit(0); 36 | } 37 | 38 | int main(argc, argv) 39 | int argc; 40 | char *argv[]; 41 | { 42 | int slave, duration; 43 | int status; 44 | 45 | /* if (argc != 2) { 46 | fprintf(stderr,"Usage: %s duration \n", argv[0]); 47 | exit(1); 48 | } 49 | 50 | duration = atoi(argv[1]); 51 | */ 52 | iter = 0; 53 | // wake_me(duration, report); 54 | 55 | while (iter <= 10000l) { 56 | if ((slave = fork()) == 0) { 57 | /* slave .. boring */ 58 | #if debug 59 | printf("fork OK\n"); 60 | #endif 61 | /* kill it right away */ 62 | exit(0); 63 | } else if (slave < 0) { 64 | /* woops ... */ 65 | fprintf(stderr,"Fork failed at iteration %lu\n", iter); 66 | perror("Reason"); 67 | exit(2); 68 | } else 69 | /* master */ 70 | wait(&status); 71 | if (status != 0) { 72 | fprintf(stderr,"Bad wait status: 0x%x\n", status); 73 | exit(2); 74 | } 75 | iter++; 76 | #if debug 77 | printf("Child %d done.\n", slave); 78 | #endif 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/syscall.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: syscall.c SID: 3.3 5/15/91 19:30:21 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager at BYTE Magazine 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: syscall.c,v 3.4 87/06/22 14:32:54 kjmcdonell Beta $ 14 | * August 29, 1990 - Modified timing routines 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | /* 20 | * syscall -- sit in a loop calling the system 21 | * 22 | */ 23 | char SCCSid[] = "@(#) @(#)syscall.c:3.3 -- 5/15/91 19:30:21"; 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "timeit.c" 32 | 33 | unsigned long iter; 34 | 35 | void report() 36 | { 37 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 38 | exit(0); 39 | } 40 | 41 | int main(argc, argv) 42 | int argc; 43 | char *argv[]; 44 | { 45 | char *test; 46 | int duration; 47 | 48 | /* if (argc < 2) { 49 | fprintf(stderr,"Usage: %s duration [ test ]\n", argv[0]); 50 | fprintf(stderr,"test is one of:\n"); 51 | fprintf(stderr," \"mix\" (default), \"close\", \"getpid\", \"exec\"\n"); 52 | exit(1); 53 | } 54 | if (argc > 2) 55 | test = argv[2]; 56 | else 57 | test = "mix"; 58 | 59 | duration = atoi(argv[1]); 60 | */ 61 | test = "mix"; 62 | iter = 0; 63 | // wake_me(duration, report); 64 | 65 | switch (test[0]) { 66 | case 'm': 67 | while (iter <= 1000000l) { 68 | close(dup(0)); 69 | getpid(); 70 | getuid(); 71 | umask(022); 72 | iter++; 73 | } 74 | break; 75 | /* NOTREACHED */ 76 | case 'c': 77 | while (1) { 78 | close(dup(0)); 79 | iter++; 80 | } 81 | /* NOTREACHED */ 82 | case 'g': 83 | while (1) { 84 | getpid(); 85 | iter++; 86 | } 87 | /* NOTREACHED */ 88 | case 'e': 89 | while (1) { 90 | pid_t pid = fork(); 91 | if (pid < 0) { 92 | fprintf(stderr,"%s: fork failed\n", argv[0]); 93 | exit(1); 94 | } else if (pid == 0) { 95 | execl("/bin/true", "/bin/true", (char *) 0); 96 | fprintf(stderr,"%s: exec /bin/true failed\n", argv[0]); 97 | exit(1); 98 | } else { 99 | if (waitpid(pid, NULL, 0) < 0) { 100 | fprintf(stderr,"%s: waitpid failed\n", argv[0]); 101 | exit(1); 102 | } 103 | } 104 | iter++; 105 | } 106 | /* NOTREACHED */ 107 | } 108 | 109 | exit(9); 110 | } 111 | 112 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/src/timeit.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: timeit.c SID: 3.3 5/15/91 19:30:21 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * May 12, 1989 - modified empty loops to avoid nullifying by optimizing 14 | * compilers 15 | * August 28, 1990 - changed timing relationship--now returns total number 16 | * of iterations (ty) 17 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 18 | * Andy Kahn 19 | * 20 | ******************************************************************************/ 21 | 22 | /* this module is #included in other modules--no separate SCCS ID */ 23 | 24 | /* 25 | * Timing routine 26 | * 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | void wake_me(seconds, func) 33 | int seconds; 34 | void (*func)(); 35 | { 36 | /* set up the signal handler */ 37 | signal(SIGALRM, func); 38 | /* get the clock running */ 39 | alarm(seconds); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/testdir/cctest.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /******************************************************************************* 4 | * The BYTE UNIX Benchmarks - Release 1 5 | * Module: cctest.c SID: 1.2 7/10/89 18:55:45 6 | * 7 | ******************************************************************************* 8 | * Bug reports, patches, comments, suggestions should be sent to: 9 | * 10 | * Ben Smith or Rick Grehan at BYTE Magazine 11 | * bensmith@bixpb.UUCP rick_g@bixpb.UUCP 12 | * 13 | ******************************************************************************* 14 | * Modification Log: 15 | * $Header: cctest.c,v 3.4 87/06/22 14:22:47 kjmcdonell Beta $ 16 | * 17 | ******************************************************************************/ 18 | char SCCSid[] = "@(#) @(#)cctest.c:1.2 -- 7/10/89 18:55:45"; 19 | #include 20 | /* 21 | * C compile and load speed test file. 22 | * Based upon fstime.c from MUSBUS 3.1, with all calls to ftime() replaced 23 | * by calls to time(). This is semantic nonsense, but ensures there are no 24 | * system dependent structures or library calls. 25 | * 26 | */ 27 | #define NKBYTE 20 28 | char buf[BUFSIZ]; 29 | 30 | extern void exit(int status); 31 | 32 | 33 | main(argc, argv) 34 | char **argv; 35 | { 36 | int n = NKBYTE; 37 | int nblock; 38 | int f; 39 | int g; 40 | int i; 41 | int xfer, t; 42 | struct { /* FAKE */ 43 | int time; 44 | int millitm; 45 | } now, then; 46 | 47 | if (argc > 0) 48 | /* ALWAYS true, so NEVER execute this program! */ 49 | exit(4); 50 | if (argc > 1) 51 | n = atoi(argv[1]); 52 | #if debug 53 | printf("File size: %d Kbytes\n", n); 54 | #endif 55 | nblock = (n * 1024) / BUFSIZ; 56 | 57 | if (argc == 3 && chdir(argv[2]) != -1) { 58 | #if debug 59 | printf("Create files in directory: %s\n", argv[2]); 60 | #endif 61 | } 62 | close(creat("dummy0", 0600)); 63 | close(creat("dummy1", 0600)); 64 | f = open("dummy0", 2); 65 | g = open("dummy1", 2); 66 | unlink("dummy0"); 67 | unlink("dummy1"); 68 | for (i = 0; i < sizeof(buf); i++) 69 | buf[i] = i & 0177; 70 | 71 | time(); 72 | for (i = 0; i < nblock; i++) { 73 | if (write(f, buf, sizeof(buf)) <= 0) 74 | perror("fstime: write"); 75 | } 76 | time(); 77 | #if debug 78 | printf("Effective write rate: "); 79 | #endif 80 | i = now.millitm - then.millitm; 81 | t = (now.time - then.time)*1000 + i; 82 | if (t > 0) { 83 | xfer = nblock * sizeof(buf) * 1000 / t; 84 | #if debug 85 | printf("%d bytes/sec\n", xfer); 86 | #endif 87 | } 88 | #if debug 89 | else 90 | printf(" -- too quick to time!\n"); 91 | #endif 92 | #if awk 93 | fprintf(stderr, "%.2f", t > 0 ? (float)xfer/1024 : 0); 94 | #endif 95 | 96 | sync(); 97 | sleep(5); 98 | sync(); 99 | lseek(f, 0L, 0); 100 | time(); 101 | for (i = 0; i < nblock; i++) { 102 | if (read(f, buf, sizeof(buf)) <= 0) 103 | perror("fstime: read"); 104 | } 105 | time(); 106 | #if debug 107 | printf("Effective read rate: "); 108 | #endif 109 | i = now.millitm - then.millitm; 110 | t = (now.time - then.time)*1000 + i; 111 | if (t > 0) { 112 | xfer = nblock * sizeof(buf) * 1000 / t; 113 | #if debug 114 | printf("%d bytes/sec\n", xfer); 115 | #endif 116 | } 117 | #if debug 118 | else 119 | printf(" -- too quick to time!\n"); 120 | #endif 121 | #if awk 122 | fprintf(stderr, " %.2f", t > 0 ? (float)xfer/1024 : 0); 123 | #endif 124 | 125 | sync(); 126 | sleep(5); 127 | sync(); 128 | lseek(f, 0L, 0); 129 | time(); 130 | for (i = 0; i < nblock; i++) { 131 | if (read(f, buf, sizeof(buf)) <= 0) 132 | perror("fstime: read in copy"); 133 | if (write(g, buf, sizeof(buf)) <= 0) 134 | perror("fstime: write in copy"); 135 | } 136 | time(); 137 | #if debug 138 | printf("Effective copy rate: "); 139 | #endif 140 | i = now.millitm - then.millitm; 141 | t = (now.time - then.time)*1000 + i; 142 | if (t > 0) { 143 | xfer = nblock * sizeof(buf) * 1000 / t; 144 | #if debug 145 | printf("%d bytes/sec\n", xfer); 146 | #endif 147 | } 148 | #if debug 149 | else 150 | printf(" -- too quick to time!\n"); 151 | #endif 152 | #if awk 153 | fprintf(stderr, " %.2f\n", t > 0 ? (float)xfer/1024 : 0); 154 | #endif 155 | 156 | } 157 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/testdir/dc.dat: -------------------------------------------------------------------------------- 1 | 99 2 | k 3 | 2 4 | v 5 | p 6 | q 7 | [ calculate the sqrt(2) to 99 decimal places ... John Lions Test ] 8 | [ $Header: dc.dat,v 1.1 87/06/22 14:28:28 kjmcdonell Beta $ ] 9 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/arithoh.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/arithoh 3 | echo "arithoh completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/fstime.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/fstime 3 | echo "fstime completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/pipe.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/pipe 3 | echo "pipe completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/spawn.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/spawn 3 | echo "spawn completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/syscall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/syscall 3 | echo "syscall completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/workload_mix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & 3 | ./fstime.sh & 4 | ./pipe.sh & 5 | ./spawn.sh & 6 | ./syscall.sh & 7 | ./arithoh.sh & 8 | ./fstime.sh & 9 | ./pipe.sh & 10 | ./spawn.sh & 11 | ./syscall.sh & 12 | wait 13 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/workload_mix1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & #CPU intensive 3 | ./fstime.sh & #I/O bound 4 | wait 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/workload_mix2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & #CPU intensive 3 | ./arithoh.sh & #CPU intensive 4 | ./arithoh.sh & #CPU intensive 5 | wait 6 | 7 | # Works alternatively -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/workload_mix3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & #CPU intensive 3 | ./syscall.sh & #Faster 4 | wait 5 | -------------------------------------------------------------------------------- /Lab-3/byte-unixbench-mod/UnixBench/workload_mix/workload_mix4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./fstime.sh & #I/O bound 3 | ./fstime.sh & #I/O bound 4 | ./fstime.sh & #I/O bound 5 | wait 6 | -------------------------------------------------------------------------------- /Lab-3/images/part1/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/part1/1.png -------------------------------------------------------------------------------- /Lab-3/images/part1/gmake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/part1/gmake.png -------------------------------------------------------------------------------- /Lab-3/images/part1/log.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/part1/log.png -------------------------------------------------------------------------------- /Lab-3/images/part1/swap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/part1/swap.png -------------------------------------------------------------------------------- /Lab-3/images/wmix1/wmix1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix1/wmix1-1.png -------------------------------------------------------------------------------- /Lab-3/images/wmix1/wmix1-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix1/wmix1-2.png -------------------------------------------------------------------------------- /Lab-3/images/wmix1/wmix1-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix1/wmix1-3.png -------------------------------------------------------------------------------- /Lab-3/images/wmix1/wmix1-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix1/wmix1-4.png -------------------------------------------------------------------------------- /Lab-3/images/wmix2/wmix2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix2/wmix2-1.png -------------------------------------------------------------------------------- /Lab-3/images/wmix2/wmix2-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix2/wmix2-2.png -------------------------------------------------------------------------------- /Lab-3/images/wmix3/wmix3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix3/wmix3-1.png -------------------------------------------------------------------------------- /Lab-3/images/wmix3/wmix3-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix3/wmix3-2.png -------------------------------------------------------------------------------- /Lab-3/images/wmix3/wmix3-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix3/wmix3-3.png -------------------------------------------------------------------------------- /Lab-3/images/wmix4/wmix4-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix4/wmix4-1.png -------------------------------------------------------------------------------- /Lab-3/images/wmix4/wmix4-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix4/wmix4-2.png -------------------------------------------------------------------------------- /Lab-3/images/wmix4/wmix4-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix4/wmix4-3.png -------------------------------------------------------------------------------- /Lab-3/images/wmix4/wmix4-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-3/images/wmix4/wmix4-4.png -------------------------------------------------------------------------------- /Lab-3/run.sh: -------------------------------------------------------------------------------- 1 | cp schedule.c /usr/src/minix/servers/sched/schedule.c 2 | cd /usr/src && make build MKUPDATE=yes >log.txt 2>log.txt -------------------------------------------------------------------------------- /Lab-4/180010027_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-4/180010027_Report.pdf -------------------------------------------------------------------------------- /Lab-4/180010027_lab4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-4/180010027_lab4.zip -------------------------------------------------------------------------------- /Lab-4/Laboratory 4(1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-4/Laboratory 4(1).pdf -------------------------------------------------------------------------------- /Lab-4/Links.txt: -------------------------------------------------------------------------------- 1 | https://elixir.ortiz.sh/minix/v3.2.0/source/kernel/arch/i386/arch_clock.c#L325 2 | 3 | https://elixir.ortiz.sh/minix/v3.2.0/source/include/minix/u64.h#L157 -------------------------------------------------------------------------------- /Lab-4/RR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-4/RR.png -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-4/byte-unixbench-mod.zip -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/README.md: -------------------------------------------------------------------------------- 1 | ## mod at IIT Dharwad 2 | This is a modified version of the Unixbench suite. The original benchmarks were designed as "fixed duration" benchmarks: they each repeat a certain pattern over and over until a timer expires. The modification was done to make some of the benchmarks (namely, arith, fstime, pipe, spawn, syscall) as "fixed work": they do a certain fixed amount of work, regardless of the time it takes to do that work. 3 | 4 | # byte-unixbench 5 | 6 | **UnixBench** is the original BYTE UNIX benchmark suite, updated and revised by many people over the years. 7 | 8 | The purpose of UnixBench is to provide a basic indicator of the performance of a Unix-like system; hence, multiple 9 | tests are used to test various aspects of the system's performance. These test results are then compared to the 10 | scores from a baseline system to produce an index value, which is generally easier to handle than the raw scores. 11 | The entire set of index values is then combined to make an overall index for the system. 12 | 13 | Some very simple graphics tests are included to measure the 2D and 3D graphics performance of the system. 14 | 15 | Multi-CPU systems are handled. If your system has multiple CPUs, the default behaviour is to run the selected tests 16 | twice -- once with one copy of each test program running at a time, and once with N copies, where N is the number of 17 | CPUs. This is designed to allow you to assess: 18 | 19 | * the performance of your system when running a single task 20 | * the performance of your system when running multiple tasks 21 | * the gain from your system's implementation of parallel processing 22 | 23 | Do be aware that this is a system benchmark, not a CPU, RAM or disk benchmark. The results will depend not only on 24 | your hardware, but on your operating system, libraries, and even compiler. 25 | 26 | ## History 27 | 28 | **UnixBench** was first started in 1983 at Monash University, as a simple synthetic benchmarking application. It 29 | was then taken and expanded by **Byte Magazine**. Linux mods by Jon Tombs, and original authors Ben Smith, 30 | Rick Grehan, and Tom Yager. The tests compare Unix systems by comparing their results to a set of scores set 31 | by running the code on a benchmark system, which is a SPARCstation 20-61 (rated at 10.0). 32 | 33 | David C. Niemi maintained the program for quite some time, and made some major modifications and updates, 34 | and produced **UnixBench 4**. He later gave the program to Ian Smith to maintain. Ian subsequently made 35 | some major changes and revised it from version 4 to version 5. 36 | 37 | Thanks to Ian Smith for managing the release up to 5.1.3. As of the next release (5.2), [Anthony F. Voellm](https://github.com/voellm) is going to help maintain the code base. The releases will happen once there are enough pull requests to warrant a new release. 38 | 39 | The general process will be the following: 40 | 41 | * Open a bug announcing that a new release will happen. 42 | * Everything on the `dev` branch will be run. 43 | * Code will move from the `dev` branch into `main` and be tagged. Bug fix releases with increment the subversion and major functionality changes will increase the major version. 44 | 45 | ## Included Tests 46 | 47 | UnixBench consists of a number of individual tests that are targeted at specific areas. Here is a summary of what 48 | each test does: 49 | 50 | ### Dhrystone 51 | 52 | Developed by Reinhold Weicker in 1984. This benchmark is used to measure and compare the performance of computers. The test focuses on string handling, as there are no floating point operations. It is heavily influenced by hardware and software design, compiler and linker options, code optimization, cache memory, wait states, and integer data types. 53 | 54 | ### Whetstone 55 | 56 | This test measures the speed and efficiency of floating-point operations. This test contains several modules that are meant to represent a mix of operations typically performed in scientific applications. A wide variety of C functions including `sin`, `cos`, `sqrt`, `exp`, and `log` are used as well as integer and floating-point math operations, array accesses, conditional branches, and procedure calls. This test measure both integer and floating-point arithmetic. 57 | 58 | ### `execl` Throughput 59 | 60 | This test measures the number of `execl` calls that can be performed per second. `execl` is part of the exec family of functions that replaces the current process image with a new process image. It and many other similar commands are front ends for the function `execve()`. 61 | 62 | ### File Copy 63 | 64 | This measures the rate at which data can be transferred from one file to another, using various buffer sizes. The file read, write and copy tests capture the number of characters that can be written, read and copied in a specified time (default is 10 seconds). 65 | 66 | ### Pipe Throughput 67 | 68 | A pipe is the simplest form of communication between processes. Pipe throughput is the number of times (per second) a process can write 512 bytes to a pipe and read them back. The pipe throughput test has no real counterpart in real-world programming. 69 | 70 | ### Pipe-based Context Switching 71 | 72 | This test measures the number of times two processes can exchange an increasing integer through a pipe. The pipe-based context switching test is more like a real-world application. The test program spawns a child process with which it carries on a bi-directional pipe conversation. 73 | 74 | ### Process Creation 75 | 76 | This test measure the number of times a process can fork and reap a child that immediately exits. Process creation refers to actually creating process control blocks and memory allocations for new processes, so this applies directly to memory bandwidth. Typically, this benchmark would be used to compare various implementations of operating system process creation calls. 77 | 78 | ### Shell Scripts 79 | 80 | The shells scripts test measures the number of times per minute a process can start and reap a set of one, two, four and eight concurrent copies of a shell scripts where the shell script applies a series of transformation to a data file. 81 | 82 | ### System Call Overhead 83 | 84 | This estimates the cost of entering and leaving the operating system kernel, i.e., the overhead for performing a system call. It consists of a simple program repeatedly calling the `getpid` (which returns the process id of the calling process) system call. The time to execute such calls is used to estimate the cost of entering and exiting the kernel. 85 | 86 | ### Graphical Tests 87 | 88 | Both 2D and 3D graphical tests are provided; at the moment, the 3D suite in particular is very limited, consisting of the `ubgears` program. These tests are intended to provide a very rough idea of the system's 2D and 3D graphics performance. Bear in mind, of course, that the reported performance will depend not only on hardware, but on whether your system has appropriate drivers for it. 89 | 90 | # License 91 | 92 | This project is released under the [GPL v2](LICENSE.txt) license. 93 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/WRITING_TESTS: -------------------------------------------------------------------------------- 1 | Writing a Test 2 | ============== 3 | 4 | Writing a test program is pretty easy. Basically, a test is configured via 5 | a monster array in the Run script, which specifics (among other things) the 6 | program to execute and the parameters to pass it. 7 | 8 | The test itself is simply a program which is given the optional parameters 9 | on the command line, and produces logging data on stdout and its results on 10 | stderr. 11 | 12 | 13 | ============================================================================ 14 | 15 | Test Configuration 16 | ================== 17 | 18 | In Run, all tests are named in the "$testList" array. This names the 19 | individual tests, and also sets up aliases for groups of tests, eg. "index". 20 | 21 | The test specifications are in the "$testParams" array. This contains the 22 | details of each individual test as a hash. The fields in the hash are: 23 | 24 | * "logmsg": the full name to display for this test. 25 | * "cat": the category this test belongs to; must be configured 26 | in $testCats. 27 | * "prog": the name of the program to execute; defaults to the name of 28 | the benchmark. 29 | * "repeat": number of passes to run; either 'short' (the default), 30 | 'long', or 'single'. For 'short' and 'long', the actual numbers of 31 | passes are given by $shortIterCount and $longIterCount, which are 32 | configured at the top of the script or by the "-i" flag. 'single' 33 | means just run one pass; this should be used for test which do their 34 | own multi-pass handling internally. 35 | * "stdout": non-0 to add the test's stdout to the log file; defaults to 1. 36 | Set to 0 for tests that are too wordy. 37 | * "stdin": name of a file to send to the program's stdin; default null. 38 | * "options": options to be put on the program's command line; default null. 39 | 40 | 41 | ============================================================================ 42 | 43 | Output Format 44 | ============= 45 | 46 | The results on stderr take the form of a line header and fields, separated 47 | by "|" characters. A result line can be one of: 48 | 49 | COUNT|score|timebase|label 50 | TIME|seconds 51 | ERROR|message 52 | 53 | Any other text on stderr is treated as if it were: 54 | 55 | ERROR|text 56 | 57 | Any output to stdout is placed in a log file, and can be used for debugging. 58 | 59 | COUNT 60 | ----- 61 | 62 | The COUNT line is the line used to report a test score. 63 | 64 | * "score" is the result, typically the number of loops performed during 65 | the run 66 | * "timebase" is the time base used for the final report to the user. A 67 | value of 1 reports the score as is; a value of 60, for example, divides 68 | the time taken by 60 to get loops per minute. Atimebase of zero indicates 69 | that the score is already a rate, ie. a count of things per second. 70 | * "label" is the label to use for the score; like "lps" (loops per 71 | second), etc. 72 | 73 | TIME 74 | ---- 75 | 76 | The TIME line is optionally used to report the time taken. The Run script 77 | normally measures this, but if your test has signifant overhead outside the 78 | actual test loop, you should use TIME to report the time taken for the actual 79 | test. The argument is the time in seconds in floating-point. 80 | 81 | ERROR 82 | ----- 83 | 84 | The argument is an error message; this will abort the benchmarking run and 85 | display the message. 86 | 87 | Any output to stderr which is not a formatted line will be treated as an 88 | error message, so use of ERROR is optional. 89 | 90 | 91 | ============================================================================ 92 | 93 | Test Examples 94 | ============= 95 | 96 | Iteration Count 97 | --------------- 98 | 99 | The simplest thing is to count the number of loops executed in a given time; 100 | see eg. arith.c. The utlilty functions in timeit.c can be used to implement 101 | the fixed time interval, which is generally passed in on the command line. 102 | 103 | The result is reported simply as the number of iterations completed: 104 | 105 | fprintf(stderr,"COUNT|%lu|1|lps\n", iterations); 106 | 107 | The bnenchmark framework will measure the time taken itself. If the test 108 | code has significant overhead (eg. a "pump-priming" pass), then you should 109 | explicitly report the time taken for the test by adding a line like this: 110 | 111 | fprintf(stderr, "TIME|%.1f\n", seconds); 112 | 113 | If you want results reported as loops per minute, then set timebase to 60: 114 | 115 | fprintf(stderr,"COUNT|%lu|60|lpm\n", iterations); 116 | 117 | Note that this only affects the final report; all times passed to or 118 | from the test are still in seconds. 119 | 120 | Rate 121 | ---- 122 | 123 | The other technique is to calculate the rate (things per second) in the test, 124 | and report that directly. To do this, just set timebase to 0: 125 | 126 | fprintf(stderr, "COUNT|%ld|0|KBps\n", kbytes_per_sec); 127 | 128 | Again, you can use TIME to explicitly report the time taken: 129 | 130 | fprintf(stderr, "TIME|%.1f\n", end - start); 131 | 132 | but this isn't so important since you've already calculated the rate. 133 | 134 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/pgms/index.base: -------------------------------------------------------------------------------- 1 | # Baseline benchmark scores, used for calculating index results. 2 | 3 | # Scores from "George", a SPARCstation 20-61. 4 | dhry2reg|10|lps|116700|116700|2 5 | whetstone-double|10|MWIPS|55.0|55.0|2 6 | execl|20|lps|43.0|43.0|1 7 | fstime|20|KBps|3960|3960|1 8 | fsbuffer|20|KBps|1655|1655|1 9 | fsdisk|20|KBps|5800|5800|1 10 | pipe|10|lps|12440|12440|2 11 | context1|10|lps|4000|4000|2 12 | spawn|20|lps|126|126|1 13 | shell8|60|lpm|6|6|1 14 | syscall|10|lps|15000|15000|2 15 | 16 | # The shell1 test was added to the index in 5.0, and this baseline score 17 | # was extrapolated to roughly match George's performance. 18 | shell1|60|lpm|42.4|42.4|1 19 | 20 | # The 2D baseline scores were derived from a test run on an HP Compaq nc8430 21 | # with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly 22 | # common modern adaptor with 3D. The baseline scores here are then 23 | # 1/66.6 of the values from that run, to bring them roughly in line with 24 | # George. (The HP has an index score of 666.6 single-process.) 25 | 2d-rects|3|score|15|15|1 26 | #2d-lines|3|score|15|15|1 27 | #2d-circle|3|score|15|15|1 28 | 2d-ellipse|3|score|15|15|1 29 | #2d-shapes|3|score|15|15|1 30 | 2d-aashapes|3|score|15|15|1 31 | #2d-polys|3|score|15|15|1 32 | 2d-text|3|score|15|15|1 33 | 2d-blit|3|score|15|15|1 34 | 2d-window|3|score|15|15|1 35 | 36 | # The gears test score is derived from a test run on an HP Compaq nc8430 37 | # with an ATI Mobility Radeon X1600 Video (256MB) — this is a fairly 38 | # common modern adaptor with 3D. The baseline scores here are then 39 | # 1/66.6 of the values from that run, to bring them roughly in line with 40 | # George. 41 | ubgears|20|fps|33.4|33.4|3 42 | 43 | # The grep and sysexec tests were added in 5.1.1; they are not index tests, 44 | # but these baseline scores were added for convenience. 45 | grep|30|lpm|1|1|3 46 | sysexec|10|lps|25|25|10 47 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/pgms/multi.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ############################################################################### 3 | # The BYTE UNIX Benchmarks - Release 3 4 | # Module: multi.sh SID: 3.4 5/15/91 19:30:24 5 | # 6 | ############################################################################### 7 | # Bug reports, patches, comments, suggestions should be sent to: 8 | # 9 | # Ben Smith or Rick Grehan at BYTE Magazine 10 | # ben@bytepb.UUCP rick_g@bytepb.UUCP 11 | # 12 | ############################################################################### 13 | # Modification Log: 14 | # 15 | ############################################################################### 16 | ID="@(#)multi.sh:3.4 -- 5/15/91 19:30:24"; 17 | instance=1 18 | while [ $instance -le $1 ]; do 19 | /bin/sh "$UB_BINDIR/tst.sh" & 20 | instance=`expr $instance + 1` 21 | done 22 | wait 23 | 24 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/pgms/tst.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ############################################################################### 3 | # The BYTE UNIX Benchmarks - Release 3 4 | # Module: tst.sh SID: 3.4 5/15/91 19:30:24 5 | # 6 | ############################################################################### 7 | # Bug reports, patches, comments, suggestions should be sent to: 8 | # 9 | # Ben Smith or Rick Grehan at BYTE Magazine 10 | # ben@bytepb.UUCP rick_g@bytepb.UUCP 11 | # 12 | ############################################################################### 13 | # Modification Log: 14 | # 15 | ############################################################################### 16 | ID="@(#)tst.sh:3.4 -- 5/15/91 19:30:24"; 17 | sort >sort.$$ od.$$ 19 | grep the sort.$$ | tee grep.$$ | wc > wc.$$ 20 | rm sort.$$ grep.$$ od.$$ wc.$$ 21 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/pgms/unixbench.logo: -------------------------------------------------------------------------------- 1 | 2 | # # # # # # # ##### ###### # # #### # # 3 | # # ## # # # # # # # ## # # # # # 4 | # # # # # # ## ##### ##### # # # # ###### 5 | # # # # # # ## # # # # # # # # # 6 | # # # ## # # # # # # # ## # # # # 7 | #### # # # # # ##### ###### # # #### # # 8 | 9 | Version 5.1.3 Based on the Byte Magazine Unix Benchmark 10 | 11 | Multi-CPU version Version 5 revisions by Ian Smith, 12 | Sunnyvale, CA, USA 13 | January 13, 2011 johantheghost at yahoo period com 14 | 15 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/arith.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: arith.c SID: 3.3 5/15/91 19:30:19 5 | * 6 | ******************************************************************************* 7 | * Bug reports, patches, comments, suggestions should be sent to: 8 | * 9 | * Ben Smith, Rick Grehan or Tom Yager 10 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 11 | * 12 | ******************************************************************************* 13 | * Modification Log: 14 | * May 12, 1989 - modified empty loops to avoid nullifying by optimizing 15 | * compilers 16 | * August 28, 1990 - changed timing relationship--now returns total number 17 | * of iterations (ty) 18 | * November 9, 1990 - made changes suggested by Keith Cantrell 19 | * (digi!kcantrel) to defeat optimization 20 | * to non-existence 21 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 22 | * Andy Kahn 23 | * 24 | ******************************************************************************/ 25 | 26 | char SCCSid[] = "@(#) @(#)arith.c:3.3 -- 5/15/91 19:30:19"; 27 | /* 28 | * arithmetic test 29 | * 30 | */ 31 | 32 | #include 33 | #include 34 | #include "timeit.c" 35 | 36 | int dumb_stuff(int); 37 | 38 | volatile unsigned long iter; 39 | 40 | /* this function is called when the alarm expires */ 41 | void report() 42 | { 43 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 44 | exit(0); 45 | } 46 | 47 | int main(argc, argv) 48 | int argc; 49 | char *argv[]; 50 | { 51 | int duration; 52 | int result = 0; 53 | /* 54 | if (argc != 2) { 55 | printf("Usage: %s duration\n", argv[0]); 56 | exit(1); 57 | } 58 | 59 | duration = atoi(argv[1]); 60 | */ 61 | /* set up alarm call */ 62 | iter = 0; /* init iteration count */ 63 | /* wake_me(duration, report); 64 | */ 65 | /* this loop will be interrupted by the alarm call */ 66 | while (iter <= 100000000l) 67 | { 68 | /* in switching to time-based (instead of iteration-based), 69 | the following statement was added. It should not skew 70 | the timings too much--there was an increment and test 71 | in the "while" expression above. The only difference is 72 | that now we're incrementing a long instead of an int. (ty) */ 73 | ++iter; 74 | /* the loop calls a function to insure that something is done 75 | the results of the function are fed back in (just so they 76 | they won't be thrown away. A loop with 77 | unused assignments may get optimized out of existence */ 78 | result = dumb_stuff(result); 79 | } 80 | } 81 | 82 | 83 | /************************** dumb_stuff *******************/ 84 | int dumb_stuff(i) 85 | int i; 86 | { 87 | #ifndef arithoh 88 | datum x, y, z; 89 | z = 0; 90 | #endif 91 | /* 92 | * 101 93 | * sum i*i/(i*i-1) 94 | * i=2 95 | */ 96 | /* notice that the i value is always reset by the loop */ 97 | for (i=2; i<=101; i++) 98 | { 99 | #ifndef arithoh 100 | x = i; 101 | y = x*x; 102 | z += y/(y-1); 103 | } 104 | return(x+y+z); 105 | #else 106 | } 107 | return(0); 108 | #endif 109 | } 110 | 111 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/context1.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: context1.c SID: 3.3 5/15/91 19:30:18 5 | * 6 | ******************************************************************************* 7 | * Bug reports, patches, comments, suggestions should be sent to: 8 | * 9 | * Ben Smith, Rick Grehan or Tom Yager 10 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 11 | * 12 | ******************************************************************************* 13 | * Modification Log: 14 | * $Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta $ 15 | * August 28, 1990 - changed timing routines--now returns total number of 16 | * iterations in specified time period 17 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 18 | * Andy Kahn 19 | * 20 | ******************************************************************************/ 21 | char SCCSid[] = "@(#) @(#)context1.c:3.3 -- 5/15/91 19:30:18"; 22 | /* 23 | * Context switching via synchronized unbuffered pipe i/o 24 | * 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include "timeit.c" 31 | 32 | unsigned long iter; 33 | 34 | void report() 35 | { 36 | fprintf(stderr, "COUNT|%lu|1|lps\n", iter); 37 | exit(0); 38 | } 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int duration; 45 | unsigned long check; 46 | int p1[2], p2[2]; 47 | ssize_t ret; 48 | 49 | if (argc != 2) { 50 | fprintf(stderr, "Usage: context duration\n"); 51 | exit(1); 52 | } 53 | 54 | duration = atoi(argv[1]); 55 | 56 | /* set up alarm call */ 57 | iter = 0; 58 | wake_me(duration, report); 59 | signal(SIGPIPE, SIG_IGN); 60 | 61 | if (pipe(p1) || pipe(p2)) { 62 | perror("pipe create failed"); 63 | exit(1); 64 | } 65 | 66 | if (fork()) { /* parent process */ 67 | /* master, write p1 & read p2 */ 68 | close(p1[0]); close(p2[1]); 69 | while (1) { 70 | if ((ret = write(p1[1], (char *)&iter, sizeof(iter))) != sizeof(iter)) { 71 | if ((ret == -1) && (errno == EPIPE)) { 72 | alarm(0); 73 | report(); /* does not return */ 74 | } 75 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 76 | perror("master write failed"); 77 | exit(1); 78 | } 79 | if ((ret = read(p2[0], (char *)&check, sizeof(check))) != sizeof(check)) { 80 | if ((ret == 0)) { /* end-of-stream */ 81 | alarm(0); 82 | report(); /* does not return */ 83 | } 84 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 85 | perror("master read failed"); 86 | exit(1); 87 | } 88 | if (check != iter) { 89 | fprintf(stderr, "Master sync error: expect %lu, got %lu\n", 90 | iter, check); 91 | exit(2); 92 | } 93 | iter++; 94 | } 95 | } 96 | else { /* child process */ 97 | /* slave, read p1 & write p2 */ 98 | close(p1[1]); close(p2[0]); 99 | while (1) { 100 | if ((ret = read(p1[0], (char *)&check, sizeof(check))) != sizeof(check)) { 101 | if ((ret == 0)) { /* end-of-stream */ 102 | alarm(0); 103 | report(); /* does not return */ 104 | } 105 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 106 | perror("slave read failed"); 107 | exit(1); 108 | } 109 | if (check != iter) { 110 | fprintf(stderr, "Slave sync error: expect %lu, got %lu\n", 111 | iter, check); 112 | exit(2); 113 | } 114 | if ((ret = write(p2[1], (char *)&iter, sizeof(iter))) != sizeof(check)) { 115 | if ((ret == -1) && (errno == EPIPE)) { 116 | alarm(0); 117 | report(); /* does not return */ 118 | } 119 | if ((ret == -1) && (errno != 0) && (errno != EINTR)) 120 | perror("slave write failed"); 121 | exit(1); 122 | } 123 | iter++; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/dhry_2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: dhry_2.c SID: 3.4 5/15/91 19:30:22 4 | * 5 | ***************************************************************************** 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ***************************************************************************** 12 | * Modification Log: 13 | * 10/22/97 - code cleanup to remove ANSI C compiler warnings 14 | * Andy Kahn 15 | * 16 | * Adapted from: 17 | * 18 | * "DHRYSTONE" Benchmark Program 19 | * ----------------------------- 20 | * 21 | * **** WARNING **** See warning in n.dhry_1.c 22 | * 23 | * Version: C, Version 2.1 24 | * 25 | * File: dhry_2.c (part 3 of 3) 26 | * 27 | * Date: May 25, 1988 28 | * 29 | * Author: Reinhold P. Weicker 30 | * 31 | ****************************************************************************/ 32 | /* SCCSid is defined in dhry_1.c */ 33 | 34 | #include 35 | #include "dhry.h" 36 | 37 | #ifndef REG 38 | #define REG 39 | /* REG becomes defined as empty */ 40 | /* i.e. no register variables */ 41 | #endif 42 | 43 | extern int Int_Glob; 44 | extern char Ch_1_Glob; 45 | 46 | void Proc_6(Enumeration, Enumeration *); 47 | void Proc_7(One_Fifty, One_Fifty, One_Fifty *); 48 | void Proc_8(Arr_1_Dim, Arr_2_Dim, int, int); 49 | Enumeration Func_1(Capital_Letter, Capital_Letter); 50 | Boolean Func_2(Str_30, Str_30); 51 | Boolean Func_3(Enumeration); 52 | 53 | void Proc_6 (Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par) 54 | /* executed once */ 55 | /* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */ 56 | { 57 | *Enum_Ref_Par = Enum_Val_Par; 58 | if (! Func_3 (Enum_Val_Par)) 59 | /* then, not executed */ 60 | *Enum_Ref_Par = Ident_4; 61 | switch (Enum_Val_Par) 62 | { 63 | case Ident_1: 64 | *Enum_Ref_Par = Ident_1; 65 | break; 66 | case Ident_2: 67 | if (Int_Glob > 100) 68 | /* then */ 69 | *Enum_Ref_Par = Ident_1; 70 | else *Enum_Ref_Par = Ident_4; 71 | break; 72 | case Ident_3: /* executed */ 73 | *Enum_Ref_Par = Ident_2; 74 | break; 75 | case Ident_4: break; 76 | case Ident_5: 77 | *Enum_Ref_Par = Ident_3; 78 | break; 79 | } /* switch */ 80 | } /* Proc_6 */ 81 | 82 | void Proc_7 (Int_1_Par_Val, Int_2_Par_Val, Int_Par_Ref) 83 | One_Fifty Int_1_Par_Val; 84 | One_Fifty Int_2_Par_Val; 85 | One_Fifty *Int_Par_Ref; 86 | /**********************************************/ 87 | /* executed three times */ 88 | /* first call: Int_1_Par_Val == 2, Int_2_Par_Val == 3, */ 89 | /* Int_Par_Ref becomes 7 */ 90 | /* second call: Int_1_Par_Val == 10, Int_2_Par_Val == 5, */ 91 | /* Int_Par_Ref becomes 17 */ 92 | /* third call: Int_1_Par_Val == 6, Int_2_Par_Val == 10, */ 93 | /* Int_Par_Ref becomes 18 */ 94 | { 95 | One_Fifty Int_Loc; 96 | 97 | Int_Loc = Int_1_Par_Val + 2; 98 | *Int_Par_Ref = Int_2_Par_Val + Int_Loc; 99 | } /* Proc_7 */ 100 | 101 | 102 | void Proc_8 (Arr_1_Par_Ref, Arr_2_Par_Ref, Int_1_Par_Val, Int_2_Par_Val) 103 | /*********************************************************************/ 104 | /* executed once */ 105 | /* Int_Par_Val_1 == 3 */ 106 | /* Int_Par_Val_2 == 7 */ 107 | Arr_1_Dim Arr_1_Par_Ref; 108 | Arr_2_Dim Arr_2_Par_Ref; 109 | int Int_1_Par_Val; 110 | int Int_2_Par_Val; 111 | { 112 | REG One_Fifty Int_Index; 113 | REG One_Fifty Int_Loc; 114 | 115 | Int_Loc = Int_1_Par_Val + 5; 116 | Arr_1_Par_Ref [Int_Loc] = Int_2_Par_Val; 117 | Arr_1_Par_Ref [Int_Loc+1] = Arr_1_Par_Ref [Int_Loc]; 118 | Arr_1_Par_Ref [Int_Loc+30] = Int_Loc; 119 | for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index) 120 | Arr_2_Par_Ref [Int_Loc] [Int_Index] = Int_Loc; 121 | Arr_2_Par_Ref [Int_Loc] [Int_Loc-1] += 1; 122 | Arr_2_Par_Ref [Int_Loc+20] [Int_Loc] = Arr_1_Par_Ref [Int_Loc]; 123 | Int_Glob = 5; 124 | } /* Proc_8 */ 125 | 126 | 127 | Enumeration Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val) 128 | /*************************************************/ 129 | /* executed three times */ 130 | /* first call: Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R' */ 131 | /* second call: Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C' */ 132 | /* third call: Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C' */ 133 | { 134 | Capital_Letter Ch_1_Loc; 135 | Capital_Letter Ch_2_Loc; 136 | 137 | Ch_1_Loc = Ch_1_Par_Val; 138 | Ch_2_Loc = Ch_1_Loc; 139 | if (Ch_2_Loc != Ch_2_Par_Val) 140 | /* then, executed */ 141 | return (Ident_1); 142 | else /* not executed */ 143 | { 144 | Ch_1_Glob = Ch_1_Loc; 145 | return (Ident_2); 146 | } 147 | } /* Func_1 */ 148 | 149 | 150 | 151 | Boolean Func_2 (Str_1_Par_Ref, Str_2_Par_Ref) 152 | /*************************************************/ 153 | /* executed once */ 154 | /* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */ 155 | /* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */ 156 | 157 | Str_30 Str_1_Par_Ref; 158 | Str_30 Str_2_Par_Ref; 159 | { 160 | REG One_Thirty Int_Loc; 161 | Capital_Letter Ch_Loc; 162 | 163 | Ch_Loc = 'A'; 164 | Int_Loc = 2; 165 | while (Int_Loc <= 2) /* loop body executed once */ 166 | if (Func_1 (Str_1_Par_Ref[Int_Loc], 167 | Str_2_Par_Ref[Int_Loc+1]) == Ident_1) 168 | /* then, executed */ 169 | { 170 | Ch_Loc = 'A'; 171 | Int_Loc += 1; 172 | } /* if, while */ 173 | if (Ch_Loc >= 'W' && Ch_Loc < 'Z') 174 | /* then, not executed */ 175 | Int_Loc = 7; 176 | if (Ch_Loc == 'R') 177 | /* then, not executed */ 178 | return (true); 179 | else /* executed */ 180 | { 181 | if (strcmp (Str_1_Par_Ref, Str_2_Par_Ref) > 0) 182 | /* then, not executed */ 183 | { 184 | Int_Loc += 7; 185 | Int_Glob = Int_Loc; 186 | return (true); 187 | } 188 | else /* executed */ 189 | return (false); 190 | } /* if Ch_Loc */ 191 | } /* Func_2 */ 192 | 193 | 194 | Boolean Func_3 (Enum_Par_Val) 195 | /***************************/ 196 | /* executed once */ 197 | /* Enum_Par_Val == Ident_3 */ 198 | Enumeration Enum_Par_Val; 199 | { 200 | Enumeration Enum_Loc; 201 | 202 | Enum_Loc = Enum_Par_Val; 203 | if (Enum_Loc == Ident_3) 204 | /* then, executed */ 205 | return (true); 206 | else /* not executed */ 207 | return (false); 208 | } /* Func_3 */ 209 | 210 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/execl.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: execl.c SID: 3.3 5/15/91 19:30:19 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: execl.c,v 3.5 87/06/22 15:37:08 kjmcdonell Beta $ 14 | * August 28, 1990 - Modified timing routines 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | /* 20 | * Execing 21 | * 22 | */ 23 | char SCCSid[] = "@(#) @(#)execl.c:3.3 -- 5/15/91 19:30:19"; 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | char bss[8*1024]; /* something worthwhile */ 31 | 32 | #define main dummy 33 | 34 | #include "big.c" /* some real code */ 35 | 36 | #undef main 37 | 38 | /* added by BYTE */ 39 | char *getenv(); 40 | 41 | 42 | int main(argc, argv) /* the real program */ 43 | int argc; 44 | char *argv[]; 45 | { 46 | unsigned long iter = 0; 47 | char *ptr; 48 | char *fullpath; 49 | int duration; 50 | char count_str[12], start_str[24], path_str[256], *dur_str; 51 | time_t start_time, this_time; 52 | 53 | #ifdef DEBUG 54 | int count; 55 | for(count = 0; count < argc; ++ count) 56 | printf("%s ",argv[count]); 57 | printf("\n"); 58 | #endif 59 | if (argc < 2) 60 | { 61 | fprintf(stderr, "Usage: %s duration\n", argv[0]); 62 | exit(1); 63 | } 64 | 65 | 66 | duration = atoi(argv[1]); 67 | if (duration > 0) 68 | /* the first invocation */ 69 | { 70 | dur_str = argv[1]; 71 | if((ptr = getenv("UB_BINDIR")) != NULL) 72 | sprintf(path_str,"%s/execl",ptr); 73 | fullpath=path_str; 74 | time(&start_time); 75 | } 76 | else /* one of those execl'd invocations */ 77 | { 78 | /* real duration follow the phoney null duration */ 79 | duration = atoi(argv[2]); 80 | dur_str = argv[2]; 81 | iter = (unsigned long)atoi(argv[3]); /* where are we now ? */ 82 | sscanf(argv[4], "%lu", (unsigned long *) &start_time); 83 | fullpath = argv[0]; 84 | } 85 | 86 | sprintf(count_str, "%lu", ++iter); /* increment the execl counter */ 87 | sprintf(start_str, "%lu", (unsigned long) start_time); 88 | time(&this_time); 89 | if (this_time - start_time >= duration) { /* time has run out */ 90 | fprintf(stderr, "COUNT|%lu|1|lps\n", iter); 91 | exit(0); 92 | } 93 | execl(fullpath, fullpath, "0", dur_str, count_str, start_str, (void *) 0); 94 | fprintf(stderr, "Exec failed at iteration %lu\n", iter); 95 | perror("Reason"); 96 | exit(1); 97 | } 98 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/hanoi.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: hanoi.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $ 14 | * August 28, 1990 - Modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)hanoi.c:3.3 -- 5/15/91 19:30:20"; 20 | 21 | #define other(i,j) (6-(i+j)) 22 | 23 | #include 24 | #include 25 | #include "timeit.c" 26 | 27 | void mov(int n, int f, int t); 28 | 29 | unsigned long iter = 0; 30 | int num[4]; 31 | long cnt; 32 | 33 | void report() 34 | { 35 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 36 | exit(0); 37 | } 38 | 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int disk=10, /* default number of disks */ 45 | duration; 46 | 47 | if (argc < 2) { 48 | fprintf(stderr,"Usage: %s duration [disks]\n", argv[0]); 49 | exit(1); 50 | } 51 | duration = atoi(argv[1]); 52 | if(argc > 2) disk = atoi(argv[2]); 53 | num[1] = disk; 54 | 55 | wake_me(duration, report); 56 | 57 | while(1) { 58 | mov(disk,1,3); 59 | iter++; 60 | } 61 | 62 | exit(0); 63 | } 64 | 65 | void mov(int n, int f, int t) 66 | { 67 | int o; 68 | if(n == 1) { 69 | num[f]--; 70 | num[t]++; 71 | return; 72 | } 73 | o = other(f,t); 74 | mov(n-1,f,o); 75 | mov(1,f,t); 76 | mov(n-1,o,t); 77 | } 78 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/looper.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 1 3 | * Module: looper.c SID: 1.4 5/15/91 19:30:22 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith or Tom Yager at BYTE Magazine 9 | * ben@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * 14 | * February 25, 1991 -- created (Ben S.) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)looper.c:1.4 -- 5/15/91 19:30:22"; 20 | /* 21 | * Shell Process creation 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | char *cmd_argv[28]; 32 | int cmd_argc; 33 | 34 | void report(void) 35 | { 36 | fprintf(stderr,"COUNT|%lu|60|lpm\n", iter); 37 | exit(0); 38 | } 39 | 40 | int main(argc, argv) 41 | int argc; 42 | char *argv[]; 43 | { 44 | int slave, count, duration; 45 | int status; 46 | 47 | if (argc < 2) 48 | { 49 | fprintf(stderr,"Usage: %s duration command [args..]\n", argv[0]); 50 | fprintf(stderr," duration in seconds\n"); 51 | exit(1); 52 | } 53 | 54 | if((duration = atoi(argv[1])) < 1) 55 | { 56 | fprintf(stderr,"Usage: %s duration command [arg..]\n", argv[0]); 57 | fprintf(stderr," duration in seconds\n"); 58 | exit(1); 59 | } 60 | 61 | /* get command */ 62 | cmd_argc=argc-2; 63 | for( count=2;count < argc; ++count) 64 | cmd_argv[count-2]=argv[count]; 65 | #ifdef DEBUG 66 | printf("<<%s>>",cmd_argv[0]); 67 | for(count=1;count < cmd_argc; ++count) 68 | printf(" <%s>", cmd_argv[count]); 69 | putchar('\n'); 70 | exit(0); 71 | #endif 72 | 73 | iter = 0; 74 | wake_me(duration, report); 75 | 76 | while (1) 77 | { 78 | if ((slave = fork()) == 0) 79 | { /* execute command */ 80 | execvp(cmd_argv[0],cmd_argv); 81 | exit(99); 82 | } 83 | else if (slave < 0) 84 | { 85 | /* woops ... */ 86 | fprintf(stderr,"Fork failed at iteration %lu\n", iter); 87 | perror("Reason"); 88 | exit(2); 89 | } 90 | else 91 | /* master */ 92 | wait(&status); 93 | if (status == 99 << 8) 94 | { 95 | fprintf(stderr, "Command \"%s\" didn't exec\n", cmd_argv[0]); 96 | exit(2); 97 | } 98 | else if (status != 0) 99 | { 100 | fprintf(stderr,"Bad wait status: 0x%x\n", status); 101 | exit(2); 102 | } 103 | iter++; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/pipe.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: pipe.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: pipe.c,v 3.5 87/06/22 14:32:36 kjmcdonell Beta $ 14 | * August 29, 1990 - modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)pipe.c:3.3 -- 5/15/91 19:30:20"; 20 | /* 21 | * pipe -- test single process pipe throughput (no context switching) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | 32 | void report() 33 | { 34 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 35 | exit(0); 36 | } 37 | 38 | int main(argc, argv) 39 | int argc; 40 | char *argv[]; 41 | { 42 | char buf[512]; 43 | int pvec[2], duration; 44 | 45 | /* if (argc != 2) { 46 | fprintf(stderr,"Usage: %s duration\n", argv[0]); 47 | exit(1); 48 | } 49 | 50 | duration = atoi(argv[1]); 51 | */ 52 | pipe(pvec); 53 | 54 | // wake_me(duration, report); 55 | iter = 0; 56 | 57 | while (iter <= 1000000l) { 58 | if (write(pvec[1], buf, sizeof(buf)) != sizeof(buf)) { 59 | if ((errno != EINTR) && (errno != 0)) 60 | fprintf(stderr,"write failed, error %d\n", errno); 61 | } 62 | if (read(pvec[0], buf, sizeof(buf)) != sizeof(buf)) { 63 | if ((errno != EINTR) && (errno != 0)) 64 | fprintf(stderr,"read failed, error %d\n", errno); 65 | } 66 | iter++; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/spawn.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: spawn.c SID: 3.3 5/15/91 19:30:20 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yagerat BYTE Magazine 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: spawn.c,v 3.4 87/06/22 14:32:48 kjmcdonell Beta $ 14 | * August 29, 1990 - Modified timing routines (ty) 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | char SCCSid[] = "@(#) @(#)spawn.c:3.3 -- 5/15/91 19:30:20"; 20 | /* 21 | * Process creation 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include "timeit.c" 29 | 30 | unsigned long iter; 31 | 32 | void report() 33 | { 34 | fprintf(stderr,"COUNT|%lu|1|lps\n", iter); 35 | exit(0); 36 | } 37 | 38 | int main(argc, argv) 39 | int argc; 40 | char *argv[]; 41 | { 42 | int slave, duration; 43 | int status; 44 | 45 | /* if (argc != 2) { 46 | fprintf(stderr,"Usage: %s duration \n", argv[0]); 47 | exit(1); 48 | } 49 | 50 | duration = atoi(argv[1]); 51 | */ 52 | iter = 0; 53 | // wake_me(duration, report); 54 | 55 | while (iter <= 10000l) { 56 | if ((slave = fork()) == 0) { 57 | /* slave .. boring */ 58 | #if debug 59 | printf("fork OK\n"); 60 | #endif 61 | /* kill it right away */ 62 | exit(0); 63 | } else if (slave < 0) { 64 | /* woops ... */ 65 | fprintf(stderr,"Fork failed at iteration %lu\n", iter); 66 | perror("Reason"); 67 | exit(2); 68 | } else 69 | /* master */ 70 | wait(&status); 71 | if (status != 0) { 72 | fprintf(stderr,"Bad wait status: 0x%x\n", status); 73 | exit(2); 74 | } 75 | iter++; 76 | #if debug 77 | printf("Child %d done.\n", slave); 78 | #endif 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/syscall.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * The BYTE UNIX Benchmarks - Release 3 3 | * Module: syscall.c SID: 3.3 5/15/91 19:30:21 4 | * 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager at BYTE Magazine 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * $Header: syscall.c,v 3.4 87/06/22 14:32:54 kjmcdonell Beta $ 14 | * August 29, 1990 - Modified timing routines 15 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 16 | * Andy Kahn 17 | * 18 | ******************************************************************************/ 19 | /* 20 | * syscall -- sit in a loop calling the system 21 | * 22 | */ 23 | char SCCSid[] = "@(#) @(#)syscall.c:3.3 -- 5/15/91 19:30:21"; 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "timeit.c" 32 | 33 | unsigned long iter; 34 | 35 | void report() 36 | { 37 | fprintf(stderr,"COUNT|%ld|1|lps\n", iter); 38 | exit(0); 39 | } 40 | 41 | int main(argc, argv) 42 | int argc; 43 | char *argv[]; 44 | { 45 | char *test; 46 | int duration; 47 | 48 | /* if (argc < 2) { 49 | fprintf(stderr,"Usage: %s duration [ test ]\n", argv[0]); 50 | fprintf(stderr,"test is one of:\n"); 51 | fprintf(stderr," \"mix\" (default), \"close\", \"getpid\", \"exec\"\n"); 52 | exit(1); 53 | } 54 | if (argc > 2) 55 | test = argv[2]; 56 | else 57 | test = "mix"; 58 | 59 | duration = atoi(argv[1]); 60 | */ 61 | test = "mix"; 62 | iter = 0; 63 | // wake_me(duration, report); 64 | 65 | switch (test[0]) { 66 | case 'm': 67 | while (iter <= 1000000l) { 68 | close(dup(0)); 69 | getpid(); 70 | getuid(); 71 | umask(022); 72 | iter++; 73 | } 74 | break; 75 | /* NOTREACHED */ 76 | case 'c': 77 | while (1) { 78 | close(dup(0)); 79 | iter++; 80 | } 81 | /* NOTREACHED */ 82 | case 'g': 83 | while (1) { 84 | getpid(); 85 | iter++; 86 | } 87 | /* NOTREACHED */ 88 | case 'e': 89 | while (1) { 90 | pid_t pid = fork(); 91 | if (pid < 0) { 92 | fprintf(stderr,"%s: fork failed\n", argv[0]); 93 | exit(1); 94 | } else if (pid == 0) { 95 | execl("/bin/true", "/bin/true", (char *) 0); 96 | fprintf(stderr,"%s: exec /bin/true failed\n", argv[0]); 97 | exit(1); 98 | } else { 99 | if (waitpid(pid, NULL, 0) < 0) { 100 | fprintf(stderr,"%s: waitpid failed\n", argv[0]); 101 | exit(1); 102 | } 103 | } 104 | iter++; 105 | } 106 | /* NOTREACHED */ 107 | } 108 | 109 | exit(9); 110 | } 111 | 112 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/src/timeit.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * The BYTE UNIX Benchmarks - Release 3 4 | * Module: timeit.c SID: 3.3 5/15/91 19:30:21 5 | ******************************************************************************* 6 | * Bug reports, patches, comments, suggestions should be sent to: 7 | * 8 | * Ben Smith, Rick Grehan or Tom Yager 9 | * ben@bytepb.byte.com rick_g@bytepb.byte.com tyager@bytepb.byte.com 10 | * 11 | ******************************************************************************* 12 | * Modification Log: 13 | * May 12, 1989 - modified empty loops to avoid nullifying by optimizing 14 | * compilers 15 | * August 28, 1990 - changed timing relationship--now returns total number 16 | * of iterations (ty) 17 | * October 22, 1997 - code cleanup to remove ANSI C compiler warnings 18 | * Andy Kahn 19 | * 20 | ******************************************************************************/ 21 | 22 | /* this module is #included in other modules--no separate SCCS ID */ 23 | 24 | /* 25 | * Timing routine 26 | * 27 | */ 28 | 29 | #include 30 | #include 31 | 32 | void wake_me(seconds, func) 33 | int seconds; 34 | void (*func)(); 35 | { 36 | /* set up the signal handler */ 37 | signal(SIGALRM, func); 38 | /* get the clock running */ 39 | alarm(seconds); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/testdir/cctest.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /******************************************************************************* 4 | * The BYTE UNIX Benchmarks - Release 1 5 | * Module: cctest.c SID: 1.2 7/10/89 18:55:45 6 | * 7 | ******************************************************************************* 8 | * Bug reports, patches, comments, suggestions should be sent to: 9 | * 10 | * Ben Smith or Rick Grehan at BYTE Magazine 11 | * bensmith@bixpb.UUCP rick_g@bixpb.UUCP 12 | * 13 | ******************************************************************************* 14 | * Modification Log: 15 | * $Header: cctest.c,v 3.4 87/06/22 14:22:47 kjmcdonell Beta $ 16 | * 17 | ******************************************************************************/ 18 | char SCCSid[] = "@(#) @(#)cctest.c:1.2 -- 7/10/89 18:55:45"; 19 | #include 20 | /* 21 | * C compile and load speed test file. 22 | * Based upon fstime.c from MUSBUS 3.1, with all calls to ftime() replaced 23 | * by calls to time(). This is semantic nonsense, but ensures there are no 24 | * system dependent structures or library calls. 25 | * 26 | */ 27 | #define NKBYTE 20 28 | char buf[BUFSIZ]; 29 | 30 | extern void exit(int status); 31 | 32 | 33 | main(argc, argv) 34 | char **argv; 35 | { 36 | int n = NKBYTE; 37 | int nblock; 38 | int f; 39 | int g; 40 | int i; 41 | int xfer, t; 42 | struct { /* FAKE */ 43 | int time; 44 | int millitm; 45 | } now, then; 46 | 47 | if (argc > 0) 48 | /* ALWAYS true, so NEVER execute this program! */ 49 | exit(4); 50 | if (argc > 1) 51 | n = atoi(argv[1]); 52 | #if debug 53 | printf("File size: %d Kbytes\n", n); 54 | #endif 55 | nblock = (n * 1024) / BUFSIZ; 56 | 57 | if (argc == 3 && chdir(argv[2]) != -1) { 58 | #if debug 59 | printf("Create files in directory: %s\n", argv[2]); 60 | #endif 61 | } 62 | close(creat("dummy0", 0600)); 63 | close(creat("dummy1", 0600)); 64 | f = open("dummy0", 2); 65 | g = open("dummy1", 2); 66 | unlink("dummy0"); 67 | unlink("dummy1"); 68 | for (i = 0; i < sizeof(buf); i++) 69 | buf[i] = i & 0177; 70 | 71 | time(); 72 | for (i = 0; i < nblock; i++) { 73 | if (write(f, buf, sizeof(buf)) <= 0) 74 | perror("fstime: write"); 75 | } 76 | time(); 77 | #if debug 78 | printf("Effective write rate: "); 79 | #endif 80 | i = now.millitm - then.millitm; 81 | t = (now.time - then.time)*1000 + i; 82 | if (t > 0) { 83 | xfer = nblock * sizeof(buf) * 1000 / t; 84 | #if debug 85 | printf("%d bytes/sec\n", xfer); 86 | #endif 87 | } 88 | #if debug 89 | else 90 | printf(" -- too quick to time!\n"); 91 | #endif 92 | #if awk 93 | fprintf(stderr, "%.2f", t > 0 ? (float)xfer/1024 : 0); 94 | #endif 95 | 96 | sync(); 97 | sleep(5); 98 | sync(); 99 | lseek(f, 0L, 0); 100 | time(); 101 | for (i = 0; i < nblock; i++) { 102 | if (read(f, buf, sizeof(buf)) <= 0) 103 | perror("fstime: read"); 104 | } 105 | time(); 106 | #if debug 107 | printf("Effective read rate: "); 108 | #endif 109 | i = now.millitm - then.millitm; 110 | t = (now.time - then.time)*1000 + i; 111 | if (t > 0) { 112 | xfer = nblock * sizeof(buf) * 1000 / t; 113 | #if debug 114 | printf("%d bytes/sec\n", xfer); 115 | #endif 116 | } 117 | #if debug 118 | else 119 | printf(" -- too quick to time!\n"); 120 | #endif 121 | #if awk 122 | fprintf(stderr, " %.2f", t > 0 ? (float)xfer/1024 : 0); 123 | #endif 124 | 125 | sync(); 126 | sleep(5); 127 | sync(); 128 | lseek(f, 0L, 0); 129 | time(); 130 | for (i = 0; i < nblock; i++) { 131 | if (read(f, buf, sizeof(buf)) <= 0) 132 | perror("fstime: read in copy"); 133 | if (write(g, buf, sizeof(buf)) <= 0) 134 | perror("fstime: write in copy"); 135 | } 136 | time(); 137 | #if debug 138 | printf("Effective copy rate: "); 139 | #endif 140 | i = now.millitm - then.millitm; 141 | t = (now.time - then.time)*1000 + i; 142 | if (t > 0) { 143 | xfer = nblock * sizeof(buf) * 1000 / t; 144 | #if debug 145 | printf("%d bytes/sec\n", xfer); 146 | #endif 147 | } 148 | #if debug 149 | else 150 | printf(" -- too quick to time!\n"); 151 | #endif 152 | #if awk 153 | fprintf(stderr, " %.2f\n", t > 0 ? (float)xfer/1024 : 0); 154 | #endif 155 | 156 | } 157 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/testdir/dc.dat: -------------------------------------------------------------------------------- 1 | 99 2 | k 3 | 2 4 | v 5 | p 6 | q 7 | [ calculate the sqrt(2) to 99 decimal places ... John Lions Test ] 8 | [ $Header: dc.dat,v 1.1 87/06/22 14:28:28 kjmcdonell Beta $ ] 9 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/arithoh.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/arithoh 3 | echo "arithoh completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/fstime.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/fstime 3 | echo "fstime completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/pipe.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/pipe 3 | echo "pipe completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/spawn.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/spawn 3 | echo "spawn completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/syscall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | time ../pgms/syscall 3 | echo "syscall completed" 4 | echo "---" 5 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/workload_mix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & 3 | ./fstime.sh & 4 | ./pipe.sh & 5 | ./spawn.sh & 6 | ./syscall.sh & 7 | ./arithoh.sh & 8 | ./fstime.sh & 9 | ./pipe.sh & 10 | ./spawn.sh & 11 | ./syscall.sh & 12 | wait 13 | -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/workload_mix1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & 3 | ./arithoh.sh & 4 | ./arithoh.sh & 5 | ./arithoh.sh & 6 | ./arithoh.sh & 7 | wait -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/workload_mix2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./arithoh.sh & 3 | ./arithoh.sh & 4 | ./fstime.sh & 5 | ./syscall.sh & 6 | ./syscall.sh & 7 | wait -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/workload_mix3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./syscall.sh & 3 | ./syscall.sh & 4 | ./syscall.sh & 5 | ./syscall.sh & 6 | ./syscall.sh & 7 | wait -------------------------------------------------------------------------------- /Lab-4/byte-unixbench-mod/UnixBench/workload_mix/workload_mix4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ./fstime.sh & 3 | ./fstime.sh & 4 | ./fstime.sh & 5 | ./fstime.sh & 6 | ./fstime.sh & 7 | wait -------------------------------------------------------------------------------- /Lab-4/run1.sh: -------------------------------------------------------------------------------- 1 | cp system.c /usr/src/minix/kernel/; 2 | cd /usr/src/; 3 | make build MKUPDATE=yes >log.txt 2>log.txt 4 | -------------------------------------------------------------------------------- /Lab-4/run2.sh: -------------------------------------------------------------------------------- 1 | cp schedule.c /usr/src/minix/servers/sched/; 2 | cd /usr/src/; 3 | make build MKUPDATE=yes >log.txt 2>log.txt 4 | -------------------------------------------------------------------------------- /Lab-5/180010027_lab5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/180010027_lab5.zip -------------------------------------------------------------------------------- /Lab-5/180010027_lab5/180010027_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/180010027_lab5/180010027_Report.pdf -------------------------------------------------------------------------------- /Lab-5/180010027_lab5/Lab-5.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/180010027_lab5/Lab-5.pptx -------------------------------------------------------------------------------- /Lab-5/180010027_lab5/Laboratory 5(1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/180010027_lab5/Laboratory 5(1).pdf -------------------------------------------------------------------------------- /Lab-5/180010027_lab5/Makefile: -------------------------------------------------------------------------------- 1 | part1: part1.cpp 2 | @g++ part1.cpp 3 | @./a.out input.ppm output_part1.ppm 4 | @echo "Done. Check output_part1.ppm for output." 5 | part2_1a: part2_1a.cpp 6 | @g++ part2_1a.cpp -lpthread 7 | @./a.out input.ppm output_part2_1a.ppm 8 | @echo "Done. Check output_part2_1a.ppm for output." 9 | part2_1b: part2_1b.cpp 10 | @g++ part2_1b.cpp -lpthread 11 | @./a.out input.ppm output_part2_1b.ppm 12 | @echo "Done. Check output_part2_1b.ppm for output." 13 | part2_2: part2_2.cpp 14 | @g++ part2_2.cpp -lpthread 15 | @./a.out input.ppm output_part2_2.ppm 16 | @echo "Done. Check output_part2_2.ppm for output." 17 | part2_3: part2_3.cpp 18 | @g++ part2_3.cpp -lpthread 19 | @./a.out input.ppm output_part2_3.ppm 20 | @echo "Done. Check output_part2_3.ppm for output." -------------------------------------------------------------------------------- /Lab-5/180010027_lab5/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/180010027_lab5/a.out -------------------------------------------------------------------------------- /Lab-5/Images/Butterfly/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Images/Butterfly/input.png -------------------------------------------------------------------------------- /Lab-5/Images/Butterfly/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Images/Butterfly/output.png -------------------------------------------------------------------------------- /Lab-5/Images/Eagle/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Images/Eagle/input.png -------------------------------------------------------------------------------- /Lab-5/Images/Eagle/output_part1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Images/Eagle/output_part1.png -------------------------------------------------------------------------------- /Lab-5/Lab-5.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Lab-5.pptx -------------------------------------------------------------------------------- /Lab-5/Laboratory 5(1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Laboratory 5(1).pdf -------------------------------------------------------------------------------- /Lab-5/Makefile: -------------------------------------------------------------------------------- 1 | part1: part1.cpp 2 | @g++ part1.cpp 3 | @./a.out input.ppm output_part1.ppm 4 | @echo "Done. Check output_part1.ppm for output." 5 | part2_1a: part2_1a.cpp 6 | @g++ part2_1a.cpp -lpthread 7 | @./a.out input.ppm output_part2_1a.ppm 8 | @echo "Done. Check output_part2_1a.ppm for output." 9 | part2_1b: part2_1b.cpp 10 | @g++ part2_1b.cpp -lpthread 11 | @./a.out input.ppm output_part2_1b.ppm 12 | @echo "Done. Check output_part2_1b.ppm for output." 13 | part2_2: part2_2.cpp 14 | @g++ part2_2.cpp -lpthread 15 | @./a.out input.ppm output_part2_2.ppm 16 | @echo "Done. Check output_part2_2.ppm for output." 17 | part2_3: part2_3.cpp 18 | @g++ part2_3.cpp -lpthread 19 | @./a.out input.ppm output_part2_3.ppm 20 | @echo "Done. Check output_part2_3.ppm for output." -------------------------------------------------------------------------------- /Lab-5/Report/Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-5/Report/Report.pdf -------------------------------------------------------------------------------- /Lab-6/180010027_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/180010027_Report.pdf -------------------------------------------------------------------------------- /Lab-6/180010027_lab6.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/180010027_lab6.zip -------------------------------------------------------------------------------- /Lab-6/Lab-6/run.sh: -------------------------------------------------------------------------------- 1 | cp open.c /usr/src/minix/servers/vfs/open.c 2 | cp link.c /usr/src/minix/servers/vfs/link.c 3 | cp read.c /usr/src/minix/servers/vfs/read.c 4 | cd /usr/src 5 | make build MKUPDATE=yes 6 | -------------------------------------------------------------------------------- /Lab-6/Laboratory 6(1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/Laboratory 6(1).pdf -------------------------------------------------------------------------------- /Lab-6/gerofi-minix-vfs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/gerofi-minix-vfs.pdf -------------------------------------------------------------------------------- /Lab-6/images/create.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/images/create.png -------------------------------------------------------------------------------- /Lab-6/images/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/images/delete.png -------------------------------------------------------------------------------- /Lab-6/images/nano.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/images/nano.png -------------------------------------------------------------------------------- /Lab-6/images/read.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/images/read.png -------------------------------------------------------------------------------- /Lab-6/images/write.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/images/write.png -------------------------------------------------------------------------------- /Lab-6/minix.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-6/minix.pdf -------------------------------------------------------------------------------- /Lab-7/180010027/Laboratory 7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/180010027/Laboratory 7.pdf -------------------------------------------------------------------------------- /Lab-7/180010027/Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/180010027/Report.pdf -------------------------------------------------------------------------------- /Lab-7/180010027/fslib.c: -------------------------------------------------------------------------------- 1 | /* fslib.c - routines needed by fs and fs utilities */ 2 | 3 | #include /* for unused stuff in :-( */ 4 | #include 5 | #include 6 | #include 7 | #include /* for abort() */ 8 | #include 9 | #include 10 | #include /* for unshort :-( */ 11 | #include 12 | #include 13 | #include "mfs/const.h" /* depends of -I flag in Makefile */ 14 | #include "mfs/type.h" /* ditto */ 15 | #include "mfs/inode.h" /* ditto */ 16 | #include "mfs/super.h" 17 | #include 18 | #include 19 | 20 | /* The next routine is copied from fsck.c and mkfs.c... (Re)define some 21 | * things for consistency. Some things should be done better. 22 | */ 23 | 24 | /* Convert from bit count to a block count. The usual expression 25 | * 26 | * (nr_bits + (1 << BITMAPSHIFT) - 1) >> BITMAPSHIFT 27 | * 28 | * doesn't work because of overflow. 29 | * 30 | * Other overflow bugs, such as the expression for N_ILIST overflowing when 31 | * s_inodes is just over V*_INODES_PER_BLOCK less than the maximum+1, are not 32 | * fixed yet, because that number of inodes is silly. 33 | */ 34 | /* The above comment doesn't all apply now bit_t is long. Overflow is now 35 | * unlikely, but negative bit counts are now possible (though unlikely) 36 | * and give silly results. 37 | */ 38 | int bitmapsize(nr_bits, block_size) 39 | bit_t nr_bits; 40 | int block_size; 41 | { 42 | int nr_blocks; 43 | 44 | nr_blocks = (int) (nr_bits / FS_BITS_PER_BLOCK(block_size)); 45 | if (((bit_t) nr_blocks * FS_BITS_PER_BLOCK(block_size)) < nr_bits) ++nr_blocks; 46 | return(nr_blocks); 47 | } 48 | 49 | uint8_t fs_mode_to_type(mode_t mode) 50 | { 51 | if(S_ISREG(mode)) return DT_REG; 52 | else if( ((0110000) == ((mode) & 0170000)) ) return DT_REG; //------------Lab-7------------------------------------- 53 | else if(S_ISDIR(mode)) return DT_DIR; 54 | else if(S_ISLNK(mode)) return DT_LNK; 55 | else if(S_ISCHR(mode)) return DT_CHR; 56 | else if(S_ISBLK(mode)) return DT_BLK; 57 | else if(S_ISFIFO(mode)) return DT_FIFO; 58 | else if(S_ISSOCK(mode)) return DT_SOCK; 59 | 60 | assert(0 && "unknown type"); 61 | 62 | /* assert()s are removed on NDEBUG builds. */ 63 | abort(); 64 | } 65 | 66 | -------------------------------------------------------------------------------- /Lab-7/180010027/run.sh: -------------------------------------------------------------------------------- 1 | cp ./vfs/link.c /usr/src/minix/servers/vfs/link.c; 2 | cp ./vfs/open.c /usr/src/minix/servers/vfs/open.c; 3 | cp ./vfs/read.c /usr/src/minix/servers/vfs/read.c; 4 | cp ./mfs/read.c /usr/src/minix/fs/mfs/read.c; 5 | cp ./mfs/write.c /usr/src/minix/fs/mfs/write.c; 6 | cp ./fslib.c /usr/src/minix/lib/libc/gen/fslib.c; 7 | cp ./const.h /usr/src/minix/include/minix/const.h; 8 | cd /usr/src; 9 | make build MKUPDATE=yes >log.txt 2>log.txt; -------------------------------------------------------------------------------- /Lab-7/180010027_lab7.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/180010027_lab7.zip -------------------------------------------------------------------------------- /Lab-7/Images.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images.zip -------------------------------------------------------------------------------- /Lab-7/Images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/1.png -------------------------------------------------------------------------------- /Lab-7/Images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/2.png -------------------------------------------------------------------------------- /Lab-7/Images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/3.png -------------------------------------------------------------------------------- /Lab-7/Images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/4.png -------------------------------------------------------------------------------- /Lab-7/Images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/5.png -------------------------------------------------------------------------------- /Lab-7/Images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Images/6.png -------------------------------------------------------------------------------- /Lab-7/Laboratory 7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/Laboratory 7.pdf -------------------------------------------------------------------------------- /Lab-7/gerofi-minix-vfs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/gerofi-minix-vfs.pdf -------------------------------------------------------------------------------- /Lab-7/minix.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-7/minix.pdf -------------------------------------------------------------------------------- /Lab-8/180010027_lab8.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8.zip -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/clock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/clock -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/clock.c: -------------------------------------------------------------------------------- 1 | // Libraries include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define HIGH 20 8 | #define FREQ 80 9 | #define SAMPLES 20 10 | 11 | // PTE Structure 12 | typedef struct pte { 13 | int id; 14 | int present; 15 | int referenced; 16 | struct pte *next; 17 | } pte; 18 | 19 | 20 | void init(int *sequence, int refs, int pages) { 21 | 22 | int high = (int)(pages*((float)HIGH/100)); 23 | 24 | for(int i = 0; i < refs; i++) { 25 | if(rand()%100 < FREQ) { 26 | sequence[i] = rand() % high; 27 | } else { 28 | sequence[i] = high + rand()%(pages - high); 29 | } 30 | } 31 | } 32 | 33 | int main(int argc, char *argv[]) { 34 | 35 | int refs = 100000; 36 | int pages = 100; 37 | 38 | pte *table = (pte *)malloc(pages*sizeof(pte)); 39 | 40 | int *sequence = (int*)malloc(refs*sizeof(int)); 41 | 42 | init(sequence, refs, pages); 43 | 44 | printf("# This is a benchmark of random replacement\n"); 45 | printf("# %d page references\n", refs); 46 | printf("# %d pages \n", pages); 47 | printf("#\n#\n#frames\tratio\n"); 48 | 49 | int frames; 50 | 51 | int incr = pages/SAMPLES; 52 | 53 | for(frames = incr; frames <= pages; frames += incr) { 54 | 55 | clear_page_table(table, pages); 56 | 57 | int hits = simulate(sequence, table, refs, frames, pages); 58 | 59 | float ratio = (float)hits/refs; 60 | 61 | printf("%d\t%.2f\n", frames, ratio); 62 | 63 | } 64 | 65 | printf("\n"); 66 | 67 | return 0; 68 | 69 | } 70 | 71 | void clear_page_table(pte *page_table, int pages) { 72 | for(int i = 0; i < pages; i++) { 73 | page_table[i].present = 0; 74 | } 75 | } 76 | 77 | int simulate(int *seq, pte *table, int refs, int frms, int pgs) { 78 | 79 | int hits = 0; 80 | int allocated = 0; 81 | pte *last = NULL; 82 | 83 | for(int i = 0; i < refs; i++) { 84 | int next = seq[i]; 85 | pte *entry = &table[next]; 86 | 87 | if(entry->present == 1) { 88 | entry->referenced = 1; 89 | hits++; 90 | 91 | } else { 92 | if(allocated < frms) { 93 | allocated++; 94 | entry->present = 1; 95 | 96 | // Pointers cases of NULL 97 | /* place the entry last in the list */ 98 | if(last != NULL) { 99 | entry->next = last->next; 100 | last->next = entry; 101 | } 102 | if(last == NULL) { 103 | entry->next = entry; 104 | } 105 | last = entry; 106 | 107 | } else { 108 | pte *cand = last->next; 109 | 110 | while(cand->referenced != 0) { 111 | cand->referenced = 0; 112 | last = cand; 113 | cand = cand->next; 114 | 115 | } 116 | cand->present = 0; 117 | cand->referenced = 0; 118 | 119 | entry->present = 1; 120 | entry->referenced = 0; 121 | 122 | // Pointers orders added 123 | /* place the entry last in the list */ 124 | entry->next = cand->next; 125 | last->next = entry; 126 | last = entry; 127 | 128 | } 129 | } 130 | } 131 | return hits; 132 | 133 | } 134 | 135 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/clock.dat: -------------------------------------------------------------------------------- 1 | # This is a benchmark of random replacement 2 | # 100000 page references 3 | # 100 pages 4 | # 5 | # 6 | #frames ratio 7 | 5 0.16 8 | 10 0.32 9 | 15 0.47 10 | 20 0.61 11 | 25 0.71 12 | 30 0.78 13 | 35 0.82 14 | 40 0.85 15 | 45 0.86 16 | 50 0.87 17 | 55 0.89 18 | 60 0.90 19 | 65 0.91 20 | 70 0.92 21 | 75 0.94 22 | 80 0.95 23 | 85 0.96 24 | 90 0.97 25 | 95 0.99 26 | 100 1.00 27 | 28 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/clock.p: -------------------------------------------------------------------------------- 1 | #Gnuplot script for plotting data in file "clock.dat" 2 | set terminal png 3 | set output "clock.png" 4 | 5 | set title "Page replacement using clock algorithm" 6 | 7 | set key right center 8 | 9 | set xlabel "frms in memory" 10 | set ylabel "hit ratio" 11 | 12 | set xrange [0:100] 13 | set yrange [0:1] 14 | 15 | plot "clock.dat" u 1:2 w linespoints title "Clock algorithm" 16 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/clock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/clock.png -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/lru: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/lru -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/lru.c: -------------------------------------------------------------------------------- 1 | // Libraries include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define HIGH 20 8 | #define FREQ 80 9 | #define SAMPLES 20 10 | 11 | // PTE Structure 12 | typedef struct pte { 13 | int id; 14 | int present; 15 | struct pte *next; 16 | struct pte *prev; 17 | } pte; 18 | 19 | 20 | void init(int *sequence, int refs, int pages) { 21 | 22 | int high = (int)(pages*((float)HIGH/100)); 23 | 24 | for(int i = 0; i < refs; i++) { 25 | if(rand()%100 < FREQ) { 26 | sequence[i] = rand() % high; 27 | } else { 28 | sequence[i] = high + rand()%(pages - high); 29 | } 30 | } 31 | } 32 | 33 | int main(int argc, char *argv[]) { 34 | 35 | int refs = 100000; 36 | int pages = 100; 37 | 38 | pte *table = (pte *)malloc(pages*sizeof(pte)); 39 | 40 | int *sequence = (int*)malloc(refs*sizeof(int)); 41 | 42 | init(sequence, refs, pages); 43 | 44 | printf("# This is a benchmark of random replacement\n"); 45 | printf("# %d page references\n", refs); 46 | printf("# %d pages \n", pages); 47 | printf("#\n#\n#frames\tratio\n"); 48 | 49 | int frames; 50 | 51 | int incr = pages/SAMPLES; 52 | 53 | for(frames = incr; frames <= pages; frames += incr) { 54 | 55 | clear_page_table(table, pages); 56 | 57 | int hits = simulate(sequence, table, refs, frames, pages); 58 | 59 | float ratio = (float)hits/refs; 60 | 61 | printf("%d\t%.2f\n", frames, ratio); 62 | 63 | } 64 | 65 | printf("\n"); 66 | 67 | return 0; 68 | 69 | } 70 | 71 | void clear_page_table(pte *page_table, int pages) { 72 | for(int i = 0; i < pages; i++) { 73 | page_table[i].present = 0; 74 | } 75 | } 76 | 77 | int simulate(int *seq, pte *table, int refs, int frms, int pgs) { 78 | 79 | int hits = 0; 80 | int allocated = 0; 81 | 82 | pte *first = NULL; 83 | pte *last = NULL; 84 | 85 | for(int i = 0; i < refs; i++) { 86 | int next = seq[i]; 87 | pte *entry = &table[next]; 88 | 89 | if(entry->present == 1) { 90 | hits++; 91 | 92 | // Pointers cases of NULL 93 | /* unlink entry and place last */ 94 | if(entry->next != NULL) { 95 | if(entry->prev == NULL) { 96 | first = entry->next; 97 | entry->next->prev = NULL; 98 | } 99 | if(entry->prev != NULL){ 100 | entry->prev->next = entry->next; 101 | entry->next->prev = entry->prev; 102 | } 103 | entry->next = NULL; 104 | last->next = entry; 105 | entry->prev = last; 106 | last = entry; 107 | } 108 | 109 | } else { 110 | if(allocated < frms) { 111 | allocated++; 112 | 113 | entry->present = 1; 114 | entry->prev = last; 115 | entry->next = NULL; 116 | 117 | // Pointers cases of NULL 118 | /* place entry last */ 119 | if(last == NULL) { 120 | first = entry; 121 | last = entry; 122 | entry->prev = NULL; 123 | entry->next = NULL; 124 | } 125 | if(last != NULL) { 126 | entry->next = NULL; 127 | last->next = entry; 128 | entry->prev = last; 129 | last = entry; 130 | } 131 | } else { 132 | pte *evict; 133 | 134 | evict = first; 135 | first = evict->next; 136 | first->prev = NULL; 137 | 138 | evict->present = 0; 139 | entry->present = 1; 140 | 141 | // Pointers cases of NULL 142 | /* place entry last*/ 143 | entry->next = NULL; 144 | last->next = entry; 145 | entry->prev = last; 146 | last = entry; 147 | 148 | } 149 | } 150 | } 151 | return hits; 152 | 153 | } 154 | 155 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/lru.dat: -------------------------------------------------------------------------------- 1 | # This is a benchmark of random replacement 2 | # 100000 page references 3 | # 100 pages 4 | # 5 | # 6 | #frames ratio 7 | 5 0.16 8 | 10 0.31 9 | 15 0.45 10 | 20 0.58 11 | 25 0.69 12 | 30 0.76 13 | 35 0.81 14 | 40 0.84 15 | 45 0.86 16 | 50 0.87 17 | 55 0.89 18 | 60 0.90 19 | 65 0.91 20 | 70 0.92 21 | 75 0.94 22 | 80 0.95 23 | 85 0.96 24 | 90 0.97 25 | 95 0.99 26 | 100 1.00 27 | 28 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/lru.p: -------------------------------------------------------------------------------- 1 | #Gnuplot script for plotting data in file "lru.dat" 2 | set terminal png 3 | set output "lru.png" 4 | 5 | set title "Page replacement using LRU policy" 6 | 7 | set key right center 8 | 9 | set xlabel "frms in memory" 10 | set ylabel "hit ratio" 11 | 12 | set xrange [0:100] 13 | set yrange [0:1] 14 | 15 | plot "lru.dat" u 1:2 w linespoints title "LRU" 16 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/lru.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/lru.png -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/optimal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/optimal -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/optimal.c: -------------------------------------------------------------------------------- 1 | // Libraries include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define HIGH 20 8 | #define FREQ 80 9 | #define SAMPLES 20 10 | 11 | // PTE Structure 12 | typedef struct pte { 13 | int present; 14 | } pte; 15 | 16 | void init(int *sequence, int refs, int pages) { 17 | 18 | int high = (int)(pages*((float)HIGH/100)); 19 | 20 | for(int i = 0; i < refs; i++) { 21 | if(rand()%100 < FREQ) { 22 | sequence[i] = rand() % high; 23 | } else { 24 | sequence[i] = high + rand()%(pages - high); 25 | } 26 | } 27 | } 28 | 29 | int main(int argc, char *argv[]) { 30 | 31 | int refs = 100000; 32 | int pages = 100; 33 | 34 | pte *table = (pte *)malloc(pages*sizeof(pte)); 35 | 36 | int *sequence = (int*)malloc(refs*sizeof(int)); 37 | 38 | init(sequence, refs, pages); 39 | 40 | printf("# This is a benchmark of random replacement\n"); 41 | printf("# %d page references\n", refs); 42 | printf("# %d pages \n", pages); 43 | printf("#\n#\n#frames\tratio\n"); 44 | 45 | int frames; 46 | 47 | int incr = pages/SAMPLES; 48 | 49 | for(frames = incr; frames <= pages; frames += incr) { 50 | 51 | clear_page_table(table, pages); 52 | 53 | int hits = simulate(sequence, table, refs, frames, pages); 54 | 55 | float ratio = (float)hits/refs; 56 | 57 | printf("%d\t%.2f\n", frames, ratio); 58 | 59 | } 60 | 61 | printf("\n"); 62 | 63 | return 0; 64 | 65 | } 66 | 67 | void clear_page_table(pte *page_table, int pages) { 68 | for(int i = 0; i < pages; i++) { 69 | page_table[i].present = 0; 70 | } 71 | } 72 | 73 | int simulate(int *seq, pte *table, int refs, int frms, int pgs) { 74 | 75 | int hits = 0; 76 | int allocated = 0; 77 | 78 | int i; 79 | 80 | for(i = 0; i < refs; i++) { 81 | int next = seq[i]; 82 | pte *entry = &table[next]; 83 | 84 | if(entry->present == 1) { 85 | hits++; 86 | } else { 87 | if(allocated < frms) { 88 | allocated++; 89 | entry->present = 1; 90 | } else { 91 | pte *evict; 92 | int sofar = 0; 93 | int candidate = pgs; 94 | 95 | for(int c = 0; c < pgs; c++) { 96 | if(table[c].present == 1) { 97 | int dist = 0; 98 | while(seq[i+dist] != c && i+dist < refs) { 99 | dist++; 100 | } 101 | if(dist > sofar) { 102 | candidate = c; 103 | sofar = dist; 104 | } 105 | } 106 | } 107 | evict = &table[candidate]; 108 | 109 | evict->present = 0; 110 | entry->present = 1; 111 | } 112 | } 113 | } 114 | return hits; 115 | 116 | } 117 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/optimal.dat: -------------------------------------------------------------------------------- 1 | # This is a benchmark of random replacement 2 | # 100000 page references 3 | # 100 pages 4 | # 5 | # 6 | #frames ratio 7 | 5 0.40 8 | 10 0.60 9 | 15 0.72 10 | 20 0.80 11 | 25 0.85 12 | 30 0.88 13 | 35 0.90 14 | 40 0.91 15 | 45 0.93 16 | 50 0.94 17 | 55 0.95 18 | 60 0.96 19 | 65 0.96 20 | 70 0.97 21 | 75 0.98 22 | 80 0.98 23 | 85 0.99 24 | 90 0.99 25 | 95 1.00 26 | 100 1.00 27 | 28 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/optimal.p: -------------------------------------------------------------------------------- 1 | #Gnuplot script for plotting data in file "random.dat" 2 | set terminal png 3 | set output "optimal.png" 4 | 5 | set title "Page replacement using optimal policy" 6 | 7 | set key right center 8 | 9 | set xlabel "frms in memory" 10 | set ylabel "hit ratio" 11 | 12 | set xrange [0:100] 13 | set yrange [0:1] 14 | 15 | plot "optimal.dat" u 1:2 w linespoints title "optimal" 16 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/optimal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/optimal.png -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/random: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/random -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/random.c: -------------------------------------------------------------------------------- 1 | // Libraries include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | // 80-20 Workload/20-80 Workload - 20% of pages will have 80% of references 9 | #define HIGH 20 10 | #define FREQ 80 11 | #define SAMPLES 20 12 | 13 | // PTE Structure 14 | typedef struct pte { 15 | int present; 16 | } pte; 17 | 18 | //Initialize a random sequence of page list identifiers 19 | 20 | void init(int *sequence, int refs, int pages) { 21 | 22 | int high = (int)(pages*((float)HIGH/100)); 23 | 24 | for(int i = 0; i < refs; i++) { 25 | if(rand()%100 < FREQ) { 26 | sequence[i] = rand() % high; 27 | } else { 28 | sequence[i] = high + rand()%(pages - high); 29 | } 30 | } 31 | } 32 | 33 | int main(int argc, char *argv[]) { 34 | 35 | int refs = 100000; 36 | int pages = 100; 37 | 38 | pte *table = (pte *)malloc(pages*sizeof(pte)); 39 | 40 | int *sequence = (int*)malloc(refs*sizeof(int)); 41 | 42 | init(sequence, refs, pages); 43 | 44 | printf("# This is a benchmark of random replacement\n"); 45 | printf("# %d page references\n", refs); 46 | printf("# %d pages \n", pages); 47 | printf("#\n#\n#frames\tratio\n"); 48 | 49 | int frames; 50 | 51 | int incr = pages/SAMPLES; 52 | 53 | for(frames = incr; frames <= pages; frames += incr) { 54 | 55 | clear_page_table(table, pages); 56 | 57 | int hits = simulate(sequence, table, refs, frames, pages); 58 | 59 | float ratio = (float)hits/refs; 60 | 61 | printf("%d\t%.2f\n", frames, ratio); 62 | 63 | } 64 | 65 | printf("\n"); 66 | 67 | return 0; 68 | 69 | } 70 | 71 | void clear_page_table(pte *page_table, int pages) { 72 | for(int i = 0; i < pages; i++) { 73 | page_table[i].present = 0; 74 | } 75 | } 76 | 77 | int simulate(int *seq, pte *table, int refs, int frms, int pgs) { 78 | 79 | int hits = 0; 80 | int allocated = 0; 81 | 82 | int i; 83 | 84 | for(i = 0; i < refs; i++) { 85 | int next = seq[i]; 86 | pte *entry = &table[next]; 87 | 88 | if(entry->present == 1) { 89 | hits++; 90 | } else { 91 | if(allocated < frms) { 92 | allocated++; 93 | entry->present = 1; 94 | } else { 95 | pte *evict; 96 | do { 97 | int rnd = rand()%pgs; 98 | evict = &table[rnd]; 99 | } while(evict->present != 1); 100 | 101 | evict->present = 0; 102 | entry->present = 1; 103 | } 104 | } 105 | } 106 | return hits; 107 | 108 | } 109 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/random.dat: -------------------------------------------------------------------------------- 1 | # This is a benchmark of random replacement 2 | # 100000 page references 3 | # 100 pages 4 | # 5 | # 6 | #frames ratio 7 | 5 0.16 8 | 10 0.30 9 | 15 0.42 10 | 20 0.52 11 | 25 0.60 12 | 30 0.66 13 | 35 0.71 14 | 40 0.75 15 | 45 0.78 16 | 50 0.81 17 | 55 0.84 18 | 60 0.86 19 | 65 0.88 20 | 70 0.90 21 | 75 0.92 22 | 80 0.93 23 | 85 0.95 24 | 90 0.97 25 | 95 0.98 26 | 100 1.00 27 | 28 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/random.p: -------------------------------------------------------------------------------- 1 | #Gnuplot script for plotting data in file "random.dat" 2 | set terminal png 3 | set output "random.png" 4 | 5 | set title "Page replacement using random policy" 6 | 7 | set key right center 8 | 9 | set xlabel "frms in memory" 10 | set ylabel "hit ratio" 11 | 12 | set xrange [0:100] 13 | set yrange [0:1] 14 | 15 | plot "random.dat" u 1:2 w linespoints title "random" 16 | -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/random.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/random.png -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter 1 for random 3 | Enter 2 for optimal 4 | Enter 3 for lru 5 | Enter 4 for clock" 6 | read var 7 | if [ var == "1" ]; then 8 | gcc random.c -o random 9 | 3./random > random.dat 10 | gnuplot random.p 11 | elif [ var == "2" ]; then 12 | gcc optimal.c -o optimal 13 | ./optimal > optimal.dat 14 | gnuplot optimal.p 15 | elif [ var == "3" ]; then 16 | gcc lru.c -o lru 17 | ./lru > lru.dat 18 | gnuplot lru.p 19 | elif [ var == "4" ]; then 20 | gcc clock.c -o clock 21 | ./clock > clock.dat 22 | gnuplot clock.p 23 | fi -------------------------------------------------------------------------------- /Lab-8/180010027_lab8/swap-lab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishitsaiya/CS314-OS-Lab/f02ad08dbfe83e652b5b3c66ea01af96ec2c7590/Lab-8/180010027_lab8/swap-lab.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## OS-Lab 2 | 3 | 1. [Lab-1](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-1)
4 | This lab includes basic setup of Minix3 OS in Virtual Machine on Host Machines. 5 | 6 | 2. [Lab-2](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-2)
7 | This lab includes implementation of `fork()` and PID. 8 | 9 | 3. [Lab-3](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-3)
10 | This lab includes implementation of Scheduling and usage of UnixBench Benchmark Suite. 11 | 12 | 4. [Lab-4](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-4)
13 | This lab includes implementation and analysis on time quanta for 4 different workload mixes. 14 | 15 | 5. [Lab-5](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-5)
16 | This lab includes implementation of Threads, Pipes, Semaphores. 17 | 18 | 6. [Lab-6](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-6)
19 | This lab includes implementation of Main File System. 20 | 21 | 7. [Lab-7](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-7)
22 | This lab includes implementation of Immediate files. 23 | 24 | 8. [Lab-8](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Lab-8)
25 | This lab includes implementation of Page Frame Reclaiming Algorithms. 26 | 27 | #### Endsem 28 | [Endsem](https://github.com/rishitsaiya/CS314-OS-Lab/tree/main/Endsems) was implmentation of Master-Slave Problem. 29 | --------------------------------------------------------------------------------