Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
My problem is that when creating my producers and consumers, if I don't create an equal number of both, the program will hang once it reaches the pthread_join() for-loops in my main. (i.e. more produce than consume, more consume than produce)
Assignment requirements:
-Use a buffer of size 5
-Create any number of producers and consumers
-Display the current state of the buffer per thread
a) including the current read/write position of the thread
b) if the value consumed is prime
-A final report output of the entire simulation.
3. The attempts at a solution (include all code and scripts):
I have tried moving my locks around in each thread, not using joins, not using my vectors, joining in different orders, checking for segfaults in the loops.
As well as tons of cout statements.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Texas A&M University Corpus Christi
Corpus Chrsti, TX, USA
Dr. Ajay Katangar
falcon.tamucc.edu/~akatangur/fa12/COSC3346/
Seems like one sort of exclusive on the one container is enough. Nobody can add or remove but by that lock. Now, a container manager that serviced queues of work in and filled small queues of work into each consumer, that might make for less contention, as no party has to deal with anyone but the manager, who is on the other end of a ring buffer or some such low contention mechanism. The manager can give precedence to output over input and serve all in rotation on each side. Even with threads, ipc flavors of mechanisms are nice. You could do it in the old message queue mechanism, too. Since everyone is in one memory space, messages just have item pointers.
Seems like one sort of exclusive on the one container is enough. Nobody can add or remove but by that lock. Now, a container manager that serviced queues of work in and filled small queues of work into each consumer, that might make for less contention, as no party has to deal with anyone but the manager, who is on the other end of a ring buffer or some such low contention mechanism. The manager can give precedence to output over input and serve all in rotation on each side. Even with threads, ipc flavors of mechanisms are nice. You could do it in the old message queue mechanism, too. Since everyone is in one memory space, messages just have item pointers.
I'm sorry but I just don't know what you're trying to say.
If you lock the global-lock, they try and fail to get data, both will stay locked until the writer adds more -- which it can't because the global lock is stuck locked.
For single-reader and single-writer, just two semaphores should do -- one for read, one for write. Keep the read and write separate and they shouldn't stomp on each other.
Procedure for writing:
1) Wait on write-sem
2) Add data to array
3) Increment and wrap write position
4) Post on read-sem
Procedure for reading:
1) Wait on read-sem
2) Copy element from array
3) Increment and wrap read position
4) Post on write-sem
If you want these to work with multiple readers or multiple writers, you will need a global mutex around items 2 and 3, but only lock the global mutex after you've waited on the read or write sem! Otherwise you'll deadlock(reader blocking for more data, writer can't add more data because the global mutex is locked).
If you lock the global-lock, they try and fail to get data, both will stay locked until the writer adds more -- which it can't because the global lock is stuck locked.
For single-reader and single-writer, just two semaphores should do -- one for read, one for write. Keep the read and write separate and they shouldn't stomp on each other.
Procedure for writing:
1) Wait on write-sem
2) Add data to array
3) Increment and wrap write position
4) Post on read-sem
Procedure for reading:
1) Wait on read-sem
2) Copy element from array
3) Increment and wrap read position
4) Post on write-sem
If you want these to work with multiple readers or multiple writers, you will need a global mutex around items 2 and 3, but only lock the global mutex after you've waited on the read or write sem! Otherwise you'll deadlock(reader blocking for more data, writer can't add more data because the global mutex is locked).
I thought I was doing that essentially?
My Producer's mutex lock is after wait, with my Consumer's mutex lock after it's wait. Both have the unlock before the post.
Is it the type of semaphores I'm using are wrong? I've got empty initialized to buffer_size, which is 5, with full as zero?
(Although, I'm still not clear why this should work if I have more than 5 threads of each going. That would leave the extras in limbo or something?)
Although, I'm still not clear why this should work if I have more than 5 threads of each going. That would leave the extras in limbo or something?)
The simplest model is with a mutex - like pthread_mutex_t
pseudo code
Do the exact same thing with write or send. Only keep the mutex during the read or write. Do nothing else when you own the mutex.
This model is fine for the situation where you have some processing to do with data, and you read/write and process using threads. I/O is always slower than other operations.
If you have almost no processing to do then threads are not going to gain you much if anything. Your app will always be I/O bound whether it is threaded or not. The idea with threads is to do data crunching or whatever in parallel. When parallelism gains you nothing, skip threading. So if you are misusing threading, then other threads are in fact superfluous.
To avoid hanging on pthread_join, make the threads not joinable, when they return or call
they simply go away.
Could you please give your inputs on the below issue:
source.xml
<?xml version="1.0" encoding="UTF-16"?>
<P1 >
<C1 type="i"><2></C1>
<V1 type="string"><6.2></V1>
<D1 type="string">
<D2><1.0></D2>
<D2><2.0></D2>
</D1>
......................
......................
many more... (7 Replies)
Hi All,
I am transfering a file through sftp. But the script is hanging at exit occasionally.
I am suspecting that sftp is hanging due to buffer size issue.
If that is the case can any body suggest a solution.
Please find the code.
echo "cd /${CUST_ID}/inbound/${SAFET_ID}" >... (0 Replies)
/bin/sed -n '$q;5633653,0 p' lfile
lfile is a log file that is being updated several times a second.
so, the command above figures out the line number it wants in the lfile and then proceeds to output all lines from that line number to the end of the file.
the problem is, the end of the... (2 Replies)
Hey folks. Long time lurker, first time poster. I'm a bit of a newbie at "coding" (obviously, scripting is a teensy bit different than coding) and I've run into a problem that I just can't seem to get around.
I'm going through a list of servers to check their name, IP, reverse-NSLOOKUP name and... (2 Replies)
Hi Folks,
When i execute the command df -kh in my system the o/p hangs..
The command runs fine but takes a lot of time before coming back to the # prompt.
Can anyone please suggest the possible cause and solution?. (10 Replies)
Hi Experts,
I have a programming assignment that asks us to implement a pipegrep function. it basically has 5 stages and each stage has a thread and buffers are used between stages.
am currently implementing stage 1 . In stage 1 am suppose to read directory and store the filenames in buffer1... (15 Replies)
Hello,
I have the following shell script and when i execute, it keeps hanging and nothing happens
Please let me know.
Requirement is to read data from file and pass it to the sql and create files as shown.
code
/********
#!/bin/sh
while read user.dat
do
echo "user = $1 email =... (1 Reply)
Ok, this question my be different. I can ping our unix box, but when we I to access the webpage I cant. To access the webpage I type http://ipaddress:some port. How do I check if a port is hanging and how would I un hang it. Sorry if question doesnt make sense. (1 Reply)
hi all,
i had a program which created a socket on port 7113, but for some reason the program was hunbg and I had to Ctrl+c it. I ran the program for a couple of times and now when I do a netstat -a I see that these sockets are lingering and that might be the reason why my program is not... (1 Reply)
Hello,
I just tried to run this command: /usr/lib/sendmail -d0.1 -bt < /dev/null | grep -i version
Its doing what I want: writing out the sendmail version.
But on some machines it writing the version and then exit to the prompt but on others its writing the version but then hangs, I need to... (0 Replies)