LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2007, 03:04 PM   #1
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Rep: Reputation: 0
Questions about multythreading


Hi everybody! Here is my questions:
1) Are there any difference to linux between thread and process?
2) I know that in linux i can determine the thread id with pthread_self() or gettid(). But for the same thread they return different values. What function goes wrong?
3) Can I assign thread to a given CPU on an SMP system?
4) I have multithreaded application. How can i check my self and prove that it works on all CPUs in the system?
You are welcome to discuss.

P.S. Linux laptop 2.6.17-5mdv #1 SMP Wed Sep 13 14:32:31 EDT 2006 i686 Intel(R) Celeron(R) M processor 1400MHz GNU/Linux

Last edited by tduglas; 04-20-2007 at 12:51 AM.
 
Old 04-19-2007, 04:13 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I think I can answer 1 & 2 for you.

1) For one, processes are complete separate process. So, to share data, you have to use shared memory, pipes, and other forms of IPC. W/ threads, they share the same adress space, so you don't have to worry about how your going to access the data, just how to do it safely.

2) Pthread's exact implementation is left to the implementer. pthread_self probably won't return a process id, but it could I suppose. If your using pthreads, you should use pthread-related functions, not process related functions. Conceptually, threads and processes could have a n:m relationship, so one single thread may run within multiple processes throughout its lifetime.
 
Old 04-19-2007, 11:57 PM   #3
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
according to a certain book on kernel, on linux kernel space, threads and processes are the same.
 
Old 04-20-2007, 12:44 AM   #4
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by 95se
2) Pthread's exact implementation is left to the implementer. pthread_self probably won't return a process id, but it could I suppose. If your using pthreads, you should use pthread-related functions, not process related functions. Conceptually, threads and processes could have a n:m relationship, so one single thread may run within multiple processes throughout its lifetime.
Can you give me an example of how one single thread may run winthin multiple processes throughout its lifetime?
 
Old 04-20-2007, 12:47 AM   #5
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by archieval
according to a certain book on kernel, on linux kernel space, threads and processes are the same.
Thanks, that's what i wanted to see.
Question 1 is closed.
 
Old 04-20-2007, 01:52 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by tduglas
Thanks, that's what i wanted to see.
Question 1 is closed.
You shouldn't close that question too fast.
The Linux threading model has changed. In 2.4 kernels, the default is the LinuxThreads library, which is not fully conformant with the POSIX standard. Each thread is a process with its own pid.
In kernel 2.6, NPTL (Native Posix Thread Library) is now the default. NPTL uses the CLONE_PID flag so threads share the same pid.
 
Old 04-20-2007, 04:46 AM   #7
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
and what about questions 3 and 4?
 
Old 04-20-2007, 09:56 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by tduglas
and what about questions 3
You may try pthread_setaffinity_np or sched_setaffinity.
Quote:
and 4?
Try xcpustate or a similar utility to see if all CPUs are busy.
 
Old 04-20-2007, 03:22 PM   #9
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre
You may try pthread_setaffinity_np or sched_setaffinity.
Strange thing, man pthread_setaffinity_np prints "No manual entry...".


But from manual page of sched_setaffinity i can conclude, this function is linux-specific. I can use gettid() with it to assign given thread to the CPU. So, my code is going to be system-dependent.

Searching google for man pthread_setaffinity_np didn't help to get me specifications. Does anyone have complete description?
 
Old 04-20-2007, 03:50 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by tduglas
Strange thing, man pthread_setaffinity_np prints "No manual entry...".
Yep, looks like no one contributed a manual page ...
Multi-threading support, implementation and documentation used to be somewhat chaotic with Linux.
Quote:
But from manual page of sched_setaffinity i can conclude, this function is linux-specific. I can use gettid() with it to assign given thread to the CPU. So, my code is going to be system-dependent.
It would be too with pthread_setaffinity_np, the _np suffix means "non portable".
As far as I know, pthread_setaffinity_np is a wrapper to sched_setaffinity so you may directly use the latter.

You may also have a look at plpa:
http://www.open-mpi.org/software/plpa/

Last edited by jlliagre; 04-20-2007 at 03:51 PM.
 
Old 04-22-2007, 09:43 PM   #11
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Can sched_setaffinity() be use to increase the probability of task to be executed right away when the current task is made to sleep? Or the set_nice_priority() is enough?

Moreover, what would be the most efficient way of task creation? pthread_create() or clone()?
Sorry for too many questions, I want to know these too. Thanks!
 
Old 04-23-2007, 12:59 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by archieval
Can sched_setaffinity() be use to increase the probability of task to be executed right away when the current task is made to sleep? Or the set_nice_priority() is enough?
I'm usure about what you are trying to achieve.
Quote:
Moreover, what would be the most efficient way of task creation?
What kind of task ?
Quote:
pthread_create() or clone()?
Clone is non portable, use fork() if you want to create a process.
 
Old 04-23-2007, 02:04 PM   #13
tduglas
LQ Newbie
 
Registered: Apr 2007
Location: Moscow
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
By the way, does anybody know, how to evaluate the CPU time, consumed by a given thread?
 
Old 04-23-2007, 06:03 PM   #14
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
does this help?
 
Old 04-24-2007, 03:06 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by tduglas
By the way, does anybody know, how to evaluate the CPU time, consumed by a given thread?
I'm using prstat on Solaris to get thread statistics.
I don't what is the equivalent, if any, with Linux. Perhaps some top option but I'm unsure.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic questions on hostname and domain name + related postfix questions Moebius Linux - Newbie 7 09-04-2007 11:50 AM
swaret, questions, questions!! BashTin Slackware 11 04-26-2007 03:37 AM
Solaris - Questions! Questions! Questions! qs_tahmeed Solaris / OpenSolaris 2 07-16-2005 05:27 AM
window manager questions and/or theme questions t3gah Linux - Software 2 02-27-2005 04:16 PM
Questions Mentes Slackware 3 07-12-2003 04:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration