Created
September 22, 2014 20:28
-
-
Save iamsurya/7310c4882108124b03b7 to your computer and use it in GitHub Desktop.
Bank.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include "bank.h" | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
static int current_balance; | |
int BalanceFlag = 0; /* 0 for No Activity, 1 for Activity in the last 10 seconds */ | |
pthread_mutex_t BalanceLock, FlagLock; | |
void *ActivityCheck (void *param) | |
{ | |
while(1) | |
{ | |
/* Count to 10 seconds */ | |
sleep(10); | |
/* If an activity has occured, Reset the Flag */ | |
/* Lock the Flag modification */ | |
while(pthread_mutex_init(&FlagLock, NULL) != 0); | |
if(BalanceFlag == 1) | |
{ | |
// printf("prompt> "); | |
BalanceFlag = 0; | |
pthread_mutex_destroy(&FlagLock); | |
continue; | |
} | |
/* Try locking, its okay to fail */ | |
pthread_mutex_init(&FlagLock, NULL); | |
/* Destroy the lock */ | |
pthread_mutex_destroy(&FlagLock); | |
/* If an activity hasn't occured */ | |
bank_do_command("withdraw", 2); | |
printf("$2 fined \n"); | |
printf("prompt> "); | |
fflush(stdout); | |
/* Withdraw will Set Flag, which we don't want */ | |
BalanceFlag = 0; | |
} | |
return NULL; | |
} | |
int bank_init(int initial_balance) | |
{ | |
int err; | |
if (initial_balance < 0) return 0; | |
current_balance = initial_balance; | |
/* Launch Thread for Activity Check here */ | |
pthread_t athread; | |
err = pthread_create(&athread,NULL, ActivityCheck,NULL); | |
if(err != 0) printf("Error Creating Thread\n"); | |
return 1; | |
} | |
//performs a command on the bank account. | |
//for recognized commands, the function | |
// returns the current balance, whether or not the requested | |
// command was performed. | |
//returns -1 if the command wasn't recognized. | |
int bank_do_command(char *command, int value) | |
{ | |
/* Don't know if we should have a mutex lock here */ | |
if (strcmp(command, "balance") == 0) return current_balance; | |
if (strcmp(command, "deposit") == 0) | |
{ | |
if (value < 0) return -1; //generic error return | |
/* Obtain Lock to change balance */ | |
while(pthread_mutex_init(&BalanceLock, NULL) != 0); | |
while(pthread_mutex_init(&FlagLock, NULL) != 0); | |
BalanceFlag = 1; | |
pthread_mutex_destroy(&FlagLock); | |
current_balance += value; | |
pthread_mutex_destroy(&BalanceLock); | |
return current_balance; | |
} | |
if (strcmp(command, "withdraw") == 0) | |
{ | |
if (value < 0 || current_balance < value) return current_balance; | |
/* Obtain Lock to change balance */ | |
while(pthread_mutex_init(&BalanceLock, NULL) != 0); | |
while(pthread_mutex_init(&FlagLock, NULL) != 0); | |
BalanceFlag = 1; | |
pthread_mutex_destroy(&FlagLock); | |
current_balance -= value; | |
pthread_mutex_destroy(&BalanceLock); | |
return current_balance; | |
} | |
//I don't recognize this command | |
return -1; | |
} | |
/* How to free things before exec */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment