统计所有C语言源程序中,下列系统函数的被调用次数。
printf open close read write fork signal
统计结果输出到myresult.txt文件按中,格式如下:
printf 49
open 13
close 13
read 24
write 16
fork 8
signal 0
参考博客:https://blog.csdn.net/weixin_45552549/article/details/103747721,您可以看看的
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
void op(int fd,char s[]){
fd=open(s,O_RDWR,0644);
char buf[1024];
int fd0;
fd0=open("data.txt",O_CREAT | O_RDWR,0644);
int flag;
while(flag = read(fd,buf,strlen(buf)))
write(fd0,buf,flag); //将s[]读到的内容写入data.txt
close(fd);
close(fd0);
} //创建data.txt用已存储.c文件内代码
void rop(int fd,int j,char s[]){
fd=open(s,O_RDWR,0644);
char buf[1024];
int fd0;
fd0=open("data.txt",O_CREAT | O_RDWR,0644);
int flag;
while(flag = read(fd,buf,strlen(buf)))
write(fd0,buf,flag);
close(fd);
close(fd0);
} //与上个函数类似,主要不同是加了j参数,用来实现lseek()指示写的位置
char *a[9]={"main1.c","a.c","b.c","d.c","func1.c","func2.c","mycopy.c","read.c","write.c"};
//需要统计的.c文件名数组,用来传递name
int main(){
int fd;
pid_t pid;
int i;
int j=0;
for(i=1;i<9;++i){
sleep(1);
j += 1024; //每次写入位置需要变化
if((pid=fork())==0){
break;
}
}
if(pid==0){
printf("son%d\n",i); //指示生成几个子进程
if(i==1)
op(fd,a[i]);
else
rop(fd,j,a[i]);
}else{
op(fd,a[0]);
}
fd=open("myresult.txt",O_CREAT | O_RDWR,0644); //生成myresult.txt用来存储最终结果
close(fd);
return 0;
}