problem in running pthread application

Ratheesh ratheeshreddy at gmail.com
Sat Oct 14 16:14:03 EST 2006


Hello everybody,
                         I have a problem in running a pthread based
application whose source is given below.
//sample.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread1_function(void *);
void *thread2_function(void *);

int main(void)
{
 pthread_t thread1,thread2;
 char *message1 = "Thread 1";
 char *message2 = "Thread 2";
 int iret1,iret2;
 iret1 = pthread_create(&thread1,NULL,thread1_function,(void*)message1);
 iret2 = pthread_create(&thread2,NULL,thread2_function,(void*)message2);

 pthread_join(thread1,iret1);
 pthread_join(thread2,iret2);
 printf("Thread 1 returns: %d\n",iret1);
 printf("Thread 2 returns: %d\n",iret2);
 exit(0);
}

void *thread1_function(void *ptr)
{
 char *message;
 message = (char *) ptr;
 while(1)
 {
  printf("%s\n",message);
  sleep(5);
 }
}

void *thread2_function(void *ptr)
{
 char *message;
 message = (char *) ptr;
 while(1)
 {
  printf("%s\n",message);
  sleep(8);
 }
}
//end of sample.c

I compiled using the command $powerpc-linux-uclibc-gcc -lpthread -o sample
sample.c

when I run on my target board(MPC850 based custom board), I find multiple
copies of the same program(I am invoking only once) is running. ps -e
command show the following output.

PID  Uid     VmSize Stat Command
    1 root        392 S   init
    2 root            SW  [keventd]
    3 root            SWN [ksoftirqd_CPU0]
    4 root            SW  [kswapd]
    5 root            SW  [bdflush]
    6 root            SW  [kupdated]
    7 root            SW  [rpciod]
   36 root       1284 S   /bin/snmpd -Dagentx
   41 root       1024 S   /bin/bash
   42 root        372 S   /sbin/syslogd -n -m 0
   43 root        348 S   /sbin/klogd -n
   51 root        296 S   sample
   52 root        296 S   sample
   53 root        296 S   sample
   54 root        296 S   sample
   56 root        372 R   ps

Why is this program is running multiple copies in the memory? How to avoid
this.If I compile
the same program in PC(x86 with glibc) It is running fine(no multiple
copies). So how to avoid this what is wrong with my program? Please help me.

Regards,

Ratheesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ozlabs.org/pipermail/linuxppc-embedded/attachments/20061014/4d5b1d72/attachment.htm 


More information about the Linuxppc-embedded mailing list