简体   繁体   中英

Why my program is going in the infinite loop?

I am trying to compile and execute C , C++ and Java codes taken as argument to a Java file and then check that the generated solution is correct or not as most of the website judge the solutions.Please anybody can tell me why my code is going in infinite loop and no out put is coming in file_name_output.txt . My other all files are correct as i have tested them by running the program on terminal.Here is my code :

import java.io.*;
import java.util.Scanner;

class test
{
    public static void main(String args[])
    {
        String s=null,file_name,extension;
        int pos = args[0].lastIndexOf(".");

        extension = args[0].substring(pos+1);
        file_name = args[0].substring(0,pos);

        int lang = 0; //  1 -> c,c++ , 2 -> java


        try
        {   

            Process compile = null;

            switch(extension)
            {
                case "c"    :    compile = Runtime.getRuntime().exec("gcc -g "+ args[0] + " -o "+file_name+" -lm");
                            lang = 1;           
                            break;
                case "cpp"  :    compile = Runtime.getRuntime().exec("g++ -g "+ args[0] + " -o "+file_name);
                            lang = 1;
                            break;
                case "java" :    compile = Runtime.getRuntime().exec("javac "+ args[0]);
                            lang = 2;
            }

            BufferedReader stdError = new BufferedReader(new InputStreamReader(compile.getErrorStream()));

            if((s = stdError.readLine()) != null)
            {
                System.out.println("Compile Time Error OR Warning : ");

                System.out.println(s);
                while((s = stdError.readLine()) != null)
                {
                    System.out.println(s);
                }
            }

            double startTime, run_time;
            Process run;

            if(lang == 1)
            {
                 startTime = System.nanoTime();

                 run = Runtime.getRuntime().exec("./"+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");

                 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
            }
            else
            {

                startTime = System.nanoTime();

                 run = Runtime.getRuntime().exec("java "+file_name+" < "+file_name+"_input.txt > "+file_name+"_output.txt");

                 run_time = (System.nanoTime()-startTime)/(double)Math.pow(10,6);
            }



            System.out.println("RunTime : "+ run_time+" ms");

            BufferedReader out_put = new BufferedReader(new FileReader(new File(file_name+"_output.txt")));

            BufferedReader run_stdError = new BufferedReader(new InputStreamReader(run.getErrorStream()));


            if(( s = run_stdError.readLine()) != null)
            {
                System.out.println("Runtime Error : ");

                System.out.println(s);

                while((s = run_stdError.readLine()) != null )
                {
                    System.out.println(s);
                }
            }
            else if((s = out_put.readLine()) != null)
            {
                String s_string = null;
                int failed = 0;

                File fs = new File(file_name+".txt");

                BufferedReader br  = new BufferedReader(new FileReader(fs));

                if((!s.equals(s_string = br.readLine())))
                {
                    failed = 1;
                }

                while(((s = out_put.readLine()) != null) & ((s_string = br.readLine()) != null) & (failed == 0))
                {
                    if(!s.equals(s_string) )
                    {
                        failed = 1;
                        break;
                    }
                }

                if((failed == 1) || s != null || s_string != null)
                {
                    System.out.println("Submmision Failed : ");
                    System.out.println("Either Output Is Wrong.\nOR\nYour Output Is Not According To The Given Format. ");
                    System.exit(0);

                }
                else
                {
                    System.out.println("Submission Successful.");
                }

            }
        }   
        catch(IOException e)
        {
            System.out.println("Some Error Has Occured : ");
            e.printStackTrace();
            System.exit(-1);
        }

    }
}

Diagnosis

Your program is not in an endless loop, it is blocking , and this is the line where it happens:

s = run_stdError.readLine()

unless there's something on the subprocess's stderr , this is going to block until the process dies. However, while waiting here you don't consume the process's stdout . It fills its output buffer and blocks.

The result: an interprocess deadlock.

Suggested fix

Use a ProcessBuilder and use its API to achieve redirection into files with no effort of your own. You have the redirectOutput(File) and redirectError(File) methods in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM