dup2

Race condition when using dup2

牧云@^-^@ 提交于 2019-12-02 23:20:34
This manpage for the dup2 system call says: EBUSY (Linux only) This may be returned by dup2() or dup3() during a race condition with open(2) and dup(). What race condition does it talk about and what should I do if dup2 gives EBUSY error? Should I retry like in the case of EINTR ? There is an explanation in fs/file.c , do_dup2() : /* * We need to detect attempts to do dup2() over allocated but still * not finished descriptor. NB: OpenBSD avoids that at the price of * extra work in their equivalent of fget() - they insert struct * file immediately after grabbing descriptor, mark it larval if *

Can someone explain what dup() in C does?

余生颓废 提交于 2019-12-02 16:20:58
I know that dup, dup2, dup3 " create a copy of the file descriptor oldfd "(from man pages). However I can't digest it. As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier to just fd=fd2; Whenever we want to duplicate a file descriptor? And something else.. dup() uses the lowest-numbered unused descriptor for the new descriptor. Does that mean that it can also take as value stdin , stdout or stderr if we assume that we have close() -ed one of those? Just wanted to respond to myself on the second question after

Redirect FROM stderr to another file descriptor

六月ゝ 毕业季﹏ 提交于 2019-11-29 16:01:25
My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else. Here is my first attempt: bool redirect_stderr (int fd) { return dup2 (2, fd) > 0; } Here, fd was successfully obtained from open("/foo/bar",O_APPEND|O_CREAT) After this function returns true, std::cerr<<"blah" goes to the terminal and not to the file. What am I doing wrong? Thanks. UPDATE Thanks, larsmans, but I'm not there yet... void redirect_stderr_to (const char * output_file) { int fd = open (output_file, O_APPEND | O_CREAT,

Does this multiple pipes code in C makes sense?

荒凉一梦 提交于 2019-11-29 08:39:40
I've created a question about this a few days . My solution is something in the lines of what was suggested in the accepted answer. However, a friend of mine came up with the following solution: Please note that the code has been updated a few times (check the edit revisions) to reflect the suggestions in the answers below. If you intend to give a new answer, please do so with this new code in mind and not the old one which had lots of problems. #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char *argv[]){ int fd[2], i, aux, std0, std1; do {

Pipes, dup2 and exec()

血红的双手。 提交于 2019-11-29 07:59:09
I have to write a shell that can run pipes. For example commands like ls -l | wc -l ". I have successfully parsed the command given by the user as below: "ls" = firstcmd "-l" = frsarg "wc" = scmd "-l" = secarg Now I have to use two forks since the commands are two and a pipe. The code block that I wrote to exec the command is the following: pid_t pid; int fd[2]; pipe(fd); pid = fork(); if(pid==0) { dup2(fd[WRITE_END], STDOUT_FILENO); close(fd[READ_END]); execlp(firstcmd, firstcmd, frsarg, (char*) NULL); } else { pid=fork(); if(pid==0) { dup2(fd[READ_END], STDIN_FILENO); close(fd[WRITE_END]);

Redirect stdout from python for C calls

≡放荡痞女 提交于 2019-11-28 11:13:14
This is a follow up question from here specifically concerning its answer . From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and ran into this answer which almost brings me all the way to the solution. The critical part of this answer is this code segment: 1. def redirect_stdout(): 2. print "Redirecting stdout" 3. sys.stdout.flush() # <--- important when redirecting to files 4. newstdout = os.dup(1) 5. devnull = os.open('/dev/null', os.O_WRONLY) 6. os.dup2(devnull, 1) 7. os

Redirect FROM stderr to another file descriptor

大城市里の小女人 提交于 2019-11-28 09:27:23
问题 My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else. Here is my first attempt: bool redirect_stderr (int fd) { return dup2 (2, fd) > 0; } Here, fd was successfully obtained from open("/foo/bar",O_APPEND|O_CREAT) After this function returns true, std::cerr<<"blah" goes to the terminal and not to the file. What am I doing wrong? Thanks. UPDATE Thanks, larsmans, but I'm not there yet..

Using dup2 for piping

不打扰是莪最后的温柔 提交于 2019-11-27 11:23:22
How do I use dup2 to perform the following command? ls -al | grep alpha | more theprole A Little example with the first two commands. You need to create a pipe with the pipe() function that will go between ls and grep and other pipe between grep and more. What dup2 does is copy a file descriptor into another. Pipe works by connecting the input in fd[0] to the output of fd[1]. You should read the man pages of pipe and dup2. I may try and simplify the example later if you have some other doubts. #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h>

Having trouble with fork(), pipe(), dup2() and exec() in C

拟墨画扇 提交于 2019-11-27 07:01:44
Here's my code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <wait.h> #include <readline/readline.h> #define NUMPIPES 2 int main(int argc, char *argv[]) { char *bBuffer, *sPtr, *aPtr = NULL, *pipeComms[NUMPIPES], *cmdArgs[10]; int fdPipe[2], pCount, aCount, i, status, lPids[NUMPIPES]; pid_t pid; pipe(fdPipe); while(1) { bBuffer = readline("Shell> "); if(!strcasecmp(bBuffer, "exit")) { return 0; } sPtr = bBuffer; pCount = -1; do { aPtr = strsep(&sPtr, "|"); pipeComms[++pCount] = aPtr; } while(aPtr); for(i = 0; i < pCount; i++) { aCount = -1; do { aPtr = strsep(&pipeComms

Can popen() make bidirectional pipes like pipe() + fork()?

大兔子大兔子 提交于 2019-11-26 22:09:06
I'm implementing piping on a simulated file system in C++ (with mostly C). It needs to run commands in the host shell but perform the piping itself on the simulated file system. I could achieve this with the pipe() , fork() , and system() system calls, but I'd prefer to use popen() (which handles creating a pipe, forking a process, and passing a command to the shell). This may not be possible because (I think) I need to be able to write from the parent process of the pipe, read on the child process end, write the output back from the child, and finally read that output from the parent. The man