feat: project 1
This commit is contained in:
parent
d6ef47cff4
commit
f04d70f615
16 changed files with 702 additions and 0 deletions
107
project1/phase1/phase1.c
Normal file
107
project1/phase1/phase1.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define NUM_ACCOUNTS 8
|
||||
#define NUM_TELLERS 128
|
||||
#define TRANSACTIONS_PER_TELLER 100
|
||||
|
||||
// Monetary value in cents
|
||||
typedef long currency;
|
||||
|
||||
void printf_currency(currency c) {
|
||||
if (c < 0) {
|
||||
c = -c;
|
||||
printf("-");
|
||||
}
|
||||
printf("$%ld.%.2ld", c / 100, c % 100);
|
||||
}
|
||||
|
||||
// Accounts (shared resource)
|
||||
typedef struct account {
|
||||
int account_id;
|
||||
currency balance;
|
||||
int transaction_count;
|
||||
} account;
|
||||
|
||||
// Global account array
|
||||
account accounts[NUM_ACCOUNTS];
|
||||
|
||||
// Teller threads
|
||||
|
||||
pthread_t tellers[NUM_TELLERS];
|
||||
|
||||
typedef struct teller_args {
|
||||
int teller_id;
|
||||
currency total;
|
||||
} teller_args;
|
||||
|
||||
void *teller_thread(void *arg) {
|
||||
teller_args *args = (teller_args *)arg;
|
||||
int id = args->teller_id;
|
||||
currency total = 0;
|
||||
|
||||
unsigned int seed = time(NULL) + pthread_self();
|
||||
for (int i = 0; i < TRANSACTIONS_PER_TELLER; i++) {
|
||||
printf("Thread %u: ", id);
|
||||
// Deposit or withdraw random currency amount from $0 to $1000
|
||||
currency random_amount = ((long)rand_r(&seed)) % 100000;
|
||||
if (rand_r(&seed) % 2) {
|
||||
printf("Withdrawing ");
|
||||
printf_currency(random_amount);
|
||||
random_amount = -random_amount;
|
||||
} else {
|
||||
printf("Depositing ");
|
||||
printf_currency(random_amount);
|
||||
}
|
||||
|
||||
int random_acc = rand_r(&seed) % NUM_ACCOUNTS;
|
||||
accounts[random_acc].balance += random_amount;
|
||||
accounts[random_acc].transaction_count++;
|
||||
|
||||
printf(" into account %u\n", random_acc);
|
||||
|
||||
total += random_amount;
|
||||
}
|
||||
|
||||
args->total = total;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Initialize all accounts with 0 balance
|
||||
for (int i = 0; i < NUM_ACCOUNTS; i++) {
|
||||
accounts[i] = (account){.account_id = i, .balance = 0};
|
||||
}
|
||||
|
||||
// Initialize tellers
|
||||
teller_args args[NUM_TELLERS];
|
||||
for (int i = 0; i < NUM_TELLERS; i++) {
|
||||
args[i] = (teller_args){.teller_id = i};
|
||||
pthread_create(&tellers[i], NULL, teller_thread, &args[i]);
|
||||
}
|
||||
|
||||
// Wait for all threads to finish
|
||||
currency expected_total = 0;
|
||||
for (int i = 0; i < NUM_TELLERS; i++) {
|
||||
pthread_join(tellers[i], NULL);
|
||||
expected_total += args[i].total;
|
||||
}
|
||||
|
||||
currency actual_total = 0;
|
||||
for (int i = 0; i < NUM_ACCOUNTS; i++) {
|
||||
actual_total += accounts[i].balance;
|
||||
}
|
||||
|
||||
printf("Total balance: ");
|
||||
printf_currency(actual_total);
|
||||
printf("\nExpected total: ");
|
||||
printf_currency(expected_total);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue