Question

In: Computer Science

Reduce the following "top -b -n 1" output to its first, second, and last columns, and...

Reduce the following "top -b -n 1" output to its first, second, and last columns, and include only those processes belonging to "root". 
Use fscanf and strtok please.


top - 05:00:58 up 543 days, 8:56, 1 user, load average: 0.11, 0.03, 0.01
Tasks: 112 total, 1 running, 111 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 499976 total, 41456 free, 51636 used, 406884 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 392008 avail Mem
1 root 20 0 185324 4988 3032 S 0.0 1.0 7:08.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 3:19.04 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root 20 0 0 0 0 S 0.0 0.0 7:29.74 rcu_sched
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
10 root rt 0 0 0 0 S 0.0 0.0 4:36.48 watchdog/0
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
12 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf
14 root 20 0 0 0 0 S 0.0 0.0 0:13.48 khungtaskd
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
16 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 devfreq_wq
27 root 20 0 0 0 0 S 0.0 0.0 22:01.55 kswapd0
28 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 vmstat
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_ma+
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ecryptfs-kt+
46 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
47 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 acpi_therma+
48 root 20 0 0 0 0 S 0.0 0.0 0:00.00 vballoon
49 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
50 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
51 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
52 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
53 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
54 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
55 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
56 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
57 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
58 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_0
59 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_0
60 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_1
61 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_1
67 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ipv6_addrco+
80 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq
81 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 charger_man+
128 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
129 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
130 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
131 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
132 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
133 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
134 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
135 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
137 root 20 0 0 0 0 S 0.0 0.0 0:00.00 scsi_eh_2
138 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 scsi_tmf_2
145 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
496 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 raid5wq
526 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
551 root 20 0 0 0 0 S 0.0 0.0 4:41.75 jbd2/vda1-8
552 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ext4-rsv-co+
614 root 20 0 27708 2248 1924 S 0.0 0.4 48:36.07 systemd-jou+
621 root 0 -20 0 0 0 S 0.0 0.0 0:36.37 kworker/0:1H
632 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 iscsi_eh
648 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ib_addr
651 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ib_mcast
652 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ib_nl_sa_wq
653 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kauditd
654 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ib_cm
657 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 iw_cm_wq
660 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 rdma_cm
689 root 20 0 102968 228 0 S 0.0 0.0 0:00.00 lvmetad
796 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kvm-irqfd-c+
1427 root 20 0 5220 116 0 S 0.0 0.0 25:47.02 iscsid
1428 root 10 -10 5720 3524 2436 S 0.0 0.7 123:01.13 iscsid
1449 message+ 20 0 42972 2008 1392 S 0.0 0.4 0:10.67 dbus-daemon
1455 syslog 20 0 256392 1564 0 S 0.0 0.3 13:44.06 rsyslogd
1459 root 20 0 28728 2752 2248 S 0.0 0.6 0:37.68 systemd-log+
1462 root 20 0 653228 3996 1192 S 0.0 0.8 4:47.32 lxcfs
1469 root 20 0 4396 1156 1072 S 0.0 0.2 0:00.00 acpid
1471 root 20 0 274488 1016 212 S 0.0 0.2 27:45.06 accounts-da+
1483 root 20 0 27728 2176 1896 S 0.0 0.4 1:34.16 cron
1490 daemon 20 0 26044 1724 1520 S 0.0 0.3 0:00.84 atd
1520 root 20 0 13372 192 52 S 0.0 0.0 0:04.41 mdadm
1594 root 20 0 14472 1588 1452 S 0.0 0.3 0:00.00 agetty
5845 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0
5906 root 20 0 92832 6816 5884 S 0.0 1.4 0:00.04 sshd
5942 root 20 0 21428 5372 3368 S 0.0 1.1 0:00.06 bash
5958 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/u2:1
6134 root 20 0 65512 5844 5140 S 0.0 1.2 0:00.00 sshd
6135 sshd 20 0 65512 3200 2484 S 0.0 0.6 0:00.00 sshd
6141 root 20 0 65512 5784 5072 S 0.0 1.2 0:00.00 sshd
6142 sshd 20 0 65512 3208 2484 S 0.0 0.6 0:00.00 sshd
6147 root 20 0 40388 3492 2988 R 0.0 0.7 0:00.00 top
6433 root 20 0 277088 764 0 S 0.0 0.2 0:00.56 polkitd
8836 systemd+ 20 0 100324 1552 1312 S 0.0 0.3 0:10.54 systemd-tim+
9724 root 20 0 42364 2344 1808 S 0.0 0.5 0:08.68 systemd-ude+
14463 postgres 20 0 293408 14644 12936 S 0.0 2.9 0:23.56 postgres
14465 postgres 20 0 293408 1704 0 S 0.0 0.3 0:00.67 postgres
14466 postgres 20 0 293408 3408 1704 S 0.0 0.7 0:22.84 postgres
14467 postgres 20 0 293408 2076 372 S 0.0 0.4 0:22.46 postgres
14468 postgres 20 0 293792 3324 1328 S 0.0 0.7 0:13.53 postgres
14469 postgres 20 0 148392 1876 116 S 0.0 0.4 0:13.18 postgres
17737 www-data 20 0 819836 4652 1880 S 0.0 0.9 0:36.39 apache2
17738 www-data 20 0 819844 4948 2008 S 0.0 1.0 0:36.33 apache2
18046 root 20 0 36840 2220 1460 S 0.0 0.4 0:00.00 systemd
18051 root 20 0 209056 2344 0 S 0.0 0.5 0:00.00 (sd-pam)
20779 root 20 0 0 0 0 S 0.0 0.0 0:01.12 kworker/u2:0
25939 root 20 0 71584 4064 2876 S 0.0 0.8 0:17.42 apache2
27861 root 20 0 0 0 0 S 0.0 0.0 0:01.00 kworker/0:1
32109 root 20 0 14656 1328 1192 S 0.0 0.3 0:00.02 agetty
32497 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfsalloc
32498 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 xfs_mru_cac+
32699 root 20 0 65512 2516 1804 S 0.0 0.5 5:54.88 sshd

