LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 851|回复: 2

如何在xinetd服务中创建java子进程?

[复制链接]
发表于 2008-3-5 17:51:48 | 显示全部楼层 |阅读模式
我自己设计了一个服务器端的程序,并将其作为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)种情况,应该如何解决?
//请注意修改程序中的文件路径 谢谢 :)
发表于 2008-3-6 09:34:53 | 显示全部楼层
我猜是找不到HelloWorld.class
用inetd启动,程序环境里的当前目录可能和你手工运行时不一样
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-6 09:35:56 | 显示全部楼层
如果使用
system("/opt/IBMJDK51/bin/java -cp /home/wangf/test HelloWorld");
替换掉execl那行就可以创建java进程

或者使用
execl("/opt/IBMJDK51/bin/java", "/opt/IBMJDK51/bin/java", "-cp", "/home/wangf/test", "HelloWorld") ;

虽然问题暂时解决了,可是为什么呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表