

CST

Students in this course will learn about the use and design of modern operating systems, focusing on Linux. On the “use” side, students will learn the Linux command line, to write shell scripts, and to build programs with GNU utilities like awk, sed, and make. On the “design” side, students will develop a deep understanding of process management, memory management, file systems, and concurrency, and how they apply to modern technologies like virtualization and cloud computing.
Prerequisite(s)/Corequisite(s): [Prereq: CST 238 and MATH 130 with a C- or better]
Typically Offered: Fall, Spring
Units: 4

Memory Management Program
Description:
Description:
This code displays an example of managing cache, and calculating the page faults, when there is a hit or not with the cached memory.
// # 334 Lab 4 FIFO page replacement simulation File I/O template
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "queue.h" // using queue and dequeue for cache queue
int main(int argc, char* argv[] ) {
int C_SIZE = atoi(argv[1]); // size of cache passed by user
// ref_page cache[C_SIZE]; // cache that stores pages
char pageCache[100]; // cache that holds teh input from test file
int i;
int totalFaults = 0; // keep track of the total page faults/miss
int totalHits = 0;
// create the queue
struct queue* my_queue = queue_create();
// reset i for total counter
i=0;
// can use file handling for file
while ( fgets( pageCache, 100, stdin) ) {
int page_num = atoi(pageCache); // stores number read from file as int
// page replacement goes here
// also is it full? when full then dequeue last used one
// if this is not in queue? then enqueue
if ( queue_find(my_queue, page_num) == NULL) {
// if queue is full, then need to dequeue? can maybe use {i}, and total the hits and misses for total
if (queue_length(my_queue) == C_SIZE) {
// FIFO, remove first node
dequeue(my_queue);
}
// now queue should have a free space
enqueue(my_queue, page_num);
totalFaults++;
printf("Page number fault: %d\n", page_num);
}
// if its in queue, then its a hit.
else {
totalHits++;
}
// miss is when is not in cache
// hit is when it is cache
//printf("testing: %d\n", page_num);
// hit rate is hit/ total
i++;
}
queue_destroy(my_queue);
printf("\nCache size: %d \n", C_SIZE);
printf("Total pages: %d \n", i);
printf("Total Page Faults: %d \n", totalFaults);
printf("Total Page Hits: %d \n", totalHits++);
printf("Hit rate: %d / %d = %0.6f%% \n", totalHits, i, ( (float)totalHits/(float)i) * 100 );
return 0;
}

Multithreading Program
Description:
Description:
This code displays an example of a basic program that deals with managing a multithreaded program.
/* Name: Ethan
* Date: August 1, 2023
* Title: Lab 6 - task
* Description: This program will fix the producer and consumer issue, producing and consuming the alphabet
*/
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define NTHREADS 2
#define BUFF_SIZE 1
pthread_t p_threads[NTHREADS];
pthread_t c_threads[NTHREADS];
sem_t mutex;
sem_t sem_empty;
sem_t sem_full;
char buffer[BUFF_SIZE]; // 26 letters in alphabet
int count = 0; // index for buffer
char letter = 'A'; //initialize to setup for producer
// producer functon to produce the letters adding them to buffer
void* producer(void* args) {
while (letter <= 'Z') {
//produce a letter - {letter}
// add letter to the buffer
sem_wait(&sem_empty); // if empty is 0, no more elements and should wait
sem_wait(&mutex); // get lock for critical section
buffer[count] = letter;
//count++; // incremenet to next index and letter
printf("PRODUCED: %c\n", letter);
sem_post(&mutex); // release lock for next thread
sem_post(&sem_full); // produced a letter for buffer, ready to be consumed
letter++; // increment to next letter in alphabet
}
printf("~~~~~~~\nProducer END\n~~~~~~~\n");
return NULL;
}
// consumer function to consume the buffer, and do something with the letters - display them
void* consumer(void* args) {
char l;
while (letter <= 'Z' ) {
// remove the letter from the buffer
sem_wait(&sem_full); // wait until there is someting produced in buffer; if full is 0 then it will wait until > 0
sem_wait(&mutex);
l = buffer[count];
//count--;
printf("The %d letter CONSUMED is: %c\n", count, l);
sem_post(&mutex);
sem_post(&sem_empty); // now there is an empty slot that was consumed
// consume
}
printf("~~~~~~~\nConsumer END\n~~~~~~~\n");
return NULL;
}
int main()
{
sem_init(&mutex, 0, 1);
sem_init(&sem_empty, 0, BUFF_SIZE); // empty with 26 elements, size of only 1 buffer
sem_init(&sem_full, 0, 0); // full with 0 elements
pthread_create(&p_threads[0], NULL, producer, NULL);
pthread_create(&c_threads[0], NULL, consumer, NULL);
// join threads
pthread_join(p_threads[0], NULL);
printf("Thread PRODUCE returned \n");
pthread_join(c_threads[0], NULL);
printf("Thread CONSUMER returned \n");
printf("Main thread done.\n");
sem_destroy(&sem_empty);
sem_destroy(&sem_full);
sem_destroy(&mutex);
return 0;
}

You are viewer number:

