簡體   English   中英

為什么我的程序進入無限循環?

[英]Why my program is going in the infinite loop?

我正在嘗試編譯和執行C,C ++和Java代碼作為Java文件的參數,然后檢查生成的解決方案是否正確,因為大多數網站都對解決方案進行了評估。任何人都可以告訴我為什么我的代碼會運行在無限循環中,沒有輸出進入file_name_output.txt 我的所有其他文件都是正確的,因為我已經通過在終端上運行程序進行了測試,這是我的代碼:

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);
        }

    }
}

診斷

您的程序不是處於無限循環中,而是在阻塞 ,這是發生這種情況的那一行:

s = run_stdError.readLine()

除非子進程的stderr上有東西,否則它將阻塞直到該進程終止。 但是,在這里等待時,您不會消耗進程的stdout 它填充其輸出緩沖區和塊。

結果:進程間死鎖。

建議的修復

使用ProcessBuilder並使用其API可以ProcessBuilder實現重定向到文件的功能。 您在那里有redirectOutput(File)redirectError(File)方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM