⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s.c

📁 在linux平台上实现两个进程之间的交互。一客户进程向一远程进程提交一道作业;远程进程完成作业后向客户进程返回执行结果或者是结束的信息。
💻 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 + -