Havert2 2022-12-14 16:33 采纳率: 100%
浏览 10
已结题

关于#linux#的问题,如何解决?

统计所有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条回答 默认 最新

  • dp_shiyu 2022-12-14 17:05
    关注

    参考博客: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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月23日
  • 已采纳回答 12月15日
  • 创建了问题 12月14日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。