国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[Linux]進(jìn)程間通信--管道

這篇具有很好參考價(jià)值的文章主要介紹了[Linux]進(jìn)程間通信--管道。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

[Linux]進(jìn)程間通信–管道

進(jìn)程間通信的目的

  • 數(shù)據(jù)傳輸:一個(gè)進(jìn)程需要將它的數(shù)據(jù)發(fā)送給另一個(gè)進(jìn)程 。
  • 資源共享:多個(gè)進(jìn)程之間共享同樣的資源。
  • 通知事件:一個(gè)進(jìn)程需要向另一個(gè)或一組進(jìn)程發(fā)送消息,通知它(它們)發(fā)生了某種事件(如進(jìn)程終止時(shí)要通知父進(jìn)程)。
  • 進(jìn)程控制:有些進(jìn)程希望完全控制另一個(gè)進(jìn)程的執(zhí)行(如Debug進(jìn)程),此時(shí)控制進(jìn)程希望能夠攔截另一個(gè)進(jìn)程的所有陷入和異常,并能夠及時(shí)知道它的狀態(tài)改變。

實(shí)現(xiàn)進(jìn)程間通信的原理

進(jìn)程是具有獨(dú)立性的,一個(gè)進(jìn)程是無(wú)法看到另一個(gè)進(jìn)程的代碼和數(shù)據(jù)的,為了讓進(jìn)程間通信,要做的工作就是讓不同的進(jìn)程看到同一份“資源”。

任何進(jìn)程通信手段需要解決的問(wèn)題如下:

  • 讓不同的進(jìn)程看到同一份“資源”
  • 讓一方進(jìn)行讀取,另一方進(jìn)行寫(xiě)入

不同的進(jìn)程間通信手段本質(zhì)的區(qū)別就是讓不同的進(jìn)程看到同一份“資源”的方式不同。

匿名管道

匿名管道是一種以文件為媒介的通信方式,匿名管道是一個(gè)內(nèi)存級(jí)別的文件,擁有和普通文件一樣的緩沖區(qū),但是操作系統(tǒng)不會(huì)將緩沖區(qū)刷新至外設(shè),匿名管道雖然是文件,但是由于沒(méi)有文件路徑,進(jìn)程是無(wú)法通過(guò)系統(tǒng)文件接口來(lái)操作的,因此匿名管道通常用于父子進(jìn)程之間使用。

匿名管道的通信原理

由于匿名管道沒(méi)有文件路徑,進(jìn)程是無(wú)法通過(guò)系統(tǒng)文件接口來(lái)操作的特性,匿名管道必須通過(guò)父進(jìn)程創(chuàng)建,子進(jìn)程繼承父進(jìn)程文件描述符表的方式,使得不同的進(jìn)程看到同一個(gè)文件:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

由于匿名管道只支持單向通信,在使用匿名管道進(jìn)行通信時(shí),父進(jìn)程必須分別以讀方式和寫(xiě)方式打開(kāi)管道文件,子進(jìn)程繼承了文件描述符表后,一方關(guān)閉讀端,一方關(guān)閉寫(xiě)端進(jìn)行通信。

注意: 如果父進(jìn)程只以讀方式或者寫(xiě)方式打開(kāi),子進(jìn)程繼承文件描述符表后,也是同樣的方式,子進(jìn)程自身無(wú)法打開(kāi)該管道,因此導(dǎo)致無(wú)法通信。

系統(tǒng)接口

Linux系統(tǒng)提供了創(chuàng)建匿名管道的系統(tǒng)接口pipe:

//pipe所在的頭文件和聲明
#include <unistd.h>

int pipe(int pipefd[2]);
  • pipefd為輸出型參數(shù),用于接收以讀方式和寫(xiě)方式打開(kāi)管道的文件描述符。
  • pipefd[0]獲取讀端文件描述符,pipefd[1]獲取寫(xiě)端文件描述符。
  • 成功返回0,失敗返回-1,錯(cuò)誤碼被設(shè)置。

編寫(xiě)如下代碼測(cè)試pipe接口:

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    //創(chuàng)建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    if (n < 0)//出錯(cuò)判斷
    {
        cout << "errno: " << errno << "strerror: " << strerror(errno) << endl;
        exit(1); 
    }
    //創(chuàng)建子進(jìn)程
    pid_t id = fork();
    assert(id != -1);//出錯(cuò)判斷

    //進(jìn)行通信 -- 父進(jìn)程進(jìn)行讀取,子進(jìn)程進(jìn)行寫(xiě)入
    if (id == 0)
    {
        //子進(jìn)程
        close(pipefd[0]);
        
        const string str = "hello world";
        int cnt = 1;
        char buffer[1024];
        while(1)
        {
            snprintf(buffer, sizeof(buffer), "%s, 我是子進(jìn)程, 我的pid:%d, 計(jì)數(shù)器:%d", str.c_str(), getpid(), cnt++);
            write(pipefd[1], buffer, strlen(buffer));//向管道寫(xiě)入數(shù)據(jù)
            sleep(1);
        }
        close(pipefd[1]);
        exit(0);
    }

    //父進(jìn)程
    close(pipefd[1]);
    char buffer[1024];
    while(1)
    {
        read(pipefd[0], buffer, sizeof(buffer) - 1);//從管道讀取數(shù)據(jù)
        cout << "我是父進(jìn)程," << "child give me: " << buffer << endl;
    }
    close(pipefd[0]);
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

從運(yùn)行結(jié)果可以看出,建立管道后,父子進(jìn)程就能夠進(jìn)行數(shù)據(jù)通信。

管道特性

  1. 單向通信,半雙工的,數(shù)據(jù)只能向一個(gè)方向流動(dòng);需要雙方通信時(shí),需要建立起兩個(gè)管道
  2. 管道的本質(zhì)是文件,因此管道的生命周期隨進(jìn)程
  3. 管道通信,通常適用于具有“血緣關(guān)系的進(jìn)程”,諸如父子進(jìn)程、兄弟進(jìn)程等
  4. 管道的數(shù)據(jù)是以字節(jié)流的形式傳輸?shù)模x寫(xiě)次數(shù)的多數(shù)不是強(qiáng)相關(guān)的
  5. 具有一定的協(xié)同機(jī)制

管道的協(xié)同場(chǎng)景

場(chǎng)景一: 如果管道內(nèi)部的數(shù)據(jù)被讀端讀取完了,寫(xiě)端不寫(xiě)入,讀端就只能等待

編寫(xiě)如下代碼(如下代碼只是在前文測(cè)試pipe接口的代碼上做略微改動(dòng),主要改動(dòng)已用-----標(biāo)識(shí))進(jìn)行驗(yàn)證:

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    //創(chuàng)建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    if (n < 0)
    {
        cout << "errno: " << errno << "strerror: " << strerror(errno) << endl;
        exit(1); 
    }
    //創(chuàng)建子進(jìn)程
    pid_t id = fork();
    assert(id != -1);

    //進(jìn)行通信 -- 父進(jìn)程進(jìn)行讀取,子進(jìn)程進(jìn)行寫(xiě)入
    if (id == 0)
    {
        //子進(jìn)程
        close(pipefd[0]);
        
        const string str = "hello world";
        int cnt = 1;
        char buffer[1024];
        while(1)
        {
            snprintf(buffer, sizeof(buffer), "%s, 我是子進(jìn)程, 我的pid:%d, 計(jì)數(shù)器:%d", str.c_str(), getpid(), cnt++);
            write(pipefd[1], buffer, strlen(buffer));
            sleep(100); // ---------  模擬寫(xiě)入暫停  --------- 
        }
        close(pipefd[1]);
        exit(0);
    }

    //父進(jìn)程
    close(pipefd[1]);
    char buffer[1024];
    while(1)
    {
        read(pipefd[0], buffer, sizeof(buffer) - 1);
        cout << "我是父進(jìn)程," << "child give me: " << buffer << endl;
    }
    close(pipefd[0]);
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

