void *blue(void *num_b){ int number; int local_b; number = *(int *)(num_b); synch_begin(blue); check_blue++; if(check_blue>N) { synch_wait(); count_b ++; if(count_b%N!=0 && count_b!=i_blue){ synch_notify(); } } else{ count_b++; } synch_end(blue); printf("blue %d\n",number); synch_begin(mtx); un_count_b++; local_b = un_count_b; synch_end(mtx); if(local_b%N == 0 || i_blue == local_b) { printf("count_blue %d\n",local_b); if(i_red != un_count_r) { synch_begin(red); synch_notify(); synch_end(red); } else { if(i_blue == local_b) { synch_begin(main); synch_notify(); synch_end(main); } else { do{ sleep(1); synch_begin(blue); synch_notify(); synch_end(blue); }while(check_blue-local_b<=1); } } } return(NULL); }
static void play_live(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { struct timeval tv, tp, tm; char buf[BUFFER_SIZE]; int len; if (cont) tp=*cont; else { gettimeofday(&tp, 0); synch_init_wait(&tp, arg); } // using read() not fread(), we need unbuffered IO while ((len=read(fileno(f), buf, BUFFER_SIZE))>0) { gettimeofday(&tv, 0); tm=tv; tsub(tm, tp); synch_wait(&tm, arg); tp=tv; synch_print(buf, len, arg); } }
static void play_nh_recorder(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { char buf[BUFFER_SIZE]; int b,i,i0; struct timeval tv; uint32_t t,tp; t=0; i=b=0; while ((b=fread(buf+b-i, 1, BUFFER_SIZE-(b-i), f))>0) { i0=0; for (i=0;i<b;i++) if (!buf[i]) { if (i0<i) synch_print(buf+i0, i-i0, arg); if (i+4>b) // timestamp happened on a block boundary { if (b<5) // incomplete last timestamp return; memmove(buf+i, buf, b-i); goto block_end; } else { tp=t; t=little_endian(*(uint32_t*)(buf+i+1)); i0=i+=4; tv.tv_sec=(t-tp)/100; tv.tv_usec=(t-tp)%100*10000; synch_wait(&tv, arg); } } if (i0<i) synch_print(buf+i0, i-i0, arg); block_end:; } }
static void play_baudrate(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { char buf[BUFFER_SIZE]; size_t b; struct timeval tv; tv.tv_sec=0; tv.tv_usec=200000; while ((b=fread(buf, 1, 60, f))>0) // 2400 baud { synch_print(buf, b, arg); synch_wait(&tv, arg); } }
void *roller_coaster(){ int i; int count; count = 0; while(1) { synch_begin(roll); synch_wait(); synch_end(roll); while(wait_pas>=N){ printf("The roller coaster is ready for new ride!\n"); for(i=0; i<N; i++) { count++; synch_begin(pass); synch_notify(); synch_end(pass); } wait_pas = wait_pas - N; sleep(1); printf("The roller coaster begin!\n"); sleep(2); printf("Passengers abord the roller_coaster!\n"); sleep(1); } synch_begin(main); synch_notify(); synch_end(main); } return (NULL); }
void *passenger(void *num_p){ int k; synch_begin(main); k = *(int *)(num_p); synch_notify(); synch_end(main); synch_begin(pass); synch_wait(); synch_end(pass); printf("i am passenger %d\n", k); return (NULL); }
int main(int argc, char *argv[]) { int check, k, i; int number; char command[10]; pthread_t p_rc, *p_pass; k = 0; synch(main); synch(roll); synch(pass); check = pthread_create(&p_rc, NULL, &roller_coaster, NULL); if(check!=0) { printf("Problem to create roller_coaster thread\n"); return(7); } while(1) { printf("For new passengers press 'new' and the number of passengers\n"); printf("Else for exit press 'exit'\n"); printf("wait_pas: %d\n",wait_pas); scanf(" %9s",command); if(strcmp(command,"new")==0){ scanf("%d",&number); wait_pas = wait_pas + number; p_pass = (pthread_t *)malloc(sizeof(pthread_t)*number); if(p_pass==NULL){ printf("Problem with memory allocation\n"); return(2); } for(i=0;i<number;i++){ synch_begin(main); k++; check = pthread_create(&p_pass[i], NULL, &passenger, &k); if(check!=0) { printf("Problem to create %d thread\n", i); free(p_pass); return(7); } synch_wait(); synch_end(main); } free(p_pass); } else if(strcmp(command,"exit")==0){ printf("Roller coaster has closed for today!\n"); break; } else{ printf("Try again!\n"); continue; } printf("wait_pas: %d\n",wait_pas); synch_begin(roll); synch_notify(); synch_end(roll); synch_begin(main); synch_wait(); synch_end(main); } return (0); }
int main(int argc, char *argv[]){ char c; int i; int check; int *array_r, *array_b; pthread_t *p_red, *p_blue; synch(main); synch(red); synch(blue); synch(mtx); while(1) { i_blue = 0; i_red = 0; count_b = 0; count_r = 0; un_count_b = 0; un_count_r = 0; check_blue = 0; check_red = 0; printf("Press 0 to insert a red car, 1 for blue car or 'q' to exit\n"); do{ c = getchar(); if(c=='0'){ //red i_red++; } else if(c=='1'){ //blue i_blue++; } else if(c=='q') { return(0); } }while(c!='\n'); if(i_red==0 && i_blue==0){ return(0); } printf("Α new day dawned\n"); p_red = (pthread_t*)malloc(sizeof(pthread_t)*i_red); if(p_red==NULL){ printf("Problem with memory allocation\n"); return(2); } p_blue = (pthread_t*)malloc(sizeof(pthread_t)*i_blue); if(p_blue==NULL){ printf("Problem with memory allocation\n"); return(2); } array_r = (int*)malloc(sizeof(int)*i_red); if(array_r==NULL){ printf("Problem with memory allocation\n"); return(2); } array_b = (int*)malloc(sizeof(int)*i_blue); if(array_b==NULL){ printf("Problem with memory allocation\n"); return(2); } printf("red %d, blue %d\n",i_red, i_blue); for(i=0; i<i_red; i++){ array_r[i] = i + 1; check = pthread_create(&p_red[i], NULL, &red, &array_r[i]); if(check!=0) { printf("Problem to create %d thread\n", i); return(7); } } for(i=0; i<i_blue; i++){ array_b[i] = i + 1; check = pthread_create(&p_blue[i], NULL, &blue, &array_b[i]); if(check!=0) { printf("Problem to create %d thread\n", i); return(7); } } synch_begin(main); synch_wait(); synch_end(main); printf("End of the day\n"); } return(0); }
void *red(void *num_r){ int number; int local_r; number = *(int *)(num_r); synch_begin(red); check_red++; if(i_blue==0){ if(check_red>N) { synch_wait(); count_r ++; if(count_r%N!=0 && count_r!=i_red){ synch_notify(); } } else{ count_r++; } } else { synch_wait(); count_r ++; if(count_r%N!=0 && count_r!=i_red){ synch_notify(); } } synch_end(red); printf("red %d\n",number); synch_begin(mtx); un_count_r++; local_r = un_count_r; synch_end(mtx); if(local_r%N == 0 || i_red == local_r) { printf("count_red %d\n",local_r); if(i_blue != un_count_b) { synch_begin(blue); synch_notify(); synch_end(blue); } else { if(i_red == local_r) { synch_begin(main); synch_notify(); synch_end(main); } else { do{ sleep(1); synch_begin(red); synch_notify(); synch_end(red); }while(check_red-local_r<=1); } } } return(NULL); }
static void play_ttyrec(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { char buf[BUFFER_SIZE]; size_t b,w; struct timeval tv,tl; int first=1; struct ttyrec_header head; if (cont) { tv=*cont; first=0; } while (fread(&head, 1, sizeof(struct ttyrec_header), f) ==sizeof(struct ttyrec_header)) { b=little_endian(head.len); /* printf("b=%u, time=%i.%09i\n", b, little_endian(head.sec), little_endian(head.usec)); */ // pre-read the first block to make the timing more accurate if (b>BUFFER_SIZE) w=BUFFER_SIZE; else w=b; if (fread(buf, 1, w, f)<w) goto the_end; b-=w; if (first) { tv.tv_sec=little_endian(head.sec); tv.tv_usec=little_endian(head.usec); synch_init_wait(&tv, arg); first=0; } else { tl=tv; tv.tv_sec=little_endian(head.sec); tv.tv_usec=little_endian(head.usec); tl.tv_sec=tv.tv_sec-tl.tv_sec; if ((tl.tv_usec=tv.tv_usec-tl.tv_usec)<0) { tl.tv_usec+=1000000; tl.tv_sec--; } synch_wait(&tl, arg); } while (w>0) { synch_print(buf, w, arg); if (!b) break; if (b>BUFFER_SIZE) w=BUFFER_SIZE; else w=b; if (fread(buf, 1, w, f)<w) goto the_end; b-=w; } } the_end:; }
void *blue(void *num_b){ int number; int i; number = *(int *)(num_b); synch_begin(blue); check_blue++; if(check_blue == i_blue) { synch_notify(); } if(i_blue!=1){ synch_wait(); } if(count_b%N==0){ count_b++; for(i=1; i<N && i<=(i_blue-count_b); i++) { synch_notify(); } } else { count_b++; } synch_end(blue); printf("blue %d\n",number); pthread_mutex_lock(&mtx); un_count_b++; if(un_count_b%N == 0 || i_blue == un_count_b) { printf("count_blue %d\n",un_count_b); if(i_red != un_count_r) { synch_begin(red); synch_notify(); synch_end(red); } else { if(i_blue == un_count_b) { synch_begin(main); synch_notify(); synch_end(main); } else { synch_begin(blue); synch_notify(); synch_end(blue); } } } pthread_mutex_unlock(&mtx); return(NULL); }