or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

file-system.mdhardware.mdindex.mdnetwork.mdoperating-system.mdprocess-management.mdsystem-info.md

process-management.mddocs/

0

# Process Management

1

2

Detailed process and thread information including CPU usage, memory consumption, and process relationships with filtering and sorting capabilities.

3

4

## Process Operations

5

6

The OperatingSystem interface provides comprehensive process management capabilities.

7

8

```java { .api }

9

interface OperatingSystem {

10

// Process retrieval methods

11

List<OSProcess> getProcesses();

12

List<OSProcess> getProcesses(Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);

13

List<OSProcess> getProcesses(Collection<Integer> pids);

14

OSProcess getProcess(int pid);

15

16

// Process hierarchy methods

17

List<OSProcess> getChildProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);

18

List<OSProcess> getDescendantProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);

19

20

// Current process methods

21

int getProcessId();

22

OSProcess getCurrentProcess();

23

int getProcessCount();

24

25

// Thread methods

26

int getThreadId();

27

OSThread getCurrentThread();

28

int getThreadCount();

29

}

30

```

31

32

## OSProcess Interface

33

34

Comprehensive process information including resource usage, command line details, and state information.

35

36

```java { .api }

37

interface OSProcess {

38

// Basic process information

39

String getName();

40

String getPath();

41

String getCommandLine();

42

String getCurrentWorkingDirectory();

43

44

// Process identity

45

int getProcessID();

46

int getParentProcessID();

47

String getUser();

48

String getUserID();

49

String getGroup();

50

String getGroupID();

51

52

// Process state and properties

53

State getState();

54

int getThreadCount();

55

int getPriority();

56

int getBitness();

57

long getAffinityMask();

58

59

// Memory usage

60

long getVirtualSize();

61

long getResidentSetSize();

62

int getMinorFaults();

63

int getMajorFaults();

64

65

// CPU usage

66

double getProcessCpuLoadCumulative();

67

double getProcessCpuLoadBetweenTicks(OSProcess priorSnapshot);

68

long getKernelTime();

69

long getUserTime();

70

long getContextSwitches();

71

72

// Timing information

73

long getUpTime();

74

long getStartTime();

75

76

// I/O statistics

77

long getBytesRead();

78

long getBytesWritten();

79

long getOpenFiles();

80

81

// Process details

82

String[] getArguments();

83

Map<String, String> getEnvironmentVariables();

84

List<OSThread> getThreadDetails();

85

86

enum State {

87

NEW, RUNNING, SLEEPING, WAITING, ZOMBIE, STOPPED, OTHER, INVALID

88

}

89

}

90

```

91

92

## Process Filtering

93

94

Pre-built predicates for filtering process lists.

95

96

```java { .api }

97

class ProcessFiltering {

98

static final Predicate<OSProcess> ALL_PROCESSES;

99

static final Predicate<OSProcess> VALID_PROCESS;

100

static final Predicate<OSProcess> NO_PARENT;

101

static final Predicate<OSProcess> BITNESS_64;

102

static final Predicate<OSProcess> BITNESS_32;

103

}

104

```

105

106

### Filter Descriptions

107

108

- **`ALL_PROCESSES`** - No filtering, includes all processes

109

- **`VALID_PROCESS`** - Excludes processes with INVALID state

110

- **`NO_PARENT`** - Only processes that are their own parent (excludes child processes)

111

- **`BITNESS_64`** - Only 64-bit processes

112

- **`BITNESS_32`** - Only 32-bit processes

113

114

## Process Sorting

115

116

Pre-built comparators for sorting process lists.

117

118

```java { .api }

119

class ProcessSorting {

120

static final Comparator<OSProcess> NO_SORTING;

121

static final Comparator<OSProcess> CPU_DESC;

122

static final Comparator<OSProcess> RSS_DESC;

123

static final Comparator<OSProcess> UPTIME_ASC;

124

static final Comparator<OSProcess> UPTIME_DESC;

125

static final Comparator<OSProcess> PID_ASC;

126

static final Comparator<OSProcess> PARENTPID_ASC;

127

static final Comparator<OSProcess> NAME_ASC;

128

}

129

```

130

131

### Sort Descriptions

132

133

- **`NO_SORTING`** - No sorting applied

134

- **`CPU_DESC`** - Sort by decreasing cumulative CPU percentage

135

- **`RSS_DESC`** - Sort by decreasing Resident Set Size (memory usage)

136

- **`UPTIME_ASC`** - Sort by uptime, newest processes first

137

- **`UPTIME_DESC`** - Sort by uptime, oldest processes first

138

- **`PID_ASC`** - Sort by Process ID (ascending)

139

- **`PARENTPID_ASC`** - Sort by Parent Process ID (ascending)

140

- **`NAME_ASC`** - Sort by process name (case insensitive, ascending)

141

142

## OSThread Interface

143

144

Thread information within processes.

145

146

