OSA B7

B7 :Write Java script to display all logged in users. Count the number of logged-in users. Write a program to create a foreground and background process for the selected user and display its status.



Program:

import java.io.BufferedReader;
            import java.io.InputStreamReader;
            public class assign7
            {
            public static void main(String[] args){
            int count=0;
            try{
            String line,line1;
            Process p=Runtime.getRuntime().exec("who");
                                    //getRuntime().exec("who");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while((line=input.readLine())!=null)
            {
            System.out.println(line);
            count++;
            }
            System.out.println("No.of users are:"+count);
            input.close();
            Process q=Runtime.getRuntime().exec("ps");
            BufferedReader input1 = new BufferedReader(new InputStreamReader(q.getInputStream()));
            while((line1=input1.readLine())!=null)
            {
            System.out.println(line1);
            //count++;
            }
            //System.out.println("No.of users are:"+count);
            input.close();
            }
            catch(Exception e)
            {
            e.printStackTrace();
            }
            }
            }

Output:
  [student@localhost ~]$ javac assign7.java
[student@localhost ~]$ java assign7
student  :0           2013-10-09 08:29 (:0)
student  pts/0        2013-10-09 11:57 (:0)
No.of users are:2
  PID TTY          TIME CMD
 5413 pts/0    00:00:00 bash
 5733 pts/0    00:00:00 java
 5747 pts/0    00:00:00 ps

0 comments:

Post a Comment