1. 问题描述
- /usr/include下面存放的是所有系统调用和库函数的头文件。试图编写一个程序,能够根据用户给出的关键字,在所有/usr/include下的头文件中匹配该关键字。如果找到,则返回包含这个关键字的文件名。
要求:主进程负责生成n个子进程,子进程分别去所有文件的一个子集(1/n个文件)中进行模式匹配,并将找到的结果传给主进程——找到一个发送一个。一旦主进程收到任何子进程返回的结果,立即在终端上打印出来。
(Linux OS Course Experiment of NEU)
以下内容仅供参考
2. 问题分析
- 首先是打开“/usr/include”目录,然后循环读取目录将文件名存入字符串数组中并计数(保存为count),然后创建消息队列。
- 之后,prefork 10(n)个子进程,判断fork()返回值,为0(子进程)则退出循环,否则父进程继续fork直至fork完成。
- 在prefork()代码之后,判断fork()返回值,若为0开始匹配工作(每个子进程负责count/10(n)个文件名匹配)且一旦匹配成功就发送消息给父进程;否则父进程循环接收消息(阻塞)。
3. 流程图
4. 程序代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<signal.h>
#include <sys/msg.h>
#include <dirent.h>
#include <sys/stat.h>
#include <regex.h>
#define MSGIDENTIFIER 900
struct matchedfile{
int flag;
char filename[1024];
} mf;
char *path = "/usr/include";
int main(int ac, char *av[]){
int rv_fork;
int msqid;
int i;
char pattern[256];
DIR *dp;
struct dirent *p;
char *filenames[512];
int index=0;
regex_t reg;
regmatch_t match;
signal(SIGCHLD,SIG_IGN);
printf("%s\n","Enter reg exp:");
read(0,pattern,sizeof(reg));
pattern[strlen(pattern)-1]='\0';
// printf("pattern:%s.",pattern);
// exit(1);
chdir(path);
dp=opendir(path);
if(dp==NULL){
perror("Cannot open directory");
exit(1);
}
p=readdir(dp);
while(p!=NULL){
if(p->d_name[0]!='.'){
struct stat sb;
stat(p->d_name, &sb);
if(!S_ISDIR(sb.st_mode)){
char *filename=p->d_name;
filenames[index]=filename;
index=index+1;
}
}
p=readdir(dp);
}
int num = index/10;
printf("%s\n","---------------");
msgget(MSGIDENTIFIER, 0777 | IPC_CREAT);
for(i=0;i<10;i++) {
printf("fork child %d\n",(i+1));
rv_fork = fork();
if(0==rv_fork){
break;
}
}
if (0==rv_fork) {
//printf("child process %d with pid %d\n",(i+1),getpid());
int j;
for(j=i*num;j<i*num+num;j++){
//printf("child process %d handle file %d\n",i,j);
char *filename=filenames[j];
regcomp(®,pattern,REG_EXTENDED);
int result = regexec(®,filename,1,&match,0);
if(result==REG_NOMATCH){
//printf("child process %d:%s do not match file with name %s(file %d)", (i+1), pattern, filename, j);
continue;
}else{
//printf("child process %d:%s match file with name %s(file %d)", (i+1), pattern, filename, j);
msqid = msgget(MSGIDENTIFIER, 0777);
strcpy(mf.filename,filename);
mf.flag=1;
msgsnd(msqid, &mf, sizeof(struct matchedfile), 0);
}
}
regfree(®);
exit(0);
} else {
printf("my(parent) pid id %d\n",getpid());
msqid = msgget(MSGIDENTIFIER, 0777);
do {
msgrcv(msqid, &mf, sizeof(struct matchedfile), 0, 0);
printf("Matched file's name:%s\n", mf.filename);
} while (mf.flag != 0);
msgctl(msqid, IPC_RMID, 0);
}
}