进程间通信方式:

同主机进程间数据交换机制: pipe(无名管道) / fifo(有名管道)/ message queue(消息队列)和共享内存。
必备基础: fork() 创建一个与之前完全一样的进程,这两个进程执行没有固定的先后顺序,哪个进程先执行要看系统的进程调度策略。
一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都
复制到新的新进程中,只有少数值与原来的进程的值不同。相当于克隆了一个自己。
vfork : 与fork用法相同,但是他和父进程共享同样的数据存储,因此无需完全复制父进程的地址空间。
// fork() study example 1#include <unistd.h>
#include <stdio.h>
int main ()
{
pid_t fpid; //fpid表示fork函数返回的值
int count=0; // fork 会将这个变量存在两个不同的内存中,所以两次count的值都是 1 ,而不是 1,2 。
fpid=fork();
if (fpid < 0)
printf("error in fork!");
else if (fpid == 0) {
printf("i am the child process, my process id is %d、n",getpid());
printf("我是爹的儿子\n");//对某些人来说中文看着更直白。
count++;
}
else {
printf("i am the parent process, my process id is %d\n",getpid());
printf("我是孩子他爹\n");
count++;
}
printf("统计结果是: %d\n",count);
return 0;
}
运行结果:

在for之前只有一个进程执行代码,但是在fork之后就会再创建一个进程去同时执行这段代码。
程序通过fork的返回值fpid判断是子进程还是父进程,还是创建进程失败。
fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:
1)在父进程中,fork返回新创建子进程的进程ID; //相当于父进程指向自己的子进程,而子进程没有孩子进程可以指向。
2)在子进程中,fork返回0;
3)如果出现错误,fork返回一个负值;
fork出错可能有两种原因:
1)当前的进程数已经达到了系统规定的上限,这时errno的值被设置为EAGAIN。
2)系统内存不足,这时errno的值被设置为ENOMEM。
#include <unistd.h>
#include <stdio.h>
int main(void)
{
int i=0;
printf("i son/pa ppid pid fpid\n");
//ppid指当前进程的父进程pid
//pid指当前进程的pid,
//fpid指fork返回给当前进程的值
for(i=0;i<2;i++){
pid_t fpid=fork();
if(fpid==0)
printf("%d child %4d %4d %4d\n",i,getppid(),getpid(),fpid);
else
printf("%d parent %4d %4d %4d\n",i,getppid(),getpid(),fpid);
}
return 0;
}

1. 在执行第一个循环时:
pid=5944 的进程 ,创建了 一个子进程 5945
2. 第二次循环中:
5944 的进程穿件了 pid=5946的子进程
5945 的进程作为父进程创建了 pid=5947 的子进程
p5947的父进程 应该是 5945 ,但是 这时 5945进程肯能已经死亡。 (具体原因自己还没有弄明白,如需深入学习可以见参考资料)
进程间通信: 管道及无名管道
竞争条件:两个或者多个进程读写某些共享数据,而最后的结果取决于进程的运行的精确时序,称为竞争条件。(把条件理解成情况,竞争情况,貌似更加容易理解一些=。=)
互斥:互斥是一种手段,它使共享数据的进程无法同时对其共享的数据进行处理
临界区:即访问共享内存的程序片。也就是说,通过合理的安排,使得两个进程不可能同时处在临界区中,就能避免竞争条件。满足一下四个条件
a/ 任何两个进程不能同时处于临界区
b/ 不应对cpu速度和数量做任何假设
c/ 临界区外运行的进程不得阻塞其他进程
d/ 不得使进程无限期的等待进入临界区
忙等待互斥的几种实现方法
a/ 屏蔽中断:屏蔽中断之后cpu不会被切换到其他进程,他就可以检查和修改共享内存,而不必担心其他进程的进入。
缺点:多核的系统无效(其他进程任然可以占用其他的CPU继续访问公共内存)
用户程序来控制中断会很危险(使想一下,一个进程屏蔽中断后不再打开中断,那你的系统就GG了)
结论:屏蔽中断对系统本身是一项很有用的技术,但对用户进程不是一种合适的通用互斥机制。
b/ 锁变量:屏蔽中断的软件实现机制。
假定一个共享(锁)变量,初值为0,代表临界区内无进程,进程进入临界区后将其改变为1,代表临界区内有进程;倘若进程在进入临界区之前,
锁变量值为1,该进程将等待其值变为0。未能实现的原因:与假脱机目录的疏漏一样,如果一个进程进入临界区,但是在它把锁变量置1之前被中断,另一
个进程进入临界区后将0置1,这样, 当前一个进程再次运行时它也将锁变量置1,这样临界区内依然存在两 个进程。
c/严格轮换原理:共享turn变量,用来记录轮到那个进程进入临界区。
当turn=0时,只有进程0能进入临界区,进程0在离开临界区前将turn置1,从而标志,轮到进程1进入临界区。
缺点:严格地轮换,可能导致临界区外的进程阻塞需要进入临界区的进程(例如:当turn=0时,意味着只有进程0能进入临界区,这时如果进程
0还要100年才会退出临界区,而进程1需要马上进入,那进程1还要白白等100 年.)
总结:当一个进程比另一个进程慢了许多的情况下,不宜用这种方式。
d/ Peterson解法
这是Peterson本人发明的一种简单的互斥算法。
#define FALSE 0 #define TRUE 0 #define N 2 int turn ; //当前轮转到那个进程 int intre[N]; //初始化置为false,即没有在临界区等待读写共享数据的
void enter_region(int process){ int other ; other = 1-process; intre[process] = TRUE; turn = process; while(turn==process&&intre[other]==TRUE) ; //一直循环,直到other进程退出临界区
} void leave_process(int process){ intre[process] = FALSE; }
我们分情况跑一遍程序:
a、进程0通过调用enter_region()进入临界区,此时1也想调用enter_region() 来进入临界区,但interested[0]为TRUE,因此被while循环挂起,当进程0出临
界区时调用leave_region(),将interested[0]设为FALSE,进程1即可及时进入临界 区。
b、当进程0在调用enter_region()过程的任意时刻被中断,进程1进入临界区 后进程0再次进行时,依然会被挂起。(实际上while循环体中的两条判断句就
保证了,当一个进程在临界区中时,另一个想进入临界区的进程必然会被挂起)。
信号量
编程步骤:

