【发布时间】:2016-11-03 05:00:49
【问题描述】:
我有一个客户端进程,它派生出一个子进程以通过 svc_run() 方法侦听传入的 RPC。我需要做的是从父进程中杀死该子进程,然后重新分叉子进程,为其提供新的 CLIENT* 到新的 RPC 服务器。
以下是我的相关代码:
// Client Main
CLIENT* connectionToServer;
int pipe[2];
int childPID;
int parentPID;
static void usr2Signal()
{
ServerData sd;
clnt_destroy(connectionToServer);
(void) read(pipe[0], &sd, sizeof(sd));
// Kill child process.
kill(childPID, SIGTERM);
close(pipe[0]);
// RPC connection to the new server
CLIENT *newServerConn =
clnt_create(
sd.ip,
sd.programNum,
1,
"tcp");
if (!newServerConn)
{
// Connection error.
exit(1);
}
connectionToServer = newServerConn;
// Respawn child process.
if (pipe(pipe) == -1)
{
// Pipe error.
exit(2);
}
childPID = fork();
if (childPID == -1)
{
// Fork error.
exit(3);
}
if (childPID == 0)
{
// child closes read pipe and listens for RPCs.
close(pipe[0]);
parentPID = getppid();
svc_run();
}
else
{
// parent closes write pipe and returns to event loop.
close(pipe[1]);
}
}
int main(int argc, char *argv[])
{
/* Some initialization code */
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
// TCP connection error.
exit(1);
}
if (!svc_register(transp, /*other RPC program args*/, IPPROTO_TCP))
{
// RPC register error
exit(1);
}
connectionToServer = clnt_create(
192.168.x.xxx, // Server IP.
0x20000123, // Server RPC Program Number
1, // RPC Version
"tcp");
if (!connectionToServer)
{
// Connection error
exit(1);
}
// Spawn child process first time.
if (pipe(pipe) == -1)
{
// Pipe error
exit(1);
}
childPID = fork();
if (childPID == -1)
{
// Fork error.
exit(1);
}
if (childPID == 0)
{
// Close child's read pipe.
close(pipe[0]);
parentPID = getppid();
// Listen for incoming RPCs.
svc_run ();
exit (1);
}
/* Signal/Communication Code */
// Close parent write pipe.
close(pipe[1]);
// Parent runs in event loop infinitely until a signal is sent.
eventLoop();
cleanup();
}
在我的服务器代码中,我有启动新连接的服务调用。此调用由服务器上的某些其他操作调用。
// Server Services
void newserverconnection_1_svc(int *unused, struct svc_req *s)
{
// This service is defined in the server code
ServerData sd;
/* Fill sd with data:
Target IP: 192.168.a.aaa
RPC Program Number: 0x20000321
... other data
*/
connecttonewserver_1(&sd, connectionToServer); // A client service.
}
回到我的客户,我有以下服务:
// Client Service
void connecttonewserver_1_svc(ServerData *sd, struct svc_req *s)
{
// Send the new server connection data to the parent client processs
// via the pipe and signal the parent.
write(pipe[1], sd, sizeof(sd));
kill(parentPID, SIGUSR2);
}
我的问题是,在我启动新连接之前一切都运行良好。我没有进入任何错误部分,但是在设置新连接后大约 5 秒,我的客户端变得无响应。它不会崩溃,并且子进程似乎还活着,但是当我在父事件循环中定义的事件由鼠标单击触发时,我的客户端将不再接收 RPC 或显示任何打印语句。我可能在为子进程生成这个新的 RPC 循环时做错了什么,但我看不到是什么。有什么想法吗?
【问题讨论】:
-
分配给
connectionToServer并在usr2Signal()中使用的内存未被释放。然后你在main()中再次使用它。 -
所以我认为我的 main 只执行一次,并且是最先执行的。在 main 中的 fork 之后,我的子进程运行“svc_run()”,直到子进程终止。除了触发“usr2Signal”的信号外,父级继续到 eventLoop 并无限期地在那里运行。我在 main 中没有看到“connectionToServer”的用法?
-
另外,我的“usr2Signal”中的“clnt_destroy”不会清理分配的内存吗?
-
这里的一些文档linux.die.net/man/3/clnt_destroy 谈到了释放指针,但我刚刚看到的一件事是关闭套接字。那可能是我的问题。我会检查并回复。
-
所以我不知道这应该是官方的答案,但我没有杀死子进程来让它工作。相反,我必须通过 usr2Signal 将新的 IP 数据传递给父进程,然后更新我的 connectionToServer 指针。这会造成泄漏,因为我从不清理旧指针,但它至少按我现在的预期运行。稍后我会研究内存清理。