LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 11-19-2010, 02:06 AM   #1
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Rep: Reputation: 0
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!
 
Old 11-19-2010, 05:09 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 11-22-2010, 01:23 AM   #3
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 11-22-2010, 03:20 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 11-22-2010, 04:48 PM   #5
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
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.
 
Old 11-22-2010, 09:41 PM   #6
mashhype
LQ Newbie
 
Registered: Sep 2010
Distribution: Ubuntu Linux 10.04 Lucid Lynx
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Protected mode MTK358 Programming 38 04-17-2010 12:19 PM
Why I have some protected packages marozsas Suse/Novell 3 06-04-2007 01:32 PM
Protected PDF Schopy Linux - Software 3 04-13-2005 06:03 PM
protected folders tommytomato Linux - Security 6 09-21-2004 11:23 PM
protected directories tommytomato Linux - Security 2 01-11-2004 10:34 PM


All times are GMT -5. The time now is 06:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration