#include #include #include #include #include #include #include int fd_lab[2],fd_non[2],fd_adt[2]; int lab_ip_count,lab_op_count,non_ip_count,non_op_count,adt_ip_count,adt_op_count; lab_ip_count=0; lab_op_count=0; non_ip_count=0; non_op_count=0; adt_ip_count=0; adt_op_count=0; sema_t sema_lab; sema_t sema_non; sema_t sema_adt; void func_lab(void); void func_adt(void); void func_non(void); void func_master(void); main() { thread_t t_lab,t_non,t_adt; thread_t t_master; pipe(fd_lab); pipe(fd_non); pipe(fd_adt); while(sema_init(&sema_lab,0,USYNC_THREAD,NULL)!=0); while(sema_init(&sema_adt,0,USYNC_THREAD,NULL)!=0); while(sema_init(&sema_non,0,USYNC_THREAD,NULL)!=0); printf("sema done\n"); (void) thr_create(NULL,0,(void *)func_lab,NULL,0,&t_lab); (void) thr_create(NULL,0,(void *)func_adt,NULL,0,&t_adt); (void) thr_create(NULL,0,(void *)func_non,NULL,0,&t_non); (void)thr_create(NULL,0,(void *)func_master,NULL,0,&t_master); printf("Threads Created\n"); thr_join(t_master,NULL,NULL); thr_join(t_lab,NULL,NULL); thr_join(t_non,NULL,NULL); thr_join(t_adt,NULL,NULL); } void func_lab(void) { char buff[1]; printf("In func_lab() :fd_lab[0] = %d \n",fd_lab[0]); while(1) { read(fd_lab[1],buff,1); lab_op_count++; printf("Read a LAB data \n"); sema_wait(&sema_lab); } } void func_non(void) { char buff[1]; printf("In func_non() \n"); while(1) { read(fd_non[1],buff,1); non_op_count++; printf("Read a NON data \n"); sema_wait(&sema_non); } } void func_adt(void) { char buff[1]; printf("In func_adt() \n"); while(1) { read(fd_adt[1],buff,1); adt_op_count++; printf("Read a ADT data \n"); sema_wait(&sema_adt); } } void func_master(void) { double drand48(); int trans_data; char buff[1]; int toss; printf("In func_master() fd_non[0]=%d,fd_adt[0]=%d,fd_lab[0]=%d\n",fd_non[0],fd_adt[0],fd_lab[0]); while(1) { trans_data = (drand48()*3)+1; switch(trans_data) { case 1: /* its Lab */ buff[0] = 'L'; write(fd_lab[0],buff,1); lab_ip_count++; printf("Entered the LAB transaction %d \n",lab_ip_count); (void) sema_post(&sema_lab); break; case 2: /* its NON */ buff[0] = 'N'; write(fd_non[0],buff,1); non_ip_count++; printf("Entered the NON transaction %d \n",non_ip_count); (void) sema_post(&sema_non); break; case 3: /* its ADT case */ toss = drand48()*2+1; switch(toss) { case 1: /* Normal ADT */ buff[0] = 'A'; write(fd_adt[0],buff,1); adt_ip_count++; printf("Entered the NORMAL ADT transaction %d \n",adt_ip_count); sema_post(&sema_adt); break; case 2: /* Special ADT */ buff[0] = 'A'; while(sema_trywait(&sema_adt)==0)sema_post(&sema_adt); while(sema_trywait(&sema_lab)==0)sema_post(&sema_lab); while(sema_trywait(&sema_non)==0)sema_post(&sema_non); /* Every thing is empty */ printf("Attention ::::::: Check all the six counters \n"); printf("non_ip_count =%d, non_op_count =%d,adt_ip_count =%d,adt_op_count = %d, lab_ip_count = %d,lab_op_count =%d \n",non_ip_count,non_op_count,adt_ip_count,adt_op_count,lab_ip_count,lab_op_count); write(fd_adt[0],buff,1); adt_ip_count++; printf("Entered the SPECIAL ADT transaction %d \n",adt_ip_count); sema_post(&sema_adt); /* Verify this step wiht Lin */ while(sema_trywait(&sema_adt) == 0) sema_post(&sema_adt); /* Tricky step */ break; default: printf("Error"); exit(3); } break; default: printf("Error"); exit(3); } } }