冬天OS(三十二):printf & printl

--------------------------------------------------------

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 ... 文件系统的课题到这里算是告一段落了,文件系统是操作系统中的重点,也是偏复杂的一个课题,下一节将进入内存管理的话题...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sssnial-jz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值