简体   繁体   中英

Same process duplicated with different pids in linux

Description

I am having a python script which spans some processes using os.popen(command) . After starting all processes it will monitor(every 3 seconds) the processes using psutil.process_iter() method. Some times it show extra process(duplicates) with the same process name(command). But when I debug using watch -n 1 ps -ef | grep commandwatch -n 1 ps -ef | grep command there are no duplicates.

Environment Details:

CentOS 7 and python 3.7

Code

import psutil
def monitor_process(process_map):
   processes = psutil.process_iter()
   current_process_map = {}
   print("current_process_map::"+str(current_process_map))
   print("process_map::"+str(process_map))
   for process in processes:
      process_cmd_line = process.cmdline()
      current_process_map[process_cmd_line[1]] = current_process_map.get(process_cmd_line[1],0)+1
   for k,v in process_map:
      if v != current_process_map[k]:
         return False #spawned process and current process count mismatch
   return True

Problem

The above code returns false(current_process_map > process_map) initially(approx within 30 secs) after that it works properly(current_process_map == process_map). Is this a problem with python library or centos(linux)?.

In Unix whenever we want to create a new process, we fork the current process, creating a new child process which is exactly the same as the parent process; then we do an exec system call to replace all the data from the parent process with that for the new process.

This is expected behavior from linux. I have changed the implementation to get all pids after creating them and checking them after 5 seconds. In this way i can able to avoid duplicates.

fork and execv system call

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