ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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!
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.