여기서 스레드 별 및 프로세스 별 설정을 구분해야합니다.
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]));
//}
제출자는 쓰레드 당 설정을 요구했다, 아니? –
왜 자신을 argv [0]의 길이로 제한합니까? – user2815333