當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python os.WCOREDUMP()用法及代碼示例


Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。

os.WCOREDUMP()Python中的方法用於檢查是否為該進程生成了核心轉儲。此方法采用流程狀態代碼,由os.wait()os.system()或者os.waitpid()方法作為參數。

用法: os.WCOREDUMP(status)

參數:
status:此參數采用os.system(),os.wait()方法或os.waitpid()方法返回的過程狀態代碼(整數值)。

返回類型:此方法返回“布爾”類的布爾值。如果為該進程生成了核心轉儲,則返回True,否則返回False。

代碼1:用於os.WCOREDUMP()方法

# Python program to explain os.WCOREDUMP() method  
  
# importing os module   
import os  
  
# Create a child process 
# using os.fork() method  
pid = os.fork() 
  
  
# pid greater than 0 
# indicates the parent process  
if pid : 
      
    # Wait for the completion of 
    # the child process and get 
    # child's pid and 
    # exit status indication 
    info = os.wait()     
  
    # info is a tuple 
    # info[0] represents child's id 
    # info[1] represents exit status code 
  
    print("\nIn parent process") 
      
    # Check if core dump was 
    # generated for the child process 
    core_dump = os.WCOREDUMP(info[1])  
  
    print("Was core dump generated?", core_dump) 
  
else : 
    print("In Child process") 
    print("Process ID:", os.getpid()) 
    print("Hello ! Geeks") 
      
    # os.abort() method will 
    # generate a SIGABRT signal  
    # to the current process 
    # and will produce core dump. 
    os.abort()
輸出:
In Child process
Process ID: 15059
Hello! Geeks

In parent process
Was core dump generated? True

代碼2:用於os.WCOREDUMP()方法

# Python program to explain os.WCOREDUMP() method  
  
# importing os module   
import os  
  
# Create a child process 
# using os.fork() method  
pid = os.fork() 
  
  
# pid greater than 0 
# indicates the parent process  
if pid : 
      
    # Create one more child 
    pid2 = os.fork() 
  
    if pid2 : 
      
  
        # Wait for the completion of 
        # first child process and get 
        # its pid and 
        # exit status indication 
        # using os.waitpid() method 
        child1_info = os.waitpid(pid, 0)     
  
        # Wait for the completion of 
        # second child process and get 
        # its pid and exit status indication 
        # using os.waitpid() method 
        child2_info = os.waitpid(pid2, 0)     
  
                  
        # child_info is a tuple, where 
        # child_info[0] represents child's id 
        # child_info[1] represents exit status code 
  
        print("\nIn parent process") 
  
        # Check if core dump was 
        # generated for the  
        # first child process 
        core_dump = os.WCOREDUMP(child1_info[1])  
        print("Was core dump generated for first child process?") 
        print(core_dump) 
  
        # Check if core dump was 
        # generated for the  
        # first child process 
        core_dump = os.WCOREDUMP(child2_info[1])  
        print("\nWas core dump generated for second child process?") 
        print(core_dump) 
  
          
    else : 
        print("\nIn second child process") 
        print("Process id:", os.getpid()) 
        print("Hey ! there") 
        print("Exiting")  
  
else : 
    print("In Child process") 
    print("Process ID:", os.getpid()) 
    print("Hello ! Geeks") 
      
    # os.abort() method will 
    # generate a SIGABRT signal  
    # to the current process 
    # and will produce core dump. 
    os.abort()
輸出:
In first child process
Process ID: 16289
Hello! Geeks

In second child process
Process id: 16290
Hey! there
Exiting

In parent process
Was core dump generated for first child process?
True

Was core dump generated for second child process?
False

參考文獻: https://docs.python.org/3/library/os.html#os.WCOREDUMP



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.WCOREDUMP() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。