Thread: Shell Programming - Process Table

  1. #16
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I do walk the the list in the subsequent lines of code but since the debugger stops before doing it and I'm at the first function call I avoid posting it. Also walking through the debugger the process is quite simple:

    there is a global variable job that points to the list, and in the operation temp points to it first. Addition to the list is done from the beginning, and at the time of the call there are two elements.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't walk the list in the successive lines of code. How do you find the element to delete without walking the list?

  3. #18
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Actually toDel or temp at that point points to the first element. Hence nothing points to it other than temp.

    I do walk the list, but only later so the problem is before. All what happens before is adding to a global list two nodes, and then try to delete the first (head) one, which results in the above.

    This line job = temp->next, means that the global list should now point to its second element. Then I try to free the first, now point to only by temp.

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try:
    Code:
    if (temp != NULL && temp->pid == pid){
        job = temp->next;
        toDel = temp;
        free(toDel); 
        temp = NULL;
        }
    }
    If you are freeing the node and then testing for NULL in a loop, beware that a free'd node is not automatically NULL, so at the next iteration temp will not be NULL, but temp->next will not exist either.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #20
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Looks like you don't get what I try to say. The debugger doesn't go past the free(temp) call. So adding any line past it won't help.

    I don't walk the list, I check right away the first element, and it happens to be the desired element to delete.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by simpatico_qa View Post
    Looks like you don't get what I try to say. The debugger doesn't go past the free(temp) call. So adding any line past it won't help.

    I don't walk the list, I check right away the first element, and it happens to be the desired element to delete.
    If that's what's actually happening, you wouldn't be getting errors. Since you're getting errors, that's not what's happening. If free(temp) is throwing an error, then temp must not point at memory that was malloc'ed (either it's a pointer to New Jersey, or you've passed in an array). So show your code.

  7. #22
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    If that's what's actually happening, you wouldn't be getting errors. Since you're getting errors, that's not what's happening. If free(temp) is throwing an error, then temp must not point at memory that was malloc'ed (either it's a pointer to New Jersey, or you've passed in an array). So show your code.
    Ditto. My first guess was that you would say

    Quote Originally Posted by simpatico_qa View Post
    Looks like you don't get what I try to say. The debugger doesn't go past the free(temp) call. So adding any line past it won't help.
    And I was right...the point being, there must be some strange malicious EM field tinkering with your computer if what you are saying is true.

    Debuggers are not infallible. I do not use eclipse. Why don't you throw this in:
    Code:
    if (temp != NULL && temp->pid == pid){
        job = temp->next;
        toDel = temp;
        free(toDel); 
        printf("->HERE<-\n"); fflush(stdout);
        }
    }
    Make sure you include the fflush() command!! This will verify that you are not being misled by the debugger.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #23
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    This is getting interesting. Actually in the debugger I see that both jobs and temp point to the list nodes (structures) wanted.

    I've attached a screenshot of the debugger with memory addresses.

    I'll remark again, on Mac OS X this is not an issue. It shows up running the code on Linux.

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by simpatico_qa View Post
    This is getting interesting*. Actually in the debugger I see that both jobs and temp point to the list nodes (structures) wanted.
    So now you are trying to prove this:
    there must be some strange malicious EM field tinkering with your computer
    Congratulations. It looks like you are stuck with a problem that cannot be solved.

    *no
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that still leaves us option b: the memory at 0x00100170 was not allocated by malloc and therefore cannot be freed, and I forgot option c: the memory at 0x00100170 was freed in a previous call but if you didn't set your links correctly when you deleted it it's still in your list.

  11. #26
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Was allocated memory as shown below. First call to free in the whole program, so discard c?

    Code:
    void addJob(const pid_t pid, const int status, const char* fileName){
    	jobs* new = malloc(sizeof(jobs*));
        new->pid = pid;
        new->status = status;
        new->fileName = fileName;
        new->next = job;
        job = new;
        printf("%d added to the list.\n",pid);
    }

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh wait, looks like there is a problem right there:
    Code:
    jobs* new = malloc(sizeof(jobs*));
    it should be:
    Code:
    jobs* new = malloc(sizeof(jobs));
    or better yet:
    Code:
    jobs* new = malloc(sizeof(*new));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    also, in the debugger you see it points to a struct with all fields initialized, doesn't that imply that memory is allocated to it?

  14. #29
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    why *new would be better than jobs? Doesn't it cost an extra 'follow'?

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by simpatico_qa View Post
    also, in the debugger you see it points to a struct with all fields initialized, doesn't that imply that memory is allocated to it?
    There is memory allocated to it, but this amount of memory:
    Code:
    jobs* new = malloc(sizeof(jobs*));
    is different than this amount of memory:
    Code:
    jobs* new = malloc(sizeof(jobs));
    I don't think (*new) is better than (jobs), but laserlight is generally more intelligent and knowledgeable than I.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM