Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Quiz about synchronization of 3 process

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quiz about synchronization of 3 process

    I'm trying to solve following quiz:

    The confluence of 3 two-way roads is managed by a French-style roundabout so:
    1. the right of way is for those who are already traveling at the roundabout;
    2. the direction of travel is counterclockwise.


    Each sector i of the roundabout (section between two consecutive accesses i and i+1 mod3 ) can contain a maximum MAX_i number of cars.
    Represent the the behavior of cars when crossing the roundabout.
    Each car is represented by by a process that can recall three functions:
    • INPUT(i): used to access from outside to the roundabout from access i;
    • NEXT(i): used to move from sector i of the roundabout to the next one;
    • EXIT(i): used to exit the roundabout at the end of sector i.


    For simplicity, set MAX_i = 1 for each sector and simplify the priority rule. for vehicles coming from outside, establishing that a car can enter the roundabout access only if the sector is not currently occupied and the sector is not previous. The issue of deadlock is being discussed.
    This is my solution, what do you think?
    class Example {
    semaphore mutex = 1;
    semaphore sectors[3] = {0, 0, 0};
    boolean isFree = true;
     
    public void input (int i) {
        P(mutex);
        while (sectors[i]==0) {
            //wait for the sector
        }
        isFree = true;
        P(sectors[i]);
        V(mutex);
    }
     
    public void next (int i) {
        P(mutex);
        while (sectors[i+1]==0) {
            //wait for the next sector
        }
        isFree = true;
        V(mutex);
    }
     
    public void exit (int i) {
        P(mutex); //only 1 process can enter in the rondabout
        if (isFree) { //check the rondabout
            isFree = false;
            V(sectors[i]);
        }
        V(mutex);
    }
    I think that the deadlock can be caused only if all the process try to access to the same sector, right?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Quiz about synchronization of 3 process

    Also posted at: https://www.dreamincode.net/forums/t...-of-3-process/

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Handling IO Synchronization
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: September 17th, 2014, 11:28 AM
  2. synchronization
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2013, 08:29 AM
  3. problem in synchronization
    By codenoob in forum Threads
    Replies: 3
    Last Post: December 8th, 2011, 08:29 AM
  4. synchronization problems
    By stroodlepup in forum Threads
    Replies: 2
    Last Post: February 28th, 2011, 09:26 AM
  5. synchronization
    By bbr201 in forum Java Theory & Questions
    Replies: 6
    Last Post: August 29th, 2010, 09:32 AM