【发布时间】:2020-06-14 09:08:18
【问题描述】:
我希望子进程循环监听父进程。如果它从父级接收到“exit”,则终止,如果接收到“cmd”,则将再次读取以使用 system() 执行的实际命令。 到目前为止我有这个代码,但是第二次阅读只是给了我相同的(“cmd”)
孩子:
pid = fork();
if(pid == 0){
close(fd_pipe[1]); // Close writing end
dup2(fd_pipe[0],0); close(fd_pipe[0]);
//close(fd_pipe[0]);
//create file descriptor for user file
int userFile_fd =
open(users_table.at(get_user(u_id)).get_filename().c_str(), O_APPEND|O_WRONLY);
//redirect output to user file
dup2(userFile_fd,1);
close(userFile_fd);
//listen in a loop till receive "exit"
while(true){
char buf[100];
read (0, &buf, 100);
cout << buf << endl;
//logout
if(strstr(buf, bye) != NULL){
cout << "Exiting..." << endl;
kill(getpid(), SIGKILL);
break;
}
//command
else if(strcmp(buf, cmd) == 0){
read (0, &buf, 100);
cout << "reading buf again: "<<buf << endl;
system(buf);
}
}//end while
}//end if (child process)
父母:
while(status == true){
//get input
cout << ("ucmd>");
getline (cin, command);
//some preprocessing code and then...
//this works fine
else if(command.compare(logout)==0)
{
cout << " UM: Loggin out USER-"<<u_id<<" associated pipe ID: "<<user_pipe[u_id]<<endl;
char exit2[] = "exit";
write (user_pipe[u_id], exit2, sizeof(exit2));//sends exit message
}
//cmd
else if(command.compare(cmd)==0)
{
write (user_pipe[u_id], cmd, sizeof(cmd));//cmd command
write (user_pipe[u_id], argument.c_str(), sizeof(argument.c_str()));//cmd command
//call_cmd(u_id, argument);
}
【问题讨论】:
-
您不能在 C 中使用
cout << ("ucmd>");— 那是 C++。你需要知道读取了多少字节——你没有捕捉到这些信息,所以你在盲目地工作。您提供的代码不是 MCVE (Minimal, Complete, Verifiable Example)(或 MRE 或 SO 现在使用的任何名称)或 SSCCE (Short, Self-Contained, Correct Example)。这意味着我们必须做太多工作来测试您的代码。 -
在忽略每个系统调用返回的结果的情况下,您无法编写正确的 Unix 代码。
-
"你不能使用 cout ");"这个问题有标签 c++
标签: c++ pipe fork system-calls dup