Hi, all!
I'm having some trouble with a C assignment where we have to create a simulated bank account manager. It needs to be able to handle transactions and check balance. They gave us a Bank library
Code:
which we have to use, and each request is handled by a thread. I've got my real project code but it's a little big, so I've made a small program (that doesn't take requests) that just illustrates the problem I'm having. The requests handled in the threads don't have their data persist to the main thread or to other threads spawned later.
Code:
Will output
Code:
What could I be doing wrong here? Is there some fundamental thing about threads in C that I've failed to grasp?
Thank you!
I'm having some trouble with a C assignment where we have to create a simulated bank account manager. It needs to be able to handle transactions and check balance. They gave us a Bank library
Code:
/** Do not modify this file **/
#include "Bank.h"
#include <stdlib.h>
#include <unistd.h>
int *BANK_accounts; //Array for storing account values
#define WAIT_TIME 10000
/*
* Intialize back accounts
* Input: int n - Number of bank accounts
* Return: 1 if succeeded, 0 if error
*/
int initialize_accounts( int n )
{
BANK_accounts = (int *) malloc(sizeof(int) * n);
if(BANK_accounts == NULL) return 0;
int i;
for( i = 0; i < n; i++)
{
BANK_accounts[i] = 0;
}
return 1;
}
/*
* Read a bank account
* Input: int ID - Id of bank account to read
* Return: Value of bank account ID
*/
int read_account( int ID )
{
usleep( WAIT_TIME );
return BANK_accounts[ID - 1];
}
/*
* Write value to bank account
* Input: int ID - Id of bank account to write to
* Input: int value - value to write to account
*/
void write_account( int ID, int value)
{
usleep( WAIT_TIME );
BANK_accounts[ID - 1] = value;
}
/*
* Deallocate the memory for bank accounts
*/
void free_accounts()
{
free(BANK_accounts);
}
which we have to use, and each request is handled by a thread. I've got my real project code but it's a little big, so I've made a small program (that doesn't take requests) that just illustrates the problem I'm having. The requests handled in the threads don't have their data persist to the main thread or to other threads spawned later.
Code:
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "Bank.h"
pthread_mutex_t mutex;
int main(int argc, char** argv)
{
// Create 10 accounts
initialize_accounts(10);
// Put 200 in account 2 in main thread
pthread_mutex_lock(&mutex);
write_account(2, 200);
printf("%d\n", read_account(2));
pthread_mutex_unlock(&mutex);
// Create thread
int p = fork();
if(p == 0)
{
// Put 300 in account 3 in child thread
pthread_mutex_lock(&mutex);
write_account(3, 300);
pthread_mutex_unlock(&mutex);
exit(0);
}
else
{
// Main thread sleep for a second
sleep(1);
}
// Try to read account 3 (will print 0)
pthread_mutex_lock(&mutex);
printf("%d\n", read_account(3));
pthread_mutex_unlock(&mutex);
}
Will output
Code:
200
0
What could I be doing wrong here? Is there some fundamental thing about threads in C that I've failed to grasp?
Thank you!