Hello all, U have a project that I am trying to figure out how to start, I don't have very strong programming skills, so I'm actually quite lost here. Any help is appreciated. Basically here is what I have to do:

8 processes are characterized by 5 readers and 3 writers.
Critical section in this problem is reading shared data buffer for reader and updating shared data
buffer for writer processes. It is up to you to implement any shared data for readers and writers
but you have to specify clearly following things in your sample output.
• When reader or writer enters its critical section, it has to report whether there are any reader(s)
or writer(s) other than himself.
• You may print out the data you read or write when you implement real buffer. (Optional)
• You have to print out “Panic Messages” when the rules behind this semi critical section problem
are not observed.
In your main program, you run the random number generator function to choose a process to
execute. The chosen process starts (resumes) execution and after one instruction, it will be
returned. (You should force each process run exactly one instruction then returns and waiting for
its turn.)
You can implement this using switch statement in C. Each process is one big switch statement
and will be returned after each instruction. You need to keep track of program counter of each
process to resume at the right place once it will be chosen to run by keeping global counter
variable per process.
Subproject 1: You should implement a CS problem with test and set operation as studied in the
class for this project.
In this simulation, only one reader or writer should be in its critical section. Panic messages
should be generated whenever multiple reader or writer processes are found in the critical
section.
Subproject 2: You should implement a SCS problem with binary and counting semaphores as
studied in the class for this project.
Multiple reader processes can be inside their critical section without any writer process. For a
practical reason, let’s limit the number of reader inside the critical section is limited to 3.
That means if a reader process trying to get in the critical section finds 3 other readers
already inside their critical section, 4th reader should be blocked entering critical section
until one of the readers already inside the critical section get out of it.
For writer process to go into its critical section, it should check whether there is any reader or
writer process is in the critical section.

Recommended Answers

All 10 Replies

What I don't understand is why the teacher would give you such a large project without having taught you the basics! I mean come on.. that project is obviously not a beginner project and yet you who do not know where to even start, has to write that much code..

At least attempt something and myself or others will try and push you along the way or even contribute a couple lines of code.

Well, since this is a C++ forum, let's start with some basic OO analysis. What are the objects that your program will be handling? What are their members? What methods should they have? How should they be logically organized into classes?

In more detail: Whose responsibility is it to manage critical sections? How will that be implemented?

And too many university computer science programs focus almost entirely on theoretical concepts, and leave basic programming skills as "an exercise for the reader". But still, this sounds like a junior- or senior-level course in conceptual operating system design, and how you've gotten this far without knowing how to approach a decent-sized program is quite disconcerting. What kinds of programs -have- you written so far?

I'm hoping to transfer to another school next semester because I have no intentions of becoming a programmer anymore. I've gotten so confused in this whole process of learning C++ so I'm quite frightened of it actually.

But I've gotten some info from the professor and this is what he gave me,

(writer function)
writer () }
   p(wrt);
   
   v(wrt);

}

(Reader function)
reader 1 () }
   p(mutex);
   readcount ++ ;
   if (readcount ==1) p(wrt);
   v(mutex);
//this goes into critical section
   p(mutex);
   readcount --;
   if (readcount ==0) v(wrt);
   v(mutex);

}

reader 2 () }
   p(mutex);
   readcount ++ ;
   if (readcount ==1) p(wrt);
   v(mutex);
//this goes into critical section
   p(mutex);
   readcount --;
   if (readcount ==0) v(wrt);
   v(mutex);

}

so now from my understanding this will be the function code for the program but now how and what do I write to make this work? Prof said I can just copy and paste this into code and change numbers around as I will need 5 readers and 3 writers, is that correct?

Depends on what the p() and v() functions are supposed to do, it's been a while since I had to study the theory of resource contention with producers and consumers.

At the very minimum the "}" near the starts of "functions" need to be switched to "{", and the readers and writers shouldn't really be functions anyway, they should be instances of a Reader class and a Writer class, respectively, and the program can call the doMyThing (or whatever you want to call it) method for each instance in the order necessary to demonstrate the conditions specified in the assignment.

C++ isn't frightening. Not knowing what you're doing can be, but as a previous responder wondered, how did you manage to get to a relatively high-level computer science course with such limited programming knowledge? Should you be in a lower-level course? Or did you get through the lower-level courses mostly on luck and maybe getting others to do your programming for you? No offense intended, I'm just curious why you're in this predicament, understanding it might help us help you.

Well I transfered to this curercnt college that I am in now and I was given credit for a intro to C++ class even though I never took C++ at the other school that I trasnfered from. I was able to get by the second intro to C++ class and actually understood some things of C++, but now all that info is just in bits and pieces. I definitely put myself in this predicament but hey its a lesson learned. So basically I'm now just trying to get by this class and I will be moving on from this school. I've been thinking about just dropping the class but I feel like I can get by the class with at least a B-.

