2010-12-16 3 views
13

나는 멀티 쓰레드 애플리케이션을 가지고 있는데, htop (예)은 각 쓰레드마다 다른 이름을 보여주고 싶습니다. 실행하는 데 사용되는 "명령 행"이 무엇인지 보여줍니다. 메인.리눅스에서 스레드 이름 변경 (htop)

나는

prctl(PR_SET_NAME, .....) 

를 사용하여 시도했지만 그것은 단지 수 16 바이트까지 이름을 지정할 경우에만 상단에 그 전화와 함께 작동합니다.

트릭은/proc/PID/cmdline 내용을 수정하는 것이지만 그것은 읽기 전용 필드입니다.

누구나 달성 방법을 알고 계십니까?

답변

5

여기서 스레드 별 및 프로세스 별 설정을 구분해야합니다.

prctl (PR_SET_NAME, ...)은 스레드별로 이름을 설정하고 "ps"가 c 스위치 (예 : ps Hcx)를 사용하여 해당 이름을 표시하도록 할 수 있습니다. 상단의 c 스위치를 사용하여 동일한 작업을 수행 할 수 있으므로 htop이 비슷한 기능을 수행한다고 가정합니다.

"ps"는 ​​보통 당신에게 (ps Hax와 같이) 당신이 프로그램을 시작한 명령 줄 이름과 인자 (실제로/proc/PID/cmdline이 알려주는 것)이며 직접 수정할 수 있습니다 argv [0] (원래 길이까지),하지만 이것은 프로세스 별 설정입니다. 즉, 다른 스레드에 다른 이름을 부여 할 수 없습니다. 다음

나는 일반적으로 전체 프로세스 이름을 변경하는 데 사용하는 코드입니다 :

// procname is the new process name 
char *procname = "new process name"; 

// Then let's directly modify the arguments 
// This needs a pointer to the original arvg, as passed to main(), 
// and is limited to the length of the original argv[0] 
size_t argv0_len = strlen(argv[0]); 
size_t procname_len = strlen(procname); 
size_t max_procname_len = (argv0_len > procname_len) ? (procname_len) : (argv0_len); 

// Copy the maximum 
strncpy(argv[0], procname, max_procname_len); 
// Clear out the rest (yes, this is needed, or the remaining part of the old 
// process name will still show up in ps) 
memset(&argv[0][max_procname_len], '\0', argv0_len - max_procname_len); 

// Clear the other passed arguments, optional 
// Needs to know argv and argc as passed to main() 
//for (size_t i = 1; i < argc; i++) { 
// memset(argv[i], '\0', strlen(argv[i])); 
//} 
+2

제출자는 쓰레드 당 설정을 요구했다, 아니? –

+0

왜 자신을 argv [0]의 길이로 제한합니까? – user2815333

16

버전 0.8.4 이후, htop가 옵션이 있습니다 표시 사용자 정의 스레드 이름을

보도 F2을 입력하고 Display options 메뉴를 선택하십시오. 당신이 볼 수 :

htop custom thread names