开发者

Problem forking processes and creating threads

开发者 https://www.devze.com 2023-02-11 09:20 出处:网络
My program is supposed to fork three processes. Each of these processes will create three threads and fork two additional processes. These two additional processes will create three threads.

My program is supposed to fork three processes. Each of these processes will create three threads and fork two additional processes. These two additional processes will create three threads. Here is my code. I've tried to keep things simple with nested loops. I think at some point I might be forking more processes or creating more threads.

#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);
}

int main(void){
    int i, j, k, l;
    int threadLev开发者_JAVA百科el1[2];
    int threadLevel2[3];
    printf("\n");   
    for (i = 1 ; i < 4 ; i++){ // Loop to fork the three main processes.
        if (fork() != 0){
            sleep(4);   
        }
        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++){           
                pid_t 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++){                          
                        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 0; 
}


You have a problem in your code here:

void *printme(void* Array){
    int *Arr = (int *) Array;
    int len = sizeof(Arr) / sizeof(int);

The value len will always be the same no matter what is passed in to printme. That's because C passes arrays as pointers, not as objects with embedded lengths.

0

精彩评论

暂无评论...
验证码 换一张
取 消