1.
Linux下进程间通信方式有有那些?
1.1管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。进程的亲缘关系通常是指父子进程关系。
1.2有名管道 (named pipe) : 有名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信。
1.3信号量( semophore ) : 信号量是一个计数器,可以用来控制多个进程对共享资源的访问。它常作为一种锁机制,防止某进程正在访问共享资源时,其他进程也访问该资源。因此,主要作为进程间以及同一进程内不同线程之间的同步手段。
1.4消息队列( message queue ) : 消息队列是由消息的链表,存放在内核中并由消息队列标识符标识。消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。
1.5信号 ( sinal ) : 信号是一种比较复杂的通信方式,用于通知接收进程某个事件已经发生。
1.6共享内存( shared memory ) :共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问。共享内存是最快的 IPC 方式,它是针对其他进程间通信方式运行效率低而专门设计的。它往往与其他通信机制,如信号两,配合使用,来实现进程间的同步和通信。
1.7套接字( socket ) : 套解口也是一种进程间通信机制,与其他通信机制不同的是,它可用于不同及其间的进程通信。
2.何为管道?
管道是进程间通信中最古老的方式,它包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信。
3.如何创建一个无名管道?
无名管道由pipe()函数创建:
#include <unistd.h>
int pipe(int filedis[2]);
参数filedis返回两个文件描述符:filedes[0]为读而打开,filedes[1]为写而打开。filedes[1]的输出是filedes[0]的输入。
程序示例:
-
-
-
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
-
-
- int main()
- {
- int filedes[2];
- char buf[100] = {0};
- int ret;
- pid_t pid;
- char s[100] = "Hello World!\n";
-
- ret = pipe(filedes);
- if(ret < 0)
- {
- perror("pipe");
- exit(1);
- }
-
- pid = fork();
- if(pid < 0)
- {
- perror("fork");
- exit(2);
- }
- else if(0 == pid)
- {
- printf("Child begin write!\n");
- write(filedes[1],s,strlen(s));
- }
- else
- {
- read(filedes[0],buf,sizeof(buf));
- printf("Parent Read:%s",buf);
- }
-
-
- return 0;
- }
-
-
-
-
-
-
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
-
-
- int main()
- {
- int filedes[2];
- char buf[100] = {0};
- int ret;
- pid_t pid;
- char s[100] = "Hello World!\n";
-
- ret = pipe(filedes);
- if(ret < 0)
- {
- perror("pipe");
- exit(1);
- }
-
- pid = fork();
- if(pid < 0)
- {
- perror("fork");
- exit(2);
- }
- else if(0 == pid)
- {
- printf("Child begin write!\n");
- write(filedes[1],s,strlen(s));
- }
- else
- {
- read(filedes[0],buf,sizeof(buf));
- printf("Parent Read:%s",buf);
- }
-
-
- return 0;
- }
-
-
-
4.如何创建一个有名管道?
在Linux系统下,有名管道可由两种方式创建:命令行方式mknod系统调用和函数mkfifo。下面的两种途径都在当前目录下生成了一个名为myfifo的有名管道:
方式一:mkfifo("myfifo","rw");
方式二:mknod myfifo p
生成了有名管道后,就可以使用一般的文件I/O函数如open、close、read、write等来对它进行操作。
-
- #include <stdio.h>
- #include <unistd.h>
- void main() {
- FILE * in_file;
- int count = 1;
- char buf[80];
- in_file = fopen("mypipe", "r");
- if (in_file == NULL) {
- printf("Error in fdopen./n");
- exit(1);
- }
- while ((count = fread(buf, 1, 80, in_file)) > 0)
- printf("received from pipe: %s/n", buf);
- fclose(in_file);
- }
-
-
-
- #include <stdio.h>
- #include <unistd.h>
- void main() {
- FILE * out_file;
- int count = 1;
- char buf[80];
- out_file = fopen("mypipe", "w");
- if (out_file == NULL) {
- printf("Error opening pipe.");
- exit(1);
- }
- sprintf(buf,"this is test data for the named pipe example/n");
- fwrite(buf, 1, 80, out_file);
- fclose(out_file);
- }
-
- #include <stdio.h>
- #include <unistd.h>
- void main() {
- FILE * in_file;
- int count = 1;
- char buf[80];
- in_file = fopen("mypipe", "r");
- if (in_file == NULL) {
- printf("Error in fdopen./n");
- exit(1);
- }
- while ((count = fread(buf, 1, 80, in_file)) > 0)
- printf("received from pipe: %s/n", buf);
- fclose(in_file);
- }
-
-
-
- #include <stdio.h>
- #include <unistd.h>
- void main() {
- FILE * out_file;
- int count = 1;
- char buf[80];
- out_file = fopen("mypipe", "w");
- if (out_file == NULL) {
- printf("Error opening pipe.");
- exit(1);
- }
- sprintf(buf,"this is test data for the named pipe example/n");
- fwrite(buf, 1, 80, out_file);
- fclose(out_file);
- }
6. 进程间利用信号进行通信,可以使用那些函数接口: