zoukankan      html  css  js  c++  java
  • linux程序时间控制小例

    程序目的:若在5.5秒前按下回车则输出主目录下的所有文件,若没有任何动作,则输出CPU运行状况,并且能够kill掉最占CPU的进程


    程序分析:参考书本《linux软件工程师实用教程》P196页程序可以实现程序时间上的控制,输出目录则可以调用scandir函数,shell命令top用来监察CPU使用状况,按下<k>+pid则可以kill掉进程


    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <stdlib.h>
    #include <time.h>
    #define STDIN 0

    int main()
    {
      struct timeval tv;
      fd_set readfds;
      DIR * dir;
      struct dirent **namelist;
      int i;
      time_t t;
      tv.tv_sec=5;
      tv.tv_usec=500000;
      t=time(0);
      printf("%s",asctime(localtime(&t)));
      FD_CLR(STDIN,&readfds);
      FD_ZERO(&readfds);
      FD_SET(STDIN, &readfds);
      select(STDIN+1,&readfds,NULL,NULL,&tv);
      if (FD_ISSET(STDIN,&readfds))
       {
         t=time(0);
         printf("%s ",asctime(localtime(&t)));
         i=scandir("/home/king",&namelist,0,alphasort);
         while(i--)
         {
            printf("%s   ",namelist[i]->d_name);
         }
         free(namelist);
       }
      else
       {
         t=time(0);
         printf("%s ",asctime(localtime(&t)));
         system("top");
       }
    }

    运行结果:

    king@ubuntu:~$ ./4
    Sat Sep 11 17:15:45 2010

    Sat Sep 11 17:15:48 2010
     通讯录1.1.xlsx   求否工作流程.pdf   user.log   toolset   tickets   terminallock   temp   project.txt   main   mail.log   ipv6   hello   gedit.log   fifo2   fifo1   examples.desktop   example.log   example.c   example   edit   checktime.c   checktime   Videos   Ubuntu One   Templates   Public   Pictures   Music   GNUstep   Downloads   Documents   Desktop   6-4.c   6-4   402.c   402   401.c   401   4.c   4-9file   4-9.c   4-9   4-8file   4-8.c   4-8   4-7test   4-7.c   4-7   4-6file   4-6.c   4-6   4-5.c   4-5   4-4.c   4-4   4-3.c   4-3   4-2.c   4-2   4-10.c   4-1.c   4-1   4-01.c   4-01   4   3.c   3-9.c   3-9   3-8.c   3-8-1.c   3-8-1   3-8   3-7.c   3-7   3-6.c   3-6   3-4.c   3-4   3-3.c   3-3-1.c   3-3-1   3-3   3-13.c   3-12.c   3-12   3-11.c   3-11   3-10.c   3-10   3   2.c   2-0.c   2   1.c   1   .xsession-errors.old   .xsession-errors   .xinput.d   .xine   .wine   .viminfo   .update-notifier   .thumbnails   .themes   .tencent   .sudoku   .sudo_as_admin_successful   .stardict   .recently-used.xbel   .pulse-cookie   .pulse   .profile   .openoffice.org   .nautilus   .mozilla   .mission-control   .macromedia   .local   .kk.c.swp   .kde   .icons   .gvfs   .gtk-bookmarks   .gstreamer-0.10   .google   .gnome2_private   .gnome2   .gksu.lock   .gimp-2.6   .gegl-0.0   .gconfd   .gconf   .fonts.conf   .fontconfig   .face   .evolution   .esd_auth   .emacs.d   .emacs   .easytag   .dmrc   .dbus   .config   .compiz   .cache   .bashrc   .bash_logout   .bash_history   .adobe   .ICEauthority   .Fontmatrix   .3.c.swp   ..   .   king@ubuntu:~$

    king@ubuntu:~$ ./4
    Sat Sep 11 17:17:13 2010
    Sat Sep 11 17:17:19 2010

    top - 17:17:19 up  7:51,  3 users,  load average: 0.44, 0.38, 0.39
    Tasks: 212 total,   2 running, 199 sleeping,  11 stopped,   0 zombie
    Cpu(s):  9.5%us,  2.3%sy,  0.1%ni, 87.1%id,  0.7%wa,  0.1%hi,  0.2%si,  0.0%st
    Mem:   2058932k total,  1994332k used,    64600k free,   466260k buffers
    Swap:   261112k total,     4284k used,   256828k free,   486084k cached

      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
     7198 king      20   0  2544 1140  812 R    4  0.1   0:00.02 top               
     1016 root      20   0  184m 126m  24m S    2  6.3  19:50.37 Xorg              
     1787 king      20   0  407m  97m  20m S    2  4.9  53:29.51 firefox-bin       
     1897 king      20   0 31820  16m 2664 S    2  0.8   0:42.93 ubuntuone-syncd   
        1 root      20   0  2800 1380 1012 S    0  0.1   0:00.43 init              
        2 root      20   0     0    0    0 S    0  0.0   0:00.00 kthreadd          
        3 root      RT   0     0    0    0 S    0  0.0   0:00.00 migration/0       
        4 root      20   0     0    0    0 S    0  0.0   0:00.53 ksoftirqd/0       
        5 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0        
        6 root      RT   0     0    0    0 S    0  0.0   0:00.00 migration/1       
        7 root      20   0     0    0    0 S    0  0.0   0:00.87 ksoftirqd/1       
        8 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1        
        9 root      20   0     0    0    0 S    0  0.0   0:01.10 events/0          
       10 root      20   0     0    0    0 S    0  0.0   0:00.37 events/1          
       11 root      20   0     0    0    0 S    0  0.0   0:00.00 cpuset            
       12 root      20   0     0    0    0 S    0  0.0   0:00.00 khelper           
       13 root      20   0     0    0    0 S    0  0.0   0:00.00 netns   

  • 相关阅读:
    前端笔记之移动端&响应式(上)媒体查询&Bootstrap&动画库&zepto&velocity
    SVN的使用
    Git的使用
    前端笔记之HTML5&CSS3(下)2D/3D转换&animate动画
    前端笔记之HTML5&CSS3(中)选择器&伪类伪元素&CSS3效果&渐变背景&过渡
    idea|properties文件乱码
    Web 开发工具类(5) | DateUtils
    Idea | Load error: undefined path variables
    聊聊SpringBoot | 第一章:快速搭建SpringBoot第一个应用
    Springboot | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
  • 原文地址:https://www.cnblogs.com/chuxiking/p/1824044.html
Copyright © 2011-2022 走看看