A start of this problem:

#include 
#include 
#include 
int main()
{
  FILE *myfile = fopen("out.top.txt","r");
  char a[500] = "this is about as big as this line will be";
// top - 11:45:12 up 7 min,  2 users,  load average: 0.34, 1.01, 0.66
// 8729 root      20   0       0      0      0 S   0.0  0.0   0:00.03 kworker/u8+
  while (fscanf(myfile,"%[^\n]\n",a)==1) {
    // printf("line in is %s\n",a);
    char *b1 = strtok(a," "); // printf("first word is %s\n",b1);
    if (atoi(b1) > 0) {
      // printf("first word is a number\n");
      char *b2 = strtok(NULL,", "); // printf("second word is %s\n",b2);
      if (!strcmp(b2,"root")) printf("%s owner is %s\n",b1,b2);
    }
  }
}

Solutions

Expert Solution

Here is the required solution. Please mark this answer with thumbs up. :)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main() {
FILE * myfile = fopen("out.top.txt", "r");
char a[500];
while (fscanf(myfile, "%[^\n]\n", a) == 1) {
    char * b1 = strtok(a, " ");
    if (atoi(b1) > 0) {
      int pn = atoi(b1);
      b1 = strtok(NULL, " ");
      if (!strcmp(b1, "root")) {
        printf("%d root ", pn);
        char * s2 = b1;
        while (b1 != NULL) {
          s2 = b1;
          b1 = strtok(NULL, " ");
        }
        printf("%s\n", s2);
      }
    }
}
}


Related Solutions

For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
Turing machine A that does the following: • On its first and second tape, A receives...
Turing machine A that does the following: • On its first and second tape, A receives two strings w and v, w, v ∈ {0, 1}? , representing two integer numbers. When machine A is started, the tape heads are located on the left-most position, on the most significant bits of w and v. • If none of the inputs w and v is the empty word, the Turing machine A writes the binary representation of the sum of the...
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John...
Create a table with two columns. Name the table First Initial _ Last Name (e.g. John Dow will create table j_dow). You have to audit all DML statements on your table. To do this you write two triggers: 1. To log any DML statements that users might run on this table. The results must be stored in the First Initial _ Last Name _ Log table (e.g. John Dow will create table j_dow_log). The table should have unique event ID,...
1.) First answer = first blank; second answer = second blank. The test statistic for an...
1.) First answer = first blank; second answer = second blank. The test statistic for an ANOVA is a ratio of values and is noted by (F). In independent- measures ANOVAs, ________ comprises the numerator of this ratio, whereas ________ comprises the denominator of this ratio. A: systematic treatment effects and unsystematic effects; unsystematic effects B: systematic treatment effects; unsystematic effects C: unsystematic effects; systematic effects and unsystematic effects D: unsystematic effects; systematic effects 3.) Please select all that apply....
Use the given function, its first derivative, and its second derivative to answer the following: f(x)=(1/3)x^3...
Use the given function, its first derivative, and its second derivative to answer the following: f(x)=(1/3)x^3 - (1/2)x^2 - 6x + 5 f'(x)= x^2 - x - 6 = (x+2)(x-3) f''(x)= 2x - 1 a) What are the intervals of increase and the intervals of decrease b) Identify local min and max points c) What are the intervals where the function is concave up, concave down and identify the inflection points
In macroeconomics there are two key questions: first, how to measure (real) output growth? and second,...
In macroeconomics there are two key questions: first, how to measure (real) output growth? and second, how is growth connected to wellbeing? As for the first question, there are three ways to measure real GDP growth. It starts with the definition of a percentage difference between two numbers, say x1 compared to x0. When there is only one good (e.g. apples) there are no prices to worry about for the calculation. But for the economy as a whole, we need...
In macroeconomics there are two key questions: first, how to measure (real) output growth? and second,...
In macroeconomics there are two key questions: first, how to measure (real) output growth? and second, how is growth connected to wellbeing? As for the first question, there are three ways to measure real GDP growth. It starts with the definition of a percentage difference between two numbers, say x1 compared to x0. When there is only one good (e.g. apples) there are no prices to worry about for the calculation. But for the economy as a whole, we need...
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
What is the first, second, fifth, and last year’s depreciation for a $4,953,000 office building using...
What is the first, second, fifth, and last year’s depreciation for a $4,953,000 office building using the straight-line method and mid-month convention? The office building is placed in service in October. The company’s tax year runs from January to December. The cost of the land is not included in the $4,953,000. These depreciation calculations are to be used for tax purposes
Consider the time series data in columns A and B. Month (A) Value (B) 1 6...
Consider the time series data in columns A and B. Month (A) Value (B) 1 6 2 11 3 9 4 14 5 15 a. Construct a time series plot. b. Use simple linear regression analysis to find the parameters for the line c. For each new month, how much change is there in the value (on average)? d. Create a "Forecast" column and use the regression equation to provide a forecast for each month. Include a forecats for month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT