Linux - DistributionsThis forum is for Distribution specific questions.
Red Hat, Slackware, Debian, Novell, LFS, Mandriva, Ubuntu, Fedora - the list goes on and on...
Note: An (*) indicates there is no official participation from that distribution here at LQ.
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.
Hey all,
I want to implement simple map-reduce functionality in c.. I am not using multiple CPUs. I want just to understand how to do mapping and how to reduce the result.
Till now, i got an idea, i can divide the work among multiple threads and can get the result and reduce it. But, i am not able to understand the basic functionality of threads, how they work. I am creating threads using pthread_create, but as i could not understand proper working of thread, i am not getting what to do now.
I have a simple idea that, suppose i have an array of 10000 elements, and i want to get the sum. I can use around 10 threads, which can work independently, and can return the sub-array sum.
But, how to check which thread is working, and which thread has worked for how many elements??
Sir,
i am not new to thread-programming.. i am just not sure about the exact working of calling routines.
i am able to find the sum of 1000 elements correctly with 10 threads each providing the sum of 100 elements, but i want that i create some threads, say 10, they divide the work among themselves, and give me the sum of these 1000 elements, i need not to provide that every thread should calculate sum of 100 elements by passing argument from pthread_create.
so, how to do that?
help me..
Thank you
Sir,
i am not new to thread-programming.. i am just not sure about the exact working of calling routines.
i am able to find the sum of 1000 elements correctly with 10 threads each providing the sum of 100 elements, but i want that i create some threads, say 10, they divide the work among themselves, and give me the sum of these 1000 elements, i need not to provide that every thread should calculate sum of 100 elements by passing argument from pthread_create.
so, how to do that?
help me..
Thank you
I think you need to divide the work (you can say: there should be a master thread) and all the worker threads will do their jobs. for example a worker thread can sum up 100 elements in a list and stores the result. The master thread will start 10 identical threads just all of them will get another list, or another element in the list. You do not need to watch the threads, you only need to wait the threads to complete.
I think you need to divide the work (you can say: there should be a master thread) and all the worker threads will do their jobs. for example a worker thread can sum up 100 elements in a list and stores the result. The master thread will start 10 identical threads just all of them will get another list, or another element in the list. You do not need to watch the threads, you only need to wait the threads to complete.
Yes Sir, this is exactly i want to do.. Can u suggest me how to create this master-worker model of threads.please give me some tutorial regarding that or sample C-code for that.
And also, how to divide the work among master threads.
the "master" thread is usually the first thread with which you start the program (it contains the main() function).
Here you will initialize your list or array of 1000 elements.
Now you will need a function which will sum up 100 elements in an array. This could be a simple loop, something like this:
for (int i=start; i<start+100; i++) sum+= array[i];
the inputs are the value of the start index and the variable to store the result.
now you can call pthread_create to start this function 10 times using ten different start value and ten different variable to store the result.
you can use pthread_join to wait for the threads to finish and finally you need to sum the results and print it.
see the links of tutorials there are really good sample codes to start with.
the "master" thread is usually the first thread with which you start the program (it contains the main() function).
Here you will initialize your list or array of 1000 elements.
Now you will need a function which will sum up 100 elements in an array. This could be a simple loop, something like this:
for (int i=start; i<start+100; i++) sum+= array[i];
the inputs are the value of the start index and the variable to store the result.
now you can call pthread_create to start this function 10 times using ten different start value and ten different variable to store the result.
you can use pthread_join to wait for the threads to finish and finally you need to sum the results and print it.
see the links of tutorials there are really good sample codes to start with.
Ya, i got it.. Thank You, and i succesfully implemented that, and with synchronization among threads.
Thank You for your help
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.