1. 实例一,server
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct mybuf
{
int pid;
char buf[124];
};
void myfun(int signum)
{
return ;
}
int main()
{
int shmid;
int key;
struct mybuf *p;
int pid;
key=ftok("./a.c",'a');
if(key < 0)
{
printf("creat key failure\n");
return -1;
}
printf("creat key sucess\n");
shmid=shmget(key,128,IPC_CREAT | 0777);
if(shmid <0)
{
printf("creat share memory failure\n");
return -1;
}
printf("creat share memory sucess shmid=%d\n",shmid);
signal(SIGUSR2,myfun);
p=(struct mybuf *)shmat(shmid,NULL,0);
if(p == NULL)
{
printf("parent process:shmat function failure\n");
return -3;
}
//get client pid
p->pid=getpid();//write server pid to share memory
pause();//wait client read server pid;

博客探讨了在非亲缘关系进程间使用共享内存进行通信时遇到的卡死问题。通过实例展示了在server端可能出现的卡死现象,并指出在client端发送信号前增加100ms的休眠可以解决问题,但未详细说明原因。
最低0.47元/天 解锁文章
1012

被折叠的 条评论
为什么被折叠?



