[cpp]: my_systemcall()
一、说明
1、 C语言可以用【struct】替代C++的【class】。
二、代码
1 #include <iostream>
2 #include <string>
3
4
5 // system_calls tables
6 class c_sys_call_table
7 {
8 public:
9 c_sys_call_table(int no)
10 {
11 bool j = mf_check(no);
12 if ( true == j )
13 {
14 mf_run(no);
15 }
16 else
17 {
18 std::cout <<"[error]# systemcall(no=" << no << "): NOT FOUND." << std::endl ;
19 }
20 }
21 c_sys_call_table(std::string name)
22 {
23 bool j = mf_check(name);
24 if ( true == j )
25 {
26 mf_run(name);
27 }
28 else
29 {
30 std::cout <<"[error]# systemcall(name=" << name << "): NOT FOUND." << std::endl ;
31 }
32 }
33 bool mf_check(int no)
34 {
35 return ( (no >= 0) && (no < 5) ) ;
36 }
37
38 bool mf_check(std::string name)
39 {
40 for(int i=0; i<5; i++)
41 {
42 if (name == mv_st[i]) { return true ; }
43 }
44
45 return false ;
46 }
47
48 void mf_run(int no)
49 {
50 std::cout <<"[os]# " << mv_st[no] <<"(): system_call is running !" << std::endl ;
51 }
52 void mf_run(std::string name)
53 {
54 std::cout <<"[os]# " << name <<"(): system_call is running !" << std::endl ;
55 }
56 private:
57 std::string mv_st[5] = { "init", "system", "brk", "exec", "exit" } ;
58 };
59
60
61 // system_call packages
62 class c_syscalls
63 {
64 private:
65 int mv_sys_no = -1 ;
66 std::string mv_sys_name = "" ;
67 public:
68 c_syscalls(int i):mv_sys_no(i){ c_sys_call_table sci(mv_sys_no) ; }
69 c_syscalls(std::string s):mv_sys_name(s){ c_sys_call_table sn(mv_sys_name) ; }
70 void mf_msg()
71 {
72 if ( mv_sys_no >= 0 )
73 {
74 std::cout << "[os]# system_call_no := " << mv_sys_no << std::endl ;
75 }
76 else
77 {
78 std::cout << "[os]# system_call_no NOT FOUND := " << mv_sys_no << std::endl ;
79 }
80
81 if ( mv_sys_name != "" )
82 {
83 std::cout << "[os]# system_call_name := " << mv_sys_name << std::endl ;
84 }
85 else
86 {
87 std::cout << "[os]# system_call_name NOT FOUND := " << mv_sys_name << std::endl ;
88 }
89 }
90 };
91
92
93 // system call initialization and run
94 void syscall(int i)
95 {
96 c_syscalls si(i) ;
97 }
98 void syscall(std::string n)
99 {
100 c_syscalls sn(n) ;
101 }
102
103
104 // entry port
105 int main()
106 {
107 syscall(1) ;
108 syscall("exit") ;
109
110 syscall(5) ;
111 syscall("big") ;
112
113 return 0 ;
114 }
三、运行
g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
[os]# system(): system_call is running !
[os]# exit(): system_call is running !
[error]# systemcall(no=5): NOT FOUND.
[error]# systemcall(name=big): NOT FOUND.
四、参考文档
1、无
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/18002384