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 have created the threads for implementing different tasks... I want to exchange some data between them but i am really confused what should i use and what not
I can use pipes , ipc, shared memory, sockets but what is the best for my problem?
What kind of data do you want to exchange? (Small) messages, a big chunk of memory, or a stream of bytes? Is there the possibility that once your threads will be able to run on two different computers connected by a network?
1) If your threads run on the same machine, and in the same process address space, then I would suggest simply using global memory with access to the data controlled by semaphores or some such mechanism to insure that you won't have multiple writers touching the same memory at the same time. That is the easiest and induces the least overhead (which make it the fastest during run time).
2) If your threads run on the same machine, but run in different process address spaces, then I would suggest using shared memory with access to the data again controlled by semaphores or some such mechanism. This is also very easy to implement and induces just a little more overhead.
3) Lastly, if your threads will ever need to be run on separate machines, then I would suggest using TCP/IP sockets.
However, if you start out just needing to implement #1, but may eventually need to move to something like #3, then I would use sockets from the getgo. It really just boils down to how much overhead and time you want to spend in implementing the most cost effective algorithms for your specific requirements.
I have a related question. I have an app that starts up several threads, and I want them to communicate via message queues. Currently, I am developing under Cygwin. The code compiles and links (with the '-Wall' option so it's strict), but I get a 'bad system call' runtime error as soon as the first call to msgget() is made:
// Create the projector inbox
printf("Creating projector inbox...");
projector_inbox_id = msgget(INBOX_PROJECTOR, 0666 | IPC_CREAT);
if (projector_inbox_id < 1) {
fprintf(stderr, "SmartHub: unable to create the projector inbox.\n");
exit(-1);
}
The code examples I see usually use the messaging API for inter-process communication, not inter-thread communication. Is that my problem?
Can you try your program on a nativ linux machine? Or maybe you can try colinux.
Just to clarify: You run your programm under cygwin on a Windows machine, is that correct?
cygwin may not implement all system calls. You may check the cygwin manual.
Installation of colinux is a better way for using linux on windows, i guess.
Unfortunately we are a Windows shop and I cannot currently run this application natively (although I am working to remedy this). However, I have test apps that create threads and make use of mutexes and these ran without a hitch. This is what biased me towards the hypothesis that I did not understand how the message queue API was intended to be used.
You may have a look at colinux. It is called a cooperative os. it runs a complete unix kernel,
while you have your windows up and running. It is worth a look. However, installation is not
that easy i think. (i haven't tried by myself.)
you create some huge files (say 1 - 2GB) These will be used as virtual filesystems for the
linux box. In these files a linux system is installed and you run the colinux daemon and
there you go...
Be sure to design the app so that there is not a one-to-one correspondence between "user requests" and "threads to handle them." Instead, a pool of worker threads should be created, all of which sit and wait for a request, then do it, then check for another request or sleep until one arrives. This allows the total number of active threads to be predictable and constant, therefore manageable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.