175 lines
3.0 KiB
C
175 lines
3.0 KiB
C
|
|
#include <sys/types.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/msg.h>
|
|
#include "public.h"
|
|
#include "define.h"
|
|
#include "custom.h"
|
|
|
|
int msgid_recvq;
|
|
int msgid_sendq;
|
|
int msgid_print; //message queue to printer
|
|
int msgid_log; //message queue to log process
|
|
int msgid_fd;
|
|
int efd;
|
|
|
|
key_t SemKeyCard = 0x46B;//密码卡信号量键值
|
|
|
|
struDataPA *g_PA = NULL; // 20141104 nifei add
|
|
unsigned long long g_TradeCount = 0;
|
|
|
|
/*信号*/
|
|
|
|
int semid_KeyCard;//密码卡信号量
|
|
|
|
|
|
|
|
|
|
/*信号键*/
|
|
key_t SemHourKey=0x461;
|
|
key_t SemDayKey=0x462;
|
|
key_t SemCountKey=0x463;
|
|
key_t SemConnectionsKey=0x464;
|
|
key_t SemMsgInQueueCountKey=0x465;
|
|
key_t SemKeycountKey=0x466;
|
|
key_t SemTradeStatus=0x467;
|
|
key_t SemPrintErrorCode=0x468;
|
|
key_t SemThreadSwitch = 0x469;
|
|
key_t SemManageUser = 0x46A;
|
|
|
|
int _icdev; //IC卡读卡器
|
|
key_t _icdevKey = 0x570;
|
|
|
|
/*******初始化IC卡个数初始值*********/
|
|
int InitKeyCard(int semid)
|
|
{
|
|
return semctl(semid, 0, SETVAL, 1);
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------- 建立信号量 ----------------------------- */
|
|
int initsem(int *semid, key_t semkey)
|
|
{
|
|
int status = 0;
|
|
|
|
if ((*semid = semget(semkey, 1, IPC_CREAT | 0666)) == -1) /* 全局变量 */
|
|
{
|
|
perror("semget");
|
|
}
|
|
else
|
|
status = semctl(*semid, 0, SETVAL, 1);
|
|
|
|
if (*semid == -1 || status == -1)
|
|
{
|
|
return -1;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
/* ------------------------------- V 操作 ---------up-------------------------- */
|
|
int V(int Semid, int flag)
|
|
{
|
|
struct sembuf sops[1];
|
|
int rtn;
|
|
sops[0].sem_num = flag;
|
|
sops[0].sem_op = +1;
|
|
sops[0].sem_flg = SEM_UNDO;
|
|
rtn = semop(Semid, sops, 1);
|
|
if(rtn == -1)
|
|
{
|
|
printf("V error\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* ------------------------------- P 操作 ------down----------------------------- */
|
|
int P(int Semid, int flag, int senconds)
|
|
{
|
|
|
|
|
|
struct sembuf sops[1];
|
|
int rtn;
|
|
sops[0].sem_num = flag;
|
|
sops[0].sem_op = -1;
|
|
sops[0].sem_flg = SEM_UNDO;
|
|
|
|
rtn=semop(Semid,sops,1);
|
|
|
|
if(rtn==-1)
|
|
{
|
|
printf("P error\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------------ 建消息队列,共享内存,信号量 --------------------- */
|
|
int CreateIpc()
|
|
{
|
|
initsem(&_icdev,_icdevKey);
|
|
|
|
//访问密码卡信号量
|
|
initsem(&semid_KeyCard, SemKeyCard);
|
|
InitKeyCard(semid_KeyCard);
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* ------------------ delete 消息队列,共享内存,信号量 ------------------- */
|
|
int DeleteIpc()
|
|
{
|
|
//struct shmid_ds shmds;
|
|
semctl(_icdev,_icdevKey,IPC_RMID, 0);
|
|
//访问密码卡信号量
|
|
semctl(semid_KeyCard, SemKeyCard, IPC_RMID, 0);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------- 建立消息队列 ----------------------------- */
|
|
int CreateMsgQueue(int *msgid, key_t msgkey)
|
|
{
|
|
int status = 0;
|
|
if((status = msgget(msgkey, IPC_CREAT | 0666)) == -1) /* 全局变量 */
|
|
{
|
|
perror("msgget error!");
|
|
return -1;
|
|
}
|
|
*msgid=status;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int get_efd_lock(void)
|
|
{
|
|
uint64_t e_val = 0;
|
|
int ret;
|
|
ret = read(efd, &e_val, sizeof(uint64_t));
|
|
if(ret > 0)
|
|
return 0;
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
int put_efd_lock(void)
|
|
{
|
|
uint64_t e_val = 1;
|
|
int ret;
|
|
ret = write(efd, &e_val, sizeof(uint64_t));
|
|
if(ret > 0)
|
|
return 0;
|
|
else
|
|
{
|
|
printf("Failed to write efd.\n");
|
|
return -1;
|
|
}
|
|
} |