Created
May 10, 2012 22:15
-
-
Save pjmagee/2656243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(void) | |
{ | |
int up[2]; | |
int down[2]; | |
pipe(up); | |
pipe(down); | |
if(fork() == 0) | |
{ | |
close(up[0]); | |
close(down[1]); | |
int passenger_location = rand()%11; | |
int lift_location; | |
int bIsInLift = 0; | |
do | |
{ | |
write(up[1], &passenger_location, sizeof(passenger_location)); // WRITE PASSENGER LOCATION | |
read(down[0], &lift_location, sizeof(lift_location)); | |
if(bIsInLift) | |
{ | |
//Waiting in lift | |
//NOW SELECT NEW FLOOR | |
int new_location = rand()%11; | |
write(up[1], new_location, sizeof(new_location)); | |
sleep(1); | |
read(down[0], % | |
} | |
else if(passenger_location != lift_location) | |
{ | |
printf("PASSENGER: I AM ON FLOOR: %d, THE LIFT MONITOR SAYS IT IS ON FLOOR: %d\n", passenger_location, lift_location); | |
} | |
else | |
{ | |
bIsInLift = 1; | |
write(up[1], &bIsInLift, sizeof(bIsInLift)); // IM IN THE LIFT | |
} | |
} | |
while(lift_location != 10); | |
} | |
else | |
{ | |
close(up[1]); | |
close(down[0]); | |
int lift_location = 0; | |
int passenger_location; | |
int bPassengerAboard = 0; | |
do | |
{ | |
read(up[0], &passenger_location, sizeof(passenger_location)); | |
if (lift_location != passenger_location) | |
{ | |
sleep(2); // TRAVEL | |
if (lift_location < passenger_location) // LESS | |
{ | |
lift_location++; | |
} | |
else | |
{ | |
lift_location--; | |
} | |
printf("Lift: Now at floor %d\n", lift_location); | |
} | |
write(down[1], &lift_location, sizeof(lift_location)); // WRITE MY LOCATION | |
} | |
while(lift_location != 10); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment