weixin_41679306 2019-10-11 16:11 采纳率: 0%
浏览 384

C++实现ps -A | grep xxx | wc -l 的功能

求各位大佬帮忙,有可采纳答案 可以追加悬赏
需要用到这些系统函数完成

pid_t fork( void ); creates a child process that differs from the parent process only in terms of their process IDs.
int execlp( const char *file, const char *arg, ..., (char *)0 ); replaces the current process image with a new process image that will be loaded from file. The first argument arg must be the same as file.
int pipe( int filedes[2] ); creates a pair of file descriptors (which point to a pipe structure), and places them in the array pointed to by filedes. filedes[0] is for reading data from the pipe, filedes[1] is for writing data to the pipe.
int dup2( int oldfd, int newfd ); creates in newfd a copy of the file descriptor oldfd.
pid_t wait( int *status ); waits for process termination
int close( int fd ); closes a file descriptor.

只使用上面列出的系统调用。不要使用系统系统调用。模拟shell如何执行“ps -A | grep argv[1] | wc -l”。换句话说,您的父进程生成一个子进程,这个子进程生成一个曾孙进程,这个曾孙进程又生成一个曾孙进程。如下:

图片说明

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2019-10-12 09:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备