Unable to create thread

This is a discussion on Unable to create thread within the C Programming forums, part of the General Programming Boards category; Hello! I'm supposed to write a program forks 3 processes (4 in total, including the father). and then have each ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Unable to create thread

    Hello!
    I'm supposed to write a program forks 3 processes (4 in total, including the father). and then have each process fork three threads and two additional process. These two additional processes will fork three threads as well. You can guess from the unnecessary complexity that this is a class assignment.
    Anyway, I wrote the following code that displays a string that tells you what thread that is. For instance, one output should say "I'm thread 2.2.3". Meaning thread 2 from child 2 of process 3 and so on.
    My program works for first level threads. But I can't display the threads created by the additional processes. In display mode, the third number is just a random big number, which tells me some kind of address problem.
    Here is the code:
    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void *printme(void* Array){
    	int *Arr = (int *) Array;
    	int len = sizeof(Arr) / sizeof(int);
    	if (len == 1){
    		printf("I'm thread %d.%d",Arr[0],Arr[1]);
    	}
    	else if (len == 2){
    		printf("I'm thread %d.%d.%d",Arr[0],Arr[1],Arr[2]);
    	}
    	printf("\n");
    	pthread_exit(NULL);
    }
    
    void main(){
    	int i, j, k, l;
    	int threadLevel1[2];
    	int threadLevel2[3];
    	printf("\n");	
    	for (i = 1 ; i < 4 ; i++){ // Loop to fork the three main processes.
    		if (fork() != 0){
    			sleep(2);	
    		}
    		else{
    			//The newly forked process will create three threads and fork two additional processes.
    			
    			for (j = 1 ; j < 4 ; j++){
    				pthread_t *t;				
    				threadLevel1[0] = i;
    				threadLevel1[1] = j;				
    				if (pthread_create(t, NULL, printme, (void*) threadLevel1) != 0){
    					perror("pthread_create");
    					exit(1);
    				}	
    			}
    			for (k = 1; k < 3 ; k++){			
    				int a = fork();				
    				if (a != 0){
    					sleep(2);		
    				}
    				else if (a == -1){
    					perror("fork"); /* display error message */
            				exit(0); 
    				}
    				else{
    					for (l = 1 ; l < 4 ; l++){
    						printf("Coooooooool");								
    						pthread_t *t;					
    						threadLevel2[0] = i;
    						threadLevel2[1] = k;
    						threadLevel2[2] = l;	
    						if (pthread_create(t, NULL, printme, (void*) threadLevel2)!= 0)						 							{
    							perror("pthread_create");
    							exit(1);						
    						}						
    					}	
    				}
    			}
    		}
    	}	
    	return;
    }
    PS: The part where is says Cooooool doesn't get displayed. It'll be cool if it did.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Gee... I wonder if that stray brace way over on the right hand side has something to do with it.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Haha, no it doesn't. I don't how it ended up there.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The correct way to create a thread is
    Code:
     pthread_t t;
     if (pthread_create(&t, NULL, printme, (void*) threadLevel1) != 0){
    or allocate pointer before passing to pthread_create.

    Code:
    	int len = sizeof(Arr) / sizeof(int);
    This doesn't seem right. Isn't it.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    yeah, I fixed that earlier.
    About the len variable, I need to know the size of the array so that I print the correct string. I did a check and it turns out len is always 1. How do I get the size of Arr in C? Why is a function as elementary as the size of an array not implemented. It's too obscure.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unable to create pthreads for chat ap
    By Fillis52 in forum Networking/Device Communication
    Replies: 10
    Last Post: 11-03-2010, 11:11 AM
  2. fork a child or create a thread?
    By zhoufanking in forum C Programming
    Replies: 3
    Last Post: 06-21-2008, 04:48 AM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 11:59 PM
  4. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM

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