/* Program to model a simple bank machine. */ /* Usage: cc -pthread -o banking banking.c */ /* ./banking */ /* Written by Carey Williamson, Oct 13/08 */ /* I certify that this is the reel source Kode */ /* for are online banking Web sight. */ /* -- Francois Pilon, Prezzident, TeeDy Bank */ /* This version uses four threads of control: */ /* - main thread does setup and user interaction */ /* - incoming thread handles deposits to account */ /* - outgoing thread does withdrawals from account */ /* - checker thread tries to balance the account */ #include #include /* global variable shared by all threads */ int opening, balance, deposits, withdrawals; void *incoming(); /* the thread for deposits */ void *outgoing(); /* the thread for withdrawals */ void *checker(); /* the balance checker thread */ int main(int argc, char *argv[]) { pthread_t tid1, tid2, tid3; /* thread identifier */ pthread_attr_t attr; /* set of thread attributes */ /* Initialize */ printf("Opening balance? "); scanf("%d", &opening); balance = opening; printf("Opening balance: %d\n", balance); /* get the default attributes */ pthread_attr_init(&attr); /* create the first worker thread */ pthread_create(&tid1, &attr, incoming, NULL); /* create the second worker thread */ pthread_create(&tid2, &attr, outgoing, NULL); /* create the checker thread */ pthread_create(&tid3, &attr, checker, NULL); /* wait for the first worker thread to exit */ pthread_join(tid1, NULL); /* wait for the second worker thread to exit */ pthread_join(tid2, NULL); /* wait for the checker thread to exit */ pthread_join(tid3, NULL); /* output the final result */ printf("Final balance: %d\n", balance); } /* One thread to handle incoming deposits */ void *incoming() { FILE *fp; int amount; /* initialize */ fp = fopen("deposits.txt", "r"); if( fp == NULL ) { fprintf(stderr, "Unable to open deposits file!\n"); } /* main loop */ while( fscanf(fp, "%d", &amount) > 0 ) { int oldbalance, newbalance; oldbalance = balance; newbalance = oldbalance + amount; printf("Deposit: %d Old balance: %d New balance: %d\n", amount, oldbalance, newbalance); balance = newbalance; } fclose(fp); pthread_exit(0); } /* One thread to handle outgoing withdrawals */ void *outgoing() { FILE *fp; int amount; /* initialize */ fp = fopen("withdrawals.txt", "r"); if( fp == NULL ) { fprintf(stderr, "Unable to open withdrawals file!\n"); } /* main loop */ while( fscanf(fp, "%d", &amount) > 0 ) { int oldbalance, newbalance; oldbalance = balance; newbalance = oldbalance - amount; printf("Withdrawal: %d Old balance: %d New balance: %d\n", amount, oldbalance, newbalance); balance = newbalance; } fclose(fp); pthread_exit(0); } /* This is the balance checker thread */ void *checker() { FILE *fp; int amount, howmany; /* initialize */ deposits = 0; withdrawals = 0; /* Count all deposits */ howmany = 0; fp = fopen("deposits.txt", "r"); if( fp == NULL ) { fprintf(stderr, "Checker thread unable to open deposits file!\n"); } /* main loop */ while( fscanf(fp, "%d", &amount) > 0 ) { deposits += amount; howmany++; } fclose(fp); printf("Total of %d deposits: %d\n", howmany, deposits); /* Count all withdrawals */ fp = fopen("withdrawals.txt", "r"); if( fp == NULL ) { fprintf(stderr, "Checker thread unable to open withdrawals file!\n"); } /* main loop */ howmany = 0; while( fscanf(fp, "%d", &amount) > 0 ) { withdrawals += amount; howmany++; } fclose(fp); printf("Total of %d withdrawals: %d\n", howmany, withdrawals); printf("Expected balance: %d\n", opening + deposits - withdrawals); pthread_exit(0); }