Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <traffic.h>
- #include <semaphore.h>
- #include <pthread.h>
- #define map_width 12
- #define map_height 6
- /* Nun die Kartendaten */
- static map_shorts map_def[]=
- {
- S2,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,S3,
- SV,__,__,__,__,__,__,__,__,__,__,SV,
- SV,__,__,__,__,__,__,__,__,__,__,SV,
- SV,__,__,__,__,__,__,__,__,__,__,SV,
- SV,__,__,__,__,__,__,__,__,__,__,SV,
- S1,SH,SH,SH,SH,SH,SH,SH,SH,SH,SH,S4
- };
- void carOutside(int arg[]);
- void carInside(int arg[]);
- int outside[32];
- int inside[32];
- sem_t outerSem[12], innerSem[12];
- pthread_t outerCars[12], innerCars[12];
- int mainloop(void* arg){
- int outer[5] = {0, 0, 1, 0, -1};
- int inner[5] = {1, 0, 3, 0, -1};
- int i;
- for(i=0; i<12; i++){
- outer[4] = inner[4] =i;
- outer[3] = inner[3] = 40*((i+1)%2);
- pthread_create(&outerCars[i], NULL, &carOutside, &outer);
- pthread_create(&innerCars[i], NULL, &carInside, &inner);
- sleep(3);
- }
- waitAndClose();
- return 0;
- }
- void carOutside(int arg[]){
- int myIndex = arg[4];
- int frontIndex = (myIndex-1)%12;
- directions opt[] = {N, S, W, O};
- directions last = opt[arg[2]];
- int id = putCar(arg[0], arg[1], last, arg[3]);
- sem_init(&outerSem[myIndex], 0, 0);
- int i, j;
- for(i=0; i<32*2; i++){
- j = i%32;
- if(myIndex > 0)
- sem_wait(&outerSem[frontIndex]);
- if(j < 4)
- moveCar(id, S);
- else if(j == 4)
- moveCar(id, SSO);
- else if(j<15)
- moveCar(id, O);
- else if(j==15)
- moveCar(id, OON);
- else if(j<20)
- moveCar(id, N);
- else if(j == 20)
- moveCar(id, NNW);
- else if(j < 31)
- moveCar(id, W);
- else
- moveCar(id, WWS);
- if(i > 0)
- sem_post(&outerSem[myIndex]);
- }
- }
- void carInside(int arg[]){
- int myIndex = arg[4];
- int frontIndex = (myIndex-1)%12;
- sem_init(&innerSem[myIndex], 0, 0);
- directions opt[] = {N, S, W, O};
- directions last = opt[arg[2]];
- int id = putCar(arg[0], arg[1], last, arg[3]);
- int i, j;
- for(i=1; i<32*2; i++){
- if(myIndex > 0)
- sem_wait(&innerSem[frontIndex]);
- j = i%32;
- if(j < 10)
- moveCar(id, O);
- else if(j == 10)
- moveCar(id, SO);
- else if(j<15)
- moveCar(id, S);
- else if(j==15)
- moveCar(id, SW);
- else if(j<26)
- moveCar(id, W);
- else if(j == 26)
- moveCar(id, NW);
- else if(j < 31)
- moveCar(id, N);
- else
- moveCar(id, NO);
- if(i>1)
- sem_post(&innerSem[myIndex]);
- }
- }
- int main(){
- createMap(map_def, map_width, map_height, mainloop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment