My Project
Loading...
Searching...
No Matches
kernel_lib.c
Go to the documentation of this file.
1// kernel_lib.c
2/*
3 * Kernel Lib Basic Code
4 *
5 * Maintainer: Park Jiwoo
6 *
7 * Copyright (C) 2024 Park-Jiwoo
8 *
9 */
10#include <stdio.h>
11#include <stdarg.h>
12#include <string.h>
13
14// 최대 프로세스 수 정의
15#define MAX_PROCESSES 10
16
17typedef struct
18{
19 char name[256];
20 int running; // C에서 bool 대신 int 사용 (0: false, 1: true)
21} Process;
22
23static Process process_table[MAX_PROCESSES];
24static int process_count = 0;
25
26// Qt에서 구현된 함수 포인터를 사용해 출력
27static void (*qt_print_function)(const char *str) = NULL;
28
29// 새로운 az_printf 함수 구현
30void az_printf(const char *format, ...)
31{
32 if (qt_print_function)
33 {
34 char buffer[1024];
35 va_list args;
36 va_start(args, format);
37 vsnprintf(buffer, sizeof(buffer), format, args);
38 va_end(args);
39 qt_print_function(buffer);
40 }
41}
42
43// Qt에서 구현된 함수 포인터를 사용해 출력
44void register_print_function(void (*print_function)(const char *str))
45{
46 qt_print_function = print_function;
47}
48
49// cmd창의 printf 함수 구현
50int kernel_create_process(const char *process_name)
51{
52 if (process_count >= MAX_PROCESSES)
53 {
54 az_printf("\nError: Process table is full.");
55 return 0; // false
56 }
57
58 // 새로운 프로세스 생성
59 Process new_process;
60 strncpy(new_process.name, process_name, sizeof(new_process.name) - 1);
61 new_process.name[sizeof(new_process.name) - 1] = '\0';
62 new_process.running = 1; // true
63
64 // 프로세스 테이블에 추가
65 process_table[process_count++] = new_process;
66
67 az_printf("\nProcess created: %s", process_name);
68 return 1; // true
69}
70
71// 프로세스 목록 출력
73{
74 az_printf("\nProcess List:\n");
75 for (int i = 0; i < process_count; i++)
76 {
77 az_printf(" - %s (%s)\n", process_table[i].name, process_table[i].running ? "Running" : "Stopped");
78 }
79}
80
81// 프로세스 종료
82int kernel_kill_process(const char *process_name)
83{
84 for (int i = 0; i < process_count; i++)
85 {
86 if (strcmp(process_table[i].name, process_name) == 0)
87 {
88 process_table[i].running = 0; // false
89 az_printf("\nProcess killed: %s", process_name);
90 return 1; // true
91 }
92 }
93 az_printf("\nError: Process not found: %s", process_name);
94 return 0; // false
95}
void register_print_function(void(*print_function)(const char *str))
Definition kernel_lib.c:44
void az_printf(const char *format,...)
Definition kernel_lib.c:30
int kernel_create_process(const char *process_name)
Definition kernel_lib.c:50
#define MAX_PROCESSES
Definition kernel_lib.c:15
int kernel_kill_process(const char *process_name)
Definition kernel_lib.c:82
void kernel_list_processes()
Definition kernel_lib.c:72
int running
Definition kernel_lib.c:20
char name[256]
Definition kernel_lib.c:19