基于消息队列的银行系统
基本需求:
1、分为客户端和服务端(c/s架构)
2、服务端与每个客户端通信、满足它们的各种请求
3、客户端主要功能:
1、开户 2、销户 3、解锁 4、退出
5、登录 6、查询 7、存钱 8、取款
9、转账 10、修改密码
4、推荐使用的技术:消息队列 + vfork + exec系统函数
Make file
CC=gcc
STD=-std=gnu99
FLAG=-Wall -Werror
BIN=open destory unlock login query save take transfer repass
TARGE=server client
TOOLS=msgq.c tools.c
all:$(BIN) $(TARGE)
mv $(BIN) ./bin/
./server
%:%.c tools.c tools.h msgq.c msgq.h
$(CC) $(STD) $(FLAG) $(TOOLS) $< -o $@
clean:
cd ./bin/ && rm -rf $(BIN)
rm -rf *.o
rm -rf *.h.gch
rm -rf *~
基本工具类,给输入数据做一下限制
tools.h
#ifndef TOOLS_H
#define TOOLS_H
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getch.h>
void clear_stdin(void);
char* get_str(char* str,size_t len);
char get_sex(void);
int get_cmd(int start,int end);
char* get_pw(char* passwd,bool is_show,size_t size);
void any_key(void);
#endif//TOOLS_H
鉴于C语言下msg的函数使用参数过多给他封装一下
msgq.h
#ifndef MSGQ_H
#define MSGQ_H
#include <stdio.h>
int get_msg(const char *pathname, int proj_id);
int send_msg(int msgid,const void* msg,size_t len);
int recv_msg(int msgid,void* msg,size_t len,long type);
void del_msg(int msgid);
#endif//MSGQ_H
msgq.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "msgq.h"
int get_msg(const char *pathname, int proj_id)
{
int msgid = msgget(ftok(pathname,proj_id),IPC_CREAT|0644);
if(0 > msgid)
{
perror("msgget");
}
return msgid;
}
int send_msg(int msgid,const void* msg,size_t len)
{
int ret = msgsnd(msgid,msg,len-sizeof(long),0);
if(0 > ret)
{
perror("msgsnd");
}
return ret;
}
int recv_msg(int msgid,void* msg,size_t len,long type)
{
int ret = msgrcv(msgid,msg,len,type,0);
if(0 >= ret)
{
perror("msgrcv");
}
return ret;
}
void del_msg(int msgid)
{
if(msgctl(msgid,IPC_RMID,NULL))
{
perror("msgctl")