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,
I'm trying to set my program running in two threads on two CPU's in parallel. It reports that both treads have started successfully, and yet only the first thread runs on one CPU. However if I add sleep(1) statement in the beginning of the thread function I am able to see both threads run in parallel taking two CPU's. Can someone explain me what why is this happening?
Thanks is advance and here is the code I am referring to.
int errors = 0;
for (int t = 0; t < threads; t++) {
if (pthread_join(thread_data[t], &thread_result[t])) {
printf("ERROR: Thread #%d join failed\n", t + 1);
return 1;
}
int res = *(int*)(thread_result[t]);
errors += !res;
printf("Thread #%d joined, it returned %d\n", t + 1, res);
}
Even if you have two CPUs, it's entirely up to the system dispatcher as to which thread runs where, when.
Bear in mind that if a thread does any I/O, as apparently both of your threads do, this significantly affects their execution because both of them are now firmly tied to the single resource .. a virtual terminal.
You have to give the threads some actual 'real work' to do, or the 1st thread will just spin in 1 cpu and the other will never really get started.
As soon as they start doing any kind of eg disk or net i/o, you should get both going, but you can use the 'yield()' routine to hint to the OS that it should give the other thead(s) a chance.
however, as sundialsvcs said above, it's really down to the OS/kernel.
randyding:
I read you post... I still do not understand why kernel is not utilizing all available resources on 4 CPU machine...
sundialsvcs:
Interesting observation: If I replace while(1) loop with 'for' loop, thread 2 starts eventually (with like one minute delay). I have other program that uses 2 threads, both of which use alot of I/O from different file descriptors. That program works fine. The only difference I can think of is that the second program has each CPU utilization slightly below 100% due to the I/O wait...
chrism01:
I wrote this dummy code to simulate and debug my other program which does alot of in-memory computations without I/O and fails to take advantage of the dual-core machine. I'm basically trying to recreate the environment as close as possible to understand the nature of this weird behavior.
You're worrying too much about the scheduler, and your latest change actually interferes with what it's doing by ordering it to schedule them "round-robin."
Here's my friendly advice: let the scheduler do its job, and you do yours. (I don't mean any offense nor anything offensive by that: it's just an expression.) Once you've got two threads working, and if you've given them both reasonable jobs to do, the scheduler will see to it that the threads get a fair share of available CPU time and they will also see to it that the CPUs are kept busy.
When your system basically doesn't have any workload, and/or the threads aren't doing anything, you're never going to see an accurate picture. The situation is too artificial. Frankly, you're focusing on this (non-)issue much too much. (Again, no offense!) Spend your time now on telling the threads what to do.
Well, the only reason I got involved with multiple threads was to decrease the total execution time by distributing almost identical function calls over two cores. When I did that scheduler still used only one core of the CPU and execution time in fact increased. So, I was wondering why is it not using the other core.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.