LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-15-2005, 01:15 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
communication between threads


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?
 
Old 07-15-2005, 02:07 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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?
 
Old 07-15-2005, 02:37 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
I need to exchange small messages.. just to pass some stucts with data.... the threads will always run on the same pc..
 
Old 07-15-2005, 10:55 AM   #4
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hello,

My suggestions:

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.
 
Old 07-15-2005, 01:14 PM   #5
jfitzger68
LQ Newbie
 
Registered: Jul 2005
Location: San Rafael, CA, USA
Distribution: Cygwin, FedoraCore 4
Posts: 18

Rep: Reputation: 0
Related question

Hi,

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?

Thanks,
Josh Fitzgerald
 
Old 07-15-2005, 02:04 PM   #6
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
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.
 
Old 07-15-2005, 03:36 PM   #7
jfitzger68
LQ Newbie
 
Registered: Jul 2005
Location: San Rafael, CA, USA
Distribution: Cygwin, FedoraCore 4
Posts: 18

Rep: Reputation: 0
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.
 
Old 07-15-2005, 03:49 PM   #8
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
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...
 
Old 07-15-2005, 04:35 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 07-15-2005, 04:39 PM   #10
jfitzger68
LQ Newbie
 
Registered: Jul 2005
Location: San Rafael, CA, USA
Distribution: Cygwin, FedoraCore 4
Posts: 18

Rep: Reputation: 0
Wow, what a cool project! So I guess you are thinking my program should work, eh?
 
  


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
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
handset / pc communication cigarstub Linux - Networking 1 10-30-2005 10:35 AM
Stop communication otisthegbs Linux - Wireless Networking 2 12-09-2004 08:01 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
communication redirection chtthies Linux - Newbie 1 11-11-2003 08:33 PM

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

All times are GMT -5. The time now is 02:12 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