Linux大作业

Linux编程》大作业:

有一个文件夹,下面有许多个C语言源程序。要求编写一个程序,统计所有C语言源程序中,下列系统函数的被调用次数。

printf  open  close  read  write  fork  signal

统计结果输出到myresult.txt文件按中,格式如下:

        printf  49

        open    13

        close   13

        read    24

        write   16

        fork     8

        signal   0

 

建议(非强制,遵守下列建议会得到一个较好分数):

1. 对每个.c文件,生成一个子进程(或者启动一个线程)统计,统计结果存入合适的文本文件中,最后再汇总结果

2. 对每个函数,生成一个子进程(或者启动一个线程)统计,统计结果存入合适的文本文件中,最后再汇总结果。

3. 访问文件使用Linux系统IO,如open, close, read, write。

4. 编写出合适的makefile

5. 可以生成多个可执行文件,通过一个主文件启动工作。

6. 可以带Shell脚本

 

提交要求:

l  设计报告,和实验报告格式相似,描述具体的实现过程,可附带部分程序,以说明实现细节。

l  源代码 和 设计报告 电子版。 打包在一起,给我发离线文件。

l  打包文件名的格式:Linux99-<学号>-姓名.zip, 例如:张三同学的学号是123456789012,他应该上交的文件名是:Linux99-123456789012-张三.zip。打包格式不限于zip,rar和7z等都可以。

l  文件名和打包格式的规范性会影响最终分数。

l  雷同的设计报告会得到较低的分数

 

*************************************************************************
    > File Name: linuxbigwork.c
    > Author: r3t7rn
    > Mail:  r3t7rn_0@foxmail.com
    > Created Time: 2019年12月29日 星期六 15时22分
 ************************************************************************/
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<dirent.h> #include<fcntl.h> #include<pthread.h> #include<unistd.h> char *function_name[7]={"printf","open","close","read","write","fork","signal"}; int Cnt[7]={0}; int KMP(char *s,char *m,int idx){ //printf("%s\n",s); int next[7]; int i,j,l; i=0; j=-1; next[0]=-1; l=strlen(m); while(i<l){ if(j == -1 || m[i]==m[j]){ i++;j++;next[i]=j; } else j=next[j]; } int lm,ls; i=j=0; lm=strlen(m); ls=strlen(s); while(i<ls){ if(j>=lm){ Cnt[idx]++; j=0; } if(j==-1||s[i]==m[j]){ i++;j++; } else j=next[j]; } return -1; } int Count(char *base,char *d_name){ char file[256]; memset(file,'\0',sizeof(file)); strcpy(file,base); strcat(file,"/"); strcat(file,d_name); printf("this_file_\"%s\"_is_a_c_source_file\n"); int fd; fd = open(file,O_RDONLY); if(fd==-1){ perror("file open error"); exit(-1); } int f_size=0; f_size=lseek(fd,0,SEEK_END); lseek(fd,0,SEEK_SET); char buf[f_size+1]; memset(buf,'\0',sizeof(buf)); read(fd,buf,f_size); printf("%s\n",buf); for(int i=0;i<7;i++) KMP(buf,function_name[i],i); for(int i=0;i<7;i++) printf("%s : %d\n",function_name[i],Cnt[i]); close(fd); return 0; } int readFileList(char *basePath) { DIR *dir; struct dirent *ptr; char base[1000]; if ((dir=opendir(basePath)) == NULL) { perror("Open dir error..."); exit(1); } while ((ptr=readdir(dir)) != NULL) { if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir continue; else if(ptr->d_type == 8){ ///file printf("current_file:%s/%s\n",basePath,ptr->d_name); if(ptr->d_name[strlen(ptr->d_name)-1]=='c' && ptr->d_name[strlen(ptr->d_name)-2]=='.'){ Count(basePath,ptr->d_name); } } else if(ptr->d_type == 10) ///link file printf("a_link_file:%s/%s\n",basePath,ptr->d_name); else if(ptr->d_type == 4) ///dir { memset(base,'\0',sizeof(base)); strcpy(base,basePath); strcat(base,"/"); strcat(base,ptr->d_name); readFileList(base); } } closedir(dir); return 1; } int main(){ char basePath[100]; printf("Please input a path:"); scanf("%s",basePath); //memset(basePath,'\0',sizeof(basePath)); //getcwd(basePath,999); printf("the current dir is : %s\n",basePath); readFileList(basePath); for(int i=0;i<7;i++) printf("%s : %d\n",function_name[i],Cnt[i]); return 0; }

 

posted @ 2019-12-29 03:11  r3t7rn  阅读(1262)  评论(0编辑  收藏  举报