Thread: Shell Programming - Process Table

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    Shell Programming - Process Table

    Hello,

    I'm writing a shell application that makes use of the basic system calls used to manage processes. While most things seem to work I'm having a peculiar problem:

    Everytime a new process is started it's correctly added to a list. Once it's killed it's removed.

    The problem is that the moment I kill a process the list is no longer updated and will keep on showing only the processes that were in the list, and not add anymore to them.

    Hence my question:

    1. Does this already hint something that I may be doing wrong, before posting the code?
    2. I know I may avoid using my own list all together querying the system process table for children of my shell process (which I know). However all I found was that I could access /proc/pid files to make the enquiries, but that looks over complicated (open, read and close file). Besides I don't know how to know all my children all at one time. Is there a quicker way to gather the information I need?

    I'm using Unix (on Mac OS X).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My guess is, since once you try to remove something from the list your list no longer works, that your "remove from the list" bit is broken.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    that would be really humiliating..here's the code for it:

    Code:
    void delJob(pid_t pid){
    	jobs* temp = job;
    	bool found = false;
    	if (temp->pid == pid){
    		free(temp);
    		temp = NULL;
    		job = job->next;
    		job->last = job;
    		found = true;
    	}
    	else{
    		while (temp != NULL && temp->next != NULL){
    			if (temp->next->pid == pid){
    				jobs* toDel = temp->next;
    				temp->next = temp->next->next;
    				toDel = NULL;
    				if (job->last == NULL){
    					job->last = temp->next;
    				}
    				found = true;
    				printf("%d deleted from list.\n",pid);
    				return;
    			}
    			temp = temp->next;
    		}
    		if(!found) printf("%d has not been found in the job list.\n",pid);
    	}
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    job is a global variable in the h file containing this function. Memory is allocated to it in the addJob function.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If you delete the head of the list, given that temp == job, you probably shouldn't be trying to access job->next after you free temp. (It will almost certainly still "work", but theoretically job->next doesn't exist after free(temp).
    2. I don't see you setting the backwards link after deleting a node? (I'm assuming that's what ->next is supposed to mean.)
    3. I also don't see you doing free(toDel).
    4. What is if (job->last == NULL) supposed to check? (Note that setting toDel = NULL does not change every other pointer that used to point there to NULL also.)

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    gosh..it was the code for deleting the list!

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Hello, may someone point out the error here:

    Code:
    void del(int pid){
     
    list* toDel;
    
    if (temp != NULL && temp->pid == pid){
        job = temp->next;
        toDel = temp;
        free(toDel); //here's the problem. But why and how to avoid?
      }
    }
    Last edited by simpatico_qa; 06-05-2009 at 08:42 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you say that free(toDel) is the problem? What's wrong with it? (And I'm really hoping that you're just trying to show us a snippet and that's not actually your function.)

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    what's the point of "toDel"? Why don't you just free(temp)?

    And what's the problem (double free or corruption)?
    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. #10
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Well, it's the first part of it, the other part just iterates. The problem is that I get a *** glibc detected ** free() invalid next size (fast) error when executing that line. Checking in eclipse at that point of time temp points to exactly what I want to free.

    I also didn't write the local variable jobs* temp in the snippet above.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I was freeing directly temp getting the same error. It was just a poor attempt.

    It's not double free since I get the error at the first call of it anyway.
    Last edited by simpatico_qa; 06-05-2009 at 09:01 AM.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, post the actual code...
    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

  13. #13
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    what I posted is the actual code of concern! The other portion is just iteration not reached.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by simpatico_qa View Post
    what I posted is the actual code of concern! The other portion is just iteration not reached.
    You don't walk the list? Set all the links around the to-be-deleted item so that your list doesn't shatter into a million pieces? (Hint: there are no errors in the code you posted in and of itself, only (possibly) in the way it interacts with the rest of your program.)

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You don't walk the list? Set all the links around the to-be-deleted item so that your list doesn't shatter into a million pieces? (Hint: there are no errors in the code you posted in and of itself, only (possibly) in the way it interacts with the rest of your program.)
    Yeah...if you didn't deal with the "next" pointer in the node BEFORE the deleted one, you will have a pointer which is not NULL but who's content has been free'd, which *might* produce the invalid next size error.
    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