void ConceptInteractTable_MultiSet::RemoveDuplicated() { set<IdentityPair> uniqueSet(_identityPairs.begin(), _identityPairs.end()); _identityPairs.clear(); _identityPairs.insert(uniqueSet.begin(), uniqueSet.end()); }
/* Take out duplicated entries. So the result is a unique set of pids.*/ static int uniqueSet (int lhs, int rhs) { int curProc = procTable[lhs]; int dup = 0; if (lhs == rhs - 1) { if (procTable [lhs] == procTable [rhs]) return lhs; else return rhs; } rhs = uniqueSet (lhs + 1, rhs); dup = isInSet (lhs + 1, rhs, curProc); if (dup != -1) { procTable[dup] = procTable[rhs]; flagTable[dup] = flagTable[rhs]; rhs --; } return rhs; }
void initProcTable(int pid, int debuginfo) { DIR *dir = NULL; char buffer[4096] = ""; struct dirent *ent = NULL; pid_t allPpidTable[MAXPROCSARRAY]; pid_t allProcTable[MAXPROCSARRAY]; int allFlagTable[MAXPROCSARRAY]; pid_t tempPid = 0; int numAllProc = 0; int childFlag = 0; int i = 0; int currentIndex = 0; int childrenIndex = 0; pid_t pgrp = 0; char *dirNamePtr = NULL; /* Trying to find all child process by using ppid and pid relationship.*/ dir = opendir("/proc/"); while ((ent = readdir(dir)) != NULL) { dirNamePtr = ent->d_name; if (ent->d_name[0] == '.') { dirNamePtr += 1; childFlag = 1; } if (isdigit(dirNamePtr[0])) { tempPid=(pid_t)strtol(dirNamePtr,NULL,10); if(tempPid!=pid) { allPpidTable[numAllProc] = getParentPid(tempPid,childFlag); if (allPpidTable[numAllProc] == 0) { /* process must have exited, do not store information reg this pid */ continue; } allProcTable[numAllProc] = tempPid; allFlagTable[numAllProc] = childFlag; numAllProc++; } } childFlag = 0; } closedir(dir); procTable[0]=pid; flagTable[0]=0; numProc=1; while(1) { for(i=0;i<numAllProc;i++) { if(allPpidTable[i]==procTable[currentIndex]) { childrenIndex++; procTable[childrenIndex]=allProcTable[i]; flagTable[childrenIndex]=allFlagTable[i]; numProc++; } } if(currentIndex==childrenIndex) { break; } currentIndex++; } /* Trying to find all child processes by using group pid. */ dir = opendir("/proc/"); while ((ent = readdir(dir)) != NULL) { /* get pgrp related processes*/ dirNamePtr = ent->d_name; childFlag = 0; if (ent->d_name[0] == '.') { dirNamePtr += 1; childFlag = 1; } if (isdigit(dirNamePtr[0])) { tempPid=(int) strtol(dirNamePtr,NULL,10); if (tempPid==pid) continue; pgrp = getpgid (tempPid); if (pgrp == pid) { procTable[numProc]=tempPid; flagTable[numProc]=childFlag; numProc++; } } } closedir(dir); /* Trying to find all child processes via LWP on 64 bit */ if (useLWP) { sprintf(buffer, "/proc/%d/task", pid); dir = opendir(buffer); if (dir != NULL) { while ((ent = readdir(dir)) != NULL) { dirNamePtr = ent->d_name; childFlag = 0; if (isdigit(dirNamePtr[0])) { tempPid=(int) strtol(dirNamePtr,NULL,10); if (tempPid==pid) continue; procTable[numProc]=tempPid; flagTable[numProc]=childFlag; numProc++; } } closedir(dir); } } /* On some linux kernels only one of above methods works. On other kernel * versons both work. So we need to remove duplicated entries */ numProc = uniqueSet (0, numProc); if(debuginfo) { printf("Debug Info for Stats - Processes Table\n"); for(i=0;i<numProc;i++) { printf("%d-%d\n",procTable[i],flagTable[i]); } } }