場(chǎng)景二: 如果管道內(nèi)部的數(shù)據(jù)被寫(xiě)端寫(xiě)滿(mǎn)了,讀端不讀取,寫(xiě)端無(wú)法繼續(xù)寫(xiě)入

編寫(xiě)如下代碼(如下代碼只是在前文測(cè)試pipe接口的代碼上做略微改動(dòng),主要改動(dòng)已用-----標(biāo)識(shí))進(jìn)行驗(yàn)證:

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    //創(chuàng)建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    if (n < 0)
    {
        cout << "errno: " << errno << "strerror: " << strerror(errno) << endl;
        exit(1); 
    }
    //創(chuàng)建子進(jìn)程
    pid_t id = fork();
    assert(id != -1);

    //進(jìn)行通信 -- 父進(jìn)程進(jìn)行讀取,子進(jìn)程進(jìn)行寫(xiě)入
    if (id == 0)
    {
        //子進(jìn)程
        close(pipefd[0]);
        
        const string str = "hello world";
        int cnt = 1;
        char buffer[1024];
        while(1)
        {
            snprintf(buffer, sizeof(buffer), "%s, 我是子進(jìn)程, 我的pid:%d, 計(jì)數(shù)器:%d", str.c_str(), getpid(), cnt++);
            write(pipefd[1], buffer, strlen(buffer));
            printf("cnt: %d\n", cnt); // ---------  顯示寫(xiě)入過(guò)程  --------- 
            //sleep(100);
        }
        close(pipefd[1]);
        exit(0);
    }

    //父進(jìn)程
    close(pipefd[1]);
    char buffer[1024];
    while(1)
    {
        sleep(100); // ---------  模擬讀取暫停  --------- 
        read(pipefd[0], buffer, sizeof(buffer) - 1);
        cout << "我是父進(jìn)程," << "child give me: " << buffer << endl;
    }
    close(pipefd[0]);
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

場(chǎng)景三: 寫(xiě)端關(guān)閉,讀端讀完了管道內(nèi)部的數(shù)據(jù)時(shí),再讀就讀到了文件的結(jié)尾。

編寫(xiě)如下代碼(如下代碼只是在前文測(cè)試pipe接口的代碼上做略微改動(dòng),主要改動(dòng)已用-----標(biāo)識(shí))進(jìn)行驗(yàn)證:

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
    //創(chuàng)建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    if (n < 0)
    {
        cout << "errno: " << errno << "strerror: " << strerror(errno) << endl;
        exit(1); 
    }
    //創(chuàng)建子進(jìn)程
    pid_t id = fork();
    assert(id != -1);

    //進(jìn)行通信 -- 父進(jìn)程進(jìn)行讀取,子進(jìn)程進(jìn)行寫(xiě)入
    if (id == 0)
    {
        //子進(jìn)程
        close(pipefd[0]);
        
        const string str = "hello world";
        int cnt = 1;
        char buffer[1024];
        while(1)
        {
            snprintf(buffer, sizeof(buffer), "%s, 我是子進(jìn)程, 我的pid:%d, 計(jì)數(shù)器:%d", str.c_str(), getpid(), cnt++);
            write(pipefd[1], buffer, strlen(buffer));
            printf("cnt: %d\n", cnt);
            sleep(1);
            if (cnt == 5) break; // ---------  寫(xiě)端關(guān)閉  --------- 
        }
        close(pipefd[1]);
        exit(0);
    }

    //父進(jìn)程
    close(pipefd[1]);
    char buffer[1024];
    while(1)
    {
        int n = read(pipefd[0], buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            cout << "我是父進(jìn)程," << "child give me: " << buffer << endl;
        }
        else if (n == 0)// ---------  判斷讀取到文件末尾  --------- 
        {
            cout << "讀取完畢, 讀到文件結(jié)尾" << endl;
            break;
        }
        else
        {
            cout << "讀取出錯(cuò)" << endl;
            break;
        }
    }
    close(pipefd[0]);
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

