如何加强校园网站建设高端网站制作软件
news/
2025/9/27 8:49:40/
文章来源:
如何加强校园网站建设,高端网站制作软件,做中国最专业的健康门户网站,福建住房和城乡建设网站进程间通信机制(IPC)
简述
IPC#xff1a;Inter Process Communication
进程和进程之间的用户空间相互独立#xff0c;但是4G内核空间共享#xff0c;进程间的通信就是通过这4G的内核空间
分类
传统的进程间通信机制
无名管道#xff08;pipe#xff09;
有名管道Inter Process Communication
进程和进程之间的用户空间相互独立但是4G内核空间共享进程间的通信就是通过这4G的内核空间
分类
传统的进程间通信机制
无名管道pipe
有名管道fifo
信号signal
System V中的IPC对象和IPC的区别
消息队列message queue
共享内存shared memory
信号灯集semaphore
可用于主机传输的通信机制
套接字socket 消息队列
概念
在内核内存中创建一个队列进程需要将数据打包成结点添加到队尾中或者从队列中读取结点可以通过消息类型进行消息分类
特点
需要打包有特定的格式以及消息类型
按照先进先出原则但是也可以限制消息类型读取
独立于进程灯进程结束后消息队列以及其中的内容不会删除除非中期操作系统或者手动删除
在Linux系统下可以通过命令ipcs查看消息队列通过ipcrm -q msqid删除消息队列
函数
ftok()函数获得一个Key值用来创建消息队列通过相同的key值可以访问对应的消息队列
msgget()函数创建一个消息队列获得起id号
msgsnd()发送消息到指定消息队列
msgrcv()获取对应消息队列中对应的消息
msgctl()控制消息队列常用于删除消息队列
#includemyhead.h
#includesys/msg.h
#includesys/ipc.h
struct msgbuf
{long mtype; //消息类型char mtext[128]; //消息内容
};//线程1函数
void *task1(void *arg)
{int msqid *(int *)arg;//获取消息队列id号struct msgbuf msbuf;//声明消息结构体printf(A\n\t);fflush(stdout);while (1){msbuf.mtype 1;//消息类型fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);msbuf.mtext[strlen(msbuf.mtext)-1]\0;if(msgsnd(msqid,msbuf,sizeof(msbuf)-sizeof(long),0) -1){perror(msgsnd);return NULL;}//printf(线程%d:发送成功\n,getpid());printf(A\n\t);fflush(stdout);if (!strcmp(msbuf.mtext,quit)){system(clear);exit(0);}}pthread_exit(NULL);
}//线程2函数
void *task2(void *arg)
{int msqid *(int *)arg;struct msgbuf buf;ssize_t num 0;while (1){bzero(buf,sizeof(buf));if ((num msgrcv(msqid,buf,sizeof(buf.mtext),2,0))0){//perror(msgrcv);return NULL;}printf(\nB\n\t%s\n,buf.mtext);printf(A\n\t);fflush(stdout);if (!strcmp(buf.mtext,quit)){msgctl(msqid,IPC_RMID,NULL);system(clear);exit(0);}}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{key_t key ftok(./,0);if (key -1){perror(ftok);return -1;/* code */}umask(0);int msqid msgget(key,IPC_CREAT|0664);if (msqid -1){perror(msgget);return -1;}pthread_t tid1,tid2;if (pthread_create(tid1,NULL,task1,msqid) ! 0){printf(线程1创建失败\n);}if (pthread_create(tid2,NULL,task2,msqid) ! 0){printf(线程1创建失败\n);}pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}#includemyhead.h
//#includesys/msg.h
//#includesys/ipc.h
struct msgbuf
{long mtype; //消息类型char mtext[128]; //消息内容
};//线程1函数
void *task1(void *arg)
{int msqid *(int *)arg;struct msgbuf msbuf;printf(B\n\t);fflush(stdout);while (1){msbuf.mtype 2;fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);msbuf.mtext[strlen(msbuf.mtext)-1]\0;if(msgsnd(msqid,msbuf,sizeof(msbuf)-sizeof(long),0) -1){perror(msgsnd);return NULL;}//printf(线程%d:发送成功\n,getpid());printf(B\n\t);fflush(stdout);if (!strcmp(msbuf.mtext,quit)){system(clear);exit(0);}}pthread_exit(NULL);
}//线程2函数
void *task2(void *arg)
{int msqid *(int *)arg;struct msgbuf buf;ssize_t num 0;while (1){bzero(buf,sizeof(buf));if ((num msgrcv(msqid,buf,sizeof(buf.mtext),1,0))0){//perror(msgrcv);return NULL;}printf(\nA\n\t%s\n,buf.mtext);printf(B\n\t);fflush(stdout);if (!strcmp(buf.mtext,quit)){msgctl(msqid,IPC_RMID,NULL);system(clear);exit(0);}}pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{key_t key ftok(./,0);if (key -1){perror(ftok);return -1;/* code */}umask(0);int msqid msgget(key,IPC_CREAT|0664);if (msqid -1){perror(msgget);return -1;}pthread_t tid1,tid2;if (pthread_create(tid1,NULL,task1,msqid) ! 0){printf(线程1创建失败\n);}if (pthread_create(tid2,NULL,task2,msqid) ! 0){printf(线程1创建失败\n);}pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/919265.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!