0

我被分配了一个项目,在该项目中我需要使用Unix 域套接字在父进程和子进程之间进行双向通信。我最初的方法是创建一个子服务器和服务器,但我遇到了一些严重的连接和路径问题。如果有人希望看到该代码,我愿意提供。

无论如何,我已经到达了程序中打印出“父母已写”的那一行,尽管我并不完全相信它确实如此,因为没有发生后续阅读。我曾在不同的地方关闭管道并改变我的阅读/写作方式。

我现在使用socketpair()来设置一对套接字。除了套接字,我不能使用任何其他类型的 IPC。这些信息对我来说是全新的,所以请原谅草率的代码。

请忽略长#include 列表,将尽快清理。

同样,我当前的输出停止在“父母写给孩子”,然后它继续运行,没有关闭,所以我认为这是孩子读等待接收的东西。

我的程序需要能够读取和写入非常长的文件,因此您将在整个代码中看到序列化和解析工作。序列化和解析在项目的不同部分完美运行,所以我知道这不是问题。这是该死的插座!

无论如何,代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sys/wait.h>
#include <vector>
#include <fstream>

int main() {
    int sockets[2];

    std::cout << "Please enter name of text file." << std::endl;
    std::string entered_file;
    std::cin >> entered_file;
    std::string string_to_find;
    std::cout << "Please enter the string you'd like to search for." << std::endl;
    std::cin >> string_to_find;

    int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
    if (rc < 0) {
        perror("socketpair");
        exit(1);
    }

    pid_t p;
    p = fork();

    if (p == 0)  //child
    {
        char ch;
        std::string the_file_as_string = "";
        std::vector<std::string> lines_with_string;

        int r;
        while ((r = read(sockets[0], &ch, 1)) > 0) {
            the_file_as_string.push_back(ch);
        }

        std::cout << "Child has read from parent..." << std::endl;

        size_t pos = 0;
        while(the_file_as_string.find(string_to_find, pos) != std::string::npos) {
            pos = the_file_as_string.find(string_to_find, pos+string_to_find.size());
            std::string sub = the_file_as_string.substr(0,pos);
            int occurrences = 0;
            size_t pos2 = 0;
            while (sub.find("/0", pos2 ) != std::string::npos) {
                pos2 = sub.find("/0", pos2+2);
                ++ occurrences;
                pos += string_to_find.length();
            }
            occurrences = occurrences;
            std::string occurrences_string = std::to_string(occurrences);
            lines_with_string.push_back(occurrences_string);
        }
        std::cout << "child has parsed" << std::endl;
        std::string lines_with_string_as_string = "";
        for(unsigned int i=0; i<lines_with_string.size(); i++) {
             lines_with_string_as_string = lines_with_string_as_string + lines_with_string.at(i) + "/0";
         }
        int file_size = lines_with_string_as_string.size();

        write(sockets[0], lines_with_string_as_string.c_str(), file_size+1);
        close(sockets[0]);
        std::cout << "child has sent back to parent..." << std::endl;
    }
    else  //parent
    {
        close(sockets[0]);
        std::ifstream myfile;

        //open file
        myfile.open(entered_file.c_str());

        if (!myfile) {
            std::cerr << "Unable to open file datafile.txt" << std::endl;
            return 1;   // call system to stop
        }


        //here, pass lines to child one by one and return true or false based on if it finds the string
        char lines_with_string[100];
        char ch;
        std::vector<std::string> the_file;
        std::vector<int> final_line_numbers;
        std::vector<std::string> final_lines;
        std::string str;
         while (std::getline(myfile, str))
         {
             the_file.push_back(str);
         }
         std::string the_file_as_string = "";

         for(unsigned int i=0; i<the_file.size(); i++) {
             the_file_as_string = the_file_as_string + the_file.at(i) + "/0";
         }

         int big_size = the_file_as_string.size();
         write(sockets[1], the_file_as_string.c_str(), big_size+1);

         std::cout << "Parent wrote to the child..." << std::endl;

         wait(NULL);

        std::string lines_with_string_as_string = "";
        int r;
        while ((r = read(sockets[1], &ch, 1)) > 0) {
            lines_with_string_as_string.push_back(ch);
        }
        close(sockets[1]);
        std::cout << "Parent has read from child..." << std::endl;

        std::string delimit = "/0";
        size_t pos3 = 0;
        while ((pos3 = lines_with_string_as_string.find(delimit)) != std::string::npos) {
            std::string token = lines_with_string_as_string.substr(0, pos3);
            int token2 = std::stoi(token);
            final_line_numbers.push_back(token2);
            lines_with_string_as_string.erase(0, pos3 + delimit.length());
        }
        //match line numbers to array of lines (original)
        for(unsigned int i=0; i < final_line_numbers.size(); i++) {
            int find_int = final_line_numbers.at(i);
            std::string find_string = the_file.at(find_int-1);
            std::cout << find_string << std::endl;
            final_lines.push_back(find_string);
        }

        std::sort(final_lines.begin(), final_lines.end());
        std::cout << "The final outcome with lines containing " << "'" << string_to_find << "' are:" << std::endl;
        for(unsigned int i=0; i<final_lines.size(); i++) {
            std::cout << final_lines.at(i) << std::endl;
        }
         myfile.close();
         std::cout << "Program has exited completely." << std::endl;
    }
}

MINI VERSION(同样的问题,说“父母已经写信给孩子......”然后没有做任何进一步的事情,也没有退出:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sys/wait.h>
#include <vector>
#include <fstream>

int main() {

    int sockets[2];

    pid_t p;


    socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);

    p=fork();

    if (p < 0) {
        perror("forking");
    }
    else if(p > 0) {                                                //parent
        std::string long_string_to_send = "ladjfldjsfljasdfj adjlkjadlsjf a fljasdfladj ljdl aljfajdfljadfadsflajd  ajfdlkjslfjadj faldjsljalsdfljdfljadsl jladsfjasflajdflkajfl dasl fjalfjldjsfladsflajdlsfald fjljdfljadfjadfjl djfljadlfj aldjl hello";
        int big_size = long_string_to_send.size();

        write(sockets[1], long_string_to_send.c_str(), big_size+1);
        std::cout << "Parent has written to child..." << std::endl;

        wait(NULL);

        std::string lines_with_string_as_string = "";
        int r;
        char ch;
        while ((read(sockets[1], &ch, 1)) > 0) {
            lines_with_string_as_string.push_back(ch);
        }
        close(sockets[1]);
        std::cout << "Parent has received from child..." << std::endl;
        std::cout << "Parent has read the following... " << lines_with_string_as_string << std::endl;
        std::cout << "Program has exited completely" << std::endl;
    }
    else {                                                          //child
        char ch;
        std::string the_file_as_string = "";

        int r;
        while ((read(sockets[0], &ch, 1)) > 0) {
            the_file_as_string.push_back(ch);
        }

        std::cout << "Child has read from parent..." << std::endl;
        std::cout << "Child has read the following... " << the_file_as_string << std::endl;
        int file_size = the_file_as_string.size();

        write(sockets[0], the_file_as_string.c_str(), file_size+1);
        close(sockets[0]);
    }
}
4

1 回答 1

1

归结为...您的父母这样做:

write(sockets[1], ...
wait(NULL)

虽然孩子这样做:

while (read(sockets[0] ....)   > 0)
...

这个问题的关键是:为什么会返回小于或等于零的东西readsockets[0]这里的答案是:从不。您可能打算让孩子读取父级写入套接字的所有内容,但套接字的工作方式是,直到另一个对等方关闭套接字(或执行shutdown(2).

因此,在父母中,您应该执行以下操作:

write(sockets[1], ....);
shutdown(sockets[1], SHUT_WR);             <<<<<=================
wait(NULL)

通过在父端调用shutdownwith ,子级将在套接字端SHUT_WR获得一个 EOF 指示(返回值为零) 。否则,孩子的读取将永远阻塞(因为操作系统如何知道父母以后是否会写入更多数据?)。

进一步解释:父级也可以close结束套接字,但如果这样做,它将无法从子级读取响应数据。shutdown本质上允许您“半关闭”套接字;这是关闭的写作,但开放阅读。

这种方法的唯一真正替代方案是以某种方式“构建”数据(即创建“协议”)。也就是说,您可以首先以某种固定大小或其他不同的方式写入您将要发送的数据的长度,然后孩子可以准确地读取所需的字节数。然后,您可以无限期地在两个进程之间继续对话,而无需shutdownor close

编辑:
还有一个警告。fork在关闭套接字的另一端之后,每个进程的最佳实践是。换句话说,就在 fork 之后, parent 会 closesockets[0]并且 child 会 close sockets[1]。否则,close来自一侧的 a 实际上不会在另一侧产生文件结尾(因为文件描述符在读取端仍然打开 - 就在fork两个文件描述符在两个进程中打开之后)。使用shutdown/SHUT_WR绕过了这个困难,因为它明确表示出于写作目的将一侧视为关闭。

于 2018-10-15T17:18:03.980 回答