⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main_bak.c

📁 linux中管道的使用示例
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <error.h>#include <sys/types.h>int main(int argc, char **argv){    int fd_pipe[2] = {-1};    pid_t iChildProcessID = 0;    char szReadPipeBuffer[200] = {0};    char szCommandLine[200] = {0};    char *pszCommandLine = NULL;    if(argc == 1)    {        printf("Please input command:");        if((pszCommandLine = gets(szCommandLine)) == NULL)        {            perror("gets");            return EXIT_FAILURE;        }    }    else    {        pszCommandLine = argv[1];    }    if(pipe(fd_pipe) < 0)    {        perror("pipe()");    }    else    {        iChildProcessID = fork();        if(iChildProcessID < 0)        {            perror("fork()");        }        else if(iChildProcessID == 0)        {             sleep(1);             printf("In child process.\n");                          int iRet = read(fd_pipe[0],szReadPipeBuffer,200);             if(iRet)             {                 printf("Executing \"%s\" ...\n",szReadPipeBuffer);                 system(szReadPipeBuffer);             }             else             {                 perror("read from pipe.");             }        }        else        {             //int iRet = write(fd_pipe[1],argv[1],strlen(argv[1]));                  int iRet = write(fd_pipe[1],pszCommandLine,strlen(pszCommandLine));                  if(iRet < 0)             {                 perror("write to pipe.");             }                         sleep(2);             iChildProcessID = wait(NULL);        }//if(fork())    }//if(pipe(fd_pipe))...    close(fd_pipe[0]);    fd_pipe[0] = -1;    close(fd_pipe[1]);    fd_pipe[1] = -1;    return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -