linux下通过dup2标准输出重定向查看系统进程方法(也是popen函数实现的方法)
2009-08-28 17:04
981 查看
此程序等同于在linux下运行ps -A命令,popen函数正是用这种方式实现的。
程序代码:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int mysystem(char* cmdstring, char* buf, int len)
{
int fd[2];
pid_t pid;
int n, count;
memset(buf, 0, len);
if (pipe(fd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
else if (pid > 0) /* parent process */
{ close(fd[1]); /* close write end */
count = 0;
while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
count += n;
close(fd[0]);
if (waitpid(pid, NULL, 0) > 0)
return -1;
}
else /* child process */
{ close(fd[0]); /* close read end */
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{ return -1;
}
close(fd[1]);
}
if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
char buf[8192];
char *cmd="ps -A";
mysystem(cmd,buf,8192);
printf("%s", buf);
return 0;
}
运行:
[root@localhost ~]# ./mysystem
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
4 ? 00:00:00 watchdog/0
5 ? 00:00:00 events/0
6 ? 00:00:00 khelper
7 ? 00:00:00 kthread
10 ? 00:00:00 kblockd/0
11 ? 00:00:00 kacpid
72 ? 00:00:00 cqueue/0
75 ? 00:00:00 khubd
77 ? 00:00:00 kseriod
135 ? 00:00:00 pdflush
136 ? 00:00:00 pdflush
137 ? 00:00:00 kswapd0
138 ? 00:00:00 aio/0
286 ? 00:00:00 kpsmoused
316 ? 00:00:00 scsi_eh_0
321 ? 00:00:00 kmirrord
328 ? 00:00:00 kjournald
349 ? 00:00:00 kauditd
375 ? 00:00:00 udevd
710 ? 00:00:00 kgameportd
1266 ? 00:00:00 kmpathd/0
1289 ? 00:00:00 kjournald
1295 ? 00:00:00 vmhgfs
1297 ? 00:00:00 kjournald
程序代码:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
int mysystem(char* cmdstring, char* buf, int len)
{
int fd[2];
pid_t pid;
int n, count;
memset(buf, 0, len);
if (pipe(fd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
else if (pid > 0) /* parent process */
{ close(fd[1]); /* close write end */
count = 0;
while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
count += n;
close(fd[0]);
if (waitpid(pid, NULL, 0) > 0)
return -1;
}
else /* child process */
{ close(fd[0]); /* close read end */
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{ return -1;
}
close(fd[1]);
}
if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
char buf[8192];
char *cmd="ps -A";
mysystem(cmd,buf,8192);
printf("%s", buf);
return 0;
}
运行:
[root@localhost ~]# ./mysystem
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
4 ? 00:00:00 watchdog/0
5 ? 00:00:00 events/0
6 ? 00:00:00 khelper
7 ? 00:00:00 kthread
10 ? 00:00:00 kblockd/0
11 ? 00:00:00 kacpid
72 ? 00:00:00 cqueue/0
75 ? 00:00:00 khubd
77 ? 00:00:00 kseriod
135 ? 00:00:00 pdflush
136 ? 00:00:00 pdflush
137 ? 00:00:00 kswapd0
138 ? 00:00:00 aio/0
286 ? 00:00:00 kpsmoused
316 ? 00:00:00 scsi_eh_0
321 ? 00:00:00 kmirrord
328 ? 00:00:00 kjournald
349 ? 00:00:00 kauditd
375 ? 00:00:00 udevd
710 ? 00:00:00 kgameportd
1266 ? 00:00:00 kmpathd/0
1289 ? 00:00:00 kjournald
1295 ? 00:00:00 vmhgfs
1297 ? 00:00:00 kjournald
相关文章推荐
- linux下通过字符模块实现类似ps命令的查看系统进程方法
- popen 重定向标准错误输出到管道流的方法
- linux下通过go语言获得系统进程cpu使用情况的方法
- Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
- 使用dup2重定向了标准输出后,使用什么方法恢复对终端的输出?? http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=359433&page=
- 查看linux文件系统块大小的实现方法
- 【Linux调试技巧----标准输出重定向到文件】dup2和dup的妙用
- [linux]centos下通过pid查看进程的绝对路径的方法
- Linux系统下输出某进程内存占用信息的c程序实现
- APUE之dup,dup2函数重定向标准输出实例
- dup2函数运用之重定向标准输出
- [学习笔记]通过open函数改变标准输出的方法
- VC下通过系统快照实现进程管理的方法
- Linux系统教程 标准输入/输出和重定向
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- linux下通过调用fork函数实现多进程
- linux系统快速查看进程pid的方法
- 重定向已经运行进程的标准输出到文件的办法(通过ptrace注入代码到其他进程并运行)
- 重定向已经运行进程的标准输出到文件的办法(通过ptrace注入代码到其他进程并运行)
- Linux系统教程 标准输入/输出和重定向