Example #1
0
func count() int {
	procs := int(C.proc_listallpids(nil, C.int(0)))
	if procs <= 0 {
		return 0
	} else {
		return procs
	}
}
Example #2
0
func ListPids() []int {
	var buffer [4096]C.int
	pidCount := int(C.proc_listallpids(unsafe.Pointer(&buffer[0]), C.int(len(buffer))))
	result := make([]int, pidCount)
	for i := 0; i < pidCount; i++ {
		result[i] = int(buffer[i])
	}
	return result
}
Example #3
0
func listPids() []int {
	cnt := count()
	if cnt <= 0 {
		return make([]int, 0)
	}
	pids := C.malloc(C.size_t(cnt * int(unsafe.Sizeof(C.int(0)))))
	if pids == nil {
		return make([]int, 0)
	}
	defer C.free(unsafe.Pointer(pids))
	pidsCnt := C.proc_listallpids(pids, C.int(cnt*int(unsafe.Sizeof(C.int(0)))))
	if pidsCnt <= 0 {
		return make([]int, 0)
	}
	casted := (*[1 << 20]C.int)(unsafe.Pointer(pids))
	pidsCopy := make([]int, cnt)
	for i := 0; i < cnt; i++ {
		pidsCopy[i] = int(casted[i])
	}
	return trimPidArray(pidsCopy)
}