精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
1、开发平台Linux,gcc编译
2、技术点:结构体数组、多线程、线程互斥和线程条件变量,状态机逻辑处理
3、./atm2执行程序,按CTRL+C退出
核心代码如下:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
//银行结构体状态宏
#define BS_NORMAL 0
#define BS_IN 1
#define BS_OUT -1
typedef struct BANKTYPE {//银行结构体定义
int BankId;
int Money;
int InOutMoney;
int Status;
int RelatBankId;
int Days;
pthread_mutex_t lock;
pthread_cond_t condlock;
} Bank;
Bank realbank[10];
//void Loading(int, char **);
void InitBank(void);
void PrintBorrowInfo(int);
void PrintBackInfo(int);
void *BankThread(void *);
int main(int argc, char *argv[])
{
int i, pthrect;
pthread_t tid[10];
//Loading(argc, argv);
srand(time(NULL));
InitBank();//初始化结构数组
for (i = 0; i < 10; i++) {//生成是个线程,每个线程处理一家银行
pthrect =pthread_create(&tid[i], NULL, (void *) BankThread,&realbank[i].BankId);
if (pthrect != 0) {
perror("ThreadCreate Failed!\n");
exit(1);
}
}
for (i = 0; i < 10; i++)
pthread_join(tid[i], NULL);
return 0;
}
// void Loading(int arc, char *arv[])//登陆简单验证函数,可不使用
// {
// char arvbuf[2][8] = { "lll", "123456" };
// if (arc != 3) {
// perror("Argument no matching!\n");
// exit(1);
// }
// if (0 != strcmp(arvbuf[0], arv[1])) {
// perror("UserName error!\n");
// exit(1);
// }
// if (0 != strcmp(arvbuf[1], arv[2])) {
// perror("Password error!\n");
// exit(1);
// }
// return;
// }
void InitBank(void){//初始化十个银行结构体
int i;
for (i=0;i<10;i++){
realbank[i].BankId = i+1;
realbank[i].Status = BS_NORMAL;
realbank[i].Money = 3000;
realbank[i].InOutMoney = 0;
realbank[i].RelatBankId = 0;
realbank[i].Days = 0;
pthread_mutex_init(&realbank[i].lock,NULL);
pthread_cond_init(&realbank[i].condlock,NULL);
}
}
void PrintBorrowInfo(int i){//打印借入银行信息
printf("T:%d B:%d borrow B:%d $:%d for %d days!\n",
time((time_t*)NULL), realbank[i].BankId,realbank[i].RelatBankId,
realbank[i].InOutMoney,realbank[i].Days);
}
void PrintBackInfo(int i){//打印还款银行信息
printf("T:%d B:%d back B:%d $:%d!\n", time((time_t*)NULL),
realbank[i].BankId,realbank[i].RelatBankId, realbank[i].InOutMoney);
}
void *BankThread(void *arg)//线程函数体
{
int i;
int rad;
int *pint=(int*)arg;
int selfid=*pint;
int irelaid=0;
while (1)
{
switch(realbank[selfid].Status)
{
case BS_NORMAL:
{
rad = rand() % 10;
if(rad==selfid){
continue;
}
if (realbank[rad].Status != BS_NORMAL) {
continue;
}
pthread_mutex_lock(&realbank[selfid].lock);//为找到的上银行上锁,并处理相关数据
realbank[selfid].Status = BS_IN;
realbank[selfid].Days = selfid+1;
realbank[selfid].Money =1000 + (selfid+1) * 100;
realbank[selfid].InOutMoney =(selfid+1) * 100;
realbank[selfid].RelatBankId = rad;
pthread_mutex_unlock(&(realbank[selfid].lock));//解锁
PrintBorrowInfo(selfid);
pthread_mutex_lock(&(realbank[rad].lock));
realbank[rad].Status = BS_OUT;
realbank[rad].Days = rad;
realbank[rad].Money =1000 - (rad+1) * 100;
realbank[rad].InOutMoney =(rad+1) * 100;
realbank[rad].RelatBankId = selfid;
pthread_mutex_unlock(&(realbank[rad].lock));
}
break;
case BS_IN:
irelaid=realbank[selfid].RelatBankId;
if(realbank[irelaid].Status!=BS_NORMAL)
pthread_cond_signal(&(realbank[irelaid].condlock));//发送线程同步信号
break;
case BS_OUT:
irelaid=realbank[selfid].RelatBankId;
pthread_mutex_lock(&(realbank[selfid].lock));
pthread_cond_wait(&(realbank[selfid].condlock),&(realbank[selfid].lock));//阻塞线程并等待线程同步信号
realbank[selfid].Status = BS_NORMAL;
realbank[selfid].Days = 0;
realbank[selfid].Money = 1000;
PrintBackInfo(irelaid);
realbank[selfid].InOutMoney = 0;
realbank[selfid].RelatBankId = 0;
realbank[irelaid].Status = BS_NORMAL;
realbank[irelaid].Days = 0;
realbank[irelaid].Money = 1000;
realbank[irelaid].InOutMoney = 0;
realbank[irelaid].RelatBankId = 0;
pthread_mutex_unlock(&(realbank[selfid].lock));
break;
}
}
pthread_exit(NULL);
}