473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

fork or threads

i need two loops that run forever. one of it receives data and stores it
to a vector. the other one writes the elements of the vector to the
disk. this means the vector is a receiver buffer. How can I realize
this? by using threads or fork? Is it possible to access the vector in
both loops with the current data?
Jul 23 '05 #1
3 2284
"Andreas Müller" <ma**************************@amueller.org> wrote in message
news:d5**********@newsserv.zdv.uni-tuebingen.de...
i need two loops that run forever. one of it receives data and stores it
to a vector. the other one writes the elements of the vector to the
disk. this means the vector is a receiver buffer. How can I realize
this? by using threads or fork? Is it possible to access the vector in
both loops with the current data?


Way too little information... what do you mean by "receives data"? What data? "Receives" it how? Are the "receiving" and
writing to be synchronous or asynchronous?

In short, what are you trying to do (and what has it got to do with the C++ language)? Help us to help you ;-)

--
Lionel B

Jul 23 '05 #2
Lionel B wrote:
"Andreas Müller" <ma**************************@amueller.org> wrote inmessage
news:d5**********@newsserv.zdv.uni-tuebingen.de...
i need two loops that run forever. one of it receives data and stores it
to a vector. the other one writes the elements of the vector to the
disk. this means the vector is a receiver buffer. How can I realize
this? by using threads or fork? Is it possible to access the vector in
both loops with the current data?



Way too little information... what do you mean by "receives data"? Whatdata? "Receives" it how? Are the "receiving" and
writing to be synchronous or asynchronous?

In short, what are you trying to do (and what has it got to do with theC++ language)? Help us to help you ;-)


I'm using a sctp-library that receives video-frames from a sending
server. Each frame has a timestamp to determine the time I have to write
it to disk (this means to show it on the screen).
In the receiving loop I want to store all the incoming data to a vector
of structs or a map by the push_back function. this will run forever.
I guess I need another thread to write the first element of the vector
to the disk. This will occur when the timestamp of this element matches.
Is it clear enough? I need access to the vector in both threads, in the
first one to store all incoming frames to the vector, in the 2nd one to
write the elements to the disk and to delete the written element. Is it
possible to use a global vector and mutex to protect it?
Jul 23 '05 #3
On Tue, 10 May 2005 10:15:39 +0200, Andreas Müller
<ma**************************@amueller.org> wrote:
Lionel B wrote:
"Andreas Müller" <ma**************************@amueller.org> wrote in message
news:d5**********@newsserv.zdv.uni-tuebingen.de...
i need two loops that run forever. one of it receives data and stores it
to a vector. the other one writes the elements of the vector to the
disk. this means the vector is a receiver buffer. How can I realize
this? by using threads or fork? Is it possible to access the vector in
both loops with the current data?
[...]
I'm using a sctp-library that receives video-frames from a sending
server. Each frame has a timestamp to determine the time I have to write
it to disk (this means to show it on the screen).
In the receiving loop I want to store all the incoming data to a vector
of structs or a map by the push_back function. this will run forever.
I guess I need another thread to write the first element of the vector
to the disk. This will occur when the timestamp of this element matches.
Is it clear enough? I need access to the vector in both threads, in the
first one to store all incoming frames to the vector, in the 2nd one to
write the elements to the disk and to delete the written element. Is it
possible to use a global vector and mutex to protect it?


C++ contains no concept of concurrency. As the question is OT, post
on comp.programming.threads if you have further questions about
threading.

My personal recommendation is to use threads for the reader/writer and
a queue (wrapped around a list or dequeue) rather than a vector to
store the frames.

Why threads? Ease of design, mostly, but use of a shared vector and
processes may affect stability (in extreme cases). Usually, processes
have separate address spaces and so different processes will not share
global/local/heap variables. "Usually" doesn't mean it's impossible,
it just means your program would have to use shared memory (e.g.
shmget / shmat, CreateFileMapping / OpenFileMapping / MapViewOfFile)
and placement new. You'll need to create an allocater which uses
shared memory, use that allocator to create a vector which also uses
said allocator. That way, whatever dynamically allocated objects the
vector needs will be allocated within shared memory.

One problem with this approach is the extra work on your part; a
threaded approach (with mutexes &c) will probably be more efficient to
design and create than using processes. Another problem is that
shared memory may have a time/space tradeoff, depending on how it's
implemented. A consequence of this along with the fact that shared
memory often isn't resizable is that you have to worry about running
out of space. This will become particularly important when the vector
needs to grow, which will require a contiguous region of the shared
memory large enough to contain the new data. This implies shared
memory should be roughly at least 3 times what you expect the maximum
size of the vector will be. Even then, there may be times when the
reader reigns supreme and you run out of memory when the vector grows.
This is how the process/shared vector approach affects stability.

From your description, your reader/writer mostly operate at the ends
of the vector. A list or deque is much more efficient for operations
at the ends. Furthermore, for the shared memory approach, a list
doesn't need the arena to be three times as big as its expected
maximum size.

To reiterate: the "best" approach is threaded with a frame queue
(wrapping around list or dequeue) protected by a mutex or semaphore.
Just make sure you have a mechanism to prevent either the reader or
the writer from dominating access to the queue.

Kanenas
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ishwar Rattan | last post by:
Info at http://doc.python.org/ on os.fork() says that it has 'unix' semantics (on a UNIX box) on return values child pid in parent, 0 in child, no mention of failure? So, what does it return on...
7
by: C Gillespie | last post by:
Dear All, I have a function def printHello(): fp = open('file','w') fp.write('hello') fp.close() I would like to call that function using spawn or fork. My questions are:
4
by: lucifer | last post by:
hi i have code that i have to implement in c# Code: /* Become deamon + unstopable and no zombies children (= no wait()) */ if(fork() != 0) return 0; /* parent returns OK to shell */...
5
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid =...
1
by: Rotem | last post by:
Hello, We have been encountering several deadlocks in a threaded Python application which calls subprocess.Popen (i.e. fork()) in some of its threads. This has occurred on Python 2.4.1 on a...
21
by: anshul1806 | last post by:
hi all , I am wondering why a call to fork fails to print some statements in the child. It works fine when terminal is defualt for O/p . in case i try redirection it fails to redirect...
4
by: rh0dium | last post by:
Hi all, I have a problem with putting a job in the background. Here is my (ugly) script which I am having problems getting to background. There are threads about doing python script.py & ...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.