In: Computer Science
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); } } }
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);
}
}
}
}