|
我自己设计了一个服务器端的程序,并将其作为xinetd的服务进行管理
在程序中,我希望启动一个java子进程,但是在执行execl时候,却无法开启JVM,而且没有errno返回
请求各位的帮忙,谢谢!
相关的示例程序如下
1. 服务程序test.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
static int son_is_over;
void notify(int sig)
{
son_is_over = 1;
}
int main()
{
FILE *fp;
pid_t son_pid;
son_is_over = 0;
(void)signal(SIGCHLD, notify);
son_pid = fork();
if (son_pid < 0) {
printf("Error in fork()\n");
}
else if (son_pid == 0) {
fp = fopen("/home/wangf/test/Error", "w");
if (fp == NULL) {
printf("Create file error\n");
return -1;
}
fprintf(fp, "This is the son process.\n");
if (execl("/opt/IBMJDK51/bin/java", "java", "-cp", "/home/wangf/test", "HelloWorld") < 0) {
fprintf(fp, "Error number = %d\n", errno);
}
fclose(fp);
}
while (!son_is_over) {
pause();
}
printf("This is the parent process.\n");
return 1;
}
//编译gcc -o test test.c
2. 在/etc/services中增加端口绑定
test 20006/tcp
3. 服务描述文件/etc/xinetd/test
service test
{
socket_type = stream
server = /home/wangf/DCS/test4/test
port = 20006
protocol = tcp
user = root
wait = no
disable = no
}
//完成2和3的设定后,以root执行service xinetd restart,把程序test作为xinetd的服务
4. HelloWorld.java是最简单的java程序,为了能够ps观察是否有java进程被创建,所以增加了一段延迟
class HelloWorld
{
public static void main (String args[]) {
try {
Thread.sleep(5000);
}
catch(InterruptedException e){}
System.out.println("Hello World");
}
}
5. 问题的描述
1)如果直接运行./test,没有任何问题,会在屏幕上打印出Hello World以及This is the parent process。
2)如果以telnet 127.0.0.1 20006的方式访问test服务,则java子进程没有被执行,同时没有errno信息
3)如果在子进程中调用shell命令(例如ls)或者其它C程序,无论采用上述哪种方式,子进程的执行也没有任何问题。
为什么会出现第2)种情况,应该如何解决?
//请注意修改程序中的文件路径 谢谢 :) |
|