linux进程通信1


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]的输入。

程序示例:

  1. /* 无名管道的建立:适用于具有亲缘关系的进程间通信 */  
  2. /* pipe.c */  
  3.   
  4. #include <stdio.h>  
  5. #include <unistd.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8.   
  9.   
  10. int main()  
  11. {  
  12.     int filedes[2];  
  13.     char buf[100] = {0};  
  14.     int ret;  
  15.     pid_t pid;  
  16.     char s[100] = "Hello World!\n";  
  17.   
  18.     ret = pipe(filedes);  
  19.     if(ret < 0)  
  20.     {  
  21.         perror("pipe");  
  22.         exit(1);  
  23.     }  
  24.   
  25.     pid = fork();  
  26.     if(pid < 0)  
  27.     {  
  28.         perror("fork");  
  29.         exit(2);  
  30.     }  
  31.     else if(0 == pid)  
  32.     {  
  33.         printf("Child begin write!\n");  
  34.         write(filedes[1],s,strlen(s));   //filedes[1] is for writing  
  35.     }  
  36.     else  
  37.     {  
  38.         read(filedes[0],buf,sizeof(buf));   //filedes[0] is for reading  
  39.         printf("Parent Read:%s",buf);  
  40.     }  
  41.       
  42.   
  43.     return 0;  
  44. }  
  45.   
  46.   
  47. /* 子进程 借 管道 将字符串 "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函数如openclosereadwrite等来对它进行操作。


  1. /* 进程一:读有名管道*/   
  2. #include <stdio.h>   
  3. #include <unistd.h>   
  4. void main() {   
  5. FILE * in_file;   
  6. int count = 1;   
  7. char buf[80];   
  8. in_file = fopen("mypipe""r");   
  9. if (in_file == NULL) {   
  10. printf("Error in fdopen./n");   
  11. exit(1);   
  12. }   
  13. while ((count = fread(buf, 1, 80, in_file)) > 0)   
  14. printf("received from pipe: %s/n", buf);   
  15. fclose(in_file);   
  16. }   
  17.    
  18.    
  19.   /* 进程二:写有名管道*/   
  20. #include <stdio.h>   
  21. #include <unistd.h>   
  22. void main() {   
  23. FILE * out_file;   
  24. int count = 1;   
  25. char buf[80];   
  26. out_file = fopen("mypipe""w");   
  27. if (out_file == NULL) {   
  28. printf("Error opening pipe.");   
  29. exit(1);   
  30. }   
  31. sprintf(buf,"this is test data for the named pipe example/n");   
  32. fwrite(buf, 1, 80, out_file);   
  33. fclose(out_file);   
  34. }   
/* 进程一:读有名管道*/ 
#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. 进程间利用信号进行通信,可以使用那些函数接口:

 

  1. /* 信号发送 */  
  2. /* kill.c */  
  3.   
  4. #include <sys/types.h>  
  5. #include <signal.h>  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <unistd.h>  
  9. #include <wait.h>  
  10.   
  11.   
  12. int main()  
  13. {  
  14.     pid_t pid;  
  15.       
  16.     pid = fork();  
  17.     if(pid < 0)  
  18.     {  
  19.         perror("fork");  
  20.         exit(1);  
  21.     }  
  22.     else if(0 == pid)  
  23.     {  
  24.         printf("Child Process!\n");  
  25.         sleep(10);  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("Parent Process!\n");  
  30.         sleep(2);  
  31.         kill(pid,SIGINT);   //给子进程发送SIGINT 信号,使子进程结束  
  32.         waitpid(pid,NULL,0);  //确保父进程后结束  
  33.     }  
  34.   
  35.     return 0;  
  36. }  
  37.   
  38.   
  39.   
  40.   
  41. /* signal.c */  
  42.   
  43. #include <stdio.h>  
  44. #include <unistd.h>  
  45. #include <signal.h>  
  46.   
  47. void func(int sig)  
  48. {  
  49.     printf("Hello World! %d \n",sig);  //sig代表 SIGINT号  
  50. }  
  51.   
  52. int main()  
  53. {  
  54.     signal(SIGINT,func);   //只要按下Contrl + C ,则指向func函数  
  55.   
  56.     while(1);  
  57.   
  58.     return 0;  
  59. }  
  60.   
  61.   
  62. /* signal(); 用于对指定信号进行指定处理 */  
  63.   
  64.   
  65.   
  66.   
  67. /* raise.c */  
  68.   
  69. #include <stdio.h>  
  70. #include <signal.h>  
  71.   
  72. int main()  
  73. {  
  74.     int i;  
  75.   
  76.     printf("Begin!\n");  
  77.     for(i = 1;i <= 2;i++)  
  78.     {  
  79.         printf("sleep %d\n",i);  
  80.         sleep(1);  
  81.     }  
  82.     raise(SIGINT);  //给当前信号发送终止信号  
  83.   
  84.     while(1);  
  85.   
  86.     return 0;  
  87. }  
  88.   
  89.   
  90. /* alarm.c */  
  91.   
  92. #include <stdio.h>  
  93. #include <unistd.h>  
  94. #include <signal.h>  
  95.   
  96. void func(int sig)  
  97. {  
  98.     printf("Hello Wrold!\n");  
  99. }  
  100.   
  101. int main()  
  102. {  
  103.     int i;  
  104.   
  105.     signal(SIGALRM,func);  
  106.     alarm(5);   //5秒后发送SIGALRM信号给当前进程  
  107.   
  108.     for(i = 1;i < 7;i++)  
  109.     {  
  110.         printf("sleep %d!\n",i);  
  111.         sleep(1);  
  112.     }  
  113.   
  114.     return 0;  
  115. }  

猜你喜欢

转载自blog.csdn.net/qy961212/article/details/78685593