ZimingYuan / testos

A simple Riscv operating system in C refering to rCore-tutorial-v3.
7 stars 1 forks source link

printf in user & kernel #1

Open shili2017 opened 2 years ago

shili2017 commented 2 years ago

In your testos, user & kernel share the same printf function. According to my understanding, when user app calls printf, for each char, user will trap into kernel and print one character (so I guess it is a correct but inefficient design?). In comparison, in the original rCore design, user and kernel implement print functions separately.

ZimingYuan commented 2 years ago

The printf function I use is just a high level function to handle format strings. It actually calls consputc to output one character. The consputc function is implemented differently in user space and kernel space(see user/lib.c and kernel/sbicall.c). When compiling I link the different binary files which contain different consputc implementation to user program and kernel program. (英文比较差,见谅。意思是说我printf函数只是处理格式化字符串的,实际上用的是consputc输出字符。而这个函数在用户态和内核态实现不同,编译的时候用户程序和内核程序会链接包含不同实现的二进制文件)

shili2017 commented 2 years ago

Yeah I understand that you are using consputc to print a single character, so from the view of user app, each time I want to printf a string, I need to system call for each character. 从用户程序的视角来看,为了输出一个字符串,需要对每个字符都要做一次ecall,也许这样的开销会比较大?(也许合并成一个string然后只调用一次write会更合理一些……)

Btw, inspired by rCore and your os, I'm also working on my own rcc project (link). Here is how I implemented printf. I just completed chapter 4, and you are welcome to make any comments (ddw)!

Thanks qwq