软件开发第二天(记录)

   昨天我写了java查询linux的进程代码。在网上找了许多代码,都是在java代码中调用linux命令来查询进程。但是查询的信息都不是很完整。然后找到了一条linux命令似乎可用:$ ps -e -o pid,uname,pcpu,pmem,comm。    但是在java文件中调用输出不了。然后写了java applet界面。因为该方面程序不熟悉,所以写的特别慢。

package wl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class process {
    /*
     * 
     * 统计进程数量,适应与windows和linux
     * 
     * @Author syp
     * 
     * @param进程名称
     * 
     * 返回值为进程个数
     * 
     */

    public static int GetprocessNums() {
        Runtime runtime = Runtime.getRuntime();
        List<String> tasklist = new ArrayList<String>();
        java.lang.Process process = null;
        int count = 0; // 统计进程数
        try {
            /*
             * 
             * 自适应执行查询进程列表命令
             * 
             */
            Properties prop = System.getProperties();
            // 获取操作系统名称
            boolean is=false;
            String os = prop.getProperty("os.name");
            if (os != null && os.toLowerCase().indexOf("linux") > -1) {
                // 1.适应与linux

                BufferedReader reader = null;
                // 显示所有进程
                process = Runtime.getRuntime().exec("top -b -n 1");
                reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                String line = null;

                while ((line = reader.readLine()) != null) {
                    if(line.startsWith("   PID")) {
                        is=true;
                    }
                    if(is==true) {
                        
                        Pattern p = Pattern.compile("\s+");
                        Matcher m = p.matcher(line);
                        line= m.replaceAll(",");
                        
                    System.out.println(line);
                    //String[] strs=line.split(",");
                   //for(int i=0;i<strs.length;i++) {
                      //System.out.print(strs[i]+";");
                    //}
                     //System.out.println();
                    //count++;
                    //processModel pro=new processModel();
                    //pro.setPID(strs[1]);
                    //pro.setUser(strs[2]);
                    
                    }
                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return count;

    }

    
    public static void main(String[] args) throws Exception {
        System.out.println(GetprocessNums());;
    }
}
process
package wl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.MenuBar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

public class ProcessApp extends JFrame {
    static JFrame f = new JFrame("Process");
    static JPanel panel = new JPanel();
     static Object[][] cellData = {{"名称","PID","状态","用户名","cpu","内存","磁盘","网络","描述"}};
        static String[] columnNames = {"col1","col2","col3","col4","col5","col6","col7","col8","col9"};
     static JTable table = new JTable(cellData, columnNames);
    ProcessApp() {// init frame
        f.setVisible(true);
        f.setSize(1000, 800);
        f.setLayout(new BorderLayout());

    }

    void initMenu() {// init menu
        JMenuBar menubar = new JMenuBar();
        JMenu[] menus = new JMenu[] { new JMenu("文件"), new JMenu("选项"), new JMenu("查看") };
        for (int i = 0; i < menus.length; i++) {
            menubar.add(menus[i]);
        }
        f.setJMenuBar(menubar);
    }

    void initToolBar() {
        JToolBar toolbar = new JToolBar();
        JButton[] buttons = new JButton[] { new JButton("应用"), new JButton("进程"), new JButton("性能"), new JButton("联网"),
                new JButton("用户"), new JButton("性能") };
        for (int i = 0; i < buttons.length; i++) {
            toolbar.add(buttons[i]);
        }
        toolbar.setBackground(Color.white);
        f.getContentPane().add(toolbar, BorderLayout.NORTH);
    }

    void initPanel() {

        panel.setBackground(Color.WHITE);
        f.getContentPane().add(panel, BorderLayout.CENTER);
    }

    void initTable() {
          //(1)名称,cpu,内存,磁盘,网络,GPU的使用情况,端口,用户名,状态,描述
         
            table.setSize((int) (panel.getWidth()*0.2),40);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS );
          panel.add(table);
    }
    
    void setTable() {
        /**
         *  for(Member member:members){
        String[] arr=new String[3];
        arr[0]=member.getName();
        arr[1]=member.getAge();
        arr[2]=member.getTitle();
               
        // 添加数据到表格
        tableModel.addRow(arr);
    }
    // 更新表格
    table.invalidate();
}
         */
    }

    public static void main(String[] args) {
        ProcessApp p = new ProcessApp();
        p.initMenu();
        p.initToolBar();
        p.initPanel();
        p.initTable();
        System.out.println("finish");
    }
}
processApplet

 

   今天开始还在写java applet界面,画面中的字体特别小,想要把整个画面上的字体都调整大,但是试了很多方法没实现。然后再界面中画了表格,表格的尺寸有些小,放大缩小时不能随比例放大缩小。然后想先这样。将信息输出到表格中再调整表格。下午时,想用c语言来查询linux的进程。然后下载安装了code block。(安装教程:https://blog.csdn.net/qq_40118071/article/details/79107487)。然后写了helloworld运行时报错:/bin/sh: 1: g++: not found   ,然后找到解决办法:https://blog.csdn.net/guoyunfei20/article/details/75575018。后来发现用c语言获取进程有两种方法:调用cmd命令来查询进程,java也能实现。linux的进程都在proc文件夹中,读取proc文件中的文件内容获得进程信息。后来决定用java来写,调用cmd命令来查询进程。

   傍晚时,有人问我怎么查询linux中的应用。但是linux中没有查询应用的命令。linux中也没有注册表。只有在/usr/share/application中有很多应用图标文件。图片中文件中有应用信息。但是我们做的软件《迅管家》的应用模块需要查询出来应用对内存,磁盘的利用率。我参考window中的任务管理器,发现应用好像还是进程,只是比较特殊的进程。然后找到了输出进程号以及进程名称,进程文件位置的方法https://www.cnblogs.com/azhqiang/p/8527864.html

根据进程名查询进程号:

 

 pgrep eclipse

 

根据进程号查询进程的详细信息

top -p 4949

 

     《迅管家》中需要的进程信息包括"名称","PID","优先级","状态","用户名","cpu","内存","磁盘","网络","描述"。 但是top命令不能查询出进程的磁盘利用率和网络利用率。查询磁盘利用率,linux中可以安装专门的软件来查询。查询磁盘利用率需要安装iotop,然后更具进程号来查询磁盘利用率:

iotop -p 进程号

查询网络利用率需要安装iftop。

 

 

 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/wl2017/p/10808576.html