473,692 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2305
"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.programmin g.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, CreateFileMappi ng / 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
3985
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 failure? I tried it under Linux with Python-2.3.4, after few thousand forks the system just hangs! (does not return/report fork failure) Another question on module 'mutex' (stumbuled on it while looking for info on threads). What is its...
7
8812
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
16196
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 */ (void)signal(SIGCLD, SIG_IGN); /* ignore child death */
5
323
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 = fork(); value = value + 2;
1
1608
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 2.4.27 Linux kernel. Perliminary analysis of the hang shows that the child process blocks upon entering the execvp function, in which the import_lock is acquired
21
1953
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 all the print statements .
4
3888
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 & and others
5
12224
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 recently explained it to a friend of mine and at that time decided to see if I could replicate it in C#. I have created an application that, to me at least, mimics what fork would/should do on *nix/bsd systems. I know that fork spawns a new...
3
5681
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 process I launch with execv the pppd to connect an embedded system to internet. The problem is that pppd fails. I have made two test program just to discard bugs (my applications is really big) and the result is that when I call execv from a...
0
8611
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9090
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7639
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2984
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1962
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.