美文网首页
系统 第四天 进程的创建,结束,无名管道

系统 第四天 进程的创建,结束,无名管道

作者: ie大博 | 来源:发表于2016-12-02 11:13 被阅读0次

exec 创建进程

  • 然后并不理解有什么用,启动另外一个进程,本身进程结束
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
    int ret =-1;
//  ret = execl("/bin/ls","ls","/home/alex","-l",NULL);
//  ret = execl("while","while",NULL);
    //第一个参数是这个命令在哪,后面的就是命令执行的方式
    char *const argv[] ={"gedit","Dircopy.c",NULL};
    ret = execv("/usr/bin/gedit",argv);
//  printf("errno = %d\n",errno);
//  printf("error:%s\n",strerror(errno));   
    if(-1 == ret)
    {
        perror("execlp");
        return -1;
    }
    return 0;
}

结束进程

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void fun1()
{
    printf("fun1,,,");
}
void fun2()
{
    printf("fun2,,,");
}
void fun3()
{
    printf("fun3,,,");
}
int main()
{
    atexit(fun1);
    atexit(fun2);
    atexit(fun3);
// _exit(-1);//直接结束进程
//  exit(-1);//自杀,不会出现打印helloworld。
    abort();//中间放弃,核心已转储:非正常结束
    printf("helloworld\n");//当程序遇到return 0;也没有立即结束,
    //会做一下善后工作,然后进入内核层,atexit会被执行,执行的顺序
    //和注册相反
    return 0;
}

管道

  • grep抓取
  • 要想实现在一个管道实现交流,就是说和读进行,不能都说,或者都读
  • 通过pipe建立的管道叫做无名管道
#include <unistd.h>
#include<stdio.h>
#include<string.h>
int main()
{
    int pipefd[2] = {-1};
//
    int ret = -1;
    ret = pipe(pipefd);
    if(-1==ret)
    {
        perror("pipe");
        return -1;
    }
    pid_t pid = -1;
    pid = fork();

    if(pid > 0)//父进程
    {
        char caBuf[64]={'\0'};
        int iSign = 0;

        while(1)
        {
            memset(caBuf,'\0',sizeof(caBuf));
            if(0 == iSign)
            {
                printf("father write data:\n");
                scanf("%s",caBuf);
                write(pipefd[1],caBuf,strlen(caBuf));
                iSign = 1;
            }
            else if(1==iSign)
            {
                read(pipefd[0],caBuf,sizeof(caBuf));
                printf("child says:%s\n",caBuf);
                iSign = 0;
            }
            sleep(1);
        }
    }
    else if(0==pid)//子进程
    {
        char caBuf[64]={'\0'};
        int iSign = 0;

        while(1)
        {
            memset(caBuf,'\0',sizeof(caBuf));
            if(0 == iSign)
            {
                read(pipefd[0],caBuf,sizeof(caBuf));
                printf("父亲说:%s\n",caBuf);
                iSign = 1;
            }
            else if(1==iSign)
            {
                printf("孩子说:\n");
                scanf("%s",caBuf);
                write(pipefd[1],caBuf,strlen(caBuf));
                iSign = 0;
            }
            sleep(1);
        }

    }
    else if(-1 == pid)
    {
        perror("fork");
        return -1;
    }

    return 0;
}

相关文章

  • 系统 第四天 进程的创建,结束,无名管道

    exec 创建进程 然后并不理解有什么用,启动另外一个进程,本身进程结束 结束进程 管道 grep抓取 要想实现在...

  • 14.进程间通信:管道

    1. 管道,有名管道和无名管道。 1.1 无名管道主要用于父子进程或者兄弟关系的进程间的通信。通过pipe创建无名...

  • 无名管道

    无名管道是父子进程通信的手段,没有关系的进程是不能使用无名管道的 int fd[2];if (pipe(fd) =...

  • 系统编程-文件操作5

    作业 通过无名管道,让两个子进程间完成相互通信工作 命名管道 创建一个命名管道 在命名管道里插入数据 首先在一个终...

  • 四、进程操作

    创建、结束进程创建进程使用fork系统调用,结束进程使用exit。理解fork调用的意义,理解vfork调用的意义...

  • 进程间通信之有名管道

    1 有名管道 有名管道与无名管道的通信机制差不多,唯一不同在于,有名管道需要在文件系统中创建指定名称的管道文件,用...

  • Linux 进程间通信

    进程间通信 一 进程间通信 -- 管道 mkfifo test 创建管道文件 匿名管道和命名管道:匿名管道:匿名管...

  • Linux 进程之间的通信方式

    linux使用的进程间通信方式 管道(pipe)、流管道(s_pipe)、无名管道(FIFO)、 套接字 sock...

  • Android 进程通信--Binder机制

    一、起源——为什么在Android中使用binder通信机制? linux中的进程通信 管道包含无名管道和有名管道...

  • 系统编程(4)

    哲学家就餐问题: 进程间通信 无名管道,需要亲子进程来实现 利用有名管道产生不同窗口的本地聊天 共享内存间通信,使...

网友评论

      本文标题:系统 第四天 进程的创建,结束,无名管道

      本文链接:https://www.haomeiwen.com/subject/pwkxmttx.html