**場(chǎng)景四:**寫(xiě)端一直寫(xiě),讀端關(guān)閉,操作系統(tǒng)會(huì)給寫(xiě)端發(fā)送13號(hào)信號(hào)終止進(jìn)程。

編寫(xiě)如下代碼(如下代碼只是在前文測(cè)試pipe接口的代碼上做略微改動(dòng),主要改動(dòng)已用-----標(biāo)識(shí))進(jìn)行驗(yàn)證:

#include <iostream>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

using namespace std;

int main()
{
    //創(chuàng)建管道
    int pipefd[2] = { 0 };
    int n = pipe(pipefd);
    if (n < 0)
    {
        cout << "errno: " << errno << "strerror: " << strerror(errno) << endl;
        exit(1); 
    }
    //創(chuàng)建子進(jìn)程
    pid_t id = fork();
    assert(id != -1);

    //進(jìn)行通信 -- 父進(jìn)程進(jìn)行讀取,子進(jìn)程進(jìn)行寫(xiě)入
    if (id == 0)
    {
        //子進(jìn)程
        close(pipefd[0]);
        
        const string str = "hello world";
        int cnt = 1;
        char buffer[1024];
        while(1)
        {
            snprintf(buffer, sizeof(buffer), "%s, 我是子進(jìn)程, 我的pid:%d, 計(jì)數(shù)器:%d", str.c_str(), getpid(), cnt++);
            write(pipefd[1], buffer, strlen(buffer));
            printf("cnt: %d\n", cnt);
            sleep(1);
        }
        close(pipefd[1]);
        exit(0);
    }

    //父進(jìn)程
    close(pipefd[1]);
    char buffer[1024];
    while(1)
    {
        int cnt = 0;
        //sleep(100);
        int n = read(pipefd[0], buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            cout << "我是父進(jìn)程," << "child give me: " << buffer << endl;
        }
        else if (n == 0)
        {
            cout << "讀取完畢, 讀到文件結(jié)尾" << endl;
            break;
        }
        else
        {
            cout << "讀取出錯(cuò)" << endl;
            break;
        }
        //sleep(100);
        sleep(5);
        break;// ---------  讀端關(guān)閉  --------- 
    }
    close(pipefd[0]);
    int status = 0;
    waitpid(id, &status, 0);
    cout << "signal: " << (status & 0x7F) << endl;// --------- 回收子進(jìn)程獲取退出信號(hào)  --------- 
    sleep(3);
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

管道的大小

在Linux下,管道(Pipe)的大小受到操作系統(tǒng)的限制。具體來(lái)說(shuō),管道的大小由內(nèi)核參數(shù)PIPE_BUF定義,通常是4096個(gè)字節(jié)。

  • 當(dāng)要寫(xiě)入的數(shù)據(jù)量不大于PIPE_BUF時(shí),linux將保證寫(xiě)入的原子性。
  • 當(dāng)要寫(xiě)入的數(shù)據(jù)量大于PIPE_BUF時(shí),linux將不再保證寫(xiě)入的原子性。

命名管道

命名管道同樣是內(nèi)存級(jí)的文件,和匿名管道的區(qū)別就是命名管道可以在指定路徑下創(chuàng)建,并且命名可以指定,因此命名管道可以給任何兩個(gè)不同的進(jìn)程用于通信。

使用指令創(chuàng)建命名管道

Linux下使用mkfifo 指令就可以在指定路徑下創(chuàng)建命名管道。

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

命名管道同樣和匿名管道一樣滿(mǎn)足管道的協(xié)同場(chǎng)景:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

寫(xiě)端嘗試打開(kāi)管道文件,沒(méi)有讀端,寫(xiě)端就會(huì)卡在打開(kāi)文件這一步驟。

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

右側(cè)讀端開(kāi)始會(huì)等待寫(xiě)端寫(xiě)入,后續(xù)關(guān)閉右側(cè)讀端,左側(cè)寫(xiě)端進(jìn)程直接被終止。

使用系統(tǒng)調(diào)用創(chuàng)建命名管道

//mkfifo所在的頭文件和聲明
#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);
  • pathname參數(shù) – 創(chuàng)建命名管道的路徑
  • mode參數(shù) – 創(chuàng)建命名管道的文件權(quán)限
  • 成功返回0,失敗返回-1,錯(cuò)誤碼被設(shè)置。

為了測(cè)試mkfifo接口編寫(xiě)代碼進(jìn)行測(cè)試,首先設(shè)置文件結(jié)構(gòu)如下:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言

makefile文件內(nèi)容如下:

.PHONY:all
all:client server

client:client.cc
	g++ -o $@ $^ -std=c++11

server:server.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -rf client server

common.hpp主要用于讓兩個(gè)進(jìn)程獲取管道路徑,具體內(nèi)容如下:

#include <iostream>
#include <string>

#define NUM 1024

const std::string pipename = "./namepipe"; //管道的路徑和管道名

mode_t mode = 0666; //創(chuàng)建管道的文件權(quán)限

client.cc作為寫(xiě)端輸入數(shù)據(jù),具體內(nèi)容如下:

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <cassert>
#include "commn.hpp"

int main()
{
    // 打開(kāi)管道文件
    int wfd = open(pipename.c_str(), O_WRONLY);
    if (wfd < 0)
    {
        std::cerr << "errno : " << errno << "strerror : " << strerror(errno) << std::endl;
        exit(1);
    }

    //進(jìn)行通信
    while(true)
    {
        char buffer[NUM];
        std::cout << "請(qǐng)輸入內(nèi)容:";
        fgets(buffer, sizeof(buffer), stdin);//獲取用戶(hù)輸入
        buffer[strlen(buffer) - 1] = 0;

        if (strcasecmp(buffer, "quit") == 0) break;//用戶(hù)輸入quit退出進(jìn)程

        ssize_t size = write(wfd, buffer, strlen(buffer));
        assert(size >= 0);
        (void)size;
    }

    close(wfd);
    return 0;
}

server.cc作為讀端用于接收寫(xiě)端的輸入并打印,具體內(nèi)容如下:

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include "commn.hpp"

int main()
{
    umask(0);
    // 創(chuàng)建管道文件
    int n = mkfifo(pipename.c_str(), mode);
    if (n < 0)
    {
        std::cerr << "errno : " << errno << "strerror : " << strerror(errno) << std::endl;
        exit(1);
    }
    std::cout << "create fifo file success" << std::endl;

    // 以讀方式打開(kāi)管道文件
    int rfd = open(pipename.c_str(), O_RDONLY);
    if (rfd < 0)
    {
        std::cerr << "errno : " << errno << "strerror : " << strerror(errno) << std::endl;
        exit(2);
    }

    // 進(jìn)行通信
    while (true)
    {
        char buffer[NUM];

        ssize_t size = read(rfd, buffer, sizeof(buffer) - 1);
        buffer[size] = 0;
        if (size > 0)
        {
            std::cout << "client send me :" << buffer << std::endl;//輸出接收的信息
        }
        else if (size == 0)
        {
            std::cout << "client quit, me too!" << std::endl;
            break;
        }
        else
        {
            std::cerr << "errno : " << errno << "strerror : " << strerror(errno) << std::endl;
            break;
        }
    }

    close(rfd);
    unlink(pipename.c_str()); // 刪除文件
    return 0;
}

編譯代碼運(yùn)行查看結(jié)果:

[Linux]進(jìn)程間通信--管道,Linux,linux,服務(wù)器,網(wǎng)絡(luò),運(yùn)維,c語(yǔ)言文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-708894.html

到了這里,關(guān)于[Linux]進(jìn)程間通信--管道的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Linux網(wǎng)絡(luò)編程:socket、客戶(hù)端服務(wù)器端使用socket通信(TCP)

    Linux網(wǎng)絡(luò)編程:socket、客戶(hù)端服務(wù)器端使用socket通信(TCP)

    socket(套接字),用于網(wǎng)絡(luò)中不同主機(jī)間進(jìn)程的通信。 socket是一個(gè)偽文件,包含讀緩沖區(qū)、寫(xiě)緩沖區(qū)。 socket必須成對(duì)出現(xiàn)。 socket可以建立主機(jī)進(jìn)程間的通信,但需要協(xié)議(IPV4、IPV6等)、port端口、IP地址。 ??????? ?(1)創(chuàng)建流式socket套接字。 ? ? ? ? ? ? ? ? a)此s

    2024年02月11日
    瀏覽(34)
  • 【Linux】進(jìn)程通信之管道通信詳解

    【Linux】進(jìn)程通信之管道通信詳解

    ?? 作者: 阿潤(rùn)菜菜 ?? 專(zhuān)欄: Linux系統(tǒng)編程 其實(shí)管道通信是Unix中最古老的進(jìn)程間通信的形式了: 管道通信是一種進(jìn)程間通信的方式,它可以讓一個(gè)進(jìn)程的輸出作為另一個(gè)進(jìn)程的輸入,實(shí)現(xiàn)數(shù)據(jù)的傳輸、資源的共享、事件的通知和進(jìn)程的控制。 管道通信分為兩種類(lèi)型:匿名

    2023年04月19日
    瀏覽(46)
  • 【Linux】進(jìn)程通信之匿名管道通信

    【Linux】進(jìn)程通信之匿名管道通信

    我們往往需要多個(gè)進(jìn)程協(xié)同,共同完成一些事情。 數(shù)據(jù)傳輸:一個(gè)進(jìn)程需要將它的數(shù)據(jù)發(fā)送給另一個(gè)進(jìn)程 資源共享:多個(gè)進(jìn)程之間共享同樣的資源。 通知事件:一個(gè)進(jìn)程需要向另一個(gè)或一組進(jìn)程發(fā)送消息,通知它(它們)發(fā)生了某種事件(如進(jìn)程終止 時(shí)要通知父進(jìn)程)。

    2024年04月14日
    瀏覽(24)
  • Linux——進(jìn)程間通信、管道

    Linux——進(jìn)程間通信、管道

    進(jìn)程間的通信就是 在不同進(jìn)程之間傳播或交換信息。 舉個(gè)例子: 古時(shí),兩軍交戰(zhàn)不斬來(lái)使; 因?yàn)閮绍娀ハ嗍仟?dú)立的,所以使節(jié)就是兩軍之間傳話(huà)的進(jìn)行傳話(huà)的; 而在OS中,進(jìn)程之間也是相互獨(dú)立的,但某項(xiàng)工作并不是一個(gè)進(jìn)程就可以完成,而是多個(gè)進(jìn)程之間相互協(xié)助完成;

    2024年02月22日
    瀏覽(23)
  • Linux進(jìn)程通信:無(wú)名管道

    Linux進(jìn)程通信:無(wú)名管道

    (1)數(shù)據(jù)傳輸:進(jìn)程間數(shù)據(jù)傳輸; (2)通知事件:一個(gè)進(jìn)程向另一個(gè)或一組進(jìn)程發(fā)送消息,通知某個(gè)事件的發(fā)生(如子進(jìn)程終止時(shí)需通知父進(jìn)程); (3)資源共享:多個(gè)進(jìn)程共享資源,需要內(nèi)核提供同步互斥機(jī)制; (4)進(jìn)程控制:某進(jìn)程需要控制另一個(gè)進(jìn)程的執(zhí)行(如

    2023年04月24日
    瀏覽(23)
  • linux——進(jìn)程間通信——管道

    linux——進(jìn)程間通信——管道

    ??1主頁(yè)::我的代碼愛(ài)吃辣 ??2知識(shí)講解:Linux——進(jìn)程間通信——管道通信 ??3開(kāi)發(fā)環(huán)境:Centos7 ??4前言:進(jìn)程間通信(InterProcess Communication,IPC)是指在不同進(jìn)程之間傳播或交換信息。 目錄 一.什么是進(jìn)程間通信 二.進(jìn)程間通信目的 ?三.進(jìn)程間通信發(fā)展 四.什么是管道

    2024年02月08日
    瀏覽(40)
  • Linux——進(jìn)程間通信&&管道

    Linux——進(jìn)程間通信&&管道

    ??北塵_ :個(gè)人主頁(yè) ??個(gè)人專(zhuān)欄 :《Linux操作系統(tǒng)》《經(jīng)典算法試題 》《C++》 《數(shù)據(jù)結(jié)構(gòu)與算法》 ??走在路上,不忘來(lái)時(shí)的初心 數(shù)據(jù)傳輸:一個(gè)進(jìn)程需要把他的數(shù)據(jù)傳給另外一個(gè)進(jìn)程。 資源共享:多個(gè)進(jìn)程之間共享同樣的資源。 通知事件:一個(gè)進(jìn)程需要向另一個(gè)或一組

    2024年04月09日
    瀏覽(27)
  • Linux——進(jìn)程間通信(管道)

    Linux——進(jìn)程間通信(管道)

    目錄 進(jìn)程通信的目的 管道 見(jiàn)見(jiàn)豬跑(舉個(gè)例子) 文件描述符fd與管道的關(guān)系(深度理解管道) 什么是管道? ?匿名管道 pipe函數(shù)概述 父子進(jìn)程通信時(shí)與文件描述符的關(guān)系圖(理解pipe函數(shù)的關(guān)鍵) pipe函數(shù)的使用 ?管道讀寫(xiě)規(guī)則 管道的大小 自測(cè) ?使用man 7 pipe查看 使用ulimit -a查看 管

    2024年02月03日
    瀏覽(17)
  • 【Linux】進(jìn)程通信 — 管道

    【Linux】進(jìn)程通信 — 管道

    從本章開(kāi)始,我們開(kāi)始學(xué)習(xí)進(jìn)程通信相關(guān)的知識(shí),本章將來(lái)詳細(xì)探討一下管道,學(xué)習(xí)匿名管道和命名管道的原理和代碼實(shí)現(xiàn)等相關(guān)操作。目標(biāo)已經(jīng)確定,接下來(lái)就要搬好小板凳,準(zhǔn)備開(kāi)講了…???????? 在我們之前的學(xué)習(xí)中,我們知道進(jìn)程是具獨(dú)立性的。但是不要以為進(jìn)程

    2024年02月16日
    瀏覽(17)
  • [Linux]進(jìn)程間通信--管道

    [Linux]進(jìn)程間通信--管道

    數(shù)據(jù)傳輸:一個(gè)進(jìn)程需要將它的數(shù)據(jù)發(fā)送給另一個(gè)進(jìn)程 。 資源共享:多個(gè)進(jìn)程之間共享同樣的資源。 通知事件:一個(gè)進(jìn)程需要向另一個(gè)或一組進(jìn)程發(fā)送消息,通知它(它們)發(fā)生了某種事件(如進(jìn)程終止時(shí)要通知父進(jìn)程)。 進(jìn)程控制:有些進(jìn)程希望完全控制另一個(gè)進(jìn)程的執(zhí)

    2024年02月09日
    瀏覽(15)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包