由买买提看人间百态

topics

全部话题 - 话题: dup2
(共0页)
f******k
发帖数: 26
1
int main(void)
{
int pid1,pid2,p1[2];
int p2[2];
int p3[2];int status;
pipe(p1);
pipe(p2);
if (fork() == 0) {
if((fork())==0) {
close(p1[0]);
dup2(p1[1],1);
close(p1[1]);
execlp("who","who",(char *)0);
}
else
{
if ((fork())==0){
close(p1[1]);
dup2(p1[0],0);
close(p1[0]);
close(p2[0]);
dup2(p2[1],1);
close(p2[1]);
execlp("sort","sort",(
T*******I
发帖数: 5138
2
来自主题: Statistics版 - How to Macro it in SAS?
I have a job to do. I can do in a regular way, but I want to know how to use
SAS Macro to achieve it. It should be simple. Can someone here help me?
Thanks so much! Have Baozi!!
proc sort data=Origianl nodupkey
out=Origianl_nodup
dupout=Dup1;
by ID;
RUN;
proc sort data=Dup1 nodupkey out=Dup1 dupout=Dup2; by ID; RUN;
proc sort data=Dup2 nodupkey out=Dup2 dupout=Dup3; by ID; RUN;
proc sort data=Dup3 nodupkey out=Dup3 dupout=Dup4; by ID; RUN;
proc sort data=Dup4 nodupk... 阅读全帖
B********s
发帖数: 3610
3
来自主题: Programming版 - 请问如何恢复正常的IO?
帮忙看看这段代码为什么不能把输出重定向到屏幕?
int main()
{
int fdout;
fdout = open("a",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
dup2(fdout,STDOUT_FILENO);
printf("hehe\n");
dup2(1,STDOUT_FILENO);
printf("haha\n");
return 0;
}
t****t
发帖数: 6806
4
来自主题: Programming版 - 请问如何恢复正常的IO?
他这段问题不在这里(因为stdout缺省是line buffered)
我假设他STDOUT_FILENO=1
那么他作的是
dup2(fdout, 1)
...
dup2(1,1)
...
这就好象写:
a1=a_fdout;
// use a1 as a_fdout
a1=a1;
// trying to recover previous a1
这显然不make sense...
c********x
发帖数: 84
5
来自主题: Programming版 - 请问如何恢复正常的IO?
Things look more complicated than they appears,
The call:
fid = dup2(fildes, fildes2);
is equivalent to:
close(fildes2);
fid = fcntl(fildes, F_DUPFD, fildes2);
When you "dup2(fdout,STDOUT_FILENO);" you close the stdout, no way
you can use 1 to reopen it.
p*****s
发帖数: 344
6
来自主题: Linux版 - [请教]文件输入 (转载)
just some thoughts, never tried myself
1. you call "exe with -i file1 -j file2 ..." first. Assume the return
process id is , ls /proc//fd/
you should file some fd nums ( 2. by default /proc//1, 2, 3 are your pipe fd
3. use gdb attach to the process
4. use dup2(,) to swap fd so that your exe can read
from pipe
f**k
发帖数: 906
7
来自主题: Linux版 - 100伪币答谢Linux/Unix编程问题
背景:项目里面用到一个外部函数,函数只接收一个文件名,调用后将长时间写大量数
据。
任务: 需要在函数写数据的同时,就接近即时读数据。
调用函数的时候,如果文件已经存在,就fail。所以不能事先建立个同文件名的Pipe。
也曾经使用过pipe,然后dup2 那个file descriptor。问题是在调用函数以后,取不到
函数内部的file descriptor。
100伪币答谢。
B********s
发帖数: 3610
8
来自主题: Programming版 - Linux shell编程的问题
示意性代码如下:
parse() 函数返回一个pipeline p,包括多个command. execPipe()执行pipeline中的命
令。
static void execPipe(Pipe p)
{
int fds[2];
pid_t pid,parent;

if ((pid = fork()) <0)
exit(-1);
else if (pid == 0) { //child
for (all but the last command in this pipe) {

else if ((parent = fork()) == -1)
exit(-1);
else if (parent) { //parent process
dup2(fds[1],STDOUT_FILENO);
close(fds[0]);
B********s
发帖数: 3610
9
来自主题: Programming版 - 请问如何恢复正常的IO?
我用dup2把程序的stdout指向了一个文件,那么我该如何使程序的stdout重新指回显示
器? thanks.
k****f
发帖数: 3794
10
来自主题: Programming版 - 请问如何恢复正常的IO?
换stdout的时候,需要fflush的。把之前的东西刷出去。
从来没有用过open/dup2
我就是用:fopen造个FILE*
FILE*old_stdout=stdout
stdout=那个FILE*
blahblah....
fflush(stdout);
stdout=old_stdout;
X****r
发帖数: 3557
11
来自主题: Programming版 - how can I get external program's result in C
#include
#include
int main() {
int fd[2];
pipe(fd);
if (fork()) {
close(1);
dup2(fd[1], 1);
execlp("ssh", "ssh", "serv2", "ls");
} else {
int len;
char buf[256];
while ((len = read(fd[0], buf, 256)) >= 0) {
/* Process piece of output from buf[0] to buf[len-1]. */
}
wait(0);
}
}

ls'.
f**k
发帖数: 906
12
来自主题: Programming版 - 100伪币答谢Linux/Unix编程问题 (转载)
【 以下文字转载自 Linux 讨论区 】
发信人: fork (笑叹词穷,古痴今狂 终成空), 信区: Linux
标 题: 100伪币答谢Linux/Unix编程问题
发信站: BBS 未名空间站 (Sat Mar 5 18:40:00 2011, 美东)
背景:项目里面用到一个外部函数,函数只接收一个文件名,调用后将长时间写大量数
据。
任务: 需要在函数写数据的同时,就接近即时读数据。
调用函数的时候,如果文件已经存在,就fail。所以不能事先建立个同文件名的Pipe。
也曾经使用过pipe,然后dup2 那个file descriptor。问题是在调用函数以后,取不到
函数内部的file descriptor。
100伪币答谢。
w*****n
发帖数: 94
13
来自主题: Unix版 - Re: redirect in fork() then exec()
The standard way is to emulate the shell, do fork-redirect-exec yourself.
if (!fork()) { /* child */
int fd = open("a.txt", O_WRONLY|O_CREAT|O_TRUNC);
dup2(fd, 1);
if (fd != 1) close(fd);
execlp("/usr/bin/ls", "ls", "-l", NULL);
}
else /* parent */ {
do what you want, no need to wait child
}
(共0页)