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]的输入。
程序示例:
- /* 无名管道的建立:适用于具有亲缘关系的进程间通信 */
- /* pipe.c */
- #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)); //filedes[1] is for writing
- }
- else
- {
- read(filedes[0],buf,sizeof(buf)); //filedes[0] is for reading
- printf("Parent Read:%s",buf);
- }
- return 0;
- }
- /* 子进程 借 管道 将字符串 "Hello World!\n" 给 父进程显示 */
/* 无名管道的建立:适用于具有亲缘关系的进程间通信 */
/* pipe.c */
#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)); //filedes[1] is for writing
}
else
{
read(filedes[0],buf,sizeof(buf)); //filedes[0] is for reading
printf("Parent Read:%s",buf);
}
return 0;
}
/* 子进程 借 管道 将字符串 "Hello World!\n" 给 父进程显示 */
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. 进程间利用信号进行通信,可以使用那些函数接口:
- /* 信号发送 */
- /* kill.c */
- #include <sys/types.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <wait.h>
- int main()
- {
- pid_t pid;
- pid = fork();
- if(pid < 0)
- {
- perror("fork");
- exit(1);
- }
- else if(0 == pid)
- {
- printf("Child Process!\n");
- sleep(10);
- }
- else
- {
- printf("Parent Process!\n");
- sleep(2);
- kill(pid,SIGINT); //给子进程发送SIGINT 信号,使子进程结束
- waitpid(pid,NULL,0); //确保父进程后结束
- }
- return 0;
- }
- /* signal.c */
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- void func(int sig)
- {
- printf("Hello World! %d \n",sig); //sig代表 SIGINT号
- }
- int main()
- {
- signal(SIGINT,func); //只要按下Contrl + C ,则指向func函数
- while(1);
- return 0;
- }
- /* signal(); 用于对指定信号进行指定处理 */
- /* raise.c */
- #include <stdio.h>
- #include <signal.h>
- int main()
- {
- int i;
- printf("Begin!\n");
- for(i = 1;i <= 2;i++)
- {
- printf("sleep %d\n",i);
- sleep(1);
- }
- raise(SIGINT); //给当前信号发送终止信号
- while(1);
- return 0;
- }
- /* alarm.c */
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- void func(int sig)
- {
- printf("Hello Wrold!\n");
- }
- int main()
- {
- int i;
- signal(SIGALRM,func);
- alarm(5); //5秒后发送SIGALRM信号给当前进程
- for(i = 1;i < 7;i++)
- {
- printf("sleep %d!\n",i);
- sleep(1);
- }
- return 0;
- }