file-descriptor

Specification of file descriptors

我的梦境 提交于 2020-06-09 05:19:04
问题 I am trying to understand flags and modes of file descriptors. The man page for fcntl - manipulate file descriptor int fcntl(int fd, int cmd); states: File descriptor flags The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC,... File status flags Each open file description has certain associated status flags, initialized by open(2)... The file status flags and their semantics are described in open(2). Given that

Specification of file descriptors

主宰稳场 提交于 2020-06-09 05:17:46
问题 I am trying to understand flags and modes of file descriptors. The man page for fcntl - manipulate file descriptor int fcntl(int fd, int cmd); states: File descriptor flags The following commands manipulate the flags associated with a file descriptor. Currently, only one such flag is defined: FD_CLOEXEC,... File status flags Each open file description has certain associated status flags, initialized by open(2)... The file status flags and their semantics are described in open(2). Given that

Safety check prior to using fdopen

限于喜欢 提交于 2020-06-07 07:22:26
问题 Motivated by Smart-write to arbitrary file descriptor from C/C++, I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { int nbytes; const int fd = 3; FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); fclose(fp); return 0; } Should I perform some kind of

How to get the mode of a file descriptor?

送分小仙女□ 提交于 2020-06-07 06:50:47
问题 I mean to use fdopen FILE *fdopen(int fd, const char *mode); In man pages, it is stated that "The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor." So I have to first know the mode of fd (which I guess is an int ) to choose an appropriate const char *mode for the stream. I understand I should use fcntl int fcntl(int fd, int cmd); to "manipulate file descriptor" (in the following, I quote from this official source).

How to make sense of O_RDONLY = 0?

你离开我真会死。 提交于 2020-05-23 16:25:26
问题 I am dealing with file status flags. Among test I performed, I found #include <stdio.h> #include "fcntl.h" int main() { const int flag = O_RDONLY; printf( "*** Flag O_RDONLY = %5d\n", flag); return 0; } produces this output *** Flag O_RDONLY = 0 which is fully consistent with #define O_RDONLY 00 from fcntl-linux.h . How can the value zero be used as a flag? I expect an "atomic" flag to be 2^n ( n>=1 ), and "composite" flags (like O_ACCMODE ) to be simply the sum of several atomic flags (which

Pipe commands with fork and dup2

≯℡__Kan透↙ 提交于 2020-04-18 03:57:11
问题 I wrote the following code in order to pipe two commands: #include <stdlib.h> #include <unistd.h> char *program_1[3] = {"/bin/cat", "/dev/random", NULL}; char *program_2[2] = {"/bin/ls", NULL}; char *program_3[2] = {"/usr/bin/sort", NULL}; int main(void) { int fd[2]; int pid; pipe(fd); if ((pid = fork()) == 0) //Child process { dup2(fd[1], STDOUT_FILENO); close(fd[0]); execve(program_3[0], program_3, NULL); } else if (pid > 0) //Parent process { dup2(fd[0], STDIN_FILENO); close(fd[1]); execve

How do I get the file pointer that is being referenced by a file descriptor?

不羁岁月 提交于 2020-03-04 18:47:39
问题 I am trying to use dup2 to communicate between parent and child in the following code. #include <stdio.h> #include <unistd.h> void main(int argv, char *argc) { int testpipe[2]; pipe(testpipe); int PID = fork(); if (PID == 0) { dup2(testpipe[0], 0); close(testpipe[1]); execl("./multby", "multby", "3", NULL); close(testpipe[0]); close(0); } else { dup2(testpipe[1], 1); close(testpipe[0]); printf("5"); fclose(stdout); close(testpipe[1]); wait(NULL); } } I understand that fclose is needed to

Why does closing file descriptors after fork affect the child process?

删除回忆录丶 提交于 2020-02-21 13:00:20
问题 I want to run programs in linux by a button click an therefore I wrote a function execute : void execute(const char* program_call, const char* param ) { pid_t child = vfork(); if(child == 0) // child process { int child_pid = getpid(); char *args[2]; // arguments for exec args[0] = (char*)program_call; // first argument is program_call args[1] = (char*)param; // close all opened file descriptors: const char* prefix = "/proc/"; const char* suffix = "/fd/"; char child_proc_dir[16]; sprintf

Why does closing file descriptors after fork affect the child process?

萝らか妹 提交于 2020-02-21 12:59:55
问题 I want to run programs in linux by a button click an therefore I wrote a function execute : void execute(const char* program_call, const char* param ) { pid_t child = vfork(); if(child == 0) // child process { int child_pid = getpid(); char *args[2]; // arguments for exec args[0] = (char*)program_call; // first argument is program_call args[1] = (char*)param; // close all opened file descriptors: const char* prefix = "/proc/"; const char* suffix = "/fd/"; char child_proc_dir[16]; sprintf

How to force a file descriptor to buffer my output

我与影子孤独终老i 提交于 2020-01-16 04:05:48
问题 Lots of people want to switch off buffering on their file descriptors. I want the reverse: I deliberately want to configure a file descriptor to buffer, say, 1K of data before writing to disk. The reason is that I'm writing a unit test for a "flush" function of a C++ class. To test that it's working I want to write some data, check the size of the file on disk, then flush, then check that the size has grown. But in practice, by the time I do the first file size check the data has already been