Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google site



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-29-2009
Registered User
 

Join Date: Apr 2009
Posts: 3
Posix threads

Hi,
consider the code below:


Code:
#include <stdio.h>
      .
      .

struct myStruct
{  
   char *message ;
   int id;
 };
     .    
     .
     .
void *thread_function( void *ptr );
nt main()
{
     pthread_t thread1, thread2 ,thread3 ;
     struct myStruct nico1;
     struct myStruct nico2;
     struct myStruct nico3;
     nico1.message="Thread1";
     nico1.id=0;
     nico2.message="Thread 2";
     nico2.id=1;
     nico3.message="Thread 3";
     nico3.id=2;
     int  iret1, iret2,iret3;
 
     iret1 = pthread_create( &thread1, NULL, thread_function, (void*)&nico1);
     iret2 = pthread_create( &thread2, NULL, thread_function, (void*)&nico2 );
     iret3= pthread_create( &thread3, NULL, thread_function, (void*)&nico3);
       .
       .
       .
     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL);
     pthread_join( thread3, NULL); 
     
     exit(0);
}


void *thread_function( void *ptr )
{
    int k,j;
    struct myStruct *nico;
         nico=( struct myStruct*)ptr;
    k=1;
    while(k<10)
    {
        printf(" THREAD # %d \n", nico->id);
        printf("****************** \n ******************");
        printf("                    \n ");
        printf("in loop while k=%d : \n ",k);
        printf("                    \n ");
        printf("                    \n ");
        
        
        //then for j=1,...,k-1
                for(j=(nico->id); j<k; j+=3)  //Start at 0 for C
                {
            
            
            printf(" in loop for j=%d : \n ",j);
            printf("                    \n ");
    
                }
        
        
        k=k+1;
    }
 return NULL;
}

so I expected that theses three threads execute in parallel,and therfore in
I expected to have mixed displays of threads in terminal .but after the execution I have a regular display of threads,i.e. thread 1 prints all its
while loop(from 1 to 9),after thread 2 ,and after thread3 it seems that there
is no parallel execution of these threads,I try to increase the number
of iteration of loop while to 100 but nothing changes.
Any thoughts?
(I am using a multiprocessor machine and Kubuntu)

here is example of the execution:

THREAD # 0
******************
******************
in loop while k=1 :


in loop for j=0 :

THREAD # 0
******************
******************
in loop while k=2 :


in loop for j=0 :

THREAD # 0
******************
******************
in loop while k=3 :


in loop for j=0 :

THREAD # 0
******************
******************
in loop while k=4 :


in loop for j=0 :

in loop for j=3 :

THREAD # 0
******************
******************
in loop while k=5 :


in loop for j=0 :

in loop for j=3 :

THREAD # 0
******************
******************
in loop while k=6 :


in loop for j=0 :

in loop for j=3 :

THREAD # 0
******************
******************
in loop while k=7 :


in loop for j=0 :

in loop for j=3 :

in loop for j=6 :

THREAD # 0
******************
******************
in loop while k=8 :


in loop for j=0 :

in loop for j=3 :

in loop for j=6 :

THREAD # 0
******************
******************
in loop while k=9 :


in loop for j=0 :

in loop for j=3 :

in loop for j=6 :

THREAD # 1
******************
******************
in loop while k=1 :


THREAD # 1
******************
******************
in loop while k=2 :


in loop for j=1 :

THREAD # 1
******************
******************
in loop while k=3 :


in loop for j=1 :

THREAD # 1
******************
******************
in loop while k=4 :


in loop for j=1 :

THREAD # 1
******************
******************
in loop while k=5 :


in loop for j=1 :

in loop for j=4 :

THREAD # 1
******************
******************
in loop while k=6 :


in loop for j=1 :

in loop for j=4 :

THREAD # 1
******************
******************
in loop while k=7 :


in loop for j=1 :

in loop for j=4 :

THREAD # 1
******************
******************
in loop while k=8 :


in loop for j=1 :

in loop for j=4 :

in loop for j=7 :

THREAD # 1
******************
******************
in loop while k=9 :


in loop for j=1 :

in loop for j=4 :

in loop for j=7 :

THREAD # 2
******************
******************
in loop while k=1 :


THREAD # 2
******************
******************
in loop while k=2 :


THREAD # 2
******************
******************
in loop while k=3 :


in loop for j=2 :

THREAD # 2
******************
******************
in loop while k=4 :


in loop for j=2 :

THREAD # 2
******************
******************
in loop while k=5 :


in loop for j=2 :

THREAD # 2
******************
******************
in loop while k=6 :


in loop for j=2 :

in loop for j=5 :

THREAD # 2
******************
******************
in loop while k=7 :


in loop for j=2 :

in loop for j=5 :

THREAD # 2
******************
******************
in loop while k=8 :


in loop for j=2 :

in loop for j=5 :

THREAD # 2
******************
******************
in loop while k=9 :


in loop for j=2 :

in loop for j=5 :

in loop for j=8 :

Thanks,
B.

Last edited by Franklin52; 04-29-2009 at 06:29 PM.. Reason: adding code tags
Sponsored Links
  #2 (permalink)  
Old 04-29-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
 

Join Date: Dec 2008
Location: .at
Posts: 2,960
First, please put [code ][/code] tags (sans the space) around your listing, it's easier on the eyes. And since we're on the subject, could you shorten your output?

Second: Your thread has to run 8 printf()s, which translates to about 25 system instructions, times 10 = 250 instructions. Given that even a P90 from the last century can do roughly 90000000 instructions per second, your code is done long before the kernel has any reason to preempt it.
  #3 (permalink)  
Old 04-30-2009
fpmurphy's Avatar
Moderator
 

Join Date: Dec 2003
Location: Florida
Posts: 2,101
You need to create your threads as joinable i.e.


Code:
pthread_attr_t attr;
....
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
....
pthread_create( &thread1, &attr, thread_function, (void*)&nico1);
....

Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
POSIX threads and data safety radiatejava Programming 2 03-04-2009 05:59 AM
OpenMP 3.0 for C w POSIX Threads Runtime 0.5 Linux Bot OS X OpenSource RSS 0 01-10-2009 01:40 AM
Posix Krrishv Programming 2 10-11-2008 03:54 AM
Threads and Threads Count ? varungupta UNIX for Advanced & Expert Users 2 03-21-2008 09:23 PM
POSIX threads !_30 Programming 2 12-15-2006 12:27 PM



All times are GMT -4. The time now is 02:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0