2013-02-20 36 views
0

我想構建一個視頻轉換隊列,因此運行單聲道程序的服務器從隊列中提取視頻並運行ffmpeg將其轉換。當運行許多ffmpeg進程時,單聲道給我錯誤。運行過程中單聲道崩潰

所以我用ping創建了一個測試用例,因爲它是一個長時間運行的作爲ffmpeg的測試用例。

static TextWriter tw = new StreamWriter("error.txt"); 
    private static int _started; 
    public static void Main (string[] args) 
    { 

     for (int i=0; i<1000; i++) { 
      Start ("ping", "localhost"); 

     } 
     Console.WriteLine("Memory: " + (GC.GetTotalMemory(true)/1024) + "KB"); 
     Console.WriteLine(_started); 
     tw.Flush(); 
     Console.ReadLine(); 
    } 

    public static Process Start (string name, string args) 
    { 
     try { 
      var proc = new ProcessStartInfo { 
        FileName = name, 
        Arguments = args, 
        RedirectStandardOutput = true, 
        CreateNoWindow = false, 
        RedirectStandardError = false, 
        UseShellExecute = false, 
        RedirectStandardInput = false, 
      }; 
      Process.Start (proc); 
      _started++; 
     } catch (Exception ex) { 
      tw.WriteLine (ex.InnerException+Environment.NewLine+ex.Message + Environment.NewLine + ex.StackTrace, false); 
      tw.Flush(); 
     } 
     return null; 
    } 

和運行時,相同的錯誤的情況下拋出的有關ffmpeg

ApplicationName='ping', CommandLine='localhost', CurrentDirectory='' 
    at System.Diagnostics.Process.Start_noshell 
(System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process 
process) [0x00000] in <filename unknown>:0 
    at System.Diagnostics.Process.Start_common 
(System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process 
process) [0x00000] in <filename unknown>:0 
    at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo 
startInfo) [0x00000] in <filename unknown>:0 
    at test1.MainClass.Start (System.String name, System.String args) [0x00000] 
in <filename unknown>:0 

的CreateProcess:錯誤創建進程處理

所以這是我的環境:

Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1) 
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com 
    TLS:   __thread 
    SIGSEGV:  altstack 
    Notifications: epoll 
    Architecture: amd64 
    Disabled:  none 
    Misc:   softdebug 
    LLVM:   supported, not enabled. 
    GC:   Included Boehm (with typed GC and Parallel Mark) 

和極限值

core file size   (blocks, -c) 0 
data seg size   (kbytes, -d) unlimited 
scheduling priority    (-e) 0 
file size    (blocks, -f) unlimited 
pending signals     (-i) 31439 
max locked memory  (kbytes, -l) 64 
max memory size   (kbytes, -m) unlimited 
open files      (-n) 1024 
pipe size   (512 bytes, -p) 8 
POSIX message queues  (bytes, -q) 819200 
real-time priority    (-r) 0 
stack size    (kbytes, -s) 8192 
cpu time    (seconds, -t) unlimited 
max user processes    (-u) 31439 
virtual memory   (kbytes, -v) unlimited 
file locks      (-x) unlimited 

我想繼續我的項目,但我不能因爲這個異常,1年的工作與單後,我真的很喜歡它,不想學其他語言因爲這個麻煩。

我希望有一個與我的代碼,而不是單聲道運行時出現問題。

所以我的臨時解決方案:

public static void Start (string cmd, int time) 
    { 
     new Thread (p => 
     { 
      try { 
       Mono.Unix.Native.Syscall.system (String.Format("timeout {0} {1}", time, cmd)); 
      } catch (Exception ex) { 
       //log it 
      } 
     } 
     ).Start(); 
    } 
+0

原諒我的題外話的問題,而是你如何控制限值?我在控制堆棧大小特別感興趣:http://stackoverflow.com/questions/14263148/mono-regex-and-stack-size – IneQuation 2013-02-20 18:23:58

+1

https://bugzilla.novell.com/show_bug.cgi?id=536776 #C4 – 2013-02-21 13:59:08

+0

即_started ++應該[Interlocked.Increment](https://msdn.microsoft.com/en-us/library/dd78zt0c(v = VS.110)的.aspx),否則這將是不準確的帶螺紋。 – 2015-07-28 15:47:31

回答

3

必須調用WaitForExit和處置流程實例。

所以,你開始的方法將是這個樣子:

public static void Start (string name, string args) 
{ 
    new Thread (() => 
    { 
     try { 
      var proc = new ProcessStartInfo { 
        FileName = name, 
        Arguments = args, 
        RedirectStandardOutput = true, 
        CreateNoWindow = false, 
        RedirectStandardError = false, 
        UseShellExecute = false, 
        RedirectStandardInput = false, 
      }; 
      using (var p = new Process()) { 
       p.StartInfo = proc; 
       p.Start(); 
       p.WaitForExit(); 
      } 
      _started++; 
     } catch (Exception ex) { 
      tw.WriteLine (ex.InnerException+Environment.NewLine+ex.Message + Environment.NewLine + ex.StackTrace, false); 
      tw.Flush(); 
     } 
    }).Start(); 
} 
+0

你能測試這個嗎?在我的電腦上它不起作用,同樣的錯誤出現。 – croisharp 2013-02-21 17:04:12

+0

@croisharp:對不起,我忘了你需要等待Linux上的進程。看到我更新的答案。 – 2013-02-21 23:37:43

+1

那麼它類似於什麼[錯誤報告告訴你這樣做(https://bugzilla.novell.com/show_bug.cgi?id=536776#c4)(而且可能更好 - 用'WaitForExit'通話),但啓動一個線程,然後等待每個進程運行一段不確定時間的進程似乎有點容易出錯。 – 2013-02-21 23:43:37