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:

/**  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!
Quote:
Is there some fundamental thing about threads in C that I've failed to grasp?

Why the hell are you mixing posix and linux threading constructs.
Or to further expand on Mateo's comment, when you fork() you're creating a new process, not a new thread. Changes in one process will not be reflected in the other. Given you're using pthread mutexes, you probably want to use pthread_create() to spawn new threads instead.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement