Skip to contentSkip to conversation

glibc pthreads actual threads?

parisc-linux Joel Soete 2 participants 7 messages May 19, 2003 – May 22, 2003

lore
Share & tools
lore

Joel Soete

author
#0 lore raw

Hi all,

Sorry in advance if it is not the right place to put this question.

But I just start to learn more details about OS (A. Tanenbaum book) and by the way try to understand a bit more about [p]threads.

I found a paper with examples (what I need much of all) and so I try by the first one: basic_example.c (which I just change a very little for my understanding)

/* for pthreads */
#include <pthread.h>
#include <stdio.h>

/* for getpid() */
#include <sys/types.h>
#include <unistd.h>

char * buf = "abcdefghijklmnopqrstuvwxyz";
int num_pthreads = 4;
int count = 60;
int fd = 1;

void * new_thread(void * arg)

{
    int i;
    pid_t PID=getpid();


    for (i = 0; i < count; i++) {
        fprintf(stderr, "PID: %d\n", PID);
	write(fd, arg, 1); write(fd, "\n", 1);
	sleep(1);
    }
    return(NULL);
}

main()
{
pthread_t thread;
int i;

   for (i = 0; i < num_pthreads; i++) {
	if (pthread_create(&thread, NULL, new_thread, (void *)(buf + i))) {
	   fprintf(stderr, "error creating a new thread \n");
	   exit(1);
	}
	pthread_detach(thread);

}
pthread_exit(NULL);
}

Which I compile as follow (gcc-3.3):
gcc -l pthread -o basic_example basic_example.c

That runs well (on my b2k running a 2.4.20-pa35 and libc6 2.3.1-17):
[...]
PID: 31360
a
PID: 31361
b
PID: 31362
c
PID: 31363
d
PID: 31360
a
PID: 31361
b
PID: 31362
c
PID: 31363
d
[...]

But notice a very strange behaviour:
_the pid is not the same for each thread_ ??

Having just a small sunos 5.8 with gcc-3.2.2 (but with native libpthread) the pb does not occurs?

Is it a limit of glibc (in general or in 2.3 only?) for all linux or only hppa?

Thanks in advance for your attention,
Joel

---------------------------------
Vous surfez avec une ligne classique ? Economisez jusqu'à 25% avec Tiscali Complete ! Offre spéciale : première année d'abonnement offerte. ... Plus d'info sur http://complete.tiscali.be

#1 lore raw
Quoted 8 lines · Joel Soete

But notice a very strange behaviour:
_the pid is not the same for each thread_ ??

Having just a small sunos 5.8 with gcc-3.2.2 (but with native libpthread) the pb does not occurs?

Is it a limit of glibc (in general or in 2.3 only?) for all linux or only hppa?

it's a limit of your understanding ;-) POSIX specifies both behaviours are legitimate.

Signature · 3 lines
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
#2 lore raw
In reply to Joel Soete
Quoted 8 lines · Joel Soete

But notice a very strange behaviour:
_the pid is not the same for each thread_ ??

Having just a small sunos 5.8 with gcc-3.2.2 (but with native libpthread) the pb does not occurs?

Is it a limit of glibc (in general or in 2.3 only?) for all linux or only hppa?

it's a limit of your understanding ;-) POSIX specifies both behaviours are legitimate.

Signature · 3 lines
"It's not Hollywood.  War is real, war is primarily not about defeat or
victory, it is about death.  I've seen thousands and thousands of dead bodies.
Do you think I want to have an academic debate on this subject?" -- Robert Fisk
author
#3 lore raw
Quoted 2 lines

it's a limit of your understanding ;-)

Normal: as mentioned I just started to learn...

Quoted 3 lines

POSIX specifies both behaviours
are legitimate.

Thanks for those clarification,
Joel

---------------------------------
Vous surfez avec une ligne classique ? Economisez jusqu'à 25% avec Tiscali Complete ! Offre spéciale : première année d'abonnement offerte. ... Plus d'info sur http://complete.tiscali.be

author
#4 lore raw
In reply to Matthew Wilcox
Quoted 2 lines

it's a limit of your understanding ;-)

Normal: as mentioned I just started to learn...

Quoted 3 lines

POSIX specifies both behaviours
are legitimate.

Thanks for those clarification,
Joel

---------------------------------
Vous surfez avec une ligne classique ? Economisez jusqu'à 25% avec Tiscali Complete ! Offre spéciale : première année d'abonnement offerte. ... Plus d'info sur http://complete.tiscali.be

author
#5 lore raw
In reply to Matthew Wilcox
Quoted 13 lines · Joel Soete
Quoted 8 lines

But notice a very strange behaviour:
_the pid is not the same for each thread_ ??

Having just a small sunos 5.8 with gcc-3.2.2 (but with native libpthread) the pb does not occurs?

Is it a limit of glibc (in general or in 2.3 only?) for all linux or only hppa?

it's a limit of your understanding ;-) POSIX specifies both behaviours are legitimate.

Just for remind and for those who also have this kind of philophical pb :-)). In fact following test, an ardent discuss born here between high level system engineer and one of them help me to find this interesting link:
<http://www.onlamp.com/pub/a/onlamp/2002/11/07/linux_threads.html>

Joel

---------------------------------
Vous surfez avec une ligne classique ? Economisez jusqu'à 25% avec Tiscali Complete ! Offre spéciale : première année d'abonnement offerte. ... Plus d'info sur http://complete.tiscali.be

author
#6 lore raw
In reply to Matthew Wilcox
Quoted 13 lines · Joel Soete
Quoted 8 lines

But notice a very strange behaviour:
_the pid is not the same for each thread_ ??

Having just a small sunos 5.8 with gcc-3.2.2 (but with native libpthread) the pb does not occurs?

Is it a limit of glibc (in general or in 2.3 only?) for all linux or only hppa?

it's a limit of your understanding ;-) POSIX specifies both behaviours are legitimate.

Just for remind and for those who also have this kind of philophical pb :-)). In fact following test, an ardent discuss born here between high level system engineer and one of them help me to find this interesting link:
<http://www.onlamp.com/pub/a/onlamp/2002/11/07/linux_threads.html>

Joel

---------------------------------
Vous surfez avec une ligne classique ? Economisez jusqu'à 25% avec Tiscali Complete ! Offre spéciale : première année d'abonnement offerte. ... Plus d'info sur http://complete.tiscali.be