Sponsored Content
Full Discussion: POSIX Thread Help
Top Forums Programming POSIX Thread Help Post 302550713 by pharaoh on Friday 26th of August 2011 09:15:46 PM
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..
 

7 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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 - 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

6. 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

7. 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
pthread_join(3C)					   Standard C Library Functions 					  pthread_join(3C)

NAME
pthread_join - wait for thread termination SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_join(pthread_t thread, void **status); DESCRIPTION
The pthread_join() function suspends processing of the calling thread until the target thread completes. thread must be a member of the current process and it cannot be a detached thread. See pthread_create(3C). If two or more threads wait for the same thread to complete, all will suspend processing until the thread has terminated, and then one thread will return successfully and the others will return with an error of ESRCH. The pthread_join() function will not block processing of the calling thread if the target thread has already terminated. If a pthread_join() call returns successfully with a non-null status argument, the value passed to pthread_exit(3C) by the terminating thread will be placed in the location referenced by status. If the pthread_join() calling thread is cancelled, then the target thread will remain joinable by pthread_join(). However, the calling thread may set up a cancellation cleanup handler on thread prior to the join call, which may detach the target thread by calling pthread_detach(3C). See pthread_detach(3C) and pthread_cancel(3C). RETURN VALUES
If successful, pthread_join() returns 0. Otherwise, an error number is returned to indicate the error. ERRORS
EDEADLK A joining deadlock would occur, such as when a thread attempts to wait for itself. EINVAL The thread corresponding to the given thread ID is a detached thread. ESRCH No thread could be found corresponding to the given thread ID. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
pthread_cancel(3C), pthread_create(3C), pthread_detach(3C), pthread_exit(3C), wait(3C), attributes(5), standards(5) NOTES
The pthread_join(3C) function must specify the thread ID for whose termination it will wait. Calling pthread_join() also "detaches" the thread; that is, pthread_join() includes the effect of the pthread_detach() function. If a thread were to be cancelled when blocked in pthread_join(), an explicit detach would have to be performed in the cancellation cleanup han- dler. The pthread_detach() function exists primarily for this purpose. SunOS 5.11 23 Mar 2005 pthread_join(3C)
All times are GMT -4. The time now is 02:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy