Permalink
Browse files

Still a shit load to do

  • Loading branch information...
1 parent 8762532 commit 1ed325febd10a4e9a72493cd582fd721970eba9c @shanliyu committed Feb 17, 2017
Showing with 611 additions and 79 deletions.
  1. BIN .pa3d.c.swp
  2. +2 −2 mykernel3.c
  3. BIN mykernel3.o
  4. BIN pa3a
  5. BIN pa3b
  6. BIN pa3c
  7. BIN pa3d
  8. +156 −77 pa3d.c
  9. +453 −0 pa3f.c
View
Binary file not shown.
View
@@ -101,7 +101,7 @@ void MyWait (p, s)
{
semtab[s].proctab[semtab[s].tail].valid = TRUE;
semtab[s].proctab[semtab[s].tail].pid = p;
- semtab[s].tail++;
+ semtab[s].tail = (semtab[s].tail + 1) % MAXPROCS;
Block(p);
}
}
@@ -121,7 +121,7 @@ void MySignal (p, s)
{
semtab[s].proctab[semtab[s].head].valid = FALSE;
int tempHead = semtab[s].head;
- semtab[s].head++;
+ semtab[s].head = (semtab[s].head + 1) % MAXPROCS;
Unblock(semtab[s].proctab[tempHead].pid);
}
}
View
Binary file not shown.
View
BIN pa3a
Binary file not shown.
View
BIN pa3b
Binary file not shown.
View
BIN pa3c
Binary file not shown.
View
BIN pa3d
Binary file not shown.
View
@@ -165,6 +165,10 @@ struct{
}Road[MAXPROCS];
int Roadsem;
+
+ int nummiddle;
+ int nummiddlesem;
+ int genmutex;
}shm;
void Main ()
@@ -212,12 +216,9 @@ void Main ()
void InitRoad ()
{
/* do any initializations here */
- Printf("%d\n",sizeof(shm));
- //Register SHM here.
-
Regshm((char *) &shm, sizeof(shm));
- shm.FirstProc = 1;
+ shm.FirstProc = TRUE;
shm.FirstProcsem = Seminit(1);
shm.eastsem = Seminit(0);
@@ -235,12 +236,15 @@ void InitRoad ()
shm.curFromsem = Seminit(1);
+ shm.genmutex = Seminit(1);
+
for(int i = 0; i < MAXPROCS; i++)
{
shm.Road[i].occupied = FALSE;
}
shm.Roadsem = Seminit(1);
-
+ shm.nummiddle = 0;
+ shm.nummiddlesem = Seminit(1);
}
#define IPOS(FROM) (((FROM) == WEST) ? 1 : NUMPOS)
@@ -254,65 +258,71 @@ void driveRoad (from, mph)
c = Getpid (); // learn this car's ID
-
+ Wait(shm.genmutex);
Wait(shm.FirstProcsem);
-
+ /*
+ Wait(shm.curFromsem);
+ Wait(shm.carOnRoadsem);
+ Wait(shm.numwestsem);
+ Wait(shm.numeastsem);
+ Wait(shm.Roadsem);
+ */
//if there is already a process created before
- if((shm.FirstProc--) < 1)
+ if(!shm.FirstProc)
{
-
- Signal(shm.FirstProcsem);
-
- //if the side of this proc is not the same as the current side
Wait(shm.curFromsem);
+ //if the side of this proc is not the same as the current side
if(shm.curFrom != from)
{
Signal(shm.curFromsem);
- //if there is proc on road, or proc blocked from any side
Wait(shm.carOnRoadsem);
Wait(shm.numwestsem);
Wait(shm.numeastsem);
+ //if there is proc on road, or proc blocked from any side, BLOCK it
if(shm.carOnRoad != 0 || shm.numwest != 0 || shm.numeast != 0)
{
- Signal(shm.carOnRoadsem);
- Signal(shm.numwestsem);
Signal(shm.numeastsem);
+ Signal(shm.numwestsem);
+ Signal(shm.carOnRoadsem);
if(from == WEST)
{
- Wait(shm.numwestsem);
+ Signal(shm.FirstProcsem);
+
+ Wait(shm.numwestsem);
shm.numwest++;
Signal(shm.numwestsem);
- Wait(shm.westsem);
+ Signal(shm.genmutex);
+ Wait(shm.westsem);
}
else
{
- Wait(shm.numeastsem);
+ Signal(shm.FirstProcsem);
+
+ Wait(shm.numeastsem);
shm.numeast++;
Signal(shm.numeastsem);
- Wait(shm.eastsem);
+ Signal(shm.genmutex);
+ Wait(shm.eastsem);
}
}
-
//if there is no car on road, or any car blocked, then this car should enter
//and will change the curFrom accordingly
else
{
- Signal(shm.carOnRoadsem);
- Signal(shm.numwestsem);
Signal(shm.numeastsem);
+ Signal(shm.numwestsem);
- Wait(shm.curFromsem);
- shm.curFrom = from;
- Signal(shm.curFromsem);
-
- Wait(shm.carOnRoadsem);
shm.carOnRoad++;
Signal(shm.carOnRoadsem);
+
+
+ Signal(shm.FirstProcsem);
+ Signal(shm.genmutex);
}
}
@@ -321,125 +331,194 @@ void driveRoad (from, mph)
{
Signal(shm.curFromsem);
- //if there is proc blocked from any side, we BLOCK it accordingly
Wait(shm.numwestsem);
Wait(shm.numeastsem);
+ //if there is proc blocked from any side, we BLOCK it accordingly
if(shm.numwest != 0 || shm.numeast != 0)
{
Signal(shm.numwestsem);
Signal(shm.numeastsem);
+
if(from == WEST)
{
- Wait(shm.numwestsem);
+ Signal(shm.FirstProcsem);
+
+ Wait(shm.numwestsem);
shm.numwest++;
Signal(shm.numwestsem);
- Wait(shm.westsem);
+ Signal(shm.genmutex);
+ Wait(shm.westsem);
}
else
{
- Wait(shm.numeastsem);
+ Signal(shm.FirstProcsem);
+
+ Wait(shm.numeastsem);
shm.numeast++;
Signal(shm.numeastsem);
- Wait(shm.eastsem);
+ Signal(shm.genmutex);
+ Wait(shm.eastsem);
}
}
//if no proc blocked on either side and
//if first position is already filled by a proc, then BLOCK
+ //TODO NOT TRUE, WE NEED TO BLOCK IT BUT WITH SEMAPHORE OF nummiddle
else if(from == WEST )
{
- Signal(numwestsem);
- Signal(numeastsem);
-
+ Signal(shm.numwestsem);
+ Signal(shm.numeastsem);
+
Wait(Roadsem);
if(shm.Road[0].occupied)
{
- Signal(Roadsem);
+ Signal(shm.Roadsem);
- Wait(shm.numwestsem);
- shm.numwest++;
- Signal(shm.numwestsem);
+ Wait(shm.nummiddlesem);
+ shm.nummiddle;
+ Signal(shm.nummiddlesem);
- Wait(shm.westsem);
+ Signal(shm.FirstProcsem);
+ Signal(shm.genmutex);
+
+ Wait(shm.middlesem);
}
//if first position is not occupied, then we let the proc enter the road
+
else
{
- Signal(Roadsem);
+ shm.Road[0].occupied = TRUE;
+ Signal(shm.Roadsem);
+
+ Signal(shm.carOnRoadsem);
+ shm.carOnRoad++;
+ Wait(shm.carOnRoadsem);
- Wait(shm.carOnRoadsem);
- shm.carOnRoad++;
- Signal(shm.carOnRoadsem);
+ Signal(shm.FirstProcsem);
+ Signal(shm.genmutex);
}
+
}
else if(from == EAST)
{
- Signal(numwestsem);
- Signal(numeastsem);
-
+ Signal(shm.numwestsem);
+ Signal(shm.numeastsem);
+
Wait(Roadsem);
if(shm.Road[MAXPROCS - 1].occupied)
{
- Signal(Roadsem);
+ Signal(shm.Roadsem);
- Wait(shm.numeastsem);
- shm.numeast++;
- Signal(shm.numeastsem);
+ Wait(shm.nummiddlesem);
+ shm.nummiddle;
+ Signal(shm.nummiddlesem);
- Wait(shm.eastsem);
- }
- else
- {
- Signal(Roadsem);
-
- Wait(shm.carOnRoadsem);
- shm.carOnRoad++;
- Signal(shm.carOnRoadsem);
- }
+ Signal(shm.FirstProcsem);
+ Signal(shm.genmutex);
+ Wait(shm.middlesem);
}
}
}
//if this is the first proc ever created, then just let it enter.
else
{
- Signal(shm.FirstProcsem);
+ Signal(shm.FIrstProcsem);
- Wait(shm.curFromsem);
- shm.curFrom = from;
- Signal(shm.curFromsem);
+ shm.FirstProc = FALSE;
+ }
- Wait(shm.carOnRoadsem);
- shm.carOnRoad++;
- Signal(shm.carOnRoadsem);
+ /* IF THE PROC CAN EXECUTE TILL HERE, IT MEANS THAT IT IS ALLOWED TO ENTER,
+ * THEREFORE WE WILL MAKE CHANGES TO THE VALUES TO SIGNIFY THE ENTRANCE
+ */
+
+ //occupy the road
+ if(from == WEST)
+ {
+ shm.Road[0].occupied = TRUE;
+ }
+ else
+ {
+ shm.Road[NUMPOS-1].occupied = TRUE;
}
+ shm.curFrom = from;
+ shm.carOnRoad++;
EnterRoad (from);
PrintRoad ();
Printf ("Car %d enters at %d at %d mph\n", c, IPOS(from), mph);
+
+ /*
+ Signal(shm.Roadsem);
+ Signal(shm.numeastsem);
+ Signal(shm.numwestsem);
+ Signal(shm.carOnRoadsem);
+ Signal(shm.curFromsem);
+ */
+ Signal(shm.FirstProcsem);
+
+ for (i = 1; i < NUMPOS; i++)
+ {
+ Wait(shm.Roadsem);
+ Wait(shm.nummiddlesem);
- for (i = 1; i < NUMPOS; i++) {
- if (from == WEST) {
- p = i;
- np = i + 1;
- } else {
- p = NUMPOS + 1 - i;
- np = p - 1;
- }
+ if (from == WEST)
+ {
+ p = i;
+ np = i + 1;
+ }
+
+ else
+ {
+ p = NUMPOS + 1 - i;
+ np = p - 1;
+ }
+
+ /* ADDED FROM HERE */
+ //TODO: INCORRECT HERE, SHOULD LET THE PROC STAY AT THE SAME POSITION
+ //if the next position is occupied, then we BLOCK this proc
+ if(shm.Road[np-1].occupied)
+ {
+ shm.nummiddle++;
+ Wait(shm.middlesem);
+ }
+
+ shm.Road[p-1].occupied = FALSE;
+ shm.Road[np-1].occupied = TRUE;
Delay (3600/mph);
ProceedRoad ();
PrintRoad ();
- Printf ("Car %d moves from %d to %d\n", c, p, np);
+ Printf("Car %d moves from %d to %d", c, p, np);
+ Printf(" || ");
+ for(int j = 0; j < 10; j++){
+ Printf(" %d", shm.Road[j].occupied);
+ }
+ Printf(" Car on Road: %d\n", shm.carOnRoad);
+
+
+ Signal(shm.Roadsem);
+ Signal(shm.nummiddlesem);
}
Delay (3600/mph);
ProceedRoad ();
PrintRoad ();
Printf ("Car %d exits road\n", c);
+
+
+ Wait(shm.Roadsem);
+ Wait(shm.carOnRoadsem);
+
+ shm.carOnRoad--;
+ shm.Road[np-1].occupied = FALSE;
+
+ Signal(shm.carOnRoadsem);
+ Signal(shm.Roadsem);
+
}
Oops, something went wrong.

0 comments on commit 1ed325f

Please sign in to comment.