@mytechtoday/augment-extensions 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/augment-extensions/coding-standards/c/CHANGELOG.md +55 -0
- package/augment-extensions/coding-standards/c/LICENSE +22 -0
- package/augment-extensions/coding-standards/c/README.md +167 -0
- package/augment-extensions/coding-standards/c/config/defaults.json +26 -0
- package/augment-extensions/coding-standards/c/config/examples/embedded.yaml +25 -0
- package/augment-extensions/coding-standards/c/config/examples/systems.json +31 -0
- package/augment-extensions/coding-standards/c/config/schema.json +244 -0
- package/augment-extensions/coding-standards/c/docs/API.md +613 -0
- package/augment-extensions/coding-standards/c/docs/CONFIGURATION.md +259 -0
- package/augment-extensions/coding-standards/c/docs/USER_GUIDE.md +567 -0
- package/augment-extensions/coding-standards/c/examples/drivers/Makefile +33 -0
- package/augment-extensions/coding-standards/c/examples/drivers/README.md +192 -0
- package/augment-extensions/coding-standards/c/examples/drivers/dma-example.c +224 -0
- package/augment-extensions/coding-standards/c/examples/drivers/example.dts +64 -0
- package/augment-extensions/coding-standards/c/examples/drivers/platform-driver.c +174 -0
- package/augment-extensions/coding-standards/c/examples/embedded/README.md +167 -0
- package/augment-extensions/coding-standards/c/examples/embedded/gpio-control.c +172 -0
- package/augment-extensions/coding-standards/c/examples/embedded/timer-isr.c +198 -0
- package/augment-extensions/coding-standards/c/examples/embedded/uart-communication.c +212 -0
- package/augment-extensions/coding-standards/c/examples/kernel/Makefile +82 -0
- package/augment-extensions/coding-standards/c/examples/kernel/README.md +168 -0
- package/augment-extensions/coding-standards/c/examples/kernel/char-device.c +198 -0
- package/augment-extensions/coding-standards/c/examples/kernel/proc-file.c +131 -0
- package/augment-extensions/coding-standards/c/examples/kernel/simple-module.c +111 -0
- package/augment-extensions/coding-standards/c/examples/legacy/Makefile +62 -0
- package/augment-extensions/coding-standards/c/examples/legacy/README.md +255 -0
- package/augment-extensions/coding-standards/c/examples/legacy/c89-to-c11-migration.c +268 -0
- package/augment-extensions/coding-standards/c/examples/legacy/compatibility-layer.c +239 -0
- package/augment-extensions/coding-standards/c/examples/networking/Makefile +35 -0
- package/augment-extensions/coding-standards/c/examples/networking/README.md +207 -0
- package/augment-extensions/coding-standards/c/examples/networking/protocol-parser.c +270 -0
- package/augment-extensions/coding-standards/c/examples/networking/tcp-server.c +197 -0
- package/augment-extensions/coding-standards/c/examples/networking/udp-multicast.c +220 -0
- package/augment-extensions/coding-standards/c/examples/realtime/Makefile +53 -0
- package/augment-extensions/coding-standards/c/examples/realtime/README.md +199 -0
- package/augment-extensions/coding-standards/c/examples/realtime/deadline-monitoring.c +260 -0
- package/augment-extensions/coding-standards/c/examples/realtime/priority-scheduling.c +258 -0
- package/augment-extensions/coding-standards/c/examples/systems/Makefile +34 -0
- package/augment-extensions/coding-standards/c/examples/systems/README.md +123 -0
- package/augment-extensions/coding-standards/c/examples/systems/ipc-pipes.c +181 -0
- package/augment-extensions/coding-standards/c/examples/systems/process-management.c +153 -0
- package/augment-extensions/coding-standards/c/examples/systems/signal-handling.c +162 -0
- package/augment-extensions/coding-standards/c/module.json +149 -0
- package/augment-extensions/coding-standards/c/rules/categories/drivers.md +635 -0
- package/augment-extensions/coding-standards/c/rules/categories/embedded.md +510 -0
- package/augment-extensions/coding-standards/c/rules/categories/kernel.md +653 -0
- package/augment-extensions/coding-standards/c/rules/categories/legacy.md +526 -0
- package/augment-extensions/coding-standards/c/rules/categories/networking.md +735 -0
- package/augment-extensions/coding-standards/c/rules/categories/realtime.md +631 -0
- package/augment-extensions/coding-standards/c/rules/categories/systems.md +586 -0
- package/augment-extensions/coding-standards/c/rules/universal/const-correctness.md +275 -0
- package/augment-extensions/coding-standards/c/rules/universal/documentation.md +251 -0
- package/augment-extensions/coding-standards/c/rules/universal/error-handling.md +250 -0
- package/augment-extensions/coding-standards/c/rules/universal/header-guards.md +254 -0
- package/augment-extensions/coding-standards/c/rules/universal/memory-safety.md +233 -0
- package/augment-extensions/coding-standards/c/rules/universal/naming.md +146 -0
- package/augment-extensions/coding-standards/c/src/conflict-detector.ts +461 -0
- package/augment-extensions/coding-standards/c/src/prompt-generator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-evaluator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-override.ts +427 -0
- package/augment-extensions/coding-standards/c/src/template-engine.ts +217 -0
- package/augment-extensions/coding-standards/c/templates/prompts/drivers.txt +191 -0
- package/augment-extensions/coding-standards/c/templates/prompts/embedded.txt +164 -0
- package/augment-extensions/coding-standards/c/templates/prompts/kernel.txt +175 -0
- package/augment-extensions/coding-standards/c/templates/prompts/legacy.txt +280 -0
- package/augment-extensions/coding-standards/c/templates/prompts/networking.txt +259 -0
- package/augment-extensions/coding-standards/c/templates/prompts/realtime.txt +219 -0
- package/augment-extensions/coding-standards/c/templates/prompts/systems.txt +147 -0
- package/augment-extensions/coding-standards/c/tests/integration/category-specific.test.ts +356 -0
- package/augment-extensions/coding-standards/c/tests/integration/end-to-end-workflow.test.ts +377 -0
- package/augment-extensions/coding-standards/c/tests/performance/benchmarks.test.ts +407 -0
- package/augment-extensions/coding-standards/c/tests/unit/config-manager.test.ts +345 -0
- package/augment-extensions/coding-standards/c/tests/unit/conflict-detector.test.ts +294 -0
- package/augment-extensions/coding-standards/c/tests/unit/prompt-generator.test.ts +174 -0
- package/augment-extensions/coding-standards/c/tests/unit/registry.test.ts +313 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-evaluator.test.ts +318 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-override.test.ts +326 -0
- package/augment-extensions/coding-standards/c/tests/unit/template-engine.test.ts +314 -0
- package/augment-extensions/coding-standards/go/CHARACTER-COUNT-REPORT.md +135 -0
- package/augment-extensions/coding-standards/go/PHASE1-COMPLETION.md +146 -0
- package/augment-extensions/coding-standards/go/PHASE4-COMPLETION.md +184 -0
- package/augment-extensions/coding-standards/go/README.md +200 -0
- package/augment-extensions/coding-standards/go/VALIDATION-CHECKLIST.md +154 -0
- package/augment-extensions/coding-standards/go/config/examples/example-cli.json +15 -0
- package/augment-extensions/coding-standards/go/config/examples/example-microservices.json +21 -0
- package/augment-extensions/coding-standards/go/config/examples/example-multi-category.yaml +24 -0
- package/augment-extensions/coding-standards/go/config/examples/example-web.json +15 -0
- package/augment-extensions/coding-standards/go/config/schema.json +110 -0
- package/augment-extensions/coding-standards/go/docs/CATEGORIES.md +221 -0
- package/augment-extensions/coding-standards/go/docs/CONFIGURATION.md +198 -0
- package/augment-extensions/coding-standards/go/docs/TROUBLESHOOTING.md +285 -0
- package/augment-extensions/coding-standards/go/examples/cli/cobra-app.go +287 -0
- package/augment-extensions/coding-standards/go/examples/cloud-native-app.go +217 -0
- package/augment-extensions/coding-standards/go/examples/devops-tool.go +250 -0
- package/augment-extensions/coding-standards/go/examples/distributed-system.go +247 -0
- package/augment-extensions/coding-standards/go/examples/microservices/grpc-service.go +253 -0
- package/augment-extensions/coding-standards/go/examples/rest-api.go +270 -0
- package/augment-extensions/coding-standards/go/examples/web/http-server.go +224 -0
- package/augment-extensions/coding-standards/go/module.json +139 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/api-versioning.md +149 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rate-limiting.md +209 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rest-api-design.md +183 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/cloud-config.md +193 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/health-checks.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/kubernetes.md +180 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/automation.md +179 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/ci-cd-integration.md +147 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/infrastructure-as-code.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/caching.md +150 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/consensus.md +187 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/event-sourcing.md +246 -0
- package/augment-extensions/coding-standards/go/rules/cli/command-parsing.md +264 -0
- package/augment-extensions/coding-standards/go/rules/cli/configuration.md +268 -0
- package/augment-extensions/coding-standards/go/rules/cli/cross-platform.md +324 -0
- package/augment-extensions/coding-standards/go/rules/microservices/distributed-tracing.md +253 -0
- package/augment-extensions/coding-standards/go/rules/microservices/grpc.md +257 -0
- package/augment-extensions/coding-standards/go/rules/microservices/metrics.md +278 -0
- package/augment-extensions/coding-standards/go/rules/microservices/service-discovery.md +249 -0
- package/augment-extensions/coding-standards/go/rules/universal/code-organization.md +221 -0
- package/augment-extensions/coding-standards/go/rules/universal/documentation.md +269 -0
- package/augment-extensions/coding-standards/go/rules/universal/performance.md +323 -0
- package/augment-extensions/coding-standards/go/rules/universal/testing.md +162 -0
- package/augment-extensions/coding-standards/go/rules/web/graceful-shutdown.md +249 -0
- package/augment-extensions/coding-standards/go/rules/web/http-handlers.md +164 -0
- package/augment-extensions/coding-standards/go/rules/web/middleware.md +234 -0
- package/augment-extensions/coding-standards/go/rules/web/routing.md +251 -0
- package/augment-extensions/coding-standards/go/templates/prompts/api.md +160 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cli.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cloud-native.md +121 -0
- package/augment-extensions/coding-standards/go/templates/prompts/devops.md +146 -0
- package/augment-extensions/coding-standards/go/templates/prompts/distributed.md +133 -0
- package/augment-extensions/coding-standards/go/templates/prompts/microservices.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/web.md +181 -0
- package/augment-extensions/coding-standards/go/tests/integration/module-integration.test.ts +164 -0
- package/augment-extensions/coding-standards/go/tests/unit/category-selection.test.ts +147 -0
- package/augment-extensions/coding-standards/go/tests/unit/module-structure.test.ts +154 -0
- package/augment-extensions/coding-standards/go/tests/validate-character-count.ps1 +13 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.ps1 +148 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.sh +135 -0
- package/cli/dist/analysis/ast-parser.d.ts +47 -0
- package/cli/dist/analysis/ast-parser.d.ts.map +1 -0
- package/cli/dist/analysis/ast-parser.js +161 -0
- package/cli/dist/analysis/ast-parser.js.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts +27 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.js +189 -0
- package/cli/dist/analysis/complexity-analyzer.js.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts +23 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.js +237 -0
- package/cli/dist/analysis/dependency-analyzer.js.map +1 -0
- package/cli/dist/analysis/index.d.ts +9 -0
- package/cli/dist/analysis/index.d.ts.map +1 -0
- package/cli/dist/analysis/index.js +25 -0
- package/cli/dist/analysis/index.js.map +1 -0
- package/cli/dist/analysis/security-scanner.d.ts +11 -0
- package/cli/dist/analysis/security-scanner.d.ts.map +1 -0
- package/cli/dist/analysis/security-scanner.js +294 -0
- package/cli/dist/analysis/security-scanner.js.map +1 -0
- package/cli/dist/analysis/types.d.ts +151 -0
- package/cli/dist/analysis/types.d.ts.map +1 -0
- package/cli/dist/analysis/types.js +6 -0
- package/cli/dist/analysis/types.js.map +1 -0
- package/cli/dist/cli.js +24 -0
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/code-analysis.d.ts +11 -0
- package/cli/dist/commands/code-analysis.d.ts.map +1 -0
- package/cli/dist/commands/code-analysis.js +412 -0
- package/cli/dist/commands/code-analysis.js.map +1 -0
- package/modules.md +99 -3
- package/package.json +14 -2
- package/cli/dist/commands/agent.d.ts +0 -37
- package/cli/dist/commands/agent.d.ts.map +0 -1
- package/cli/dist/commands/agent.js +0 -222
- package/cli/dist/commands/agent.js.map +0 -1
- package/cli/dist/commands/beads.d.ts +0 -64
- package/cli/dist/commands/beads.d.ts.map +0 -1
- package/cli/dist/commands/beads.js +0 -377
- package/cli/dist/commands/beads.js.map +0 -1
- package/cli/dist/commands/change.d.ts +0 -54
- package/cli/dist/commands/change.d.ts.map +0 -1
- package/cli/dist/commands/change.js +0 -243
- package/cli/dist/commands/change.js.map +0 -1
- package/cli/dist/commands/clean.d.ts +0 -15
- package/cli/dist/commands/clean.d.ts.map +0 -1
- package/cli/dist/commands/clean.js +0 -63
- package/cli/dist/commands/clean.js.map +0 -1
- package/cli/dist/commands/clone.d.ts +0 -15
- package/cli/dist/commands/clone.d.ts.map +0 -1
- package/cli/dist/commands/clone.js +0 -49
- package/cli/dist/commands/clone.js.map +0 -1
- package/cli/dist/commands/config.d.ts +0 -33
- package/cli/dist/commands/config.d.ts.map +0 -1
- package/cli/dist/commands/config.js +0 -166
- package/cli/dist/commands/config.js.map +0 -1
- package/cli/dist/commands/context.d.ts +0 -38
- package/cli/dist/commands/context.d.ts.map +0 -1
- package/cli/dist/commands/context.js +0 -205
- package/cli/dist/commands/context.js.map +0 -1
- package/cli/dist/commands/create.d.ts +0 -18
- package/cli/dist/commands/create.d.ts.map +0 -1
- package/cli/dist/commands/create.js +0 -178
- package/cli/dist/commands/create.js.map +0 -1
- package/cli/dist/commands/diff.d.ts +0 -19
- package/cli/dist/commands/diff.d.ts.map +0 -1
- package/cli/dist/commands/diff.js +0 -104
- package/cli/dist/commands/diff.js.map +0 -1
- package/cli/dist/commands/doctor.d.ts +0 -14
- package/cli/dist/commands/doctor.d.ts.map +0 -1
- package/cli/dist/commands/doctor.js +0 -62
- package/cli/dist/commands/doctor.js.map +0 -1
- package/cli/dist/commands/export.d.ts +0 -28
- package/cli/dist/commands/export.d.ts.map +0 -1
- package/cli/dist/commands/export.js +0 -135
- package/cli/dist/commands/export.js.map +0 -1
- package/cli/dist/commands/import.d.ts +0 -23
- package/cli/dist/commands/import.d.ts.map +0 -1
- package/cli/dist/commands/import.js +0 -118
- package/cli/dist/commands/import.js.map +0 -1
- package/cli/dist/commands/prompt.d.ts +0 -45
- package/cli/dist/commands/prompt.d.ts.map +0 -1
- package/cli/dist/commands/prompt.js +0 -223
- package/cli/dist/commands/prompt.js.map +0 -1
- package/cli/dist/commands/spec.d.ts +0 -57
- package/cli/dist/commands/spec.d.ts.map +0 -1
- package/cli/dist/commands/spec.js +0 -279
- package/cli/dist/commands/spec.js.map +0 -1
- package/cli/dist/commands/stats.d.ts +0 -18
- package/cli/dist/commands/stats.d.ts.map +0 -1
- package/cli/dist/commands/stats.js +0 -85
- package/cli/dist/commands/stats.js.map +0 -1
- package/cli/dist/commands/task.d.ts +0 -65
- package/cli/dist/commands/task.d.ts.map +0 -1
- package/cli/dist/commands/task.js +0 -282
- package/cli/dist/commands/task.js.map +0 -1
- package/cli/dist/commands/template.d.ts +0 -17
- package/cli/dist/commands/template.d.ts.map +0 -1
- package/cli/dist/commands/template.js +0 -55
- package/cli/dist/commands/template.js.map +0 -1
- package/cli/dist/utils/agent-config.d.ts +0 -129
- package/cli/dist/utils/agent-config.d.ts.map +0 -1
- package/cli/dist/utils/agent-config.js +0 -297
- package/cli/dist/utils/agent-config.js.map +0 -1
- package/cli/dist/utils/beads-graph.d.ts +0 -17
- package/cli/dist/utils/beads-graph.d.ts.map +0 -1
- package/cli/dist/utils/beads-graph.js +0 -150
- package/cli/dist/utils/beads-graph.js.map +0 -1
- package/cli/dist/utils/beads-integration.d.ts +0 -112
- package/cli/dist/utils/beads-integration.d.ts.map +0 -1
- package/cli/dist/utils/beads-integration.js +0 -312
- package/cli/dist/utils/beads-integration.js.map +0 -1
- package/cli/dist/utils/beads-reporter.d.ts +0 -17
- package/cli/dist/utils/beads-reporter.d.ts.map +0 -1
- package/cli/dist/utils/beads-reporter.js +0 -160
- package/cli/dist/utils/beads-reporter.js.map +0 -1
- package/cli/dist/utils/cache-manager.d.ts +0 -55
- package/cli/dist/utils/cache-manager.d.ts.map +0 -1
- package/cli/dist/utils/cache-manager.js +0 -150
- package/cli/dist/utils/cache-manager.js.map +0 -1
- package/cli/dist/utils/change-manager.d.ts +0 -70
- package/cli/dist/utils/change-manager.d.ts.map +0 -1
- package/cli/dist/utils/change-manager.js +0 -412
- package/cli/dist/utils/change-manager.js.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.d.ts +0 -66
- package/cli/dist/utils/config-manager-enhanced.d.ts.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.js +0 -77
- package/cli/dist/utils/config-manager-enhanced.js.map +0 -1
- package/cli/dist/utils/context-manager.d.ts +0 -96
- package/cli/dist/utils/context-manager.d.ts.map +0 -1
- package/cli/dist/utils/context-manager.js +0 -258
- package/cli/dist/utils/context-manager.js.map +0 -1
- package/cli/dist/utils/diff-engine.d.ts +0 -78
- package/cli/dist/utils/diff-engine.d.ts.map +0 -1
- package/cli/dist/utils/diff-engine.js +0 -233
- package/cli/dist/utils/diff-engine.js.map +0 -1
- package/cli/dist/utils/export-system.d.ts +0 -101
- package/cli/dist/utils/export-system.d.ts.map +0 -1
- package/cli/dist/utils/export-system.js +0 -289
- package/cli/dist/utils/export-system.js.map +0 -1
- package/cli/dist/utils/health-checker.d.ts +0 -66
- package/cli/dist/utils/health-checker.d.ts.map +0 -1
- package/cli/dist/utils/health-checker.js +0 -285
- package/cli/dist/utils/health-checker.js.map +0 -1
- package/cli/dist/utils/import-system.d.ts +0 -74
- package/cli/dist/utils/import-system.d.ts.map +0 -1
- package/cli/dist/utils/import-system.js +0 -317
- package/cli/dist/utils/import-system.js.map +0 -1
- package/cli/dist/utils/module-cloner.d.ts +0 -40
- package/cli/dist/utils/module-cloner.d.ts.map +0 -1
- package/cli/dist/utils/module-cloner.js +0 -136
- package/cli/dist/utils/module-cloner.js.map +0 -1
- package/cli/dist/utils/prompt-manager.d.ts +0 -90
- package/cli/dist/utils/prompt-manager.d.ts.map +0 -1
- package/cli/dist/utils/prompt-manager.js +0 -302
- package/cli/dist/utils/prompt-manager.js.map +0 -1
- package/cli/dist/utils/spec-manager.d.ts +0 -65
- package/cli/dist/utils/spec-manager.d.ts.map +0 -1
- package/cli/dist/utils/spec-manager.js +0 -329
- package/cli/dist/utils/spec-manager.js.map +0 -1
- package/cli/dist/utils/stats-collector.d.ts +0 -74
- package/cli/dist/utils/stats-collector.d.ts.map +0 -1
- package/cli/dist/utils/stats-collector.js +0 -164
- package/cli/dist/utils/stats-collector.js.map +0 -1
- package/cli/dist/utils/template-engine.d.ts +0 -47
- package/cli/dist/utils/template-engine.d.ts.map +0 -1
- package/cli/dist/utils/template-engine.js +0 -204
- package/cli/dist/utils/template-engine.js.map +0 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file deadline-monitoring.c
|
|
3
|
+
* @brief Example demonstrating deadline monitoring for real-time tasks
|
|
4
|
+
*
|
|
5
|
+
* This example shows:
|
|
6
|
+
* - Deadline-driven scheduling
|
|
7
|
+
* - Execution time monitoring
|
|
8
|
+
* - Deadline miss detection
|
|
9
|
+
* - Worst-case execution time (WCET) tracking
|
|
10
|
+
* - Task timing statistics
|
|
11
|
+
*
|
|
12
|
+
* Compatible with: FreeRTOS, Zephyr RTOS, or POSIX real-time
|
|
13
|
+
* Build: See README.md for RTOS-specific instructions
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#include <stdint.h>
|
|
17
|
+
#include <stdbool.h>
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
#include <string.h>
|
|
20
|
+
|
|
21
|
+
#ifdef USE_FREERTOS
|
|
22
|
+
#include "FreeRTOS.h"
|
|
23
|
+
#include "task.h"
|
|
24
|
+
#include "timers.h"
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
/* Task timing constraints */
|
|
28
|
+
#define TASK_PERIOD_MS 100
|
|
29
|
+
#define TASK_DEADLINE_MS 80
|
|
30
|
+
#define TASK_WCET_MS 50
|
|
31
|
+
|
|
32
|
+
/* Statistics tracking */
|
|
33
|
+
typedef struct {
|
|
34
|
+
uint32_t executions;
|
|
35
|
+
uint32_t deadline_misses;
|
|
36
|
+
uint32_t min_execution_time_us;
|
|
37
|
+
uint32_t max_execution_time_us;
|
|
38
|
+
uint32_t total_execution_time_us;
|
|
39
|
+
} task_stats_t;
|
|
40
|
+
|
|
41
|
+
static task_stats_t task_stats = {0};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @brief Get current time in microseconds
|
|
45
|
+
* @return Current time in microseconds
|
|
46
|
+
*/
|
|
47
|
+
static inline uint32_t get_time_us(void)
|
|
48
|
+
{
|
|
49
|
+
#ifdef USE_FREERTOS
|
|
50
|
+
return (uint32_t)(xTaskGetTickCount() * 1000 / configTICK_RATE_HZ);
|
|
51
|
+
#else
|
|
52
|
+
/* Platform-specific implementation */
|
|
53
|
+
return 0; /* Placeholder */
|
|
54
|
+
#endif
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @brief Initialize task statistics
|
|
59
|
+
*/
|
|
60
|
+
static void init_task_stats(task_stats_t *stats)
|
|
61
|
+
{
|
|
62
|
+
memset(stats, 0, sizeof(task_stats_t));
|
|
63
|
+
stats->min_execution_time_us = UINT32_MAX;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @brief Update task statistics
|
|
68
|
+
* @param stats Statistics structure
|
|
69
|
+
* @param execution_time_us Execution time in microseconds
|
|
70
|
+
* @param deadline_met Whether deadline was met
|
|
71
|
+
*/
|
|
72
|
+
static void update_task_stats(task_stats_t *stats,
|
|
73
|
+
uint32_t execution_time_us,
|
|
74
|
+
bool deadline_met)
|
|
75
|
+
{
|
|
76
|
+
stats->executions++;
|
|
77
|
+
stats->total_execution_time_us += execution_time_us;
|
|
78
|
+
|
|
79
|
+
if (execution_time_us < stats->min_execution_time_us) {
|
|
80
|
+
stats->min_execution_time_us = execution_time_us;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (execution_time_us > stats->max_execution_time_us) {
|
|
84
|
+
stats->max_execution_time_us = execution_time_us;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!deadline_met) {
|
|
88
|
+
stats->deadline_misses++;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @brief Print task statistics
|
|
94
|
+
*/
|
|
95
|
+
static void print_task_stats(const task_stats_t *stats)
|
|
96
|
+
{
|
|
97
|
+
if (stats->executions == 0) {
|
|
98
|
+
printf("No executions yet\n");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
uint32_t avg_time = stats->total_execution_time_us / stats->executions;
|
|
103
|
+
float miss_rate = (float)stats->deadline_misses / stats->executions * 100.0f;
|
|
104
|
+
|
|
105
|
+
printf("\n=== Task Statistics ===\n");
|
|
106
|
+
printf("Executions: %u\n", (unsigned int)stats->executions);
|
|
107
|
+
printf("Deadline misses: %u (%.2f%%)\n",
|
|
108
|
+
(unsigned int)stats->deadline_misses, miss_rate);
|
|
109
|
+
printf("Min exec time: %u us\n", (unsigned int)stats->min_execution_time_us);
|
|
110
|
+
printf("Max exec time: %u us\n", (unsigned int)stats->max_execution_time_us);
|
|
111
|
+
printf("Avg exec time: %u us\n", (unsigned int)avg_time);
|
|
112
|
+
printf("======================\n\n");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @brief Simulate task workload
|
|
117
|
+
* @param workload_us Workload duration in microseconds
|
|
118
|
+
*/
|
|
119
|
+
static void simulate_workload(uint32_t workload_us)
|
|
120
|
+
{
|
|
121
|
+
uint32_t start = get_time_us();
|
|
122
|
+
|
|
123
|
+
/* Busy-wait to simulate work */
|
|
124
|
+
while ((get_time_us() - start) < workload_us) {
|
|
125
|
+
/* Simulate computation */
|
|
126
|
+
volatile uint32_t dummy = 0;
|
|
127
|
+
for (int i = 0; i < 100; i++) {
|
|
128
|
+
dummy += i;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @brief Deadline-monitored task
|
|
135
|
+
*
|
|
136
|
+
* This task monitors its own execution time and detects deadline misses
|
|
137
|
+
*/
|
|
138
|
+
void deadline_monitored_task(void *params)
|
|
139
|
+
{
|
|
140
|
+
TickType_t last_wake_time;
|
|
141
|
+
const TickType_t period = pdMS_TO_TICKS(TASK_PERIOD_MS);
|
|
142
|
+
const uint32_t deadline_us = TASK_DEADLINE_MS * 1000;
|
|
143
|
+
uint32_t iteration = 0;
|
|
144
|
+
|
|
145
|
+
(void)params;
|
|
146
|
+
|
|
147
|
+
init_task_stats(&task_stats);
|
|
148
|
+
last_wake_time = xTaskGetTickCount();
|
|
149
|
+
|
|
150
|
+
printf("Deadline-monitored task started\n");
|
|
151
|
+
printf("Period: %u ms, Deadline: %u ms\n",
|
|
152
|
+
TASK_PERIOD_MS, TASK_DEADLINE_MS);
|
|
153
|
+
|
|
154
|
+
while (1) {
|
|
155
|
+
/* Wait for next period */
|
|
156
|
+
vTaskDelayUntil(&last_wake_time, period);
|
|
157
|
+
|
|
158
|
+
/* Record start time */
|
|
159
|
+
uint32_t start_time = get_time_us();
|
|
160
|
+
TickType_t release_time = xTaskGetTickCount();
|
|
161
|
+
|
|
162
|
+
/* Perform task work */
|
|
163
|
+
/* Vary workload to demonstrate deadline monitoring */
|
|
164
|
+
uint32_t workload_us;
|
|
165
|
+
if (iteration % 10 == 0) {
|
|
166
|
+
/* Occasionally exceed deadline to demonstrate detection */
|
|
167
|
+
workload_us = (TASK_DEADLINE_MS + 10) * 1000;
|
|
168
|
+
} else {
|
|
169
|
+
/* Normal workload within deadline */
|
|
170
|
+
workload_us = (TASK_WCET_MS - 10) * 1000;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
simulate_workload(workload_us);
|
|
174
|
+
|
|
175
|
+
/* Record end time */
|
|
176
|
+
uint32_t end_time = get_time_us();
|
|
177
|
+
uint32_t execution_time_us = end_time - start_time;
|
|
178
|
+
|
|
179
|
+
/* Check deadline */
|
|
180
|
+
bool deadline_met = (execution_time_us <= deadline_us);
|
|
181
|
+
|
|
182
|
+
/* Update statistics */
|
|
183
|
+
update_task_stats(&task_stats, execution_time_us, deadline_met);
|
|
184
|
+
|
|
185
|
+
/* Log deadline miss */
|
|
186
|
+
if (!deadline_met) {
|
|
187
|
+
printf("DEADLINE MISS! Iteration %u: %u us (deadline: %u us)\n",
|
|
188
|
+
(unsigned int)iteration,
|
|
189
|
+
(unsigned int)execution_time_us,
|
|
190
|
+
(unsigned int)deadline_us);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* Print statistics every 20 iterations */
|
|
194
|
+
if (iteration % 20 == 0 && iteration > 0) {
|
|
195
|
+
print_task_stats(&task_stats);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
iteration++;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @brief Watchdog timer callback for deadline enforcement
|
|
204
|
+
*
|
|
205
|
+
* This timer fires at the deadline to detect if task is still running
|
|
206
|
+
*/
|
|
207
|
+
void deadline_watchdog_callback(TimerHandle_t timer)
|
|
208
|
+
{
|
|
209
|
+
(void)timer;
|
|
210
|
+
|
|
211
|
+
/* In a real system, this would check if the task completed */
|
|
212
|
+
/* and take corrective action if deadline was missed */
|
|
213
|
+
printf("Watchdog: Checking deadline compliance\n");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @brief Initialize deadline monitoring system
|
|
218
|
+
*/
|
|
219
|
+
int main(void)
|
|
220
|
+
{
|
|
221
|
+
BaseType_t ret;
|
|
222
|
+
TimerHandle_t watchdog_timer;
|
|
223
|
+
|
|
224
|
+
printf("Starting Deadline Monitoring Example\n");
|
|
225
|
+
|
|
226
|
+
/* Create deadline watchdog timer */
|
|
227
|
+
watchdog_timer = xTimerCreate("DeadlineWatchdog",
|
|
228
|
+
pdMS_TO_TICKS(TASK_DEADLINE_MS),
|
|
229
|
+
pdTRUE, /* Auto-reload */
|
|
230
|
+
NULL,
|
|
231
|
+
deadline_watchdog_callback);
|
|
232
|
+
if (watchdog_timer == NULL) {
|
|
233
|
+
printf("Failed to create watchdog timer\n");
|
|
234
|
+
return -1;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/* Create deadline-monitored task */
|
|
238
|
+
ret = xTaskCreate(deadline_monitored_task,
|
|
239
|
+
"DeadlineTask",
|
|
240
|
+
512,
|
|
241
|
+
NULL,
|
|
242
|
+
2, /* Medium priority */
|
|
243
|
+
NULL);
|
|
244
|
+
if (ret != pdPASS) {
|
|
245
|
+
printf("Failed to create deadline-monitored task\n");
|
|
246
|
+
return -1;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* Start watchdog timer */
|
|
250
|
+
xTimerStart(watchdog_timer, 0);
|
|
251
|
+
|
|
252
|
+
/* Start scheduler */
|
|
253
|
+
printf("Starting RTOS scheduler\n");
|
|
254
|
+
vTaskStartScheduler();
|
|
255
|
+
|
|
256
|
+
/* Should never reach here */
|
|
257
|
+
printf("Scheduler failed to start\n");
|
|
258
|
+
return -1;
|
|
259
|
+
}
|
|
260
|
+
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file priority-scheduling.c
|
|
3
|
+
* @brief Example demonstrating priority-based task scheduling for RTOS
|
|
4
|
+
*
|
|
5
|
+
* This example shows:
|
|
6
|
+
* - Task creation with different priorities
|
|
7
|
+
* - Priority-based preemption
|
|
8
|
+
* - Task synchronization
|
|
9
|
+
* - Resource sharing with priority inheritance
|
|
10
|
+
* - Deadline monitoring
|
|
11
|
+
*
|
|
12
|
+
* Compatible with: FreeRTOS, Zephyr RTOS, or similar
|
|
13
|
+
* Build: Depends on RTOS (see README.md)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#include <stdint.h>
|
|
17
|
+
#include <stdbool.h>
|
|
18
|
+
#include <stdio.h>
|
|
19
|
+
|
|
20
|
+
/* RTOS-specific includes (example for FreeRTOS) */
|
|
21
|
+
#ifdef USE_FREERTOS
|
|
22
|
+
#include "FreeRTOS.h"
|
|
23
|
+
#include "task.h"
|
|
24
|
+
#include "semphr.h"
|
|
25
|
+
#include "queue.h"
|
|
26
|
+
#endif
|
|
27
|
+
|
|
28
|
+
/* Task priorities (higher number = higher priority) */
|
|
29
|
+
#define PRIORITY_HIGH 3
|
|
30
|
+
#define PRIORITY_MEDIUM 2
|
|
31
|
+
#define PRIORITY_LOW 1
|
|
32
|
+
|
|
33
|
+
/* Task periods (in milliseconds) */
|
|
34
|
+
#define PERIOD_HIGH_MS 10
|
|
35
|
+
#define PERIOD_MEDIUM_MS 50
|
|
36
|
+
#define PERIOD_LOW_MS 100
|
|
37
|
+
|
|
38
|
+
/* Stack sizes */
|
|
39
|
+
#define STACK_SIZE 256
|
|
40
|
+
|
|
41
|
+
/* Shared resource protection */
|
|
42
|
+
static SemaphoreHandle_t resource_mutex = NULL;
|
|
43
|
+
static uint32_t shared_counter = 0;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @brief High-priority periodic task
|
|
47
|
+
*
|
|
48
|
+
* Runs every 10ms - Critical real-time task
|
|
49
|
+
*/
|
|
50
|
+
void high_priority_task(void *params)
|
|
51
|
+
{
|
|
52
|
+
TickType_t last_wake_time;
|
|
53
|
+
const TickType_t period = pdMS_TO_TICKS(PERIOD_HIGH_MS);
|
|
54
|
+
|
|
55
|
+
(void)params; /* Unused */
|
|
56
|
+
|
|
57
|
+
/* Initialize last wake time */
|
|
58
|
+
last_wake_time = xTaskGetTickCount();
|
|
59
|
+
|
|
60
|
+
while (1) {
|
|
61
|
+
/* Wait for next period */
|
|
62
|
+
vTaskDelayUntil(&last_wake_time, period);
|
|
63
|
+
|
|
64
|
+
/* Critical real-time work */
|
|
65
|
+
/* Example: Read sensor, update control output */
|
|
66
|
+
|
|
67
|
+
/* Access shared resource with mutex */
|
|
68
|
+
if (xSemaphoreTake(resource_mutex, pdMS_TO_TICKS(5)) == pdTRUE) {
|
|
69
|
+
shared_counter++;
|
|
70
|
+
xSemaphoreGive(resource_mutex);
|
|
71
|
+
} else {
|
|
72
|
+
/* Mutex timeout - log error */
|
|
73
|
+
printf("High priority task: Mutex timeout!\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* Simulate work (replace with actual task) */
|
|
77
|
+
vTaskDelay(pdMS_TO_TICKS(2));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @brief Medium-priority periodic task
|
|
83
|
+
*
|
|
84
|
+
* Runs every 50ms - Important but not critical
|
|
85
|
+
*/
|
|
86
|
+
void medium_priority_task(void *params)
|
|
87
|
+
{
|
|
88
|
+
TickType_t last_wake_time;
|
|
89
|
+
const TickType_t period = pdMS_TO_TICKS(PERIOD_MEDIUM_MS);
|
|
90
|
+
|
|
91
|
+
(void)params;
|
|
92
|
+
|
|
93
|
+
last_wake_time = xTaskGetTickCount();
|
|
94
|
+
|
|
95
|
+
while (1) {
|
|
96
|
+
vTaskDelayUntil(&last_wake_time, period);
|
|
97
|
+
|
|
98
|
+
/* Medium priority work */
|
|
99
|
+
/* Example: Process data, update display */
|
|
100
|
+
|
|
101
|
+
if (xSemaphoreTake(resource_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
|
|
102
|
+
/* Read shared resource */
|
|
103
|
+
uint32_t value = shared_counter;
|
|
104
|
+
xSemaphoreGive(resource_mutex);
|
|
105
|
+
|
|
106
|
+
printf("Medium task: Counter = %u\n", (unsigned int)value);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
vTaskDelay(pdMS_TO_TICKS(5));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @brief Low-priority periodic task
|
|
115
|
+
*
|
|
116
|
+
* Runs every 100ms - Background processing
|
|
117
|
+
*/
|
|
118
|
+
void low_priority_task(void *params)
|
|
119
|
+
{
|
|
120
|
+
TickType_t last_wake_time;
|
|
121
|
+
const TickType_t period = pdMS_TO_TICKS(PERIOD_LOW_MS);
|
|
122
|
+
|
|
123
|
+
(void)params;
|
|
124
|
+
|
|
125
|
+
last_wake_time = xTaskGetTickCount();
|
|
126
|
+
|
|
127
|
+
while (1) {
|
|
128
|
+
vTaskDelayUntil(&last_wake_time, period);
|
|
129
|
+
|
|
130
|
+
/* Low priority work */
|
|
131
|
+
/* Example: Logging, housekeeping, diagnostics */
|
|
132
|
+
|
|
133
|
+
if (xSemaphoreTake(resource_mutex, pdMS_TO_TICKS(20)) == pdTRUE) {
|
|
134
|
+
uint32_t value = shared_counter;
|
|
135
|
+
xSemaphoreGive(resource_mutex);
|
|
136
|
+
|
|
137
|
+
printf("Low task: Counter = %u\n", (unsigned int)value);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
vTaskDelay(pdMS_TO_TICKS(10));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @brief Aperiodic event handler task
|
|
146
|
+
*
|
|
147
|
+
* Responds to events with high priority
|
|
148
|
+
*/
|
|
149
|
+
void event_handler_task(void *params)
|
|
150
|
+
{
|
|
151
|
+
QueueHandle_t event_queue = (QueueHandle_t)params;
|
|
152
|
+
uint32_t event_data;
|
|
153
|
+
|
|
154
|
+
while (1) {
|
|
155
|
+
/* Wait for event (blocking) */
|
|
156
|
+
if (xQueueReceive(event_queue, &event_data, portMAX_DELAY) == pdTRUE) {
|
|
157
|
+
/* Handle event immediately */
|
|
158
|
+
printf("Event handler: Received event %u\n", (unsigned int)event_data);
|
|
159
|
+
|
|
160
|
+
/* Process event with deadline constraint */
|
|
161
|
+
/* Must complete within 5ms */
|
|
162
|
+
TickType_t start_time = xTaskGetTickCount();
|
|
163
|
+
|
|
164
|
+
/* Event processing */
|
|
165
|
+
vTaskDelay(pdMS_TO_TICKS(3));
|
|
166
|
+
|
|
167
|
+
TickType_t end_time = xTaskGetTickCount();
|
|
168
|
+
TickType_t elapsed = end_time - start_time;
|
|
169
|
+
|
|
170
|
+
if (elapsed > pdMS_TO_TICKS(5)) {
|
|
171
|
+
printf("Event handler: Deadline missed! (%u ms)\n",
|
|
172
|
+
(unsigned int)(elapsed * portTICK_PERIOD_MS));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @brief Initialize and start real-time tasks
|
|
180
|
+
*/
|
|
181
|
+
int main(void)
|
|
182
|
+
{
|
|
183
|
+
BaseType_t ret;
|
|
184
|
+
QueueHandle_t event_queue;
|
|
185
|
+
|
|
186
|
+
printf("Starting Real-Time Priority Scheduling Example\n");
|
|
187
|
+
|
|
188
|
+
/* Create mutex with priority inheritance */
|
|
189
|
+
resource_mutex = xSemaphoreCreateMutex();
|
|
190
|
+
if (resource_mutex == NULL) {
|
|
191
|
+
printf("Failed to create mutex\n");
|
|
192
|
+
return -1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/* Create event queue */
|
|
196
|
+
event_queue = xQueueCreate(10, sizeof(uint32_t));
|
|
197
|
+
if (event_queue == NULL) {
|
|
198
|
+
printf("Failed to create event queue\n");
|
|
199
|
+
return -1;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* Create high-priority task */
|
|
203
|
+
ret = xTaskCreate(high_priority_task,
|
|
204
|
+
"HighPrio",
|
|
205
|
+
STACK_SIZE,
|
|
206
|
+
NULL,
|
|
207
|
+
PRIORITY_HIGH,
|
|
208
|
+
NULL);
|
|
209
|
+
if (ret != pdPASS) {
|
|
210
|
+
printf("Failed to create high priority task\n");
|
|
211
|
+
return -1;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* Create medium-priority task */
|
|
215
|
+
ret = xTaskCreate(medium_priority_task,
|
|
216
|
+
"MediumPrio",
|
|
217
|
+
STACK_SIZE,
|
|
218
|
+
NULL,
|
|
219
|
+
PRIORITY_MEDIUM,
|
|
220
|
+
NULL);
|
|
221
|
+
if (ret != pdPASS) {
|
|
222
|
+
printf("Failed to create medium priority task\n");
|
|
223
|
+
return -1;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/* Create low-priority task */
|
|
227
|
+
ret = xTaskCreate(low_priority_task,
|
|
228
|
+
"LowPrio",
|
|
229
|
+
STACK_SIZE,
|
|
230
|
+
NULL,
|
|
231
|
+
PRIORITY_LOW,
|
|
232
|
+
NULL);
|
|
233
|
+
if (ret != pdPASS) {
|
|
234
|
+
printf("Failed to create low priority task\n");
|
|
235
|
+
return -1;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* Create event handler task */
|
|
239
|
+
ret = xTaskCreate(event_handler_task,
|
|
240
|
+
"EventHandler",
|
|
241
|
+
STACK_SIZE,
|
|
242
|
+
(void *)event_queue,
|
|
243
|
+
PRIORITY_HIGH,
|
|
244
|
+
NULL);
|
|
245
|
+
if (ret != pdPASS) {
|
|
246
|
+
printf("Failed to create event handler task\n");
|
|
247
|
+
return -1;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* Start scheduler */
|
|
251
|
+
printf("Starting RTOS scheduler\n");
|
|
252
|
+
vTaskStartScheduler();
|
|
253
|
+
|
|
254
|
+
/* Should never reach here */
|
|
255
|
+
printf("Scheduler failed to start\n");
|
|
256
|
+
return -1;
|
|
257
|
+
}
|
|
258
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Makefile for Systems Programming Examples
|
|
2
|
+
# Demonstrates POSIX-compliant build process
|
|
3
|
+
|
|
4
|
+
CC = gcc
|
|
5
|
+
CFLAGS = -Wall -Wextra -Werror -std=c11 -pedantic -O2
|
|
6
|
+
LDFLAGS =
|
|
7
|
+
TARGETS = process-management ipc-pipes signal-handling
|
|
8
|
+
|
|
9
|
+
.PHONY: all clean test
|
|
10
|
+
|
|
11
|
+
all: $(TARGETS)
|
|
12
|
+
|
|
13
|
+
process-management: process-management.c
|
|
14
|
+
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
|
|
15
|
+
|
|
16
|
+
ipc-pipes: ipc-pipes.c
|
|
17
|
+
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
|
|
18
|
+
|
|
19
|
+
signal-handling: signal-handling.c
|
|
20
|
+
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
|
|
21
|
+
|
|
22
|
+
clean:
|
|
23
|
+
rm -f $(TARGETS) *.o
|
|
24
|
+
|
|
25
|
+
test: all
|
|
26
|
+
@echo "Running process-management..."
|
|
27
|
+
./process-management
|
|
28
|
+
@echo ""
|
|
29
|
+
@echo "Running ipc-pipes..."
|
|
30
|
+
./ipc-pipes
|
|
31
|
+
@echo ""
|
|
32
|
+
@echo "Note: signal-handling requires manual testing (Ctrl+C, kill signals)"
|
|
33
|
+
@echo "Run: ./signal-handling"
|
|
34
|
+
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Systems Programming Examples
|
|
2
|
+
|
|
3
|
+
This directory contains compilable C examples demonstrating POSIX systems programming best practices.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
### 1. process-management.c
|
|
8
|
+
Demonstrates process creation and management:
|
|
9
|
+
- `fork()` and `exec()` family functions
|
|
10
|
+
- Parent-child process coordination
|
|
11
|
+
- Process exit status handling
|
|
12
|
+
- Multiple child process management
|
|
13
|
+
- Resource cleanup
|
|
14
|
+
|
|
15
|
+
**Key Concepts:**
|
|
16
|
+
- Always check `fork()` return value
|
|
17
|
+
- Use `waitpid()` to collect child status
|
|
18
|
+
- Handle both normal exit and signal termination
|
|
19
|
+
- Clean up resources on error paths
|
|
20
|
+
|
|
21
|
+
### 2. ipc-pipes.c
|
|
22
|
+
Demonstrates inter-process communication using pipes:
|
|
23
|
+
- Creating pipes with `pipe()`
|
|
24
|
+
- Unidirectional communication
|
|
25
|
+
- Bidirectional communication with two pipes
|
|
26
|
+
- Proper file descriptor management
|
|
27
|
+
- Error handling
|
|
28
|
+
|
|
29
|
+
**Key Concepts:**
|
|
30
|
+
- Close unused pipe ends
|
|
31
|
+
- Handle partial reads/writes
|
|
32
|
+
- Check all system call return values
|
|
33
|
+
- Clean up file descriptors
|
|
34
|
+
|
|
35
|
+
### 3. signal-handling.c
|
|
36
|
+
Demonstrates POSIX signal handling:
|
|
37
|
+
- Installing signal handlers with `sigaction()`
|
|
38
|
+
- Handling SIGINT, SIGTERM, SIGUSR1
|
|
39
|
+
- Async-signal-safe operations
|
|
40
|
+
- Signal masking with `sigprocmask()`
|
|
41
|
+
- Graceful shutdown
|
|
42
|
+
|
|
43
|
+
**Key Concepts:**
|
|
44
|
+
- Use `sigaction()` instead of `signal()`
|
|
45
|
+
- Only use async-signal-safe functions in handlers
|
|
46
|
+
- Use `volatile sig_atomic_t` for shared flags
|
|
47
|
+
- Implement graceful shutdown
|
|
48
|
+
|
|
49
|
+
## Building
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Build all examples
|
|
53
|
+
make
|
|
54
|
+
|
|
55
|
+
# Build specific example
|
|
56
|
+
make process-management
|
|
57
|
+
|
|
58
|
+
# Clean build artifacts
|
|
59
|
+
make clean
|
|
60
|
+
|
|
61
|
+
# Run automated tests
|
|
62
|
+
make test
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Running
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Process management
|
|
69
|
+
./process-management
|
|
70
|
+
|
|
71
|
+
# IPC pipes
|
|
72
|
+
./ipc-pipes
|
|
73
|
+
|
|
74
|
+
# Signal handling (interactive)
|
|
75
|
+
./signal-handling
|
|
76
|
+
# In another terminal:
|
|
77
|
+
kill -USR1 <pid>
|
|
78
|
+
kill -TERM <pid>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Requirements
|
|
82
|
+
|
|
83
|
+
- POSIX-compliant system (Linux, Unix, macOS, BSD)
|
|
84
|
+
- GCC or compatible C compiler
|
|
85
|
+
- C11 standard support
|
|
86
|
+
|
|
87
|
+
## Standards Compliance
|
|
88
|
+
|
|
89
|
+
All examples follow:
|
|
90
|
+
- POSIX.1-2017 (IEEE Std 1003.1-2017)
|
|
91
|
+
- C11 standard (ISO/IEC 9899:2011)
|
|
92
|
+
- Linux/GNU coding standards
|
|
93
|
+
|
|
94
|
+
## Best Practices Demonstrated
|
|
95
|
+
|
|
96
|
+
1. **Error Handling**
|
|
97
|
+
- Check all system call return values
|
|
98
|
+
- Use `perror()` for error messages
|
|
99
|
+
- Clean up resources on error paths
|
|
100
|
+
|
|
101
|
+
2. **Resource Management**
|
|
102
|
+
- Close file descriptors when done
|
|
103
|
+
- Free allocated memory
|
|
104
|
+
- Wait for child processes
|
|
105
|
+
|
|
106
|
+
3. **Signal Safety**
|
|
107
|
+
- Use async-signal-safe functions only
|
|
108
|
+
- Minimize work in signal handlers
|
|
109
|
+
- Use `volatile sig_atomic_t` for flags
|
|
110
|
+
|
|
111
|
+
4. **Code Quality**
|
|
112
|
+
- Compile with `-Wall -Wextra -Werror`
|
|
113
|
+
- Use meaningful variable names
|
|
114
|
+
- Add comprehensive comments
|
|
115
|
+
- Follow consistent style
|
|
116
|
+
|
|
117
|
+
## References
|
|
118
|
+
|
|
119
|
+
- POSIX.1-2017: https://pubs.opengroup.org/onlinepubs/9699919799/
|
|
120
|
+
- Advanced Programming in the UNIX Environment (APUE)
|
|
121
|
+
- The Linux Programming Interface (TLPI)
|
|
122
|
+
- GNU C Library Manual: https://www.gnu.org/software/libc/manual/
|
|
123
|
+
|