OK, I read the sample "code" from your instructor, and it looks like p() is intended to "lock" a resource, and v() is intended to "release" a resource. In this case the resources are (a) a single shared "mutex" object, which makes sure that only one object (a reader or a writer) can operate while it holds the mutex, and all others requesting to lock it will be blocked until whoever holds the lock releases it again; and (b) a "wrt" object which will be held by any writer (locking out any other writers), or by the first reader that requests it (released by the last active reader). The "wrt" isn't strictly a "mutex" since those are typically locked and released by the same object/process.

If you understand the logic presented by your course, coding up the Reader and Writer classes should be as simple as writing the functions/methods p() and v().

The "work" performed by a writer would go at line 4 above (between the calls to p() and v()), and for a reader at lines 15 or 28.

So how would I start this in c++?

So how would I start this in c++?

By starting it.

class Reader {
    // reader-stuff goes here
};

class Writer {
    // writer-stuff goes here
};

int main(int argc, char *argv[])
{
    vec<Reader> readers(5);
    vec<Writer> writers(3);

    // code to demonstrate whatever the assignment asks you to demonstrate
}
class Reader {
          Reader 1 () }
          p(mutex);
          readcount ++ ;
          if (readcount ==1) p(wrt);
          v(mutex);

        p(mutex);
        readcount --;
        if (readcount ==0) v(wrt);
        v(mutex);
}

      Reader 2 () }
       p(mutex);
       readcount ++ ;
       if (readcount ==1) p(wrt);
       v(mutex);

      p(mutex);
      readcount --;
      if (readcount ==0) v(wrt);
      v(mutex);

}

       Reader 3 () }
       p(mutex);
       readcount ++ ;
       if (readcount ==1) p(wrt);
       v(mutex);

      p(mutex);
      readcount --;
      if (readcount ==0) v(wrt);
      v(mutex);

}

       reader 4 () }
       p(mutex);
       readcount ++ ;
       if (readcount ==1) p(wrt);
       v(mutex);

      p(mutex);
      readcount --;
      if (readcount ==0) v(wrt);
      v(mutex);

}

      Reader 5 () }
       p(mutex);
       readcount ++ ;
       if (readcount ==1) p(wrt);
       v(mutex);

      p(mutex);
      readcount --;
      if (readcount ==0) v(wrt);
      v(mutex);
};

class Writer {
    Writer  1 () }
      
    p(wrt)
    v(wrt);

}

   Writer  2 () }
      
    p(wrt)
    v(wrt);

}

   Writer  3 () }
      
    p(wrt)
    v(wrt);

}
};

int main(int argc, char *argv[])
{
    vec<Reader> readers(5);
    vec<Writer> writers(3);

    // code to demonstrate whatever the assignment asks you to demonstrate
}

So is this right or completely wrong

I can guarantee it won't compile, so yeah, pretty wrong. I already told you that your professor typo'd the opening curly-brace as a closing one, and his notation of Reader (1)" etc., isn't valid C, C++ or any other language I can clearly recall, it's presumably "pseudocode" designed to illustrate the basic functionality of each object, rather than to be a valid implementation in any specific language.

If you don't understand the basic concept of a C++ class (or C struct, or Pascal whatever-it-was-called-in-Pascal) as a data type, which you can then create individual instances of -- in this case, say, a Reader which incorporates whatever internal data and functionality which defines how a Reader should behave, and then separately 5 reader-instances -- then I really don't think there's a better alternative than backing up, getting yourself an introductory-level programming text, and starting from the beginning. It will take you a little while to catch back up to what you're trying to do now, but it will ultimately be far less time-consuming and frustrating than trying to understand all of it in one go, with deadlines flying past.

I'm really sorry that you "placed out of" an introductory C++ programming class which you clearly needed to take before this one, but it's as much your fault for thinking you didn't need it as it is the school's fault for allowing it.

I could keep telling you what you need to do at each step, but unfortunately, you're so far out of your depth that I really don't think it would do you any good, as you likely wouldn't learn anything clearly enough to be able to transfer it to your next assignment. I'd just be doing this assignment for you, a piece at a time, and then the next one, and each would end up being absurdly late. Drop the course if you still can.

Programming isn't scary, putting yourself in this situation is what's scary. I hope this experience doesn't ruin it for you! And truly, best of luck whatever you choose to do. If you decide to stick with it, post any questions back here in the DaniWeb forums -- as you work through a "teach yourself programming" book, create new threads for each topic/program that you'd like help with, so people know to look at it. If/when you get back to this project feel free to add your next question(s) back here. As far as books, there's a sticky-thread at the top of the C++ forum list with various recommendations. Best bet is to find a book store with a good technical section, flip through several, and buy one or two that look most useful to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.