How can i parse the 14, 15, 16 and 17th value in data taken from proc/[pid]/stat

(Bidhan Bastola) #1
func (p *Unixprocess) Refresh() error {
	statPath := fmt.Sprintf("/proc/%d/stat",
	dataBytes, err := ioutil.ReadFile(statPath)
	if err != nil {
		return err

	// First, parse out the image name
	data := string(dataBytes)
	binStart := strings.IndexRune(data, '(') + 1
	binEnd := strings.IndexRune(data[binStart:], ')')
	p.binary = data[binStart : binStart+binEnd]  //Holds the process name

	// Move past the image name and start parsing the rest
	data = data[binStart+binEnd+2:]
	_, err = fmt.Sscanf(data,
		"%c %d %d  %d",

	return err

(Jakob Borg) #2

Guys. You’re all asking questions that amount to roughly the same thing, and also (in my opinion) being somewhat rude about it. Just writing a topic title and pasting a blob of code is a very lazy way to post a question, shifting the burden of understanding the question and context onto lots of readers instead of you taking the time to explain. Instead, take the time to explain your problem, show a complete snippet of code that illustrates the problem (preferably on, and provide some example data that is required to run it, etc.

If you’re all in the same class and studying these things, talk to each other. Ask your instructor who is presumably paid to explain.

If this is part of a class assignment to solve problems individually by seeking out resources on the internet you are going about it the wrong way - see above about asking questions properly.

We’re all happy to help, but speaking for just myself this is getting somewhat tiring.

Edit: I see that essentially the same thing was said in another topic, but better expressed. Anyway.

(system) #3

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.