Thread: Pointers or fork?

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

    Pointers or fork?

    Hello,

    I don't know where is the problem in my code, since in the debbuger I cannot see what goes on with the child process. May someone help.

    Code:
    void addJob(int pid, int status){
        jobs* temp = job;
        if (temp == NULL){
        	temp = malloc(sizeof(jobs*));
        	job = temp;
        }
        else{
        	while (temp != NULL) temp = temp->next;
        	temp = malloc(sizeof(jobs*));
        }
        temp->pid = pid;
        temp->status = status; /* foreground */
    }
    
    
    exe(){
    ....
    
        if (pid == 0){
        	pid_t npid = getpid();
        	printf("If successful, the process ID will be %d.\n",npid); fflush(stdout); /* I know it reaches here */
        	addJob(npid, (bg)? 0:1);
        	if (execvp(*args, args) == -1){ perror("executing command\n"); exit(1);}
        }
    where job is a global variable.
    May I know why the statement is not executed, or is so but not adding to the list? Is it an issue of shared memory amongst the processes? I've put jobs as a static variable in a header file and imported it but I still get no difference.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    forked processes do NOT share memory.

    You need to get the child pid in the PARENT (it is the return value of fork() ) and insert it into the job list in the PARENT.

    EDIT: addJob() looks all wrong, too, but that's a different issue.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I know other methods to add an element to the end of a list, but just for my better understanding, may you tell me what's wrong such that I fix this itself?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM