--------------------------------------------------------
printf & printl
--------------------------------------------------------
以前的代码中 #define printl printf ,现在我们要区分它们,printf 用于用户态,printl 用于非用户态(系统任务是可以使用 printf 的,但那将将某个进程的 TTY 作为输出介质)!
·printf 函数
PUBLIC int printf(const char *fmt, ...)
{
int i;
char buf[STR_DEFAULT_LEN];
va_list arg = (va_list)((char *)(&fmt) + 4);
i = vsprintf(buf, fmt, arg);
int c = write(1, buf, i);
assert(c == i);
return i;
}
·printl 函数
PUBLIC int printl(const char *fmt, ...)
{
int i;
char buf[STR_DEFAULT_LEN];
va_list arg = (va_list)((char *)(&fmt) + 4);
i = vsprintf(buf, fmt, arg);
printx(buf);
return i;
}
·sys_printx 函数
PUBLIC int sys_printx(int _unused1, int _unused2, char *s, struct proc *p_proc)
{
const char *p;
char ch;
char reenter_err[] = "? k_reenter is incorrect for unknown reason";
reenter_err[0] = MAG_CH_PANIC;
if (k_reenter == 0) /* printx() called in Ring<1~3> */
p = va2la(proc2pid(p_proc), s);
else if (k_reenter > 0) /* printx() called in Ring<0> */
p = s;
else /* this should NOT happen */
p = reenter_err;
if ((*p == MAG_CH_PANIC) ||
(*p == MAG_CH_ASSERT && p_proc_ready < &proc_table[NR_TASKS]))
{
disable_int();
char *v = (char *)V_MEM_BASE;
const char *q = p + 1; /* +1: skip the magic char */
while (v < (char *)(V_MEM_BASE + V_MEM_SIZE))
{
*v++ = *q++;
*v++ = RED_CHAR;
if (!*q)
{
while (((int)v - V_MEM_BASE) % (SCR_WIDTH * 16))
{
/* *v++ = ' '; */
v++;
*v++ = GRAY_CHAR;
}
q = p + 1;
}
}
__asm__ __volatile__("hlt");
}
while ((ch = *p++) != 0)
{
if (ch == MAG_CH_PANIC || ch == MAG_CH_ASSERT)
continue; /* skip the magic char */
out_char(TTY_FIRST->console, ch);
}
//__asm__ __volatile__("nop;jmp 1f;ud2;1: nop");
//__asm__ __volatile__("nop;cli;1: jmp 1b;ud2;nop");
return 0;
}
·TestB 函数
void TestB()
{
char tty_name[] = "/dev_tty1";
int fd_stdin = open(tty_name, O_RDWR);
assert(fd_stdin == 0);
int fd_stdout = open(tty_name, O_RDWR);
assert(fd_stdout == 1);
char rdbuf[128];
while (1)
{
printf("$ ");
int r = read(fd_stdin, rdbuf, 70);
rdbuf[r] = 0;
if (strcmp(rdbuf, "hello") == 0)
printf("hello world!\n");
else if (rdbuf[0])
printf("{%s}\n", rdbuf);
}
assert(0); /* never arrive here */
}
运行
printf 现在将数据输出到 TTY[0,2],printl 只将数据输出到 TTY0!
OK ... 文件系统的课题到这里算是告一段落了,文件系统是操作系统中的重点,也是偏复杂的一个课题,下一节将进入内存管理的话题...