int main(int argc, char const *argv[]) { setbuf(stdout, NULL); printf("%s created============\n",servername); /////////SemGet and Init/////////// semid = semGet(SEMNUM); printf("semid: %d\n",semid); unsigned short arr[] = {1,0}; semInit(semid,arr,SEMNUM); ////////////////create famfifo/////////////// unlink(famfifo); int ret = mkfifo(famfifo,0666); assert(ret != -1); ffd = open(famfifo,O_RDWR); assert(ffd != -1); //////////////////////////////////////////// //////////Create another thread to poll clients///////// assert(pthread_mutex_init(&mut,NULL) == 0); assert(pthread_create(&tid,NULL,pollFifo,NULL) == 0); struct Request req; int i; while(1) { semWait(semid,1); read(ffd,&req,REQSIZE); printf("Request received from: %d\n",req.pid); genearteFifo(req.pid); cfd[numclients][INSTREAM] = ifd; cfd[numclients][OUTSTREAM] = ofd; pfd[numclients].fd = ifd; pfd[numclients].events = POLLIN; pfd[numclients].revents = 0; ////////Use MUTEX to CHANGE tHIS exclusively/////////// pthread_mutex_lock(&mut); numclients++; printf("numclients %d\n",numclients); pthread_mutex_unlock(&mut); ////////////////////////////////////// semSignal(semid,1); } return 0; }
int main(int argc, char const *argv[]) { pid = getpid(); printf("Pid of this Client is: %d\n",pid); //////////semGet////////////////// semid = semGet(SEMNUM); printf("semid: %d\n",semid); //////opeing famous fifo to place request/////// fd = open(famfifo,O_WRONLY); assert(fd != -1); /////place request//////////// struct Request req; req.pid = pid; semWait(semid,0); //acquire mutex write(fd,&req,REQSIZE); //place request semSignal(semid,1); //notify server that request is available semWait(semid,1); //wait until fifo gets created by server genearteFifo(pid); //opnes 2 fifo names pid.in and pid.out already created by server semSignal(semid,0); //release mutex /////////////////////////////////////////////////////// ///////////////Server to poll msg from other clients///////////// assert(pthread_create(&tid,NULL,pollServer,NULL) == 0); /////////////Take Msg from STDIN////////////////// char buff[BUFFSIZE]; while(1) { int pos=0; char ch; struct Msg msg; printf("write===: "); while((ch = getchar()) != '\n') msg.data[pos++] = ch; msg.pid = pid; msg.data[pos] = '\0'; write(ofd,&msg,MSGSIZE); } return 0; }
int main (int argc, char *argv[]) { // Sema for tansmit the type key_t sem_type_key = ftok(PATH, TYPE); int sem_type = semget(sem_type_key, 1, IPC_CREAT | PERMS); semReset(sem_type, 0); key_t sem_control_key = ftok(PATH, CONTROL); int sem_control = semget(sem_control_key, 2, IPC_CREAT | PERMS); semReset(sem_control, 0); semReset(sem_control, 1); // Sema for control Shared Mem Stock key_t sem_DispSrv_key = ftok(PATH, STOCK); int sem_DispSrv = semget(sem_DispSrv_key, 1, IPC_CREAT | PERMS); semReset(sem_DispSrv, 0); // Shared mem between monitor and server key_t shm_DispSrv_key = ftok(PATH, STOCKSHM); int shm_DispSrv = shmget(shm_DispSrv_key, sizeof(TSharedStock), IPC_CREAT | PERMS); // Creation com display server shm // File Parameters /* Time Function for filename definition */ time_t now = time(NULL); struct tm *time = localtime(&now); char date_time[30]; strftime(date_time,sizeof(date_time),"%d%m%y_%H%M%S", time); pid_t process_id = fork(); // Premier Fork (Server, Afficheur) if (process_id < 0) { perror("Error while attempting Fork (Server/Afficheur de Resultat)"); exit(19); } //Afficheur (Parent)// else if (process_id > 0){ int level = 0; showMainMenu(level, date_time); } /*Tampon Serveur (Child)*/ else{ // DAEMON CODE START // //daemonize(); // PROCESS NOW A DAEMON // //***********// //*SEMA INIT*// //***********// // Sema for control shared mem race key_t sem_mutex_key = ftok(PATH, MUTEX); int sem_mutex = semget(sem_mutex_key, 1, IPC_CREAT | PERMS); semReset(sem_mutex, 0); key_t sem_race_key = ftok(PATH, RACE); int sem_race = semget(sem_race_key, 22, IPC_CREAT | PERMS); int r; for(r = 0; r < 22; r++) semReset(sem_race, r); //*****************// //*SHARED MEM INIT*// //*****************// key_t shm_race_key = ftok(PATH, RACESHM); int shm_race = shmget(shm_race_key, 22*sizeof(TTabCar), IPC_CREAT | PERMS); //***********// //*FORK INIT*// //***********// process_id = fork(); // Deuxieme Fork (Server, Pilot) if (process_id < 0) { perror("Error while attempting Fork (Server/Pilot)"); exit(19); } //Pilots (Child)// else if (process_id == 0) forkPilots(); // Pilot forking function //Server (Parent) else server(date_time); // Main server function for(r = 0; r < 22; r++){ if(semGet(sem_race, r) != 1) semReset(sem_race, r); semctl(sem_race, r, IPC_RMID, NULL); } if(semGet(sem_mutex, 0) != 1) semReset(sem_mutex, 0); semctl(sem_mutex, 0, IPC_RMID, NULL); if(semGet(sem_type, 0) != 1) semReset(sem_type, 0); semctl(sem_type, 0, IPC_RMID, NULL); if(semGet(sem_control, 0) != 1) semReset(sem_control, 0); if(semGet(sem_control, 1) != 1) semReset(sem_control, 1); semctl(sem_control, 0, IPC_RMID, NULL); semctl(sem_control, 1, IPC_RMID, NULL); shmctl(shm_race, IPC_RMID, NULL); shmctl(shm_DispSrv, IPC_RMID, NULL); if(semGet(sem_DispSrv, 0) != 1) semReset(sem_DispSrv, 0); semctl(sem_DispSrv, 0, IPC_RMID, NULL); } return EXIT_SUCCESS; }
/* ========================================================================= */ static void semInit(sem_t** _sem, const char* _name) { VerboseMessage("Initializing %s...\n", _name); TRY(*_sem = sem_open(_name, O_CREAT, S_IRUSR | S_IWUSR, 0), SEM_FAILED); VerboseMessage("Value of %s = %d\n", _name, semGet(*_sem)); }