📄 ipc.c
字号:
/*
ipc.c -- IPC opreation
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <errno.h>
#include <fcntl.h>
#include "mytools.h"
#include "msgsys.h"
extern int errno;
key_t getipckey ( msgfile, id )
char * msgfile;
char id;
{
int fd;
char *filename;
#ifndef wyz_mod_0518
filename = (char *) getfname("WORKDIR", msgfile);
#else
filename = (char *) getfname("TrCPDIR" , msgfile);
#endif
#ifdef DEBUG1_del
printf("getipckey file %s\n", filename);
#endif
fd = open(filename, O_CREAT|O_EXCL);
if (fd == -1 && errno != EEXIST)
return((key_t)-1);
close(fd);
return (ftok(filename, id));
}
/*
creat_msg_nonb ( msgfile, id )
char * msgfile;
char id;
{
key_t key;
int msgid;
#ifdef DEBUG1_del
errcall(WARN, "into creat_msg_nonb(), msgfile=%s,id=%d",msgfile,(int)id);
#endif
if ( ( key = getipckey ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
msgfile, id, key );
return ( -1 );
}
if ( ( msgid = create_message_queue ( key, 60 ) ) < 0 ) {
errcall ( ERROR, "create_message_queue(key=%d) return errno=%d",
key, msgid );
return ( -1 );
}
return ( msgid );
}
*/
/*
get_msg_nonb ( msgfile, id )
char * msgfile;
char id;
{
key_t key;
int msgid;
if ( ( key = getipckey ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d", msgfile, id, key );
return ( -1 );
}
if ( ( msgid = get_message_queue ( key ) ) < 0 ) {
errcall ( ERROR, "get_message_queue(key=%x) return errno=%d", key, errno);
return ( -1 );
}
return ( msgid );
}
*/
/*
rm_msg_nonb ( msgfile, id )
char * msgfile;
char id;
{
key_t key;
int ret;
if ( ( key = getipckey ( msgfile, id ) ) < 0 ) {
errcall(ERROR, "getipckey(%s, %d) return %d", msgfile, id, key);
return(-1);
}
if ( ( ret = remove_message_queue ( key ) ) < 0 ) {
errcall(ERROR, "remove_message_queue(key=%d) return errno=%d", key, ret);
return(-1);
}
return ( 0 );
}
*/
creat_msg ( msgfile, id )
char * msgfile;
char id;
{
key_t key;
int msgid;
if ( id == MONITOR ) key = 0x0981014;
else
if ( ( key = getipckey ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
msgfile, id, key );
return ( -1 );
}
if ( ( msgid = msgget ( key, IPCCREAT ) ) < 0 ) {
errcall ( ERROR, "msgget(key=%d) return errno=%d",
key, errno );
return ( -1 );
}
return ( msgid );
}
get_msg ( msgfile, id )
char * msgfile;
char id;
{
key_t key;
int msgid;
if ( id == MONITOR ) key = 0x0981014;
else
if ( ( key = getipckey ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
msgfile, id, key );
return ( -1 );
}
if ( ( msgid = msgget ( key, IPCGET ) ) < 0 ) {
errcall ( ERROR, "msgget(key=%x) return errno=%d", key, errno );
return ( -1 );
}
return ( msgid );
}
rm_msg ( msgfile, id )
char * msgfile;
char id;
{
int msgid;
if ( ( msgid = get_msg ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "get_msg() return %d", msgid );
return ( -1 );
}
if ( msgctl ( msgid, IPC_RMID, NULL ) < 0 ) {
errcall ( ERROR, "msgctl(msgid=%d,IPC_RMID) return errno=%d",
msgid, errno );
return ( -1 );
}
return ( 0 );
}
get_msg_stat ( msgfile, id, buf )
char * msgfile;
char id;
struct msqid_ds *buf;
{
int msgid;
if ( ( msgid = get_msg ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "get_msg() return %d", msgid );
return ( -1 );
}
if ( msgctl ( msgid, IPC_STAT, buf ) < 0 ) {
errcall ( ERROR, "msgctl(msgid=%d,IPC_STAT) return errno=%d",
msgid, errno );
return ( -1 );
}
return ( 0 );
}
set_msg_stat ( msgfile, id, buf )
char * msgfile;
char id;
struct msqid_ds *buf;
{
int msgid;
if ( ( msgid = get_msg ( msgfile, id ) ) < 0 ) {
errcall ( ERROR, "get_msg() return %d", msgid );
return ( -1 );
}
if ( msgctl ( msgid, IPC_SET, buf ) < 0 ) {
errcall ( ERROR, "msgctl(msgid=%d,IPC_SET) return errno=%d",
msgid, errno );
return ( -1 );
}
return ( 0 );
}
char * creat_shm ( shmfile, id, len )
char * shmfile;
char id;
int len;
{
int shmid;
key_t key;
char * shmptr;
if ( ( key = getipckey ( shmfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
shmfile, id, key );
return ( NULL );
}
if ( ( shmid = shmget ( key, len, IPCCREAT | 0666 ) ) < 0 ) {
errcall ( ERROR, "%d shmget(key=%d) return errno=%d",
getpid(), key, errno );
return ( NULL );
}
if ( ( shmptr = ( char * ) shmat ( shmid, 0, 0 ) ) == (char *)(-1) ) {
errcall ( ERROR, "shmat(key=%d) return errno=%d",
key, errno );
return ( NULL );
}
return ( shmptr );
}
char * get_shm (shmfile, id, len)
char *shmfile;
char id;
int len;
{
int shmid;
key_t key;
char *shmptr;
if ( ( key = getipckey ( shmfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d", shmfile, id, key );
return ( NULL );
}
if ( ( shmid = shmget ( key, len, IPCGET ) ) < 0 ) {
errcall ( ERROR, "pid = %d shmget(key=0x%x) return errno=%d", getpid (),key, errno );
return ( NULL );
}
if ( ( shmptr = ( char * ) shmat ( shmid, 0, 0 ) ) == NULL ) {
errcall ( ERROR, "shmat(key=%d) return errno=%d", key, errno );
return ( NULL );
}
return ( shmptr );
}
dt_shm ( shmptr )
char * shmptr;
{
if ( shmdt ( shmptr ) < 0 ) {
errcall ( ERROR, "shmdt() return errno=%d", errno );
return ( -1 ) ;
}
return ( 0 );
}
rm_shm ( shmfile, id )
char *shmfile;
char id;
{
key_t key;
int shmid;
if ( ( key = getipckey ( shmfile, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
shmfile, id, key );
return ( -1 );
}
if ( ( shmid = shmget ( key, 0, IPCGET ) ) < 0 ) {
errcall ( ERROR, "shmget(key=%d) return errno=%d",
key, errno );
return ( -1 );
}
if ( shmctl ( shmid, IPC_RMID, NULL ) < 0 ) {
int zz;
zz = errno;
errcall ( ERROR, "shmctl(key=%x,IPC_RMID) return errno=%d",
key, zz );
return ( -1 );
}
return ( 0 );
}
int creat_sem ( char *IPC_FILE , int id , int sem_number )
{
int semid;
key_t key;
int i;
if ( ( key = getipckey ( IPC_FILE, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
IPC_FILE, id, key );
return ( -1 );
}
if ( ( semid = semget ( key , sem_number , IPC_CREAT|0666 ) ) < 0 ) {
errcall ( ERROR , "Unable semget (shmfile = %s shmid = %d)!" ,\
IPC_FILE , id );
return -1;
}
for ( i=0 ; i<sem_number ; i++ )
semctl ( semid , i , SETVAL , 0 );
return semid;
}
int get_sem ( char *IPC_FILE , int id , int sem_number )
{
int semid;
key_t key;
int i;
if ( ( key = getipckey ( IPC_FILE, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
IPC_FILE, id, key );
return ( -1 );
}
if ( ( semid = semget ( key , sem_number , 0 ) ) < 0 ) {
errcall ( ERROR , "Unable semget (shmfile = %s shmid = %d)!" ,\
IPC_FILE , id );
return -1;
}
return semid;
}
rm_sem ( IPC_FILE, id )
char *IPC_FILE;
char id;
{
key_t key;
int semid;
if ( ( key = getipckey ( IPC_FILE, id ) ) < 0 ) {
errcall ( ERROR, "getipckey( %s, %d ) return %d",
IPC_FILE, id, key );
return ( -1 );
}
if ( ( semid = semget ( key, 0, IPCGET ) ) < 0 ) {
errcall ( ERROR, "semget(key=%d) return errno=%d",
key, errno );
return ( -1 );
}
if ( semctl ( semid, 0, IPC_RMID, 0 ) < 0 ) {
int zz;
zz = errno;
errcall ( ERROR, "semctl(key=%x,IPC_RMID) return errno=%d",
key, zz );
return ( -1 );
}
return ( 0 );
}
int sem_P ( int semid , int number , int sem_flag )
{
struct sembuf op_lock[1];
op_lock[0].sem_num = number;
op_lock[0].sem_op = -1;
op_lock[0].sem_flg = sem_flag;
if ( semop (semid , op_lock, 1) <0 )
return -1;
else
return 0;
}
int sem_V ( int semid , int number , int sem_flag )
{
struct sembuf op_unlock[1];
op_unlock[0].sem_num = number;
op_unlock[0].sem_op = 1;
op_unlock[0].sem_flg = sem_flag;
if ( semop (semid , op_unlock, 1) <0 )
return -1;
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -