Displaying the contents of buffers in a protected and non-protected critical section
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!
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).
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 == '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.
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.
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.
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 05:58 AM.|