[SOLVED] What do I need to know to solve this problem?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I solved a problem identical to that one (never-mind how many years ago) when I was still in high school and had consumed an entire (very illicit) pitcher of beer. And you have to ask the Internet?!
Oh really? I didn't realize people knew what bathrooms were in SE Tennessee.
Oh, and also, I skipped high school. I just wasn't born knowing what synchronization was and thought maybe someone here would be kind enough to help. Thankfully, I've seen one page full of decent people and only one jerk. Not bad, I'd say!
Yes, you are right about that. Once they are in the queue, they can all go in together as soon as someone of the opposite gender comes out. I was talking about a situation where the women would walk up to the queue together. The way I was thinking of it, only one person of either gender is generated on each cycle.
Maybe only there are only k stalls in total?
Yes, I see what you mean. That's another reason I just think there is something here I'm not getting. I'm thinking that if I read enough about threads it will make sense, but so far it hasn't happened.
It seems like the problem description is (still) missing something. It certainly looks like a metaphor for thread synchronization, where each person is a thread and a bathroom stall is a resource that needs to have access to it synchronized. However, if your program is in charge of the cycles that makes it a cooperative threading problem which really makes synchronizing so much easier that it feels like cheating...
And is your program also in charge of deciding what people want to use the bathroom? I would expect that you have to at least take some kind input indicating who wants the bathroom next, but nothing you've posted so far mentions anything like that. If there is no input you could just print a fixed string and the problem is "solved".
The original I have is on a pdf file which contains names and private information of members. But here is the entire segment relating to the problem, copied and pasted exactly:
Your two-part solution is due by email on Friday, May 03 by 11:59 pm. Here is the problem you must solve:
A dorm in a university wants to show off how progressive it is and ends its long standing practice of gender-segregated bathrooms on campus. However, as a concession to tradition, it makes a policy that when a woman is in the bathroom only other women may enter, but not men, and vice versa. On the door of every bathroom there will be a sign with a sliding marker that will indicate one of three possible states it is currently in:
The project will have two parts:
1. Using pseudocode, outline the following procedures: woman_wants_to_enter, man_wants_to_enter, woman_leaves, man_leaves.
2. In C, write an application that implements the pseudocode and uses the above procedures.
You may use whatever counters and synchronization techniques you like.
**This problem is adapted from the book, Modern Operating Systems, by Tanenbaum.
-The program must have a queue where patrons will wait if they are unable to enter the bathroom. Everyone will stay in the bathroom for a set period of time.
-The program will execute for 15 or more cycles. (A cycle is defined as one iteration of a person entering a queue if unable to enter the bathroom, and/or entering the bathroom if eligible and/or exiting the bathroom.)
The program must display the following during execution:
-The number of the cycle
-the state of the bathroom (empty, or occupied by n men or n women)
-how many people are in the queue, and what their genders are.
The program can be fully automated (executes for a set number of cycles without intervention), or manually operated (it request input from the user through the console).
**This problem is designed to highlight the use of synchronization techniques in C.
I got the impression from the problem that once the toilet was emptied, the entire queue could enter (since everybody is in the queue is the same sex). So it would just be a different cycle counter for each occupant depending on their sex.
Come to think of it, I don't see why you have to break it down into woman_wants_to_enter, man_wants_to_enter, woman_leaves, man_leaves procedures. The occupants of the toilet will be the opposite sex to those in the queue so all you need is a simple test like:
IF (arrival.sex = toilet.sex) OR (toilet.sex = nil) THEN
The problem with this strategy is that a person could be stuck indefinitely in the queue as long as people of the opposite sex are going into the bathroom. See Resource starvation. A good solution would be first-come-first-serve.
The problem with this strategy is that a person could be stuck indefinitely in the queue as long as people of the opposite sex are going into the bathroom.
Clearly not a very good policy and one a crowded nightclub would be ill advised to adopt.
It does highlight however the importance of the consultation phase of any programming project. A good programming consultant will spend time with the customer clarifying his needs and pointing out consequences like the one you mentioned. Prototyping so the customer can get a feel for what he is asking for is also a good idea.
Unfortunately, in most professions, the customer is regarded as a complete idiot and his input is not valued.
Well, I got it finished and learned how to use mutexes. All my threads are now accessing the bathroom in a very orderly fashion. Thanks for the help.
The way the problem is worded, I wouldn't have used anything more sophisticated than the sleep() function. I still don't see why you needed to model the toilet patrons with threads and mutexes but glad you learned so much.