```java { .api }

147

interface OSThread {

148

String getName();

149

int getThreadId();

150

OSProcess.State getState();

151

long getStartTime();

152

long getStartMemoryAddress();

153

long getContextSwitches();

154

long getKernelTime();

155

long getUserTime();

156

long getUpTime();

157

int getPriority();

158

}

159

```

160

161

## Usage Examples

162

163

### Get All Processes

164

165

```java

166

import oshi.SystemInfo;

167

import oshi.software.os.OperatingSystem;

168

import oshi.software.os.OSProcess;

169

170

SystemInfo si = new SystemInfo();

171

OperatingSystem os = si.getOperatingSystem();

172

173

// Get all processes (no filtering or sorting)

174

List<OSProcess> allProcesses = os.getProcesses();

175

176

System.out.println("Total processes: " + allProcesses.size());

177

for (OSProcess process : allProcesses) {

178

System.out.printf("PID: %d, Name: %s, State: %s%n",

179

process.getProcessID(),

180

process.getName(),

181

process.getState()

182

);

183

}

184

```

185

186

### Top CPU Consuming Processes

187

188

```java

189

import oshi.SystemInfo;

190

import oshi.software.os.OperatingSystem;

191

import oshi.software.os.OSProcess;

192

193

SystemInfo si = new SystemInfo();

194

OperatingSystem os = si.getOperatingSystem();

195

196

// Get top 10 processes by CPU usage

197

List<OSProcess> topCpuProcesses = os.getProcesses(

198

OperatingSystem.ProcessFiltering.VALID_PROCESS,

199

OperatingSystem.ProcessSorting.CPU_DESC,

200

10

201

);

202

203

System.out.println("Top CPU consuming processes:");

204

for (OSProcess process : topCpuProcesses) {

205

System.out.printf("%-20s PID: %5d CPU: %6.2f%% Memory: %8.1f MB%n",

206

process.getName(),

207

process.getProcessID(),

208

process.getProcessCpuLoadCumulative() * 100,

209

process.getResidentSetSize() / 1024.0 / 1024.0

210

);

211

}

212

```

213

214

### Top Memory Consuming Processes

215

216

```java

217

import oshi.SystemInfo;

218

import oshi.software.os.OperatingSystem;

219

import oshi.software.os.OSProcess;

220

221

SystemInfo si = new SystemInfo();

222

OperatingSystem os = si.getOperatingSystem();

223

224

// Get top 10 processes by memory usage

225

List<OSProcess> topMemoryProcesses = os.getProcesses(

226

OperatingSystem.ProcessFiltering.VALID_PROCESS,

227

OperatingSystem.ProcessSorting.RSS_DESC,

228

10

229

);

230

231

System.out.println("Top memory consuming processes:");

232

for (OSProcess process : topMemoryProcesses) {

233

System.out.printf("%-20s PID: %5d Memory: %8.1f MB Virtual: %8.1f MB%n",

234

process.getName(),

235

process.getProcessID(),

236

process.getResidentSetSize() / 1024.0 / 1024.0,

237

process.getVirtualSize() / 1024.0 / 1024.0

238

);

239

}

240

```

241

242

### Process Hierarchy

243

244

```java

245

import oshi.SystemInfo;

246

import oshi.software.os.OperatingSystem;

247

import oshi.software.os.OSProcess;

248

249

SystemInfo si = new SystemInfo();

250

OperatingSystem os = si.getOperatingSystem();

251

252

// Find a specific process and its children

253

OSProcess targetProcess = os.getProcess(1234); // Replace with actual PID

254

if (targetProcess != null) {

255

System.out.println("Process: " + targetProcess.getName());

256

257

// Get child processes

258

List<OSProcess> children = os.getChildProcesses(

259

targetProcess.getProcessID(),

260

OperatingSystem.ProcessFiltering.VALID_PROCESS,

261

OperatingSystem.ProcessSorting.NAME_ASC,

262

0

263

);

264

265

System.out.println("Child processes:");

266

for (OSProcess child : children) {

267

System.out.printf(" %s (PID: %d)%n", child.getName(), child.getProcessID());

268

}

269

270

// Get all descendants (children, grandchildren, etc.)

271

List<OSProcess> descendants = os.getDescendantProcesses(

272

targetProcess.getProcessID(),

273

OperatingSystem.ProcessFiltering.VALID_PROCESS,

274

OperatingSystem.ProcessSorting.NAME_ASC,

275

0

276

);

277

278

System.out.println("All descendants: " + descendants.size());

279

}

280

```

281

282

### Detailed Process Information

283

284

