Linux 环境下C编程指南,通过共享内存进行进程间通信的例子,进程间同步使用信号量来实现。
代码 11-5
使用说明:这是一个简单的服务器和客户端程序,如果启动程序时不带参数,则执行服务器程序;
如果带参数,则执行客户端程序,所带参数只有一个,就是服务器端所显示的共享内存的引用ID。
实现原理:服务器端启动后,创建信号量和共享内存,并将共享内存的引用ID显示出来,将信号量
的引用ID存放在共享内存中。客户端启动后,利用服务器端提供的内存共享ID将共享内存附加到地址段,
读取信号量以实现两个进程之间的同步。之后,这两个进程就可以利用共享内存进行进程间通信,客户
端输入的信息将在服务器端显示出来。
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define SHMDATASIZE 1000
#define BUFFERSIZE (SHMDATASIZE - sizeof(int))
#define SN_EMPTY 0
#define SN_FULL 1
int deleteSemid=0;
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
void server(void);
void client(int shmid);
void delete(void);
void sigdelete(int signum);
void locksem(int semid, int semnum);
void unlocksem(int semid, int semnum);
void waitzero(int semid, int semnum);
void clientwrite(int shmid, int semid, char *buffer);
int safesemget(key_t key, int nsems, int semflg);
int safesemctl(int semid, int semnum, int cmd, union semun arg);
int safesemop(int semid, struct sembuf *sops, unsigned nsops);
int safeshmget(key_t key, int size, int shmflg);
void *safeshmat(int shmid, const void *shmaddr, int shmflg);
int safeshmctl(int shmid, int cmd, struct shmid_ds *buf);
int main(int argc, char *argv[])
{
if ( argc < 2 )
{
server();
}
else
{
client(atoi(argv[1]));
}
return 0;
}
void server(void)
{
union semun sunion;
int semid,shmid;
void *shmdata;
char *buffer;
//1 创建信号量灯semget(key, nsems, semflg)
semid = safesemget(IPC_PRIVATE, 2, SHM_R|SHM_W);
deleteSemid = semid;
//2 当程序终止执行时,执行删除信号量,semctl(deleteSemid, 0, IPC_RMID, 0)
atexit(&delete);
//3 接收到信号 SIGINT 则执行 sigdelete 函数 exit(0)
signal(SIGINT, &sigdelete);
//4 设置两个信号灯,空与满,val =1 semctl(semid, semnum, cmd, arg)
sunion.val = 1;
safesemctl(semid, SN_EMPTY, SETVAL, sunion);
sunion.val = 0;
safesemctl(semid, SN_FULL, SETVAL, sunion);
//5 创建共享内存
shmid = safeshmget(IPC_PRIVATE, SHMDATASIZE, IPC_CREAT|SHM_R|SHM_W);
//6 将共享内存加载到自己的地址空间,shmdata为映射区的其始地址
shmdata = safeshmat(shmid, 0, 0);
//7 删除共享内存,当所有附加该共享内存的进程结束或断开与该共享内存的连接时才执行
safeshmctl(shmid, IPC_RMID, NULL);
//8 共享内存首地址为信号灯id
*(int *)shmdata = semid;
//buffer指向共享内存+四个字节后
buffer = shmdata + sizeof(int);
printf("Server is running with SHM id ** %d **/n", shmid);
while(1)
{
printf("Waiting until full...");
fflush(stdout);
//申请1个信号灯资源,使信号灯1阻塞
locksem(semid, SN_FULL);
printf("done./n");
printf("Message received: %s./n", buffer);
//释放1个信号灯资源,
unlocksem(semid, SN_EMPTY);
}
}
void client(int shmid)
{
int semid;
void *shmdata;
char *buffer;
//1 进程加载共享内存到自己地址空间
shmdata = safeshmat(shmid, 0, 0);
//2 共享内存中第一个int为信号灯
semid = *(int *)shmdata;
//3 共享内存指针
buffer = shmdata + sizeof(int);
printf("Client operational: shm id is %d, sem id is %d/n", shmid, semid);
while(1)
{
char input[3];
printf("/n/nMenu/n1.send a message/n");
printf("2.Exit/n");
//输入三个字符
fgets(input, sizeof(input), stdin);
switch(input[0])
{
case '1': clientwrite(shmid, semid, buffer);
break;
case '2': exit(0);
break;
}
}
}
//
void delete(void)
{
printf("/nMaster exiting; deleting semaphore %d./n", deleteSemid);
if (semctl(deleteSemid, 0, IPC_RMID, 0) == -1 )
{
printf("Error releasing semaphore./n");
}
}
//程序退出
void sigdelete(int signum)
{
exit(0);
}
//申请信号灯锁sb.sem_op = -1;semnum第几个信号灯
void locksem(int semid, int semnum)
{
//sem_op = -1申请一个资源
struct sembuf sb;
sb.sem_num = semnum;
sb.sem_op = -1;
sb.sem_flg = SEM_UNDO;
safesemop(semid, &sb, 1);
}
void unlocksem(int semid, int semnum)
{
struct sembuf sb;
sb.sem_num = semnum;
sb.sem_op = 1;
sb.sem_flg = SEM_UNDO;
safesemop(semid, &sb, 1);
}
void waitzero(int semid, int semnum)
{
struct sembuf sb;
sb.sem_num = semnum;
sb.sem_op = 0;
sb.sem_flg = 0;
safesemop(semid, &sb, 1);
}
void clientwrite(int shmid, int semid, char *buffer)
{
printf("Waiting until empty...");
fflush(stdout);
locksem(semid, SN_EMPTY);
printf("done./n");
printf("Enter Message: ");
fgets(buffer, BUFFERSIZE, stdin);
unlocksem(semid, SN_FULL);
}
//safeshmget
int safesemget(key_t key, int nsems, int semflg)
{
int retval;
if ( (retval=semget(key, nsems, semflg)) == -1)
{
printf("semget error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
int safesemctl(int semid, int semnum, int cmd, union semun arg)
{
int retval;
if ( (retval=semctl(semid, semnum, cmd, arg)) == -1)
{
printf("semctl error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
//信号灯操作设置
int safesemop(int semid, struct sembuf *sops, unsigned nsops)
{
int retval;
if ( (retval=semop(semid, sops, nsops)) == -1)
{
printf("semop error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
//IPC_PRIVATE, 2, SHM_R|SHM_W
/********************************************************************
函数名称: safeshmget
函数功能: 从缓存中读取nSize字节大小的数据到pBuf中
计算到缓存尾还能读出缓存字节数,如果尾部大小够读,则从尾部直
接读出,否则,则先从缓存尾读,再从缓存头读取
输入参数: No
输出参数: No
输入输出: No
返回参数: No
*******************************************************************/
int safeshmget(key_t key, int size, int shmflg)
{
int retval;
if ( (retval=shmget(key, size, shmflg)) == -1)
{
printf("shmget error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
//将共享内存加载到自己的地址空间
void *safeshmat(int shmid, const void *shmaddr, int shmflg)
{
void *retval;
if ( (retval=shmat(shmid, shmaddr, shmflg)) == (void *)-1)
{
printf("shmat error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
int safeshmctl(int shmid, int cmd, struct shmid_ds *buf)
{
int retval;
if ( (retval=shmctl(shmid, cmd, buf)) == -1)
{
printf("shmctl error: %s./n", strerror(errno));
exit(254);
}
return retval;
}
--------------------------------
atexit
语法:
#include <stdlib.h> int atexit( void (*func)(void) );
功能: 当程序终止执行时,函数调用函数指针func所指向的函数。可以执行多重调用(至少32个),这些函数以其注册的倒序执行。执行成功返回零值,失败则返回非零值。