在 Linux 上使用管道获取用户的进程
Using pipes on Linux to get the process of a user
我正在尝试制作一个代码,将参数 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;
}
哇..这对我来说是高水平的..我和我一起工作过:
ps 辅助 | grep -v 根 | grep nginx |剪切-d\-f1 |排序 |独特的
ps 辅助 | grep -v 根 | grep 阿帕奇 |剪切-d\-f1 |排序 | uniq
感谢您的启发..
两个管道就足够了,一个用于 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;
}
我正在尝试制作一个代码,将参数 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;
}
哇..这对我来说是高水平的..我和我一起工作过:
ps 辅助 | grep -v 根 | grep nginx |剪切-d\-f1 |排序 |独特的 ps 辅助 | grep -v 根 | grep 阿帕奇 |剪切-d\-f1 |排序 | uniq
感谢您的启发..
两个管道就足够了,一个用于 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;
}