该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define msg_ser 10
#define N 512
#define lenth (sizeof(my_msg_t))
typedef struct my_msg_s{
long mymsgtype;
char some_text[N];
int pid;
char name[20];
int bztype;
}my_msg_t;
typedef struct list
{
int type;
struct list *next;
}list,*linklist;
void send(linklist h,int msgid,my_msg_t *some_data)
{
int i;
for(i=0;i
{
some_data->mymsgtype=getvalue(h,i);
if(some_data->mymsgtype!=some_data->pid)
{
msgsnd(msgid,some_data,lenth,0);
}
}
}
linklist creatlist()
{
linklist h;
h=(linklist)malloc(sizeof(list));
h->next=NULL;
return h;
}
int getlenth(linklist h)
{
int n=0;
h=h->next;
while(h){
n++;
h=h->next;
}
return n;
}
int empty(linklist h)
{
if(NULL==h->next){return 1;}
else return 0;
}
int insert(linklist h,int x)
{
linklist p;
p=(linklist)malloc(sizeof(list));
p->type=x;p->next=NULL;
while(h->next)
{
h=h->next;
}
h->next=p;
return 0;
}
int delete(linklist h,int x)
{
linklist p=h->next;
while(p){
if(p->type==x){
h->next=p->next;
free(p);
p=h->next;
}
else{
h=p;p=h->next;
}
}
return 0;
}
int getvalue(linklist h,int y)
{
h=h->next;
while(y--){h=h->next;}
return h->type;
}
int main(int argc,char** argv)
{
char input1[3],input2[3];pid_t pid,pid2;
int msgid;
my_msg_t some_data;
msgid=msgget((key_t)3456,0666|IPC_CREAT);
if(msgid==-1)
{
fprintf(stderr,"msgget failed error:%d\n",errno);
exit(EXIT_FAILURE);
}
pid=fork();
if(pid<0) {printf("fork error");}
else if(pid==0)
{
pid2=getpid();
linklist h;h=creatlist();
while(1)
{
msgrcv(msgid,&some_data,lenth,msg_ser,0);
if(some_data.bztype==1)
{
insert(h,some_data.pid);
printf("name:%s id:%d online\n",some_data.name,some_data.pid);
send(h,msgid,&some_data);
}
if(some_data.bztype==2)
{
send(h,msgid,&some_data);