如何使系统调用 write() 打印到屏幕上?
对于我的操作系统类,我应该仅使用系统调用(无 printf)来实现 Linux 的 cat
阅读 此参考 我发现它被用来打印到文件。我想我应该操纵ofstream。
在示例中出现: ofstream outfile ("new.txt",ofstream::binary);
如何让它写入屏幕?
编辑:我意识到 write() 是 iostream 库的一部分,这与 int write (int fd, char *buf , int size) 系统调用相同吗?
For my OS class I'm supposed to implement Linux's cat using only system calls (no printf)
Reading this reference I found it being used to print to a file. I guess I should manipulate ofstream.
In the example appears: ofstream outfile ("new.txt",ofstream::binary);
How can I make it write to the screen?
EDIT: I realized this write() is part of iostream library, is this the same as the int write (int fd, char *buf , int size) system call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
系统调用是Linux内核提供的一种服务。在 C 编程中,函数在 libc 中定义,它为许多系统调用提供了包装器。函数调用
write()
是这些系统之一来电。传递给
write()
的第一个参数是文件要写入的描述符。符号常量STDERR_FILENO
、STDIN_FILENO
、STDOUT_FILENO
分别定义为2、0 和 unistd 中的 1 .h。您想要写入STDOUT_FILENO或STDERR_FILENO。您也可以使用
syscall()
函数来执行通过指定 syscall.h 或 unistd.h 中定义的函数编号来进行间接系统调用。使用这种方法,您可以保证您只使用系统调用。您可能会找到 Linux 系统调用快速参考(PDF 链接)很有帮助。A system call is a service provided by Linux kernel. In C programming, functions are defined in libc which provide a wrapper for many system calls. The function call
write()
is one of these system calls.The first argument passed to
write()
is the file descriptor to write to. The symbolic constantsSTDERR_FILENO
,STDIN_FILENO
, andSTDOUT_FILENO
are respectively defined to 2, 0, and 1 in unistd.h. You want to write to either STDOUT_FILENO or STDERR_FILENO.You can alternatively use the
syscall()
function to perform an indirrect system call by specifying the function number defined in syscall.h or unistd.h. Using this method, you can guarantee that you are only using system calls. You may find The Linux System Call Quick Refernence (PDF Link) to be helpful.不,
std::ostream::write
与write
系统调用不同。它确实(几乎肯定)使用了write
系统调用,至少在像 Linux 这样有这样功能的系统上,并且它通常会做非常相似的事情,但它仍然一个单独的东西。然而,Linux 将为您的进程预先打开标准输入、标准输出和标准错误流。要写入屏幕,您通常会使用
write
(即系统调用)写入流编号1
或流编号2
(分别是标准输出和标准错误)。如果您需要写入屏幕,即使这些被重定向,您通常会打开一个到
/dev/tty
的流,并(再次)使用write
来写入对它:No,
std::ostream::write
is not the same as thewrite
system call. It does (almost certainly) use thewrite
system call, at least on a system like Linux that has such a thing, and it normally does pretty similar things, but it's still a separate thing of its own.Linux will, however, pre-open standard input, standard output and standard error streams for your process. To write to the screen, you'd normally use
write
(i.e., the one that is a system call) to write to stream number1
or stream number2
(which are standard output and standard error respectively).If you need to write to the screen even if those are re-directed, you'd normally open a stream to
/dev/tty
and (again) usewrite
to write to it:第一个参数是
STDOUT
的文件描述符(通常为1
),第二个参数是要写入的缓冲区,第三个参数是缓冲区中文本的大小(-1
是不打印零终止符)。First argument is the file descriptor for
STDOUT
(usually1
), the second is the buffer to write from, third is the size of the text in the buffer (-1
is to not print zero terminator).您的参考不正确。它是 C++ 的一部分,与您的作业无关。正确的参考是 http://www.opengroup.org/onlinepubs/9699919799/functions /write.html
Your reference is incorrect. It's part of C++ and has nothing to do with your assignment. The correct reference is http://www.opengroup.org/onlinepubs/9699919799/functions/write.html