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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-19-2010, 02:06 AM
|
#1
|
LQ Newbie
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11
Rep:
|
Displaying the contents of buffers in a protected and non-protected critical section
Hi,
I have this project for my operating systems class and I have put together the basic flow chart to aid me in writing the program. I know how to use pipes as a buffer to hold info. I know how to create a binary semaphore. But what I dont know is this:
How to "use a delay adjustment parameter K in the critical section to adjust the speed of the display process to show that without semaphore protection the displayed contents of the buffer are randomly interleaved."
First off, I am definitely not asking anyone to give me the solution. But I do need some guidance. So I figure there will be an if statement with two options:
1. If true, use semaphore protection to enter/exit critical section
2. If false, no semaphore protection -- this is where the contents of the buffer should be interleaved.
Now does that mean that as each child process enters the non-protected critical section, it should "sleep" for a randomized time? I mean, will this allow my output to be interleaved??
So lets say my command line looks like this:
filename N opt K
N = number of child processes
opt = 'n' for no semaphore protection and 's' for semaphore protection
K = delay adjustment parameter
So if i enter: semaphore 4 n 100
what happens to the 100? Is it randomized using rand and srand and passed as a parameter to sleep() inside the critcal section??
Please help if you understand what I am talking about.
Thanks so much!
|
|
|
11-19-2010, 05:09 PM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
How to "use a delay adjustment parameter K in the critical section to adjust the speed of the display process to show that without semaphore protection the displayed contents of the buffer are randomly interleaved."
|
It is just asking you to place some code at the point where the display process is accessing the buffer, to slow down the access (inside the critical section). Presumably you have a display loop accessing the buffer, so the loop itself should have delays inside it. By doing this, the buffer access will be spread over a longer period of time, which increases the chance of overlaps with other child processes. This will allow the semaphore protection option to have a more visible effect.
It is not asking you to make the delay a random one (just to make it a parameter). Though that would be an interesting exercise too (perhaps for bonus points)!
Incidentally, you may find usleep more useful than sleep if this is in a *nix environment (because it is finer-grained).
Last edited by neonsignal; 11-19-2010 at 05:11 PM.
|
|
|
11-22-2010, 01:23 AM
|
#3
|
LQ Newbie
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11
Original Poster
Rep:
|
Hi,
Sorry for the late reply. I have been spending alot of time working on this project and still do not understand how to create a display loop that will interleave the output of my buffer.
Essentially here is what my program does:
1. creates N children
2. each child writes its child number(N), its PID, its parent PID, and its child PID in a buffer. *another question, is semaphore protection needed on the way in?*
3. I use a switch statement to control where the child and parent go after the fork
4. So my parent goes to default and also serves as the reader of the FIFO pipe.
5. Now, I have an if statement that says, if argv[2] == 'n' go to no semaphore protection (but with the whole K parameter thing) else go to semaphore protection (still with the whole K parameter thing).
I am stuck at how to create a loop that will display the contents of my buffer and on top of that, how to utilize the K parameter. As an example, the professor used k = 200 as a parameter and it shows that without semaphore protection, the first 3-4 bytes of whatever each child wrote into the buffer comes out, then gets mixed in a random order with the other child processes data.
|
|
|
11-22-2010, 03:20 AM
|
#4
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
If there is only a single reader process, then you will not get accidental interleaving of data on output. The accidentally interleaving can only happen when multiple processes are accessing the buffer at the same time (in your example, the multiple writing processes).
Because the writes are so fast and the pipe buffer is big, there is a good chance that the child processes will not interfere with each other (which makes for risky code, because it appears to work most of the time). To force clashes more often, you would have to extend the time taken by the writes (which for example means breaking them up into multiple writes, writing a single byte at a time and having delays in between).
However, your question talked about slowing down the display process. This would make more sense if there were multiple display processes.
The semaphore is to arbitrate between the processes that will interfere with each other. You are using the semaphore as a way of ensuring that the block of data is indivisible; so if you have multiple writing processes, they will need to use the semaphore.
There is of course nothing wrong with you talking to your lecturer about it if the question is not clear.
Last edited by neonsignal; 11-22-2010 at 03:23 AM.
|
|
|
11-22-2010, 04:48 PM
|
#5
|
LQ Newbie
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11
Original Poster
Rep:
|
Quote:
Originally Posted by neonsignal
If there is only a single reader process, then you will not get accidental interleaving of data on output. The accidentally interleaving can only happen when multiple processes are accessing the buffer at the same time (in your example, the multiple writing processes).
Because the writes are so fast and the pipe buffer is big, there is a good chance that the child processes will not interfere with each other (which makes for risky code, because it appears to work most of the time). To force clashes more often, you would have to extend the time taken by the writes (which for example means breaking them up into multiple writes, writing a single byte at a time and having delays in between).
However, your question talked about slowing down the display process. This would make more sense if there were multiple display processes.
The semaphore is to arbitrate between the processes that will interfere with each other. You are using the semaphore as a way of ensuring that the block of data is indivisible; so if you have multiple writing processes, they will need to use the semaphore.
There is of course nothing wrong with you talking to your lecturer about it if the question is not clear.
|
I think I have a better idea now...I will leave the write part alone, because it doesnt need to have any delays.
But on the way out (display/read), can I accomplish a delay in display if I insert sleep(parameter k% some number). I still dont feel like this is the correct way to interleaf, but I cant seem to find any info on this online. I feel like its supposed to be a more natural process.
The other question is, is it possible for the children that write into the buffer also turn around and read from the buffer? I think that is what is supposed to happen but again, I dont know how to do that.
Thanks again.
|
|
|
11-22-2010, 09:41 PM
|
#6
|
LQ Newbie
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11
Original Poster
Rep:
|
I had it wrong, I dont need to use PIPES to do this project. Just a simple buffer. That helps alot. The other thing I learned is that the K parameter is used as a dummy loop to kill time while in the critical section. I will implement and see if this does what it intends.
|
|
|
All times are GMT -5. The time now is 07:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|