📄 shmopt.c
字号:
/*=============================================================
功能:
作者:
王永忠:2001/04/02:规范程序设计
===============================================================*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <ctype.h>
#include "all.h"
SHM1 *sh1=NULL;
int ipcflag = 0;
int reload_num=0;
/***************************************************************
功能:
IPC 初始化
ENTRY:
RETURN:
0 SUCC
-1 FAIL
****************************************************************/
int getipc()
{
if(ipcflag == 1) return(0);
if((sh1 = (SHM1 *)gshm1()) == NULL) {
errcall(ERROR,"Get shm1 err!");
return(-1);
}
ipcflag = 1;
return(0);
}
/***************************************************************
功能:
判断是否需要重新装入系统信息
ENTRY:
RETURN:
0 NOT NEED
1 NEED
****************************************************************/
unsigned char Is_reload ()
{
int num;
if ( sh1 == NULL ) return 0;
num = sh1->reload_num;
if ( reload_num != num ) {
reload_num = num;
return 1;
}
return 0;
}
/***************************************************************
功能:
取共享内存中重新装入标志
ENTRY:
RETURN:
reload_num <-- sh1->reload_num
0 SUCC
-1 FAIL
****************************************************************/
int get_reload_num ()
{
getipc ();
if ( sh1 == NULL ) {
errcall ( ERROR , "Unable get reload num ! , sh1 is NULL!" );
return -1;
}
reload_num = sh1->reload_num;
return 0;
}
/***************************************************************
功能:
设置共享内存中重新装入标志
ENTRY:
RETURN:
0 SUCC
-1 FAIL
****************************************************************/
int set_reload_num ()
{
getipc ();
if ( sh1 == NULL ) {
errcall ( ERROR , "sh1 is NULL !" );
return -1;
}
sh1->reload_num++;
return 0;
}
dt_shm1()
{
dt_shm(sh1);
ipcflag = 0;
sh1=NULL;
}
/*---------------------------------------------
修改共享内存中进程记录
ENTRY:
execname
pid
flag
RETURN: 0 SUCC
-1 FAIL
-----------------------------------------------*/
upd_shm_pid(execname, pid, flag)
char *execname;
int pid;
char flag;
{
int i,j;
char time[9];
int p=-1;
char exec_name[31];
strcpy(exec_name, execname);
if(getipc() == -1) {
return(-1);
}
#ifdef zgx010528_del
/* 已修改incl/shmsys.h中PSTAT结构的name[9]未name[31],不会有更长的模块名 */
j = strlen(exec_name);
if (j > 8) {
j = 8;
exec_name[8] = '\0';
}
#endif
gettimef(time, ":");
for(i=0; i<EXEC_NUM; i++) {
if( is_null( sh1->pst[i].name ) ) {
if ( p == -1 )
p=i;
}
if (strcmp(sh1->pst[i].name, exec_name) == 0) {
memcpy(sh1->pst[i].time, time, 9);
sh1->pst[i].times++;
if (flag == ACTIVE) {
sh1->pst[i].pid = pid;
sh1->pst[i].times = 1;
}
if(flag != SETTIME) sh1->pst[i].pause = flag;
return(0);
}
}
if ( p != -1 ) {
i=p;
sprintf(sh1->pst[i].name, "%s", exec_name);
sh1->pst[i].pause = flag;
sh1->pst[i].pid = pid;
memcpy(sh1->pst[i].time, time, 9);
sh1->pst[i].times = 1;
return(0);
}
if(i >= EXEC_NUM) {
printf( "共享内存中EXEC_NUM太小");
return(-1);
}
return(0);
}
/*---------------------------------------------
查看共享内存中进程记录状态
ENTRY:
p->name 结构中的执行文件名
RETURN: 0 SUCC
-1 NOT FOUND
-----------------------------------------------*/
sel_shm_pid(p)
PSTAT *p;
{
int i,j;
char exec_name[31];
if(getipc() == -1)
return(-1);
strcpy(exec_name, p->name);
#ifdef zgx010528_del
/* 已修改incl/shmsys.h中PSTAT结构的name[9]未name[31],不会有更长的模块名 */
j = strlen(exec_name);
if (j > 8)
j = 8;
exec_name[j] = '\0';
#endif
for(i=0; i<EXEC_NUM; i++) {
if( is_null( sh1->pst[i].name ) ) {
return(-1);
}
if (strcmp(sh1->pst[i].name, exec_name) == 0) {
p->pause = sh1->pst[i].pause;
p->pid = sh1->pst[i].pid;
memcpy(p->time, sh1->pst[i].time, 9);
p->times = sh1->pst[i].times;
return(0);
}
}
return(-1);
}
/*---------------------------------------------
return: 0 进程处于活动状态
1 进程处于暂停或不存在状态
-----------------------------------------------*/
pid_pause(execname)
char *execname;
{
int i,j;
char flag;
char exec_name[31];
strcpy(exec_name, execname);
if(getipc() == -1) return(1);
#ifdef zgx010528_del
/* 已修改incl/shmsys.h中PSTAT结构的name[9]未name[31],不会有更长的模块名 */
j = strlen(exec_name);
if (j > 8)
j = 8;
exec_name[j] = '\0';
#endif
for(i=0; i<EXEC_NUM; i++) {
if( is_null( sh1->pst[i].name ) ) break;
if (strcmp(sh1->pst[i].name, exec_name) == 0) {
flag = sh1->pst[i].pause;
if(flag == ACTIVE)
return(0);
break;
}
}
return(1);
}
/*---------------------------------------------
return: 1 交易监控 进程处于激活状态
0 交易监控 进程处于暂停状态
-----------------------------------------------*/
monitor_ok()
{
if(getipc() == -1) return(0);
if(sh1->monitor == '1' || sh1->monitor == 1) return(1);
else return(0);
}
set_monitor(flag)
char flag;
{
if(getipc() == -1) return(-1);
sh1->monitor = flag;
return(0);
}
/*---------------------------------------------
return: 1 错误监控 进程处于激活状态
0 错误监控 进程处于暂停状态
-----------------------------------------------*/
errmsg_ok()
{
if(getipc() == -1) return(0);
if(sh1->errmsg == '1' || sh1->errmsg == 1) return(1);
else return(0);
}
set_errmsg(flag)
char flag;
{
if(getipc() == -1) return(-1);
sh1->errmsg = flag;
return(0);
}
/*---------------------------------------------
return: 1 数据流监控 进程处于激活状态
0 数据流监控 进程处于暂停状态
-----------------------------------------------*/
trace_ok()
{
if(getipc() == -1) return(0);
if(sh1->trace == '1' || sh1->trace == 1) return(1);
else return(0);
}
set_trace(flag)
char flag;
{
if(getipc() == -1) return(-1);
sh1->trace = flag;
return(0);
}
/*---------------------------------------------
return: 1 运行程序中调试信息处于激活状态
0 运行程序中调试信息处于暂停状态
-----------------------------------------------*/
debug_ok()
{
if(getipc() == -1) return(0);
if(sh1->debug == '1' || sh1->debug == 1) return(1);
else return(0);
}
set_debug(flag)
char flag;
{
if(getipc() == -1) return(-1);
sh1->debug = flag;
return(0);
}
/*---------------------------------------------------
判 str 前两个字符是否都为 数字和字母
return 0 是
-1 否
-----------------------------------------------------*/
is_null(str)
char *str;
{
if( !isalnum( str[0] ) ) return(-1);
if( !isalnum( str[1] ) ) return(-1);
return(0);
}
/*
* Modified By Lcm. 1997.11.07.
*/
/*
* Restore hostls in shared memory. Process pre get hostls from it
* when receive a request package, and increase it.
*/
int SetHostls(int hostls)
{
if (getipc() == -1) return(-1);
sh1->hostls = hostls;
return(0);
}
/*
* Get the value of hostls from shared memory.
*/
int GetHostls()
{
if (getipc() == -1) return(-1);
return(sh1->hostls);
}
/*
* Increase the value of hostls by 1.
*/
int IncreaseHostls()
{
if (getipc() == -1) return(-1);
sh1->hostls ++;
return(0);
}
/*
* Restore local center jgbm in shared memory. usually used by credit.c
* and main.c(PRE).
*/
int SetCenter(char *center,char *host)
{
if (getipc() == -1) return(-1);
memcpy(sh1->center, center, 11);
memcpy(sh1->center_host, host, 2);
sh1->center[11] = 0;
sh1->center_host[2] = 0;
return(0);
}
/*
* Get local center jgbm from shared memory. usually used by credit.c
* and main.c(PRE).
*/
char *GetCenter()
{
if (getipc() == -1) return((char *)-1);
return((char *)sh1->center);
}
int get_pid(char *execname, int *pid)
{
int i, j;
char exec_name[31];
strcpy(exec_name, execname);
if (getipc() == -1)
return(-1);
#ifdef zgx010528_del
/* 已修改incl/shmsys.h中PSTAT结构的name[9]为name[31],不会有更长的模块名 */
if ((j = strlen(exec_name)) > 8)
j = 8;
exec_name[j] = '\0';
#endif
for (i = 0; i < EXEC_NUM; i ++) {
if (is_null(sh1->pst[i].name))
break;
if (!strcmp(sh1->pst[i].name, exec_name)) {
if (sh1->pst[i].pause == KILLED)
return(-3);
*pid = sh1->pst[i].pid;
return(0);
}
}
return(-4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -