我被分配了一个项目,在该项目中我需要使用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]);
}
}