Fork() and Wait() in C++
Fork() and Wait() in C++
我在c++中有一个类,它调用fork(),然后等待子进程完成,但是当我尝试这样做时,我会得到编译器错误。
代码如下:
#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/* Connection class */
class Connection {
string destination, userName, computerName;
public:
/* constructor for class
You must pass it two strings. The first must be either the word "server" or the word "client".
This will tell us which machine we want to connect to. The second will specify the username
with which to connect to that machine.
*/
Connection(string state, string user="default_username")
{
if(state == "server")
{
cout << "You've chosen to connect to the 'server'.n";
destination = "user.palmetto.clemson.edu:";
userName = user;
cout << "Destination: " << destination << ", Username: " << userName << "n";
cout << "Full connect argument: " << userName << "@" << destination << "n";
} else if (state == "client")
{
cout << "You've chosen to connect to the 'client'.n";
destination = "NIElaparo.ces.clemson.edu:";
userName = user;
cout << "Destination: " << destination << ", Username: " << userName << "n";
cout << "Full connect argument: " << userName << "@" << destination << "n";
} else
cout << "Error. Please enter either 'server' or 'client' for first argument in Connection().n";
string atString = "@";
computerName = userName + atString + destination;
}
/* Send function for class
Accepts a string as an argument which contains the name of the file to be sent. And then it uses
exec() system calls to use "scp" to send the file.
Note: this assumes that passwordless ssh has been set up between the client and server machines.
*/
/* ITS IN THIS FUNCTION THAT I GET THE ERROR. I SPLIT THE PROCESS WITH FORK, RUN
EXECL() AND THEN WAIT() ON THE CHILD PROCESS */
int Send(string fileName)
{
pid_t pid;
// char * parmList[] = {"scp", fileName, destination, NULL};
int status;
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failedn");
exit(1);
}
else if (pid == 0) { /* for the child process: */
if (execl("scp", fileName.c_str(), computerName.c_str(), NULL) < 0) { /* execute the command */
printf("*** ERROR: exec failedn");
exit(1);
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
}
};
int main()
{
Connection *con = new Connection("server");
/* I'm just doing this to see if this works to create the string "testgoingnow". Who knows. */
//string string = "test".append("going").append("nown");
string string1 = "test";
string at = "@";
string string2 = "going";
string new_string = string1 + at + string2;
cout << new_string << "n";
Send("test.txt");
//cout << string;
return 0;
}
我得到这个编译错误:
connection.cpp: In member function ‘int Connection::Send(std::string)’:
connection.cpp:64: error: no matching function for call to ‘wait::wait(int*)’
/usr/include/bits/waitstatus.h:68: note: candidates are: wait::wait()
/usr/include/bits/waitstatus.h:68: note: wait::wait(const wait&)
也许你需要包括正确的标题,尝试
#include <sys/wait.h>
相关文章:
- netcat command in c++
- Difference in displaying cv2 Mat
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 如何修复"error: ‘_1’ was not declared in this scope"?
- Softmax Implementation in C++
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- IF-nesting in c++
- Gurobi GRBModel and GRBmodel in C++
- Tensorflow Hub in C++
- Centos7 g++ "to_string is not in a member of std"
- InitializeCriticalSectionEx Not Located In KERNEL32.Dll
- 将 lambda 表达式传递给 std::function in C++
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- std::async from std::async in windows xp