📄 s.c
字号:
# include <unistd.h># include <stdio.h> /*sever process*/# include <sys/msg.h># include <sys/types.h># include <sys/ipc.h># include <sys/wait.h># include <string.h># define msgkey 181struct mymsgform{ long mtype; char mpath[250]; }msg;int open_queue(key_t keyval){int qid; if ((qid=msgget(msgkey,IPC_CREAT|0660))==-1) { return(-1); } return(qid);}int send_message(int qid,struct mymsgform*msg){ int result,length; length=sizeof(struct mymsgform)-sizeof(long); if ((result=msgsnd(qid,msg,length,0))==-1) { return(-1); } return(qid);}int read_message(int qid,long type,struct mymsgform*msg){ int result,length; length=sizeof(struct mymsgform)-sizeof(long); if ((result=msgrcv(qid,msg,length,type,0))==-1) { return(-1); } return(result);}main(){ int qid,pid,abc,status1; pid_t pid1; printf("(s)Be ready to receive a message!\n"); if ((qid=open_queue(msgkey))==-1) { perror("open_queue"); exit(1); } /*CREATE A MESSAGE QUEUE*/ printf("(s)creat a message queue,its keyvalue is %d\n",msgkey); pid=getpid(); printf("(s)The process id is %d\n",pid); msg.mtype=1; while (abc=read_message(qid,msg.mtype,&msg)==-1) { } printf("(s)receive a message!\n"); pid1=fork(); if (pid1>0) { printf("(s)now child run the other program!\n"); execl(msg.mpath,(char*)0); waitpid(pid1,&status1,0); }else { send_message(qid,&msg); printf("(s)sever had finished the work!\n"); exit(1);} if (msgctl(qid,IPC_RMID,0)==-1) { printf("(s)the message could not be removed!"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -