dup2

dup2() and exec()

巧了我就是萌 提交于 2019-12-23 09:51:32
问题 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> int main( int argc, char **argv) { int pfds[ 2], i; size_t pbytrd; pid_t childpid; char buffer[ 200]; pipe( pfds); if( ( childpid = fork() ) == -1) { perror( "fork error: "); exit( 1); } else if( childpid == 0) { close( pfds[ 0]); dup2( pfds[1], 1); close( pfds[ 1]); for( i = 0; i < 10; i++) printf("Hello..."); execlp( "xterm","xterm","-e","./sample_a", (char *) 0); exit( 0); } else { close( pfds

Race condition when using dup2

人盡茶涼 提交于 2019-12-20 10:47:09
问题 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 ? 回答1: 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

Does this multiple pipes code in C makes sense?

独自空忆成欢 提交于 2019-12-18 05:22:08
问题 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

Toy shell not piping correctly

馋奶兔 提交于 2019-12-17 14:57:53
问题 I'm not going to lie. This is a homework question. However, as far as I'm concerned, the points are gone baby gone. Right now, I'm just looking for an answer, because I -think- I might be insane. The goal of this program is to execute the command ps -A | grep (inputstring) | wc -l in a way similar to how the shell does it. So, I spawn the processes, and have them wait on each other. The newest process, the great-grandchild, execlp("ps","ps","-A",NULL) which replaces itself with the ps -A

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

老子叫甜甜 提交于 2019-12-17 08:54:15
问题 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[+

python: multiprocessing.Pipe and redirecting stdout

自古美人都是妖i 提交于 2019-12-11 22:04:56
问题 I am using multiprocessing package to spawn a second process from which I would like to redirect stdout and stderr into the first process. I am using multiprocessing.Pipe object: dup2(output_pipe.fileno(), 1) Where output_pipe is an instance of multiprocessing.Pipe . However, when I try to read on the other end, it just hangs. I tried reading using Pipe.recv_bytes with a limit, but that raises an OSError . Is this possible at all or should I just switch to some lower level pipe functions? 回答1

Redirecting stdout to file after a fork()

余生颓废 提交于 2019-12-10 18:49:05
问题 I'm working on a simple shell, but right now I am just trying to understand redirection. I'm just hard coding an ls command and trying to write it to a file for now. Currently, the ls runs, and the output file is created, but the output still goes to stdout and the file is blank. I'm confused as to why. Thanks in advance. Here is my code: int main() { int ls_pid; /* The new process id for ls*/ char *const ls_params[] = {"/bin/ls", NULL}; /* for ls */ int file; /* file for writing */ /* Open

when using different file descripter, why is the result different? (system programming)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 00:52:23
问题 I am studying about file descripter and realized that if I use dup2() function, the result will be different. The first snippet ... int main(void){ char buf1[BUFSIZ] = "I am low\n"; printf("i am high\n"); write(1, buf1, strlen(buf1)); write(1, buf1, strlen(buf1)); write(1, buf1, strlen(buf1)); return 0; } ... produces the following result: i am high i am low i am low i am low But the second snippet ... int main(void){ int fd = open("dupout",O_CREAT | O_WRONLY, 0655); char buf1[BUFSIZ] = "I am

Creating a shell in C. How would I implement input and output redirection?

血红的双手。 提交于 2019-12-04 22:38:54
I'm creating a shell in C, and I need help implementing input and output redirection. When I try to create a file using ">" I get an error message saying the file does not exist. When I try to do something like ls > test.txt; it won't create a new file. I updated the code with the suggestions provided to me, but now I got different errors. However, a new file is still not created for the output redirection. This is my full code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #define MAX_BUF 160 #define MAX

Can someone explain what dup() in C does?

可紊 提交于 2019-12-03 02:45:59
问题 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