Results 1 to 16 of 16
  1. #1
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Angry Work with processes

    Hi, I have to create a method that is receiving as parameters an ArrayList of the active processes of the system(I receive that list from another module).This method should print what are the processes that are working on disk (reading&writing) and the no of bytes read and written.How can you do that in Java?I know it's possible, just dunno how(searched google for a few days now )

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Work with processes

    Quote Originally Posted by 99LittleBugsInTheCode View Post
    I know it's possible
    How do you know that?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Work with processes

    Quote Originally Posted by 99LittleBugsInTheCode View Post
    I receive that list from another module
    My expectation would then be that this "module" you mention can also provide you with the information you seek - and if it cannot, it needs to be modified so it can provide you the information you seek.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Quote Originally Posted by JosAH View Post
    How do you know that?

    kind regards,

    Jos
    Because it's one my projects this semester.We are developing a software with different utilities(permanent deletion of a file, check if a process is malicious, recover deleted files, digital signatures and stuffs), but this just seems impossible.Guess our teacher would've known if that is impossible :)
    Last edited by 99LittleBugsInTheCode; 05-11-2015 at 05:43 PM.

  5. #5
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Quote Originally Posted by gimbal2 View Post
    My expectation would then be that this "module" you mention can also provide you with the information you seek - and if it cannot, it needs to be modified so it can provide you the information you seek.
    Well...actually they created the class Process and they have to implement the method required for the processes that are working with CPU and memory, but the disk processes is our job, because we also have access to that class's code

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Work with processes

    So its a school project. Then you must have the code of this 'module' you mention. Post it, lets see what is in there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    package model;

    import java.util.List;

    public class Process {

    private String name;
    private int PID;
    private String sessionName;
    private int sessionNo;
    private String status;
    private double memoryUsage;
    private double readBites;
    private double writtenBites;
    private List<String> readsFromThreads;
    private List<String> writesToThreads;

    public Process() {

    }

    public Process(String name, int pID, String sessionName, int sessionNo, String status, double memoryUsage) {
    super();
    this.name = name;
    this.PID = pID;
    this.sessionName = sessionName;
    this.sessionNo = sessionNo;
    this.status = status;
    this.memoryUsage = memoryUsage;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getPID() {
    return PID;
    }

    public void setPID(int pID) {
    PID = pID;
    }

    public String getSessionName() {
    return sessionName;
    }

    public void setSessionName(String sessionName) {
    this.sessionName = sessionName;
    }

    public int getSessionNo() {
    return sessionNo;
    }

    public void setSessionNo(int sessionNo) {
    this.sessionNo = sessionNo;
    }

    public String getStatus() {
    return status;
    }

    public void setStatus(String status) {
    this.status = status;
    }

    public double getMemoryUsage() {
    return memoryUsage;
    }

    public void setMemoryUsage(double memoryUsage) {
    this.memoryUsage = memoryUsage;
    }

    public double getReadBites() {
    return readBites;
    }

    public void setReadBites(double readBites) {
    this.readBites = readBites;
    }

    public double getWrittenBites() {
    return writtenBites;
    }

    public void setWrittenBites(double writtenBites) {
    this.writtenBites = writtenBites;
    }

    public List<String> getReadsFromThreads() {
    return readsFromThreads;
    }

    public void setReadsFromThreads(List<String> readsFromThreads) {
    this.readsFromThreads = readsFromThreads;
    }

    public List<String> getWritesToThreads() {
    return writesToThreads;
    }

    public void setWritesToThreads(List<String> writesToThreads) {
    this.writesToThreads = writesToThreads;
    }

    @Override
    public String toString() {
    return "Process [name=" + name + ", PID=" + PID + ", sessionName=" + sessionName + ", sessionNo=" + sessionNo + ", memoryUsage="
    + memoryUsage + "]";
    }

    }

  8. #8
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Considering the existing methods, guess we should check only if Process.getReadBites>0 || Process.getWritenBites>0 in which case we should print that process and the no of bytes, but those methods arent implemented yet :(

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Work with processes

    That's just a storage bean class. I'd be mighty interested to see where such a 'pid' comes from. Where is the code which creates these Process instances?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Quote Originally Posted by gimbal2 View Post
    That's just a storage bean class. I'd be mighty interested to see where such a 'pid' comes from. Where is the code which creates these Process instances?
    Trying to find that...since it's not my team's part, we have no idea how are they supposed to do that

  11. #11
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    apparently, they are using a command exec("tasklist.exe"), but i don't have that part of code.Do you know if there is a command that is showing the activity of the processes on the disk? :/

  12. #12
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    It seems like Java is too high-leveled to print those info, so we need to use a windows command, but hell, no idea whatsoever

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Work with processes

    Quote Originally Posted by 99LittleBugsInTheCode View Post
    It seems like Java is too high-leveled to print those info, so we need to use a windows command, but hell, no idea whatsoever
    The tasklist.exe command would probably work. Perhaps there are some third party Java classes that provide that capability. The other way would be to use the JNI and write some OS specific code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Quote Originally Posted by jim829 View Post
    The tasklist.exe command would probably work. Perhaps there are some third party Java classes that provide that capability. The other way would be to use the JNI and write some OS specific code.

    Regards,
    Jim
    Tasklist isn't giving me enough info, I was thinking about wmic, but...

  15. #15
    Join Date
    May 2015
    Posts
    10
    Rep Power
    0

    Default Re: Work with processes

    Ok, I found the command, I'll just write it here in case anyone will need it in the future : "wmic process list io"

    import java.io.*;

    public class CmdTest {
    public static void main(String[] args) throws Exception {
    ProcessBuilder builder = new ProcessBuilder(
    "cmd.exe", "/c", "wmic process list io");
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
    line = r.readLine();
    if (line == null) { break; }
    System.out.println(line);
    }
    }
    }

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Work with processes

    Quote Originally Posted by 99LittleBugsInTheCode View Post
    apparently, they are using a command exec("tasklist.exe"), but i don't have that part of code.
    As I suspected. That's of course a massive hack, as soon as you try to run your program on anything but Windows, it will fail miserably. But it is a good lesson too: you do what you need to get the job done. And if you're stuck with Java to do things which are very operating system specific, then resorting to calling external programs is not a shame at all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Runtime Processes
    By egran001 in forum Advanced Java
    Replies: 1
    Last Post: 05-15-2012, 01:44 PM
  2. Monitoring processes
    By scamiolo in forum New To Java
    Replies: 3
    Last Post: 07-20-2011, 01:38 PM
  3. how to run parallel processes ?
    By mapred.learn in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 12:45 AM
  4. Too many java.exe processes due to netbeans
    By abhishek.jain in forum NetBeans
    Replies: 7
    Last Post: 12-29-2009, 01:52 PM
  5. netbeans processes don´t die
    By karlen in forum NetBeans
    Replies: 0
    Last Post: 06-11-2007, 09:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •