dup2

What happens when you call close() on a pipe file descriptor that was duplicated with dup2()?

两盒软妹~` 提交于 2021-02-17 04:52:34
问题 I have a question regarding file descriptors in Unix and C programming. Let's say I use pipe(fd) to get file descriptor 3 and 4 for the pipe ends, 3 connects to the read end and 4 to the write end. Now I use dup2(fd[write_end],1) to copy the descriptor of the write end (which was 4) to file descriptor 1 in my process. If I now do close(fd[write_end]) will it close descriptor 1 or descriptor 4? 回答1: After a successful call to dup2 , both file descriptors are valid. When you then call close(fd

Zombie process and fork

天大地大妈咪最大 提交于 2021-01-28 04:47:45
问题 i have a code like this... c = fork(); if(c==0) { close(fd[READ]); if (dup2(fd[WRITE],STDOUT_FILENO) != -1) execlp("ssh", "ssh", host, "ls" , NULL); _exit(1); } close(fd[WRITE]); fd[READ] and fd[WRITE] are pipe file descriptors. when i run it continuously, there are a lot of zombie processes when i use ps ax. How to rectify this? Is this because i am not using the parent to wait for the exit status of the child process... 回答1: If you have no intention to wait for your child processes, set the

Duplicate, but still use stdout

强颜欢笑 提交于 2020-08-07 06:26:08
问题 Is there some magic I can do with dup2 (or fcntl ), so that I redirect stdout to a file (i.e., anything written to descriptor 1 would go to a file), but then if I used some other mechanism, it would go to the terminal output? So loosely: int original_stdout; // some magic to save the original stdout int fd; open(fd, ...); dup2(fd, 1); write(1, ...); // goes to the file open on fd write(original_stdout, ...); // still goes to the terminal 回答1: A simple call to dup will perform the saving. Here

Restoring stdout after using dup

南楼画角 提交于 2020-07-23 03:04:13
问题 Using fork I created a child and in the child I'm executing the ls command using execl . To send the output to parent,I used pipe and dup . The parent then prints the output. The code gives the expected output, but when I tried to restore back the stdout which I saved initially in stdout_holder , nothing is printed on terminal (when I used printf("hello") or the execl statement below it). However after few observations, it is observed that hello is printed only when nothing is done after

Restoring stdout after using dup

删除回忆录丶 提交于 2020-07-23 03:03:08
问题 Using fork I created a child and in the child I'm executing the ls command using execl . To send the output to parent,I used pipe and dup . The parent then prints the output. The code gives the expected output, but when I tried to restore back the stdout which I saved initially in stdout_holder , nothing is printed on terminal (when I used printf("hello") or the execl statement below it). However after few observations, it is observed that hello is printed only when nothing is done after

Restoring stdout after using dup

天大地大妈咪最大 提交于 2020-07-23 03:02:26
问题 Using fork I created a child and in the child I'm executing the ls command using execl . To send the output to parent,I used pipe and dup . The parent then prints the output. The code gives the expected output, but when I tried to restore back the stdout which I saved initially in stdout_holder , nothing is printed on terminal (when I used printf("hello") or the execl statement below it). However after few observations, it is observed that hello is printed only when nothing is done after

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

大憨熊 提交于 2020-01-01 07:19:25
问题 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>

Redirect stdout from python for C calls

社会主义新天地 提交于 2019-12-28 17:41:17
问题 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

Receiving error code during output redirection using execvp

自闭症网瘾萝莉.ら 提交于 2019-12-25 16:26:16
问题 I am trying to redirect the output from ls to a file, in a shell I created in C. I type in: ls > junk and what I get out is: ls: cannot access >: No such file or directory Then if I use CTRL-D to exit the shell it prints the results of the ls command to the screen before exiting. I tried to use print statements to figure out where it is happening and no print statements get printed after: dup2(f, STDOUT_FILENO); Also tried dup2(f, 1); Code: pid = fork(); if(pid == 0) { // Get the arguments

Receiving error code during output redirection using execvp

感情迁移 提交于 2019-12-25 16:25:14
问题 I am trying to redirect the output from ls to a file, in a shell I created in C. I type in: ls > junk and what I get out is: ls: cannot access >: No such file or directory Then if I use CTRL-D to exit the shell it prints the results of the ls command to the screen before exiting. I tried to use print statements to figure out where it is happening and no print statements get printed after: dup2(f, STDOUT_FILENO); Also tried dup2(f, 1); Code: pid = fork(); if(pid == 0) { // Get the arguments