void *fifoController(void *args){ pid_t sentPid; char fileName[MAX_FILE_NAME]; long filesize=0; char buff; int i=0; char fifoName[MAX_FILE_NAME]; char semFifoName[MAX_FILE_NAME]; Command_e command = SEND_FILE; memset(fifoName,0,MAX_FILE_NAME); sprintf(fifoName,".%ld.ff",(long)gPid_server); memset(semFifoName,0,MAX_FILE_NAME); sprintf(semFifoName,".%ld.ffsm",(long)gPid_server); getnamed(semFifoName,&gSemP_serverSem,1); mkfifo(fifoName,FIFO_PERMS); int fd = open(fifoName,O_RDWR); while(!doneflag && read(fd,&sentPid,sizeof(pid_t))>0 && sentPid!=0){ memset(fileName,0,MAX_FILE_NAME); read(fd,fileName,MAX_FILE_NAME); read(fd,&filesize,sizeof(long)); //printf("Pid : %ld, Name:%s, Size:%ld\n",(long)sentPid,fileName,filesize); pthread_mutex_lock(&gMutex_lockSocket); int cc = write(fdClient,&command,sizeof(Command_e)); //printf("CC:%d\n",cc); int cp =write(fdClient,&sentPid,sizeof(pid_t)); //printf("CP:%d\n",cp); int cn = write(fdClient,fileName,MAX_FILE_NAME); //printf("cn:%d\n",cn); int cs =write(fdClient,&filesize,sizeof(long)); //printf("cs:%d\n",cs); for(i=0;i!=filesize;++i){ read(fd,&buff,1); int a =write(fdClient,&buff,1); //printf("a:%d, buf:%c\n",a,buff); } pthread_mutex_unlock(&gMutex_lockSocket); gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); printf("[%ld]MiniServer sent file:%s to [%ld]Client in %ld ms",(long)gPid_server, fileName,(long)sentPid,diffTime); } close(fd); unlink(fifoName); sem_close(gSemP_serverSem); sem_unlink(semFifoName); return NULL; }
int main (int argc, char *argv[]) { char buffer[BUFSIZE]; char *c; pid_t childpid = 0; int delay; volatile int dummy = 0; int i, n; sem_t *semlockp; if (argc != 4){ /* check for valid number of command-line arguments */ fprintf (stderr, "Usage: %s processes delay semaphorename\n", argv[0]); return 1; } n = atoi(argv[1]); delay = atoi(argv[2]); for (i = 1; i < n; i++) if (childpid = fork()) break; snprintf(buffer, BUFSIZE, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n", i, (long)getpid(), (long)getppid(), (long)childpid); c = buffer; if (getnamed(argv[3], &semlockp, 1) == -1) { perror("Failed to create named semaphore"); return 1; } while (sem_wait(semlockp) == -1) /* entry section */ if (errno != EINTR) { perror("Failed to lock semlock"); return 1; } while (*c != '\0') { /* critical section */ fputc(*c, stderr); c++; for (i = 0; i < delay; i++) dummy++; } if (sem_post(semlockp) == -1) { /* exit section */ perror("Failed to unlock semlock"); return 1; } if (r_wait(NULL) == -1) /* remainder section */ return 1; return 0; }
int main(int argc, char *argv[]) { sem_t *mysem; if (argc != 2) { fprintf(stderr, "Usage %s semname\n", argv[0]); return 1; } if (getnamed(argv[1], &mysem, 0) == -1) { perror("getnamed"); return 1; } if (destroynamed(argv[1], mysem) == -1) { perror("first destroy failed"); return 1; } fprintf(stderr,"Semaphore %s destroyed\n", argv[1]); if (destroynamed(argv[1], mysem) == -1) { perror("second destroy should have failed and did"); return 0; } fprintf(stderr, "Destroy successful\n"); return 1; }
void startServer(int portnum){ struct sockaddr_in clientAddr; // client socket informations socklen_t clientAddrLen; // client socket address length // initialize size of struct clientAddrLen = sizeof(struct sockaddr_in); //clear junk values memset(&clientAddr,0,clientAddrLen); if((gI_socketFd = initializeSocket(portnum))==-1){ perror("Initialize socket"); return; } fprintf(stderr,"[%ld]MainServer initialized socket\n",(long)gPid_server); // create a semafor to control writing sequential to pipe sem_unlink(PIPE_CONTROL_SEM); getnamed(PIPE_CONTROL_SEM,&gSemP_pipeSem,1); pipe(gPipe_cwpr); pipe(gPipe_crpw); // TODO : check pipe errors pthread_t th_pipeListener; pthread_create(&th_pipeListener,NULL,listenPipe,NULL); memset(&serverList,0,sizeof(hmlist_t)); //clear junks gettimeofday(&endTime,NULL); diffTime = getTimeDif(startTime,endTime); printf("[%ld]MainServer is builded in %ld ms\n",(long)gPid_server,diffTime); hmServData_t miniServer; pid_t pidChild=-1; pid_t pidClient=-1; while(!doneflag){ printf("[%ld]MainServer waiting for clients...\n",(long)gPid_server); // accept clients fdClient = accept(gI_socketFd,(struct sockaddr*)&clientAddr,&clientAddrLen); if(fdClient !=-1 && !doneflag){ // client send pid address and server creates new mini server to serv read(fdClient,&pidClient,sizeof(pid_t)); if( (pidChild = fork())==-1){ perror("Fork"); doneflag=1; }else if(pidChild==0){ // child here break; }else{ // parent here // pair server-client informations memset(&miniServer,0,sizeof(hmServData_t)); miniServer.pidServer=pidChild; miniServer.pidClient=pidClient; miniServer.fdSocket = fdClient; addList(&serverList,&miniServer); // store child informations gettimeofday(&endTime,NULL); diffTime = getTimeDif(startTime,endTime); printf("Client[%ld] connected to server[%ld] in %ld ms\n", (long)pidClient,(long)pidChild,diffTime); } }else doneflag=1; } if(pidChild ==0){ // child here // no need to re-open semafor because we forked all memory /*sem_close(gSemP_pipeSem); getnamed(PIPE_CONTROL_SEM,&gSemP_pipeSem,1);*/ gPid_server = getpid(); // assing new mini server pid deleteList(&serverList); // remove parent values startMiniServer(pidClient); // start server pthread_join(th_pipeListener,NULL); sem_close(gSemP_pipeSem); close(gI_socketFd); // close spesific socket fildes // return main and close mini server }else{ // main server here killAllChilds(SIGINT); close(gPipe_cwpr[1]); //close pipe to kill thread while(wait(NULL)!=-1){} // wait childs // TODO : create thread to wait childs deleteList(&serverList); printf("[%ld]MainServer Waiting for threads\n",(long)gPid_server); pthread_join(th_pipeListener,NULL); sem_close(gSemP_pipeSem); close(gI_socketFd); } }
void sendFile(pid_t whosent){ char fileName[MAX_FILE_NAME]; long filesize=0; pid_t pidArrival=0; int i=0; char byte; int sendServer=0; read(fdClient,&pidArrival,sizeof(pid_t)); memset(fileName,0,MAX_FILE_NAME); read(fdClient,fileName,MAX_FILE_NAME); read(fdClient,&filesize,sizeof(long)); gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); if(pidArrival!=1){ // send client or server pid_t pidArrivalServer = getClientServerPid(pidArrival); if(pidArrivalServer<=0){ printf("[%ld]MiniServer want to send file to server in %ld ms\n", (long)gPid_server,diffTime); sendServer=1; }else{ // send client printf("[%ld]MiniServer want to send file to [%ld]MiniServer-[%ld]Client pair in %ld ms\n", (long)gPid_server,(long)pidArrivalServer,(long)pidArrival,diffTime); char fifoName[MAX_FILE_NAME]; memset(fifoName,0,MAX_FILE_NAME); sprintf(fifoName,".%ld.ff",(long)pidArrivalServer); char semFifoName[MAX_FILE_NAME]; memset(semFifoName,0,MAX_FILE_NAME); sprintf(semFifoName,".%ld.ffsm",(long)gPid_server); int fd = open(fifoName,O_RDWR); sem_t *semP; getnamed(semFifoName,&semP,1); // fifonun semaforunuda kilitleki // sadece tek kişi yazsın içine sem_wait(semP); write(fd,&whosent,sizeof(pid_t)); write(fd,fileName,MAX_FILE_NAME); int a =write(fd,&filesize,sizeof(long)); //printf("SizeCheck:%d - %ld\n",a,filesize); for(i=0;i!=filesize;++i){ read(fdClient,&byte,1); int a = write(fd,&byte,1); // dosya fifoya yollandi //printf("-%c %d\n",byte,a); } gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); printf("[%ld]MiniServer sent file: %s (%ld)byte to [%ld]Client in %ld ms\n", (long)gPid_server,fileName,filesize,(long)pidArrival,diffTime); //close(fd); sem_post(semP); sem_close(semP); } } if(sendServer==1 || pidArrival==1){ // SEND SERVER // create a sem and lock file in the server sem_t *semServerFile; char semName[MAX_FILE_NAME+4]; // prepare semaphore memset(semName,0,MAX_FILE_NAME+4); sprintf(semName,".%s.sm",fileName); getnamed(semName,&semServerFile,1); sem_wait(semServerFile); gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); printf("[%ld]MiniServer locked server-file : %s to update in %ld ms\n", (long)gPid_server,fileName,diffTime); unlink(fileName); // delete ol files and create newfile int fd = open(fileName,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR| S_IRGRP | S_IROTH); gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); printf("[%ld]MiniServer file: %s transfer started in %ld ms\n", (long)gPid_server,fileName,diffTime); for(i=0;i!=filesize;++i){ read(fdClient,&byte,1); write(fd,&byte,1); } gettimeofday(&endTime,NULL); diffTime=getTimeDif(startTime,endTime); printf("[%ld]MiniServer unlocked(transfer completed) server-file : %s in %ld ms\n", (long)gPid_server,fileName,diffTime); sem_post(semServerFile); sem_close(semServerFile); close(fd); } }
void startMiniServer(pid_t pidClient){ Command_e command=DIE; DIR *pDir=NULL; char strSemNameFifo[MAX_FILE_NAME]; pthread_t th_fifoController; pDir = opendir(LOCAL_DIR); if(pDir==NULL){ perror("Dir open error :"); return; } memset(strSemNameFifo,0,MAX_FILE_NAME); sprintf(strSemNameFifo,"/%ld.smf",(long)gPid_server); getnamed(strSemNameFifo,&gSemP_fifoSem,1); pthread_create(&th_fifoController,NULL,fifoController,NULL); pthread_mutex_init(&gMutex_lockSocket,NULL); // initialize mutex write(fdClient,&gPid_server,sizeof(pid_t)); //send pid address to client while(!doneflag){ read(fdClient,&command,sizeof(Command_e)); gettimeofday(&endTime,NULL); diffTime = getTimeDif(startTime,endTime); if(!doneflag){ if(command==LS_CLIENT){ printf("[%ld]MiniServer read LS_CLIENT command from [%ld]Client at %ld ms\n", (long)gPid_server,(long)pidClient,diffTime); lsClient(fdClient,pidClient); }else if(command==LIST_SERVER){ //lock mutex and write filenames to socket printf("[%ld]MiniServer read LIST_SERVER command from [%ld]Client at %ld ms\n", (long)gPid_server,(long)pidClient,diffTime); pthread_mutex_lock(&gMutex_lockSocket); listLocalFiles(pDir,fdClient); pthread_mutex_unlock(&gMutex_lockSocket); }else if(command ==DIE){ printf("[%ld]MiniServer read DIE command from [%ld]Client at %ld ms\n", (long)gPid_server,(long)pidClient,diffTime); write(fdClient,&command,sizeof(Command_e)); doneflag=1; // TODO: CHANGE DONEFLAG WITH SOMETHING DIFF }else if(command == SEND_FILE){ sendFile(pidClient); } } } char fifoName[MAX_FILE_NAME]; memset(fifoName,0,MAX_FILE_NAME); sprintf(fifoName,".%ld.ff",(long)gPid_server); int fd = open(fifoName,O_RDWR); pid_t killpid=0; write(fd,&killpid,sizeof(pid_t)); close(fd); pthread_mutex_destroy(&gMutex_lockSocket); pthread_join(th_fifoController,NULL); closedir(pDir); sem_close(gSemP_fifoSem); pDir=NULL; // handle dangling pointers }