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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3866217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:35:06  来源:igfitidea点击:

How can I make the system call write() print to the screen?

clinuxsystem-calls

提问by andandandand

For my OS class I'm supposed to implement Linux's cat using only system calls (no printf)

对于我的操作系统类,我应该只使用系统调用(没有 printf)来实现 Linux 的 cat

Reading this referenceI found it being used to print to a file. I guess I should manipulate ofstream.

阅读这个参考我发现它被用来打印到一个文件。我想我应该操纵 ofstream。

In the example appears: ofstream outfile ("new.txt",ofstream::binary);

在示例中出现: 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?

编辑:我意识到这个 write() 是 iostream 库的一部分,这与 int write (int fd, char *buf , int size) 系统调用相同吗?

采纳答案by Jerry Coffin

Write to the file descriptor for standard output or standard error (1 and 2 respectively).

写入标准输出或标准错误的文件描述符(分别为 1 和 2)。

回答by Nikolai Fetissov

#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 (-1is to not print zero terminator).

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

回答by jschmier

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.

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

The first argument passed to write()is the file descriptor to write to. The symbolic constants STDERR_FILENO, STDIN_FILENO, and STDOUT_FILENOare respectively defined to 2, 0, and 1in unidtd.h. You want to write to either STDOUT_FILENOor STDERR_FILENO.

传递给的第一个参数write()是要写入的文件描述符。符号常量STDERR_FILENOSTDIN_FILENOSTDOUT_FILENO分别在unidtd.h 中定义为201。您想写入STDOUT_FILENOSTDERR_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.hor 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.

您也可以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);

回答by R.. GitHub STOP HELPING ICE

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

你的参考不正确。它是 C++ 的一部分,与您的任务无关。正确的参考是http://www.opengroup.org/onlinepubs/9699919799/functions/write.html

回答by Renold Wasnik

#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);