簡體   English   中英

叉,管和文件操作

[英]Fork, pipe and file operations

我有一個管道來啟用派生程序中2個進程之間的通信。 它是通過pipe()調用-http://linux.die.net/man/2/pipe創建的 一切正常,直到我要執行一些文件操作為止。

此代碼有效:

pipe.writeBuffer(message.c_str(), message.length());
ofstream file;
file.open(name.c_str(), ios::app);
file << "stringData";    // put some data to file (many times)

但這不是:

ofstream file;
file.open(name.c_str(), ios::app);
pipe.writeBuffer(message.c_str(), message.length());
file << "stringData";    // put some data to file (many times)

在第二個示例中,“ file << someStream”沒有作用-我得到了空文件。 怎么了 文件描述符有問題嗎? 管道使用fd [0]-輸入和fd [1]-輸出。 也許fstream使用相同的輸出文件處理程序?

這是“工作”示例: http : //pastebin.com/gJ4PbHvy

#include <sys/types.h>
#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <fstream>

#define maxSize 64

using namespace std;

class Pipe
{
    public:
        Pipe()
        {
            pipe(fdesc);
        }

        ~Pipe() {}

        void writeBuffer(const char* message, size_t length)
        {
            close(fdesc[0]);
            write(fdesc[1], message, length);

        }

        void readBuffer()
        {
            char buffer[maxSize];
            close(fdesc[1]);
            size_t result = read(fdesc[0], &buffer, sizeof(buffer));
            cout << buffer << endl;

        }

    private:
        int fdesc[2];

};

class Writer
{
    public:
        Writer(Pipe &obj)
        {
            pipe = obj;
        }

        ~Writer()
        {}

        void entry()
        {
            std::string name = "./myFile";
            ofstream file;
            file.open(name.c_str(), ios::app);

            std::string message = "hello world";
            pipe.writeBuffer(message.c_str(), message.length()+1);

            if (file.is_open())
            {
                file << "Hello World!" << endl;
                file.close();
            }
            else
            {
                perror("file.is_open()");
            }

            sleep(1);

        }

    private:
        Pipe pipe;

};


class Reader
{
    public:
        Reader(Pipe &obj)
        {
            pipe = obj;
        }

        ~Reader()
        {}


        void entry()
        {
            pipe.readBuffer();
            sleep(1);

        }

     private:
        Pipe pipe;

};

int main(int argc, char *argv[])
{
    Pipe pipe;
    Reader reader(pipe);
    Writer writer(pipe);

    pid_t pid = fork();

    if (pid == -1) 
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) 
    {    
        // child process
        while(1)
            reader.entry();

    } 
    else 
    {
        // parent process
        while(1)
            writer.entry();

    }

}

對於已發布的程序,獲取空文件的上述問題無法重現,因為在每次運行時它都會寫一行Hello World! 進入myFile ,但這仍然顯示錯誤,因為您打算每秒寫入一行。 原因是writeBuffer()close(fdesc[0]) :雖然在writer進程中一次關閉管道的讀取端是正確的,但每次調用writeBuffer()時這樣做都是不正確的,因為文件描述符可以(並且在當前情況下確實)在另一個文件(這里是ofstream file )的第一個close()之后已被重用,該ofstream file是在它關閉后而不是(已關閉)管道的情況下執行的,因此沒有任何操作可以寫入文件。 修復:安排程序僅關閉一次管道末端,例如通過更改

            close(fdesc[0]);

            if (0 <= fdesc[0]) close(fdesc[0]), fdesc[0] = -1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM