problems with fork

This is a discussion on problems with fork within the C Programming forums, part of the General Programming Boards category; pls help.. i have spent 1 day on this stupid problem. im pretty sure the solution is pretty simple but ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Post problems with fork

    pls help.. i have spent 1 day on this stupid problem. im pretty sure the solution is pretty simple but im new in c

    I have modified the code and removes all the unnesserary code with "....." so its easier to spot my mistake
    Im creating 100 threads
    I have to create 2 processors which take care of these threads, but its no working. The only thread which is working is the first thread

    Code:
    int main(int argc, char *argv[])
    
    ........
    
    	res = pthread_create(&patientThread, NULL, (void *)&skrenke, (void *)&p[antal]); 
    		
    ......		
    }
    
    void skrenke(void *ID)
    {
    
    ............
    
    int mod = nr%2 //each processor should take every secound thread eg. if its nr 1, processor 1 has to take care of it, and if its nr 2 processor 2 has to take care of it
    
    	if(nr==0) // nr=0 is the first thread which create the 1st processor
    
    
                  {
                  pid = fork();
                   }
    	if(pid==0 && mod==0)
    	      {
    	      printf("\nthe first processor is taking care of this thread\n");
                   }
           if(nr==1) // nr=01 is the secound thread which creates the 2nd processor 
                  {
                  pid1 = fork();
                   }
           if (pid1==0 && mod ==1)
          {
           printf("the secound processor is taking care of thread 1 and every secound thread
          }
    
    };
    the first and the secound thread prints out the right message ( they are the one which creates the processors as well), but i wanted the 3, 4... 100 thread threads to print out the right message as well.. but its not working.. pls help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by raja9911
    I have modified the code and removes all the unnesserary code with "....." so its easier to spot my mistake
    Im creating 100 threads
    Yes, you are. Now knock it the ........ off. You don't need to make a new thread every single time you want help with this same damn topic.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > I have modified the code and removes all the unnesserary code with "....."
    If you knew what was unnecessary, you would find the problem yourself.

    Personally, I prefer slightly longer, but compilable "as is" without any guess-work posts to this mangled beyond recognition mess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork problems
    By modec in forum Linux Programming
    Replies: 1
    Last Post: 10-21-2005, 12:48 AM
  2. Problems with execvp and fork....
    By LightsOut06 in forum C Programming
    Replies: 3
    Last Post: 10-09-2005, 06:34 PM
  3. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05:58 AM
  4. problems with fork() and execve()
    By kristy in forum C Programming
    Replies: 4
    Last Post: 12-21-2003, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21