如何使系统调用 write() 打印到屏幕上?

发布于 2024-09-26 08:47:16 字数 393 浏览 8 评论 0原文

对于我的操作系统类,我应该仅使用系统调用(无 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

◇流星雨 2024-10-03 08:47:17

系统调用是Linux内核提供的一种服务。在 C 编程中,函数在 libc 中定义,它为许多系统调用提供了包装器。函数调用 write() 是这些系统之一来电。

传递给 write() 的第一个参数是文件要写入的描述符。符号常量STDERR_FILENOSTDIN_FILENOSTDOUT_FILENO分别定义为20unistd 中的 1 .h。您想要写入STDOUT_FILENOSTDERR_FILENO

const char msg[] = "Hello World!";
write(STDOUT_FILENO, msg, sizeof(msg)-1);

您也可以使用 syscall() 函数来执行通过指定 syscall.hunistd.h 中定义的函数编号来进行间接系统调用。使用这种方法,您可以保证您只使用系统调用。您可能会找到 Linux 系统调用快速参考(PDF 链接)很有帮助。

/* 4 is the system call number for write() */
const char msg[] = "Hello World!";
syscall(4, STDOUT_FILENO, msg, sizeof(msg)-1);

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 constants STDERR_FILENO, STDIN_FILENO, and STDOUT_FILENO are respectively defined to 2, 0, and 1 in unistd.h. You want to write to either STDOUT_FILENO or STDERR_FILENO.

const char msg[] = "Hello World!";
write(STDOUT_FILENO, msg, sizeof(msg)-1);

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.

/* 4 is the system call number for write() */
const char msg[] = "Hello World!";
syscall(4, STDOUT_FILENO, msg, sizeof(msg)-1);
暖风昔人 2024-10-03 08:47:17

不,std::ostream::writewrite 系统调用不同。它确实(几乎肯定)使用了 write 系统调用,至少在像 Linux 这样有这样功能的系统上,并且它通常会做非常相似的事情,但它仍然一个单独的东西。

然而,Linux 将为您的进程预先打开标准输入、标准输出和标准错误流。要写入屏幕,您通常会使用 write (即系统调用)写入流编号 1 或流编号 2 (分别是标准输出和标准错误)。

如果您需要写入屏幕,即使这些被重定向,您通常会打开一个到 /dev/tty 的流,并(再次)使用 write 来写入对它:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() { 
    char msg[] = "hello\n";

    int fd = open("/dev/tty", O_WRONLY);
    write(fd, msg, sizeof(msg));
    return 0;
}

No, std::ostream::write is not the same as the write system call. It does (almost certainly) use the write 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 number 1 or stream number 2 (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) use write to write to it:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() { 
    char msg[] = "hello\n";

    int fd = open("/dev/tty", O_WRONLY);
    write(fd, msg, sizeof(msg));
    return 0;
}
苏佲洛 2024-10-03 08:47:17
#include <unistd.h>
/* ... */
const char msg[] = "Hello world";
write( STDOUT_FILENO, msg, sizeof( msg ) - 1 );

第一个参数是 STDOUT 的文件描述符(通常为 1),第二个参数是要写入的缓冲区,第三个参数是缓冲区中文本的大小(-1 是不打印零终止符)。

#include <unistd.h>
/* ... */
const char msg[] = "Hello world";
write( STDOUT_FILENO, msg, sizeof( msg ) - 1 );

First argument is the file descriptor for STDOUT (usually 1), 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).

快乐很简单 2024-10-03 08:47:17
#define _GNU_SOURCE         /* See feature_test_macros(7) */    
#include <unistd.h> // For open, close, read, write, fsync
#include <sys/syscall.h>  //For SYSCALL id __NR_xxx

//Method 1 : API    
write(1,"Writing via API\n",\
        strlen("Writing via API\n") ); 
fsync(1);
//Method 2  : Via syscall id
const char msg[] = "Hello World! via Syscall\n";
syscall(__NR_write, STDOUT_FILENO, msg, sizeof(msg)-1);     
syscall(__NR_fsync, STDOUT_FILENO );    // fsync(STDOUT_FILENO);
#define _GNU_SOURCE         /* See feature_test_macros(7) */    
#include <unistd.h> // For open, close, read, write, fsync
#include <sys/syscall.h>  //For SYSCALL id __NR_xxx

//Method 1 : API    
write(1,"Writing via API\n",\
        strlen("Writing via API\n") ); 
fsync(1);
//Method 2  : Via syscall id
const char msg[] = "Hello World! via Syscall\n";
syscall(__NR_write, STDOUT_FILENO, msg, sizeof(msg)-1);     
syscall(__NR_fsync, STDOUT_FILENO );    // fsync(STDOUT_FILENO);
假装爱人 2024-10-03 08:47:17

您的参考不正确。它是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文