void *operatore(){ while(1){ //generatore di numeri random usato da wait_client() e ask_confirmation() srand(time(NULL)); //attende un cliente wait_client(); //proteggo l'accesso dell'operatore al db pthread_mutex_lock(&op_lock); active_op++; if (active_op==1){ pthread_mutex_lock(&db_lock); } pthread_mutex_unlock(&op_lock); //controllo disponibilita' if (check_availability()){ pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); //se il posto e' disponibile chiedo conferma della prenotazione if (ask_confirmation()){ pthread_mutex_lock(&db_lock); //avuta la conferma dal cliente, ricontrollo se il posto sia ancora libero if (check_availability()){ //ricevuta la conferma, prenoto il posto e rilascio il lock sul db book_seat(); pthread_mutex_unlock(&db_lock); //confermo al cliente l'avvenuta prenotazione confirm_booking(); } else { pthread_mutex_unlock(&db_lock); printf("Ci dispiace, qualcuno ha prenotato il posto prima di lei\n"); } } else { //prenotazione non confermata, rilascio il lock sul db pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); printf("Ci dispiace che abbia cambiato idea, speriamo di rivederla al piu' presto!\n"); } } else { pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); printf("Ci dispiace, il viaggio che le interessa e' gia' al completo! Speriamo di rivederla presto.\n"); break; } } }
string mt_audio_out_util::get_device_name( int n ){ if( check_availability(n) ){ RtAudio::DeviceInfo info = infos[n]; return info.name; }else{ return "can not get audio device"; } }
void mt_audio_out_util::change_device( int n ){ if( check_availability(n) ){ stream.close(); stream.setDeviceID( n ); current_device_num = n; bool ok = stream.setup(app, out_ch, in_ch, sample_rate, buffer_size, 4); if( ok ){ cout << "Success : sound device connection ;" << n << endl; cout << get_device_info( n ) << endl; }else{ cout << "Error : sound device connection : " << n << endl; cout << get_device_info( n ) << endl; } } }
string mt_audio_out_util::get_device_info( int n ){ if( check_availability(n) ){ RtAudio::DeviceInfo info = infos[n]; stringstream ss; ss << "sound device " << n << "\n"; ss << info.name << "\n"; if (info.isDefaultInput) ss << "----* default ----*\n"; ss << "maximum output channels " << info.outputChannels << "\n"; ss << "maximum input channels " << info.inputChannels << "\n"; ss << "-----------------------------------------"; return ss.str(); }else{ return "can not get audio device"; } }
int phase1(){ /*code extracted from beej guide*/ int status, sockfd = 0; struct addrinfo hints; struct addrinfo *res, *p; // will point to the results const char *addr = "nunki.usc.edu"; char s[INET6_ADDRSTRLEN]; int new_fd = 0; struct sockaddr_storage their_addr; socklen_t sin_size; struct sigaction sa; int retVal; char buf[256]; memset(&hints, 0, sizeof hints); // make sure the struct is empty hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets hints.ai_flags = AI_PASSIVE; // fill in my IP for me if ((status = getaddrinfo(addr, HCS_TCP_STATIC_PORT, &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } /*code extracted from beej guide*/ p = res; void *server_addr; struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; server_addr = &(ipv4->sin_addr); /*convert the IP to a string and print it:*/ inet_ntop(p->ai_family, server_addr, s, sizeof s); printf("\nPhase 1: The Health Center Server has port number %s and IP address %s.\n", HCS_TCP_STATIC_PORT, s); /*loop through all the results and bind to the first we can*/ for(p = res; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("bind"); continue; } break; /*if we get here, we must have connected successfully*/ } if (p == NULL) { /*looped off the end of the list with no successful bind*/ fprintf(stderr, "failed to bind socket\n"); exit(2); } freeaddrinfo(res); /*free the linked-list*/ /*load user.txt file*/ if((retVal = create_users()) < 0) return retVal; /*load availabilities.txt file*/ if((retVal = create_availabilities()) < 0) return retVal; if(listen(sockfd, BACKLOG) == -1){ printf("Failed to listen\n"); return -1; } /*code extracted from beej guide*/ sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } /*code extracted from beej guide*/ while(1) { sin_size = sizeof their_addr; new_fd = 0; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { /*perror("Healthcenterserver Error: accept");*/ continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); int port = ntohs(((struct sockaddr_in *)&their_addr)->sin_port); if (!fork()) // this is the child process { close(sockfd); // child doesn't need the listener int number_of_bytes; memset(buf, '0' ,sizeof(buf)); if ((number_of_bytes = recv(new_fd, buf, MAXBUFLEN-1, 0)) == -1) { perror("Healthcenterserver Error: when receiving bytes\n"); exit(1); } buf[number_of_bytes] = '\0'; struct users temp_patient; retVal = check_authentication((char *)buf, &temp_patient); printf("\nPhase 1: The Health Center Server has received request from a patient with username %s and password %s.\n", temp_patient.username, temp_patient.password); if(retVal == 1){ if (send(new_fd, "success", 7, 0) == -1) { perror("Healthcenterserver Error: while sending\n"); exit(1); } printf("\nPhase 1: The Health Center Server sends the response success to patient with username %s.\n", temp_patient.username); /*start of phase2 stage1*/ retVal = phase2(&new_fd, &port, s); printf("\nPhase 2: The Health Center Server sends available time slots to patient with username %s.\n", temp_patient.username); memset(buf, '\0' ,sizeof(buf)); if ((number_of_bytes = recv(new_fd, buf, MAXBUFLEN-1, 0)) == -1) { perror("Healthcenterserver Error: when receiving bytes\n"); exit(1); } buf[number_of_bytes] = '\0'; printf("\nPhase 2: The Health Center Server receives a request for appointment %c from patient with port number %d and username %s.\n", buf[10], port, temp_patient.username); /*end of phase2 stage1*/ /*start of phase2 stage2*/ char sendBuf[256]; memset(sendBuf, '\0' ,sizeof(sendBuf)); retVal = check_availability(buf, sendBuf); if(retVal == 1){ printf("\nPhase 2: The Health Center Server confirms the following appointment %c to patient with username %s.\n", buf[10], temp_patient.username); if (send(new_fd, sendBuf, sizeof(sendBuf), 0) == -1) { perror("Healthcenterserver Error: while sending\n"); exit(1); } } else{ printf("\nPhase 2: The Health Center Server rejects the following appointment %c to patient with username %s.\n", buf[10], temp_patient.username); if (send(new_fd, "notavailable", 12, 0) == -1) { perror("Healthcenterserver Error: while sending\n"); exit(1); } } memset(sendBuf, '\0' ,sizeof(sendBuf)); /*end of phase2 stage2*/ } else{ if (send(new_fd, "failure", 7, 0) == -1) { perror("Healthcenterserver Error: while sending\n"); exit(1); } printf("\nPhase 1: The Health Center Server sends the response failure to patient with username %s.\n", temp_patient.username); } close(new_fd); exit(0); } else close(new_fd); /*parent doesn't need this*/ /*while(wait(NULL) > 0);*/ } while(wait(NULL) > 0); close(sockfd); return 0; }
int main(int argc, char const *argv[]) { int n; // number of lions int k; // number of times each lion is going to eat pid_t *pid; int status; int i,j; // iterating variables int curr_pit; // stores the current pit from which the lion is going to eat int check; // stores the pit that will be available; // read the number of lions form arguments if(argc < 3) { perror("Didn't provide the number of lions"); exit(0); } // Read the number of lions n = atoi(argv[1]); // Read the number of times each lion is going to eat k = atoi(argv[2]); pid = (pid_t*)malloc(n*sizeof(pid_t)); // Wait for the start signal from the Ranger // Create Semaphores sem_mut = semget(MUTEX, 1, IPC_CREAT|0666); sem_lion = semget(LION, N_PITS + 1, IPC_CREAT|0666); sem_jackal = semget(JACKAL, N_PITS + 1, IPC_CREAT|0666); sem_ranger = semget(RANGER, N_PITS + 1, IPC_CREAT|0666); sem_pit = semget(PIT, N_PITS + 1, IPC_CREAT|0666); sem_mem = semget(NEXT_PIT, 1, IPC_CREAT|0666); sem_lion_init = semget(INIT_LION, 1, IPC_CREAT|0666); semctl(sem_lion_init,0,SETVAL,0); down(sem_lion_init,0); printf("Lion Started %d %d\n",n,k); // fork n lions for(i = 0; i < n; i++) { pid[i] = fork(); if(pid[i] == 0) { srand(time(NULL)); // forked process // actual lion code id = i; // chosen a random pit curr_pit = rand()%N_PITS + 1; for(j = 0; j < k; j++) { check = check_availability(curr_pit); if(check == -1) { // no pit available curr_pit = (curr_pit - 1 + N_PITS - 1)%N_PITS + 1; printf("%s %d in wait queue of meat-pit %d\n",SELF,id,curr_pit); // block in the waiting queue down(sem_self,0); curr_pit = semctl(sem_mem,0,GETVAL,0); j--; continue; } //Current consumer will eat from meat-pit number 'check' curr_pit = check; // Take control printf("Before::Number of %s in meat-pit %d = %d\t\t\t%s %d\n",SELF,curr_pit,semctl(sem_self,curr_pit,GETVAL,0),SELF,id); up(sem_self,curr_pit); printf("After::Number of %s in meat-pit %d = %d\t\t\t%s %d\n",SELF,curr_pit,semctl(sem_self,curr_pit,GETVAL,0),SELF,id); printf("%s %d in control of meat-pit %d\n\n\n",SELF,id,curr_pit); // Consume down(sem_pit,curr_pit); printf("\t\t\t\t\t Meat in Pits %d %d %d\n",semctl(sem_pit, 1, GETVAL, 0),semctl(sem_pit, 2, GETVAL, 0),semctl(sem_pit, 3, GETVAL, 0)); sleep(2); // Check if last consumer to leave the pit printf("Number of %s in meat-pit %d = %d\t\t\t%s %d\n",SELF,curr_pit,semctl(sem_self,curr_pit,GETVAL,0),SELF,id); if(semctl(sem_self,curr_pit,GETVAL,0) > 1)// || semctl(sem_pit,curr_pit,GETVAL,0) == 0) { // do not send signal and continue down(sem_self,curr_pit); continue; } printf("Down karega %s %d\n",SELF,id); down(sem_self,curr_pit); // down(sem_mut,0); printf("%s %d left meat-pit %d\n",SELF,id,curr_pit); // send signals // set shared memory to current pit semctl(sem_mem,0,SETVAL,curr_pit); // wake up ranger and rival if(semctl(sem_ranger,0,GETVAL,0) == 0) // wake up ranger only when sleeping up(sem_ranger,0); if(semctl(sem_rival, 0, GETNCNT, 0) == 0) printf("Koi uthane ko nahi hai %s %d\n",SELF,id); printf("\t\t\tNumber of Jackals waiting %d\n",semctl(sem_jackal, 0, GETNCNT, 0)); printf("\t\t\tNumber of Lions waiting %d\n",semctl(sem_lion, 0, GETNCNT, 0)); upn(sem_rival,0,semctl(sem_rival, 0, GETNCNT, 0)); printf("%s %d giving signal to wait queue of all meat-pit\n",SELF,id); curr_pit = rand()%N_PITS + 1; // up(sem_mut,0); } exit(0); } else if(pid[i] < 0) { perror("Fork error!!"); exit(0); } } for(i = 0; i < n; i++) waitpid(pid[i],&status,0); free(pid); return 0; }