b/ 创建一个双通道的管道通信

插播一个知识点:
1)write的返回值大于0,表示写了部分或者是全部的数据.
2)返回的值小于0,此时出现了错误.我们要根据错误类型来处理. 如果错误为EINTR表示在写的时候出现了中断错误. 如果为EPIPE表示网络连接出现了问题(对方已经关闭了连接).

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8
9 #define MAX 100
10
11 int child_work(int pfd,char *fname)
12 {
13 int n,fd;
14 char buf[MAX];
15
16 if((fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,0666)) < 0)
17 {
18 fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
19 return -1;
20 }
21
22 while( n = read(pfd,buf,sizeof(buf)) )
23 {
24 write(fd,buf,n);
25 }
26
27 close(pfd);
28
29 return 0;
30 }
31
32 int father_work(int pfd,char *fname)
33 {
34 int fd,n;
35 char buf[MAX];
36
37 if((fd = open(fname,O_RDONLY)) < 0)
38 {
39 fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
40 return -1;
41 }
42
43 while(n = read(fd,buf,sizeof(buf)))
44 {
45 write(pfd,buf,n);
46 }
47
48 close(pfd);
49
50 return 0;
51 }
52
53 int main(int argc,char *argv[])
54 {
55 int pid;
56 int fd[2];
57
58 if(argc < 3)
59 {
60 fprintf(stderr,"usage %s argv[1] argv[2].\n",argv[0]);
61 exit(EXIT_FAILURE);
62 }
63
64 if(pipe(fd) < 0)
65 {
66 perror("Fail to pipe");
67 exit(EXIT_FAILURE);
68 }
69
70 if((pid = fork()) < 0)
71 {
72 perror("Fail to fork");
73 exit(EXIT_FAILURE);
74
75 }else if(pid == 0){
76
77 close(fd[1]);
78 child_work(fd[0],argv[2]);
79
80 }else{
81
82 close(fd[0]);
83 father_work(fd[1],argv[1]);
84 wait(NULL);
85 }
86
87 exit(EXIT_SUCCESS);
88 }
子进程: 读取 管道中的数据进入buf,然后将buf中的数据写入到 file2中去
父进程: 从file1中读取数据,然后写进管道中给子进程去读
int mkfifo(const char * path_name ,mode_t mode);
该函数的第一个参数是一个普通的路劲名,也就是创建后FIFO的名字。第二个参数与打开普通文件的open()函数中的mode参数相同。如果mkfifo的一个参数是一个已经存在路劲名时,会返回EEXIST错误,
所以一般典型的调用代码首先会检查是否返回该错误,如果确实返回该错误,那么只要调用打开FIFO的函数就可以了。
创建管道成功后,可使用open()、read()和write()等函数。

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 int main(int argc,char *argv[])
10 {
11 int fd;
12
13 if(argc < 2)
14 {
15 fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
16 exit(EXIT_FAILURE);
17 }
18
19 if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
20 {
21 fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
22 exit(EXIT_FAILURE);
23 }
24
25 if((fd = open(argv[1],O_WRONLY)) < 0)
26 {
27 fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
28 exit(EXIT_FAILURE);
29 }
30
31 printf("open for write success.\n");
32
33 return 0;
34 }
open for read

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int fd;
if(argc < 2)
{
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
exit(EXIT_FAILURE);
}
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
if((fd = open(argv[1],O_RDONLY)) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open for read success.\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX 655360
int main(int argc,char *argv[])
{
int n,fd,i;
char buf[MAX]="i am the buf in write";
// string temp ;
if(argc < 2)
{
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
exit(EXIT_FAILURE);
}
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
if((fd = open(argv[1],O_WRONLY )) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open for write success.\n");
while(1)
{
printf(">");
int i=0;
while((buf[i]=getchar())!='\n')
i++;
// scanf("%s",&temp);
// for(i=0;i<sizeof(temp);i++)
// buf[i]=temp[i];
n = write(fd,buf,i);
printf("write %d bytes.\n",n);
}
exit(EXIT_SUCCESS);
}
read from fifo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX 20
int main(int argc,char *argv[])
{
int fd,n,i;
char buf[MAX];
if(argc < 2)
{
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
exit(EXIT_FAILURE);
}
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
if((fd = open(argv[1],O_RDONLY )) < 0)
{
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
exit(EXIT_FAILURE);
}
printf("open for read success.\n");
while(1)
{
printf(">");
scanf("%d",&n);
n = read(fd,buf,n);
printf("Read %d bytes.\n",n);
for(i=0;i<20;i++)
printf("%c",buf[i]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
将write to fifo 部分中,输入一个字符串进入到buf中,再将buf中这个字符串写入到 fifo中,另一个进程再从fifo中读出数据
可以将这两个程序运行,然后输入read和write FIFO大小就可以看到效果。
参考资料:
http://blog.csdn.net/w616589292/article/details/50957456
http://www.cnblogs.com/bastard/archive/2012/08/31/2664896.html#3311781
来源:https://www.cnblogs.com/NeilZhang/p/5317825.html
