-
用命名管道分别写一个服务器程序和一个客户机程序,客户机的父进程负责每隔一秒产生一个子进程(形成一个进程扇),而每个子进程则往FIFO写入自己的PID号码。服务器负责从该FIFO中读取数据并将之打印到屏幕上。
client客户机:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/wait.h>
#include <stdlib.h>
#define FIFO_PATH "/tmp/myfifo"
int main(){
int i = 0;
//使用access()函数判断fifo管道文件是否存在
int ret0 = access(FIFO_PATH, F_OK);
if(ret0 == 0){
printf("file exist\n");
}
else{
int ret = mkfifo(FIFO_PATH, 0777);
if(ret == -1){
perror("mkfifo failed");
return -1;
}
}
//2,打开管道文件 (读写权限打开)
int fd = open(FIFO_PATH, O_RDWR);
if(fd < 0 ){
perror("open fifo failed");
return -1;
}
//创建子进程
for(i=0;i<10;i++){
pid_t pid = fork();
if(pid < 0){
perror("fork failed \n");
return -1;
}
if(pid > 0){
sleep(1);
continue; //父进程不能退出
}
if(pid == 0){
char buf[128];
pid = getpid();
bzero(buf, sizeof(buf));
sprintf(buf,"pid = %d",getpid());
write(fd, buf, sizeof(buf)); //阻塞
printf("child send info to serve:%s\n",buf);
exit(0); //终止子进程,否则每循环一次,子进程又会分裂出新的进程
}
}
return 0;
}
serve服务器:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/wait.h>
#include <stdlib.h>
#define FIFO_PATH "/tmp/myfifo"
int main()
{
int i = 0;
//使用access()函数判断fifo管道文件是否存在
int ret0 = access(FIFO_PATH, F_OK);
if(ret0 == 0){
printf("file exist\n");
}
else{
int ret = mkfifo(FIFO_PATH, 0777);
if(ret == -1){
perror("mkfifo failed");
return -1;
}
}
//2,打开管道文件 (读写权限打开)
int fd = open(FIFO_PATH, O_RDWR);
if(fd < 0 )
{
perror("open fifo failed");
return -1;
}
//循环获取管道信息,并打印到屏幕上
char buf[128];
while(1) {
bzero(buf, sizeof(buf));
read(fd, buf, sizeof(buf)); //阻塞
printf("pid info:%s\n", buf);
}
return 0;
}
总结:
1、父进程产生进程扇,父进程不能终止,否则就是产生进程链,而不是进程扇。2、如果上一个子进程没有终止,那么子进程又会分裂出父子进程,导致像细胞分裂一样无穷无尽。