-2

My question is something I find very weird. In fact, I'm listing all the child processes of a given process.

For this, I use : How to find all child processes? and RC's answer.

But, I need an exit with unsigned long. And the solution give some string based on a char[]

If I want to get unsigned long, I need to modify the code to get only numeric value. The other case will not be correct.

So I've put the char as unsigned long. Then, the scanf with %ld tags.

But I'm getting crap ...

Why does that not work ?

EDIT : Code :

const char *name = "top";
char command[100];
unsigned long parentID; unsigned long processID;

strcpy(command, "ps -C "); strcat(command, name); strcat(command, " --format '%P %p'");

FILE *fp = popen(command, "r");
    if (fp == NULL) fprintf(stderr, "Error in reading PID child processes information !\n");

while(fscanf(fp, "%lu %lu", parentID, processID) != EOF) {
    printf("PID : %lu Parent : %lu \n", processID, parentID);
}

return processID;

With, %s and char[256], this work !

EDIT : Toughts

I was thinking of two ways to do this:

  • First, using unsigned long instead of char[].
  • Second, convert char[] to unsigned long after reading the file. However, I'd need to read it line by line. Is it possible with popen ?
Community
  • 1
  • 1
Mike Telson
  • 127
  • 2
  • 2
  • 7

1 Answers1

1

So I've put the char as unsigned long. Then, the scanf() with %ld tags.

And here your program invokes undefined behavior, since the type specifier for unsigned long is %lu.

(And probably there are some other, impossible-for-us-to-tell errors as well in your code; changing the return values from char * to unsigned long is anything but trivial.)

  • Is there a way to convert char[] to unsigned long that is simplier than the ones with byte shifting ? Can I open a File with popen and read it line by line ? – Mike Telson Apr 02 '13 at 13:50
  • @MikeTelson Now how those two are related? –  Apr 02 '13 at 13:51
  • Well, if the first way does not work, I need a B plan ... This is for getting unsigned long from char[], I already have a working solution with output as char[]. I saw ways to convert from char[] to unsigned ... but they are a bit ... hard ... – Mike Telson Apr 02 '13 at 13:52
  • @MikeTelson Is that `char[]` actually a NUL-terminated string with the decimal (or whatever kind of) representation of a non-negative integer number? If it is, use `strtoul()`. –  Apr 02 '13 at 13:56
  • Well, it seems to work ! But I got also a string, that is converted to 0. But I don't think any User process has the PID "0" doesn't it ? If not, a simple if will solve the problem – Mike Telson Apr 02 '13 at 14:15
  • @MikeTelson Perhaps either there indeed is one, or the representation of the numbers is not what you assume it to be. –  Apr 02 '13 at 14:18
  • I'm completely sure my numbers are accurate PIDs. What I'm not sure is what become of the strings I'm parsing with strtoul ... well, I'll go on with that. Thanks for the response anyways. – Mike Telson Apr 02 '13 at 14:33