̽ÌÖLinuxÀú³ÌµÄÈý¸ÅÂÔËØ
̽ÌÖLinuxÀú³ÌµÄÈý¸ÅÂÔËØ
ÔÚLinuxϵͳÖУ¬Àú³ÌÊÇÅÌËã»úϵͳµÄ»ù±¾Ö´Ðе¥Î»£¬ËüÓÉÈý¸ÅÂÔËØ×é³É£ºÀú³Ì±êʶ·û£¨PID£©¡¢Àú³Ì״̬ºÍÀú³Ì¿ØÖƿ飨PCB£©¡£±¾ÎĽ«Í¨¹ýÏêϸµÄ´úÂëʾÀý£¬ÉîÈë̽ÌÖÕâÈý¸ÅÂÔËضÔLinuxÀú³ÌµÄÖ÷ÒªÐÔºÍ×÷Óá£
Ò»¡¢Àú³Ì±êʶ·û£¨PID£©
ÔÚLinuxϵͳÖУ¬Ã¿¸öÀú³Ì¶¼ÓÐÒ»¸öΨһµÄÀú³Ì±êʶ·û£¨PID£©£¬ÓÃÀ´±êʶ¸ÃÀú³ÌÔÚϵͳÖеÄΨһÉí·Ý¡£PIDÊÇÒ»¸öÕýÕûÊý£¬´Ó1×îÏÈÒÀ´ÎµÝÔö£¬Ö±µ½µÖ´ïϵͳ»®¶¨µÄ×î´óÖµ£¬È»ºóÖØдÓ1×îÏÈ·ÖÅÉ¡£
ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄCÓïÑÔ³ÌÐòΪÀý£¬ÑÝʾÔõÑù»ñÈ¡Àú³ÌµÄPID£º
#include <stdio.h> #include <unistd.h> int main() { pid_t pid; pid = getpid(); printf("Ä¿½ñÀú³ÌµÄPIDÊÇ£º%d ", pid); return 0; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËgetpid()º¯ÊýÀ´»ñÈ¡Ä¿½ñÀú³ÌµÄPID£¬²¢½«Æä´òÓ¡Êä³ö¡£Í¨¹ýÕâ¸öʾÀý£¬¿ÉÒÔ¿´µ½Àú³ÌµÄPIDÊÇÔõÑùÔÚ³ÌÐòÖÐʹÓõġ£
¶þ¡¢Àú³Ì״̬
LinuxÀú³ÌÓвî±ðµÄ״̬£¬°üÀ¨ÔËÐУ¨R£©¡¢ÆÚ´ý£¨S£©¡¢×èÖ¹£¨T£©¡¢½©Ê¬£¨Z£©µÈ¡£ÏàʶÀú³ÌµÄ״̬¹ØÓÚϵͳÐÔÄܵļà¿ØºÍµ÷ÓźÜÊÇÖ÷Òª¡£
ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄShell¾ç±¾ÎªÀý£¬ÑÝʾÔõÑùÉó²éϵͳÖеÄÀú³Ì״̬£º
#!/bin/bash ps -e -o pid,ppid,state,cmd
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËpsÏÂÁîÀ´Éó²éϵͳÖÐËùÓÐÀú³ÌµÄPID¡¢¸¸Àú³ÌID¡¢×´Ì¬ºÍÏÂÁͨ¹ýÕâ¸ö¾ç±¾¿ÉÒÔʵʱ¼à¿ØϵͳÖÐÀú³ÌµÄÔËÐÐÇéÐΡ£
Èý¡¢Àú³Ì¿ØÖƿ飨PCB£©
Àú³Ì¿ØÖƿ飨PCB£©ÊDzÙ×÷ϵͳÖÐÓÃÀ´ÖÎÀíºÍÐÎòÀú³ÌÐÅÏ¢µÄÊý¾Ý½á¹¹£¬°üÀ¨Àú³ÌµÄÖÖÖÖÊôÐÔºÍ״̬¡£PCBÊÇÄÚºËά»¤µÄÊý¾Ý½á¹¹£¬ÉúÑÄÁËÀú³ÌµÄËùÓÐÐÅÏ¢£¬ÈçÀú³Ì״̬¡¢ÓÅÏȼ¶¡¢¼Ä´æÆ÷ÐÅÏ¢¡¢ÄÚ´æ·ÖÅÉ¡¢ÎļþÐÎò·ûµÈ¡£
ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄPython³ÌÐòΪÀý£¬ÑÝʾÔõÑù»ñÈ¡Àú³ÌµÄPCBÐÅÏ¢£º
import psutil pid = 1234 process = psutil.Process(pid) print("Àú³ÌÐÅÏ¢£º", process.as_dict(attrs=['pid', 'name', 'status', 'ppid']))
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËpsutil¿âÀ´»ñÈ¡Ö¸¶¨Àú³ÌµÄPCBÐÅÏ¢£¬²¢´òÓ¡³öÀú³ÌµÄPID¡¢Ãû³Æ¡¢×´Ì¬ºÍ¸¸Àú³ÌIDµÈÊôÐÔ¡£Í¨¹ýÕâ¸öʾÀý¿ÉÒÔ¿´µ½£¬PCBÊÇÀú³ÌÖÎÀíµÄ½¹µãÊý¾Ý½á¹¹£¬Îª²Ù×÷ϵͳÌṩÁËÖÎÀíÀú³ÌµÄ»ù´¡¡£
×ÛÉÏËùÊö£¬LinuxÀú³ÌµÄÈý¸ÅÂÔËØ¡ª¡ªPID¡¢×´Ì¬ºÍPCB£¬ÊÇÀú³ÌÖÎÀíºÍµ÷ÀíµÄÖ÷Òª»ù´¡¡£Í¨¹ýÉîÈëÏàʶÕâÈý¸öÒªËØ£¬²¢Í¨¹ýÏêϸµÄ´úÂëʾÀýÑÝʾÆä×÷Ó㬿ÉÒÔ¸üºÃµØÃ÷È·Àú³ÌÔÚLinuxϵͳÖеÄÔËÐлúÖƺÍÖÎÀíÔÀí¡£
ÒÔÉϾÍÊÇ̽ÌÖLinuxÀú³ÌµÄÈý¸ÅÂÔËصÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