FreeNOS
ProcessList.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <Types.h>
19#include <Macros.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <ProcessClient.h>
23#include "ProcessList.h"
24
25ProcessList::ProcessList(int argc, char **argv)
26 : POSIXApplication(argc, argv)
27{
28 parser().setDescription("Output system process list");
29}
30
32{
33 const ProcessClient process;
34 String out;
35
36 // Print header
37 out << "ID PARENT USER GROUP STATUS CMD\r\n";
38
39 // Loop processes
40 for (ProcessID pid = 0; pid < ProcessClient::MaximumProcesses; pid++)
41 {
43
44 const ProcessClient::Result result = process.processInfo(pid, info);
45 if (result == ProcessClient::Success)
46 {
47 DEBUG("PID " << pid << " state = " << *info.textState);
48
49 // Output a line
50 char line[128];
51 snprintf(line, sizeof(line),
52 "%3d %7d %4d %5d %10s %32s\r\n",
53 pid, info.kernelState.parent,
54 0, 0, *info.textState, *info.command);
55 out << line;
56 }
57 }
58
59 // Output the table
60 write(1, *out, out.length());
61 return Success;
62}
Result
Result codes.
Definition Application.h:54
ArgumentParser & parser()
Get program arguments parser.
void setDescription(const String &desc)
Set program description.
POSIX-compatible application.
ProcessClient provides information about all processes on the local core.
static const Size MaximumProcesses
Maximum number of processes.
Result
Result codes.
Result processInfo(const ProcessID pid, Info &info) const
Get process information by its ID.
virtual Result exec()
Execute the application.
ProcessList(int argc, char **argv)
Constructor.
Abstraction of strings.
Definition String.h:42
Size length() const
Same as count().
Definition String.cpp:105
C ssize_t write(int fildes, const void *buf, size_t nbyte)
Write on a file.
Definition write.cpp:22
C int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition snprintf.cpp:22
u32 ProcessID
Process Identification Number.
Definition Types.h:140
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
Process information.
String command
Full command including program path.
ProcessInfo kernelState
Process state retrieved from the kernel.
String textState
Textual state of the process.
ProcessID parent
Parent process id.
Definition ProcessCtl.h:69