```java

285

import oshi.SystemInfo;

286

import oshi.software.os.OperatingSystem;

287

import oshi.software.os.OSProcess;

288

import oshi.software.os.OSThread;

289

290

SystemInfo si = new SystemInfo();

291

OperatingSystem os = si.getOperatingSystem();

292

293

OSProcess process = os.getProcess(1234); // Replace with actual PID

294

if (process != null) {

295

System.out.println("=== Process Details ===");

296

System.out.println("Name: " + process.getName());

297

System.out.println("Path: " + process.getPath());

298

System.out.println("Command Line: " + process.getCommandLine());

299

System.out.println("Working Directory: " + process.getCurrentWorkingDirectory());

300

System.out.println("User: " + process.getUser());

301

System.out.println("PID: " + process.getProcessID());

302

System.out.println("Parent PID: " + process.getParentProcessID());

303

System.out.println("State: " + process.getState());

304

System.out.println("Priority: " + process.getPriority());

305

System.out.println("Bitness: " + process.getBitness() + "-bit");

306

307

System.out.println("\n=== Memory Usage ===");

308

System.out.println("Virtual Size: " + process.getVirtualSize() / 1024 / 1024 + " MB");

309

System.out.println("Resident Set Size: " + process.getResidentSetSize() / 1024 / 1024 + " MB");

310

System.out.println("Page Faults (Minor): " + process.getMinorFaults());

311

System.out.println("Page Faults (Major): " + process.getMajorFaults());

312

313

System.out.println("\n=== CPU Usage ===");

314

System.out.println("CPU Load: " + (process.getProcessCpuLoadCumulative() * 100) + "%");

315

System.out.println("Kernel Time: " + process.getKernelTime() + " ms");

316

System.out.println("User Time: " + process.getUserTime() + " ms");

317

System.out.println("Context Switches: " + process.getContextSwitches());

318

319

System.out.println("\n=== Timing ===");

320

System.out.println("Start Time: " + new Date(process.getStartTime()));

321

System.out.println("Up Time: " + process.getUpTime() / 1000 + " seconds");

322

323

System.out.println("\n=== I/O ===");

324

System.out.println("Bytes Read: " + process.getBytesRead());

325

System.out.println("Bytes Written: " + process.getBytesWritten());

326

System.out.println("Open Files: " + process.getOpenFiles());

327

328

// Thread details

329

List<OSThread> threads = process.getThreadDetails();

330

System.out.println("\n=== Threads (" + threads.size() + ") ===");

331

for (OSThread thread : threads) {

332

System.out.printf(" Thread %d: %s (State: %s, Priority: %d)%n",

333

thread.getThreadId(),

334

thread.getName(),

335

thread.getState(),

336

thread.getPriority()

337

);

338

}

339

}

340

```

341

342

### Custom Process Filtering

343

344

```java

345

import oshi.SystemInfo;

346

import oshi.software.os.OperatingSystem;

347

import oshi.software.os.OSProcess;

348

import java.util.function.Predicate;

349

350

SystemInfo si = new SystemInfo();

351

OperatingSystem os = si.getOperatingSystem();

352

353

// Custom filter: processes using more than 100MB of memory

354

Predicate<OSProcess> highMemoryFilter = process ->

355

process.getResidentSetSize() > 100 * 1024 * 1024;

356

357

// Custom filter: Java processes

358

Predicate<OSProcess> javaProcessFilter = process ->

359

process.getName().toLowerCase().contains("java") ||

360

process.getCommandLine().toLowerCase().contains("java");

361

362

// Combine filters: Java processes using high memory

363

Predicate<OSProcess> combinedFilter = highMemoryFilter.and(javaProcessFilter);

364

365

List<OSProcess> filteredProcesses = os.getProcesses(

366

combinedFilter,

367

OperatingSystem.ProcessSorting.RSS_DESC,

368

20

369

);

370

371

System.out.println("High-memory Java processes:");

372

for (OSProcess process : filteredProcesses) {

373

System.out.printf("%-30s PID: %5d Memory: %8.1f MB%n",

374

process.getName(),

375

process.getProcessID(),

376

process.getResidentSetSize() / 1024.0 / 1024.0

377

);

378

}

379

```

380

381

### Process CPU Load Monitoring

382

383

```java

384

import oshi.SystemInfo;

385

import oshi.software.os.OperatingSystem;

386

import oshi.software.os.OSProcess;

387

388

SystemInfo si = new SystemInfo();

389

OperatingSystem os = si.getOperatingSystem();

390

391

OSProcess process = os.getProcess(1234); // Replace with actual PID

392

if (process != null) {

393

System.out.println("Monitoring CPU usage for: " + process.getName());

394

395

// Take initial snapshot

396

OSProcess snapshot1 = process;

397

398

try {

399

Thread.sleep(1000); // Wait 1 second

400

} catch (InterruptedException e) {

401

Thread.currentThread().interrupt();

402

}

403

404

// Take second snapshot

405

OSProcess snapshot2 = os.getProcess(process.getProcessID());

406

if (snapshot2 != null) {

407

double cpuLoad = snapshot2.getProcessCpuLoadBetweenTicks(snapshot1);

408

System.out.printf("CPU Load in last second: %.2f%%%n", cpuLoad * 100);

409

}

410

}

411

```