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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM