【问题标题】:Using pipes on Linux to get the process of a user在 Linux 上使用管道获取用户的进程
【发布时间】:2021-09-09 17:58:54
【问题描述】:

我正在尝试编写一个代码,将参数 1 作为您想要查看其进程使用的用户的名称传递,我使用管道获取具有 getent 的用户,然后我将结果传递给 greep argv[1] 到在该结果中搜索参数的用户,然后将其传递给 ps -fu 以获取该用户的进程,但我只获取 user1(主用户)的进程,我不知道为什么,谢谢。

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define error(a) {perror(a); exit(1);};

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
       error("Incorrect number of arguments");
    }

    int pfd1[2], pfd2[2], pid;

    if(pipe(pfd1) != 0){error("First pipe");}

    switch(pid = fork())
    {
        case -1: error("fork");
        case 0:
        if(close(1)==-1){error ("close");}
        if(dup(pfd1[1]) != 1){error("dup");}
        close(pfd1[0]); close(pfd1[1]);
        execlp("getent", "getent", "passwd",NULL);
        error("getent");
    }

    close(pfd1[1]);

    if(pipe(pfd2) != 0){error("Second pipe");}

    switch(pid = fork())
    {
        case -1: error("fork");
        case 0:
        close(pfd2[0]);
        if(close(0)==-1){error("close");}
        if(dup(pfd1[0]) != 0){error("dup");}
        close(pfd1[0]);
        if(close(1)==-1){error("close");}
        if(dup(pfd2[1]) !=1){error("dup");}
        close(pfd2[1]);
        execlp("grep", "grep", argv[1], NULL);
        error("grep");
    }

    printf("Parent: grep(%d) process launched\n", pid);

    close(pfd1[0]);
    close(pfd2[1]);

    switch(pid = fork())
    {
        case -1: error("fork");
        case 0:
        if(close(0)==-1){error("close");}
        if(dup(pfd2[0]) !=0){error("dup")};
        execlp("ps", "ps", "-fu", NULL);
        error("ps");
    }

    close(pfd2[0]);

    while ((pid = wait(NULL)) != -1)
    {
        printf("Parent: %d process finished\n", pid);
    }
    return 0;
}

【问题讨论】:

    标签: c linux process pipe fork


    【解决方案1】:

    哇..这对我来说是高级别的..我和我一起工作过:

    ps 辅助 | grep -v root | grep nginx |剪切-d\ -f1 |排序 |独特的 ps辅助| grep -v root | grep 阿帕奇 |剪切-d\ -f1 |排序 |独特的

    感谢您的灵感..

    【讨论】:

      【解决方案2】:

      两个管道就够了,一个用于 ps aux,另一个用于 grep 用户过滤结果

      #include <sys/wait.h>
      #include <unistd.h>
      #include <stdio.h>
      #include <stdlib.h>
      #define error(a) {perror(a); exit(1);};
      
      int main(int argc, char *argv[])
      {
          if(argc != 2){error("Incorrect number of arguments")};
          int pfd[2], pid;
          char user[100];
          sprintf(user, "%s", argv[1]);
          if (pipe(pfd) == -1) error("pipe");
          printf("parent: pipe created, channels: READ=%d and WRITE=%d\n", pfd[0], pfd[1]);
          switch (pid = fork()) {
              case -1: error("fork");
              case 0: /* 1st child: who */
              printf("1st child process created\n");
              if (close(1) == -1) error("close");
              if (dup(pfd[1]) != 1) error("dup");
              close(pfd[0]); close(pfd[1]);
              execlp("ps", "ps", "aux", NULL);
              error("execlp");
          }
          printf("parent: ps(%d) process launched\n", pid);
          switch (pid = fork())
          {
              case -1: error("fork");
              case 0: /* 2nd child process: wc -l */
              printf("2nd child process created\n");
              if (close(0) == -1) error("close");
              if (dup(pfd[0]) != 0) error("grep");
              close(pfd[0]); close(pfd[1]);
              execlp("grep", "grep", user, NULL);
              error("execlp");
          }
          printf("parent: grep(%d) process launched\n", pid);
          close(pfd[0]); close(pfd[1]);
          while ((pid = wait(NULL)) != -1)
          {
              printf("parent: %d process finished\n", pid);
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        • 1970-01-01
        相关资源
        最近更新 更多