父子进程创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork函数的头文件
int main()
{
pid_t pid;
pid = fork();//创建父进程
if(pid == -1)
{
perror("fork");
return 1;
}
if(pid>0)//父进程
{
printf("这是父进程");
sleep(10)
}
else if(pid==0)//子进程
{
printf("这是子进程");
sleep(5);
printf("子进程退出");
exit(0);
}
return 0;

}

1 return, 如果什么都不接的话,其实就是void类型函数的返回,返回后不再执行return后面的语句

如果函数执行成功返回0,不成功返回非0,一般情况下非0值常用-1来表示。

2 return 0:一般用在主函数结束时,表示程序正常终止,即告诉系统程序正常。

3 return -1::表示返回一个代数值,一般用在子函数结尾。表示程序异常终止,即告诉系统程序异常
4 return 1: 代表函数非正常终止与return-1 意思差不多

无名管道

在创建子进程之前使用pipe()把函数中定义的dfs变为管道,这样这个dfs就可以在父子进程之间(复制品和复制原件两个地址空间之间)通过read和write传递信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork()、pipe()的头文件
int main()
{
int dfs[2];
int ret = -1;
char buf[64];
pid_t pid = -1;
ret = pipe(dfs);
if(ret == -1)
{
perror("pipe");
return 1;
}
pid = fork();//创建子进程
if(pid == -1)
{
perror("fork");
return 1;
}
if(pid>0)//父进程
{
close(dfs[0]);
memset(buf,0,64);
sprintf(buf,"ABCDEFGHIJ",i++);
ret = write(dfs[1],buf,strlen(buf));
if(ret < 0)
{
perror("write");
exit(-1);
}
close(dfs[1]);


printf("这是父进程");
sleep(10);
}
else if(pid==0)//子进程
{
close(dfs[1]);
memset(buf,0,64);
ret = read(dfs[0],buf,64);
if(ret < 0)
{
perror("read");
exit(-1);
}

printf("这是子进程\n");
sleep(5);

printf("buf:%s",buf);
close(dfs[0]);

printf("子进程退出\n");
exit(0);
}
return 0;

}

有名管道

用mkfifo()创建一个管道文件在当前目录中,用这个文件代替之前的int fds[2],pipe(fds)得到的管道

只读文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork()、pipe()的头文件
int main()
{
//int dfs[2];
int ret = -1;
int fd = -1;
char buf[64];

pid_t pid = -1;
ret = mkfifo(fifor);//创建一个有名管道,此时在当前路径里就有这个文件了
if(ret == -1)
{
perror("mkfifo");
return 1;
}
//1、以只读的方式打开一个文件
fd = open("fifor",O_RDONLY);//使用之前的有名管道
if(fd == -1)
{
perror("open");
return 1;
}
//2、读管道
while(1)
{
memset(buf,0,64);
ret = read(fd,buf,64);
if(ret <= 0)
{
perror("read");
exit(-1);
}
printf("buf:%s",buf);
}
//3、关闭文件
close(fd);
return 0;

}

只写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork()、pipe()的头文件
int main()
{
//int dfs[2];
int ret = -1;
int fd = -1;
char buf[64];
int i = 0;
//1、以只写的方式打开一个文件
pid_t pid = -1;
ret = mkfifo(fifow);//创建一个有名管道,此时在当前路径里就有这个文件了
if(ret == -1)
{
perror("mkfifo");
return 1;
}

fd = open("fifow",O_WRONLY);//使用之前的有名管道
if(fd == -1)
{
perror("open");
return 1;
}
//2、写管道
while(1)
{
memset(buf,0,64);
sprintf(buf,"ABCDEFGHIJ",i++);
ret = write(fd,buf,strlen(buf));
if(ret <= 0)
{
perror("write");
exit(-1);
}
printf("write fifo:%d",ret);
printf("write buf:%s",buf);
sleep(1);
}
//3、关闭文件
close(fd);
return 0;


这样会阻塞

两个文件分别读写

第一个程序管道1写管道2读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork()、pipe()的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
//int dfs[2];
int ret = -1;
int fdw = -1;
int fdr = -1;
int i = 0;
char buf[64];
//1、以只写的方式打开一个文件
ret = mkfifo("fifo1",0644);//创建一个有名管道1,此时在当前路径里就有这个文件了
if(ret == -1)
{
perror("mkfifo");
return 1;
}

ret = mkfifo("fifo2",0644);//创建一个有名管道2,此时在当前路径里就有这个文件了
if(ret == -1)
{
perror("mkfifo");
return 1;
}

fdw = open("fifo1",O_WRONLY);//使用之前的有名管道
if(fdw == -1)
{
perror("open");
return 1;
}
fdr = open("fifo2",O_RDONLY);//使用之前的有名管道
if(fdr == -1)
{
perror("open");
return 1;
}

while(1)
{
//写管道1
memset(buf,0,64);
sprintf(buf,"hello itcast %d",i++);
ret = write(fdw,buf,strlen(buf));
if(ret <= 0)
{
perror("write");
exit(-1);
}
printf("write fifo:%d\n",ret);
printf("write buf:%s\n",buf);
sleep(1);
//读管道2
memset(buf,0,64);
ret = read(fdr,buf,64);
if(ret <= 0)
{
perror("write");
exit(-1);
}
printf("read buf:%s\n",buf);

}


//3、关闭文件
close(fdr);
close(fdw);
return 0;
}


第二个程序写管道2读管道1

啧打不开fifo文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//防止printf时出错
#include<pthread.h>//进程创建
#include<unistd.h>//包含fork()、pipe()的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
//int dfs[2];
int ret = -1;
int fdw = -1;
int fdr = -1;
int i = 0;
char buf[64];
//1、以只写的方式打开一个文件

fdw = open("fifo2",O_WRONLY);//使用之前的有名管道
if(fdw == -1)
{
perror("open");
return 1;
}
fdr = open("fifo1",O_RDONLY);//使用之前的有名管道
if(fdr == -1)
{
perror("open");
return 1;
}

while(1)
{
//读管道1
memset(buf,0,64);
ret = read(fdr,buf,64);
if(ret <= 0)
{
perror("write");
exit(-1);
}
printf("read buf:%s\n",buf);

//写管道2
memset(buf,0,64);
sprintf(buf,"ABCDEFGHIJ cast %d ",i++);
ret = write(fdw,buf,strlen(buf));
if(ret <= 0)
{
perror("write");
exit(-1);
}
printf("write fifo:%d\n",ret);
printf("write buf:%s\n",buf);
sleep(1);


}


//3、关闭文件
close(fdr);
close(fdw);
return 0;
}