Starting from:

$30

CECS 326 Assignment 7

CECS 326 Assignment 7 (5 points)



 

Lab 8 and Assignment 7 are for an implementation of the bank account transfer code shown on slides 4-5 of the Deadlocks Lecture Note. You will need to create two programs to perform the following simple logics. 

§  create two bank accounts

§  put some money in the two accounts

§  transfer some money from one account to the other

 

Bank accounts are shared data that may be accessed and manipulated by different processes.  Therefore, they must be maintained in shared memory.  Their access would require mutual exclusion through semaphores.  Use the POSIX implementation of shared memory and semaphore for this assignment.

 

For this assignment, you will need to write two C programs named master.c and transfer.c, and compile them into executables master and transfer, respectively, using gcc and link with –lpthread. The two programs should include the following header file that defines the structure of the shared memory segment:

 

/* accounts.h */

/* Header file to be used with

 * master.c and transfer.c

 */

 

struct ACCOUNTS {

     int nAccounts;       /* number of active accounts */

     int accounts[50];    /* space to hold up to 50 accounts */

};

 

Suppose the program execution is launched as follows:

 

./master 400 200 my_shm_name sem1 sem2

 

master should do the following:

 

§  Create a POSIX shared memory segment named my_shm_name   [ my_shm_name is from comandline ]

§  Set up 2 accounts in shared memory with amounts 400 and 200   [ based on data from commandline]

§  Output the current content of the ACCOUNTS struct in shared memory

§  Create two named semaphores with names sem1 and sem2                       [ sem1 & sem2 from commandline]

§  Create a child process to execute transfer, with parameters (my_shm_name, sem1, sem2, 1, 2, 50) indicating to transfer $50 from account 1 to account 2 that are maintained in shared memory named my_shm_name.  Semaphores sem1 and sem2 are for mutual exclusion to accounts 1 & 2.

§  Wait for child process to terminate

§  Upon receiving termination signal from child process, output updated content of the ACCOUNTS struct

§  Close and unlink semaphores

§  Close access to shared memory segment, unlink and exit

 

transfer should do the following:

 

§  Acquire access to shared memory segment

§  Acquire access to critical section to manipulate the two accounts

§  Make necessary transfer as indicated in parameters

§  Output actions taken

§  Close semaphores

§  Close access to shared memory segment and exit

 

The program must run successfully on Linux.  Make sure that enough clear output is produced to enable understanding of logics in the programs.

 

Do the following for this assignment:

1.      Develop two C programs that work as described above.  Make sure your source programs are properly formatted as well as adequately and clearly commented.  Detailed explanations on all system calls are required and must be in your own words.  Simply copying those from the man pages are not acceptable.

2.      Submit on BeachBoard the two C programs, a screenshot that shows successful compile of both programs as well as a successful run, and a cover page that provides your name, your student ID, course # and section, assignment #, due date, submission date, and a clear program description detailing what the programs are about.  Format of the cover page should follow the cover page template on BeachBoard.  The programs must be properly formatted and adequately commented to enhance readability and understanding.  Detailed documentation on all system calls are especially needed.

 

More products