Linux下通过共享内存进行进程间通信,进程间同步使用信号量来实现
Linux 环境下C编程指南,通过共享内存进行进程间通信的例子,进程间同步使用信号量来实现。
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;
semid = safesemget(IPC_PRIVATE, 2, SHM_R|SHM_W);
deleteSemid = semid;
atexit(&delete); //当程序终止执行时,执行 delete 函数
signal(SIGINT, &sigdelete); //接收到信号 SIGINT 则执行 sigdelete 函数
sunion.val = 1;
safesemctl(semid, SN_EMPTY, SETVAL, sunion);
sunion.val = 0;
safesemctl(semid, SN_FULL, SETVAL, sunion);
shmid = safeshmget(IPC_PRIVATE, SHMDATASIZE, IPC_CREAT|SHM_R|SHM_W);
shmdata = safeshmat(shmid, 0, 0);
safeshmctl(shmid, IPC_RMID, NULL); //删除共享内存,当所有附加该共享内存的进程结束或断开与该
//共享内存的连接时才执行
*(int *)shmdata = semid;
buffer = shmdata + sizeof(int);
printf("Server is running with SHM id ** %d **/n", shmid);
while(1)
{
printf("Waiting until full...");
fflush(stdout);
locksem(semid, SN_FULL);
printf("done./n");
printf("Message received: %s./n", buffer);
unlocksem(semid, SN_EMPTY);
}
}
void client(int shmid)
{
int semid;
void *shmdata;
char *buffer;
shmdata = safeshmat(shmid, 0, 0);
semid = *(int *)shmdata;
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);
}
void locksem(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 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);
}
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;
}
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个),这些函数以其注册的倒序执行。执行成功返回零值,失败则返回非零值。
更多推荐
所有评论(0)