POSIX Thread Help


 
Thread Tools Search this Thread
Top Forums Programming POSIX Thread Help
# 1  
Old 08-26-2011
POSIX Thread Help

I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want.
When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output
2
$:

It should be 6.
Thanks in advance
Code:
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "debug.h"

#define FFORK 1
#define FTHRE 2

void* identify(void *arg);

int main(int argc, char *argv[]){

     pthread_t th_id[2];
     short int i = 0;

     (void)argc; (void)argv;
     setvbuf(stdout,NULL,_IONBF,0);

     switch (fork()){
          case -1:
               ERROR(FFORK,"Fork Failed\n"); 
          break;

          case 0:
               if (pthread_create(&th_id[0],NULL,identify,NULL) != 0 || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
                    ERROR(FTHRE,"FTHRE\n");            
               if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
                    ERROR(FTHRE,"FTHRE\n");
               exit(0);
          break;

          default:
               switch(fork()){
                    case -1:
                         ERROR(FFORK,"FFORK\n");
                    break;
        
                    case 0:            
                         if (pthread_create(&th_id[0],NULL,identify,NULL) != 0  || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
                              ERROR(FTHRE,"FTHRE\n");            
                         if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
                              ERROR(FTHRE,"FTHRE\n");
                         exit(0);
                    break;
        
                    default:
                         switch(fork()){
                              case -1:
                                   ERROR(FFORK,"FFORK\n");
                              break;
            
                              case 0:            
                                   if (pthread_create(&th_id[0],NULL,identify,NULL)  != 0 || pthread_create(&th_id[1],NULL,identify,NULL) != 0)
                                        ERROR(FTHRE,"FTHRE\n");                        
                                    if (pthread_join(th_id[0],NULL) != 0 || pthread_join(th_id[1],NULL) != 0)
                                         ERROR(FTHRE,"FTHRE\n");
                                    exit(0);
                              break;
                
                              default:
                                   for (i=0;i < 3; i++)
                                        wait(NULL);
                         }          
               }
     }

     return 0;
}

void* identify(void *arg){
    (void) arg;
     printf("%lu\n", (unsigned long int)pthread_self());
     pthread_exit(NULL);
}


Last edited by pharaoh; 08-28-2011 at 01:02 PM..
# 2  
Old 08-26-2011
please use CODE tags and indent properly so people can read the code easily.
# 3  
Old 08-28-2011
Sorry! I've just done that can you help me!
Thanks
# 4  
Old 09-06-2011
Hi,

AFAICS, there is nothing wrong with your program. However, your assumption that the thread ID have to be unique is wrong. It is only unique for each process, but not system wide:

Code:
./a.out 
140047040235264
140047031842560
140047040235264
140047031842560
140047031842560
140047040235264

Now sort-u will produce only 2 entries:
Code:
140047031842560
140047040235264

Cheers,
/Lew
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

POSIX thread runs out of memory

i am creating threads in my program using the POSIX interface. when the thread starts executing i run out of memory and get a core dump. i have tried to increase the threads stack size using pthread_attr_setstacksize, but of no use since i guess the dynamic memory is allocated on the heap and... (1 Reply)
Discussion started by: aniketkadu2002
1 Replies

2. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

3. Programming

POSIX - Hot to check if detached thread is still active

Hello, I have created program that run threads one by one, maximum 100. Each thread will process one block of data, and once it`s finished, new thread is created with new block of data....etc I have array of values to control status of each thread, like this: array_thread_status=1... (11 Replies)
Discussion started by: orangem
11 Replies

4. Programming

POSIX Thread - Memory leak

Hi all! I am implementing an http server in c++ using the posix thread, but i am having a memory leak and i cannot find the reason. I have already commented out the section that initializes the threads and i found out, the problem is when i initialize/run the threads. In the threads i have... (1 Reply)
Discussion started by: laurovalente
1 Replies

5. Programming

Posix Thread Programming

Hello, i have 2 questions: 1. Can I get the current memory usage of a thread? 2. Can I use a member-function as (void*)(*)(void*) method to create a new thread with "pthread_create(...)"?? I would be happy about any suggestion. Regards, Rolf (2 Replies)
Discussion started by: rkasel
2 Replies

6. Programming

About Native POSIX Thread Library (NPTL)

Is there anybody has documents about NPTL I wanna study about it , but can't find the documents.... anyone help appreciate :) :) :) (1 Reply)
Discussion started by: alan.zhao
1 Replies

7. Programming

Multi threading using posix thread library

hi all, can anyone tell me some good site for the mutithreading tutorials, its application, and some code examples. -sushil (2 Replies)
Discussion started by: shushilmore
2 Replies
Login or Register to Ask a Question