int main(int argc, char *argv[]) { int listenfd, connfd, port, clientlen, i; struct sockaddr_in clientaddr; getargs(&port,&max,&bufsize, argc, argv); strcpy(policyStr, argv[4]); if(strcmp(policyStr,"SFF\0")== 0 || strcmp(policyStr,"sff\0")== 0){ policy = SFF; } else if(strcmp(policyStr,"FIFO\0")== 0 || strcmp(policyStr,"fifo\0")== 0){ policy = FIFO; } else{ fprintf(stderr, "Usage: %s <port> <numThreads> <loops> <policy>\n", argv[0]); fprintf(stderr, "Policy must be FIFO or SFF.\n"); exit(1); } // // CS537: Create some threads... // pthread_t cid[max]; //be careful about GARBAGE VALS structBuffer = (server_request *) malloc(bufsize*sizeof(server_request)); for(i = 0; i < bufsize; i++){ structBuffer[i].time = DBL_MAX; structBuffer[i].connValue = -1; structBuffer[i].size = INT_MAX; } for(i = 0; i< max; i++){ pthread_create(&cid[i], NULL, consumer, NULL); } listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); //handle_connection(listenfd, &clientaddr); // // CS537: In general, don't handle the request in the main thread. // Save the relevant info in a buffer and have one of the worker threads // do the work. However, for SFF, you may have to do a little work // here (e.g., a stat() on the filename) ... // //CALL PRODUCER, ADD connfd to buffer pthread_mutex_lock(&mutex2); producer((void *)&connfd); pthread_mutex_unlock(&mutex2); } //deadcode for(i = 0; i < max; i++){ pthread_join(cid[i], NULL); } printf("\nend"); exit(0); }
int child_listening_sock (int port) { listenfd = Open_listenfd(port); return listenfd; }
int main(int argc, char **argv) { int listenfd; int * connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; /****************************** Section to setup the DLL ******************************/ // This will be the function stub used for addition // This would be any service that we want. // For test purposes, we are keeping it simple // Later we will extend it to add even more // Services char * add_service = "adder_lib.so"; dlhandle = dlopen ("./cgi-bin/adder_lib.so", RTLD_LAZY); // This handle will be used to access particular functions // Within the library marked. //dlhandle = dlopen(add_service, RTLD_LAZY); // No need to have staticly // linked to program specified // in "service". if (dlhandle == NULL) { printf("Service %s Not Found: %s\n", add_service, dlerror()); } // Now we shall get the add function stub functionstub = dlsym(dlhandle, "add"); if (!functionstub) { printf("Problem calling add_function(): %s\n", dlerror()); } // The function stub will be available to all the threads for use. /******** Section Ends *********/ /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); while (1) { clientlen = sizeof(clientaddr); connfd = (int *) malloc( sizeof( int ) ) ; *connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //line:netp:tiny:accept Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf("Accepted connection from (%s, %s)\n", hostname, port); //doit(connfd); //line:netp:tiny:doit pthread_t pth; pthread_create(&pth,NULL,doit, connfd); } }
int main(int argc, char *argv[]) { int listenfd, port, workersCounts, bufferSize, sffbsArg, clientlen; char *scheduling = malloc(7); struct sockaddr_in clientaddr; workers_resource *workerResources; int jobTicket = 0; getargs(&port, &workersCounts, &bufferSize, scheduling, &sffbsArg, argc, argv); scheduling = strdup(argv[4]); workerResources = (workers_resource*) malloc(sizeof(workers_resource)); workerResources->groupHead = (node_request *) malloc(sizeof(node_request)); // for sff-bs // check if malloc fails if(workerResources == NULL) { fprintf(stderr, "Malloc failed."); exit(1); } workerResources->workerThreads = (worker_thread *) malloc(sizeof(worker_thread) * workersCounts); if(workerResources->workerThreads == NULL) { fprintf(stderr, "Malloc failed."); exit(1); } workerResources->bufferFront = NULL; workerResources->bufferEnd = NULL; workerResources->bufferFilled = 0; workerResources->bufferSize = bufferSize; // init lock and cv if(pthread_mutex_init(&(workerResources->bufferLock), NULL) != 0) { fprintf(stderr, "Fail to initialize lock."); } if(pthread_cond_init(&(workerResources->bufferEmpty), NULL) != 0) { fprintf(stderr, "Fail to initialize conditional variable."); } if(pthread_cond_init(&(workerResources->bufferNotEmpty), NULL) != 0) { fprintf(stderr, "Fail to initialize conditional variable."); } // // CS537: Create some threads... // int i; for(i = 0; i < workersCounts; i++) { worker_thread *tempWorker = (worker_thread *) malloc(sizeof(worker_thread)); tempWorker = &(workerResources->workerThreads[i]); tempWorker->requestHandled = 0; tempWorker->requestStatic = 0; tempWorker->requestDynamic = 0; tempWorker->thread = (pthread_t *) malloc(sizeof(pthread_t)); worker_id *workerId = (worker_id *) malloc(sizeof(worker_id)); workerId->id = i; // set the id workerId->workerResources = workerResources; if(pthread_create(tempWorker->thread, NULL, workerThread, workerId) != 0) { fprintf(stderr, "Cannot create thread."); } } listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); node_request *currRequest = (node_request *) malloc(sizeof(node_request)); currRequest->conn = (conn_request*) malloc(sizeof(conn_request)); currRequest->conn->fd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); requestInit(currRequest->conn); // set the conn with proper info //currRequest->next = NULL; // // CS537: In general, don't handle the request in the main thread. // Save the relevant info in a buffer and have one of the worker threads // do the work. However, for SFF, you may have to do a little work // here (e.g., a stat() on the filename) ... // pthread_mutex_lock(&(workerResources->bufferLock)); while(workerResources->bufferFilled >= workerResources->bufferSize) { pthread_cond_wait(&(workerResources->bufferEmpty), &(workerResources->bufferLock)); } // if nothing is in the buffer if(workerResources->bufferFilled == 0) { // fix for sffbs if(strcmp(scheduling, sffbs) == 0) { workerResources->groupHead = currRequest; jobTicket++; } workerResources->bufferFront = currRequest; workerResources->bufferEnd = currRequest; pthread_cond_signal(&(workerResources->bufferNotEmpty)); } else { // in our sff, the shortest is in front of queue if(strcmp(scheduling, sff) == 0) { // put the request to the right position based on size node_request *temp = workerResources->bufferFront; if(temp->conn->size >= currRequest->conn->size) { currRequest->next = temp; workerResources->bufferFront = currRequest; } else { while(temp != NULL) { if(temp->next == NULL || currRequest->conn->size < temp->next->conn->size) { currRequest->next = temp->next; temp->next = currRequest; break; } temp = temp->next; } } } else if(strcmp(scheduling, fifo) == 0) { // if FIFO currRequest->next = NULL; // this will be last element in buffer queue workerResources->bufferEnd->next = currRequest; // add to the end workerResources->bufferEnd = currRequest; // update buffer end } else { //sff-bs if(jobTicket % sffbsArg == 0) { // get the head of new group workerResources->groupHead = currRequest; workerResources->bufferEnd->next = currRequest; workerResources->bufferEnd = currRequest; } else { reorderSff(workerResources, workerResources->groupHead, currRequest); } jobTicket++; } } workerResources->bufferFilled++; // increase the # of buffers filled pthread_mutex_unlock(&(workerResources->bufferLock)); } }
void NetConnection::listn(int port){ listenfd=Open_listenfd(port); }
//main driver function int main(int argc, char **argv) { int listenfd, connfd, port; socklen_t clientlen; struct sockaddr_in clientaddr; struct hostent; unsigned int secretKey; if (argc != 3) { fprintf(stderr, "usage: %s <port> <secretKey>\n", argv[0]); exit(0); } port = atoi(argv[1]); secretKey = atoi(argv[2]); listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); int requestType = -1; int status = -1; rio_t rio; Rio_readinitb(&rio, connfd); int KeyCheck = checkKey(&rio, secretKey); if (KeyCheck == 0) { requestType = getRequest(&rio); if (requestType == 0) { printf("Request Type = get\n"); status = retrieveFile(&rio, connfd); } else if (requestType == 1) { printf("Request Type = put\n"); status = store(&rio, connfd); } else if (requestType == 2) { printf("Request Type = del\n"); status = deleteRequest(&rio, connfd); } else if (requestType == 3) { printf("Request Type = list\n"); status = listFilesRequest(&rio, connfd); } else { printf("Request Type = invalid"); } } if (status == 0) { printf("Operation Status = Success\n"); } else { printf("Operation Status = error\n"); } printf("-------------------------------\n"); Close(connfd); } exit(0); }
int main(int argc, char **argv) { int listenfd,connfd, port,clientlen; pid_t pid; struct sockaddr_in clientaddr; char isdaemon=0,*portp=NULL,*logp=NULL,tmpcwd[MAXLINE]; #ifdef HTTPS int sslport; char dossl=0,*sslportp=NULL; #endif openlog(argv[0],LOG_NDELAY|LOG_PID,LOG_DAEMON); cwd=(char*)get_current_dir_name(); strcpy(tmpcwd,cwd); strcat(tmpcwd,"/"); /* parse argv */ #ifdef HTTPS parse_option(argc,argv,&isdaemon,&portp,&logp,&sslportp,&dossl); sslportp==NULL ?(sslport=atoi(Getconfig("https"))) : (sslport=atoi(sslportp)); if(dossl==1||strcmp(Getconfig("dossl"),"yes")==0) dossl=1; #else parse_option(argc,argv,&isdaemon,&portp,&logp); #endif portp==NULL ?(port=atoi(Getconfig("http"))) : (port=atoi(portp)); Signal(SIGCHLD,sigChldHandler); /* init log */ if(logp==NULL) logp=Getconfig("log"); initlog(strcat(tmpcwd,logp)); /* whethe show dir */ if(strcmp(Getconfig("dir"),"no")==0) isShowdir=0; clientlen = sizeof(clientaddr); if(isdaemon==1||strcmp(Getconfig("daemon"),"yes")==0) Daemon(1,1); writePid(1); /* $https start */ #ifdef HTTPS if(dossl) { if((pid=Fork())==0) { listenfd= Open_listenfd(sslport); ssl_init(); while(1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { Close(connfd); continue; } else if(pid==0) { ishttps=1; doit(connfd); exit(1); } } } } #endif /* $end https */ listenfd = Open_listenfd(port); while (1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { Close(connfd); continue; } else if(pid==0) { doit(connfd); exit(1); } } }
int main(int argc, char **argv) { int listenfd, connfd, port, clientlen; pid_t pid; struct sockaddr_in clientaddr; char daemon=0,*portp=NULL,*logp=NULL,tmpcwd[MAXLINE]; cwd=(char*)get_current_dir_name(); strcpy(tmpcwd,cwd); strcat(tmpcwd,"/"); /* parse argv */ parse_option(argc,argv,&daemon,&portp,&logp); portp==NULL ?(port=atoi(getconfig("port"))) : (port=atoi(portp)); /* init log */ if(logp==NULL) logp=getconfig("log"); initlog(strcat(tmpcwd,logp)); /* whethe show dir */ if(strcmp(getconfig("dir"),"no")==0) isShowdir=0; if(daemon==1||strcmp(getconfig("daemon"),"yes")==0) init_daemon(); listenfd = Open_listenfd(port); /* $https start */ if((pid=Fork())==0) { listenfd= Open_listenfd(4444); ssl_init(); while(1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { close(connfd); continue; } else if(pid==0) { ishttps=1; doit(connfd); } } } /* $end https */ while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { close(connfd); continue; } else if(pid==0) { doit(connfd); } } }
int main(int argc, char **argv) { int listenfd, connfd, port; socklen_t clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); // read in the first line from the client readconfig(); readcommand(); //size_t n; char buf[128]; char user[40]; char pass[40]; rio_t rio; listenfd = Open_listenfd(port); clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Rio_readinitb(&rio, connfd); //read username and password from client. Each individual line. Rio_readlineb(&rio, user, 40); //line:netp:echo:eof Rio_readlineb(&rio, pass, 40); int login = 0; int ctr = 0; strtok(pass, "\n"); strtok(user, "\n"); while(ctr < 128) { if(strcmp(userpass[0][ctr], user) == 0 && (strcmp(userpass[1][ctr], pass)) == 0) { login++; } ctr++; } if (login == 0) { printf("User %s Failed logging in\n", user); Rio_writen(connfd, "Login Failed\n", 14); Close(connfd); exit(0); } else { // Determine the domain name and IP address of the client hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("server connected to %s (%s)\n", hp->h_name, haddrp); printf("User %s logging in from %s at TCP Port %d\n", user, haddrp, port); printf("User %s successfully logged in.\n", user); Rio_writen(connfd, "Login Successful\n", 17);//Warning if this isn't 17 it will affect every Rio_writen after this ctr = 0; while(ctr == 0) { //reads your command from the client Rio_readlineb(&rio, buf, 128); strtok(buf, "\n"); printf("User %s sent the commandline %s to be executed\n", user, buf); if(strcmp(buf, "quit") == 0) { printf("Logged out\n"); Rio_writen(connfd, "quit\n", 5);// Same warning as above ctr++; } else { char *file; char *command; char *thirdinput; char *str = buf; //buf = /bin/cat echo.c command = strtok(str, " "); //command = /bin/cat file = strtok(NULL, " "); //file = echo.c thirdinput = strtok(NULL, " "); int usecommand = 0; int ctr2 = 0; while(ctr2 < 128) { if(strcmp(execcom[ctr2], command) == 0) { usecommand++; } ctr2++; } if(usecommand == 0) { Rio_writen(connfd, "nocomm\n", 7);// Same warning as above printf("Cannot execute '%s' on this server.\n", buf); } else { printf("Forking/Execing the command '%s' on behalf of %s\n", buf, user); char buffer[MAXLINE]; int pipefd[2]; pipe(pipefd); pid_t pid = fork(); if (!pid) { close(pipefd[0]); dup2(pipefd[1], 1); dup2(pipefd[1], 2); close(pipefd[1]); char *env[] = {0}; char *arg[] = {command, file, thirdinput, NULL}; //call your executable in your current directory execve(command, arg, env); } else if (pid == -1) { perror("fork"); } else { //parent close(pipefd[1]); while (read(pipefd[0], buffer, sizeof(buffer)) != 0) { } } int status; wait(&status); //sends out the string results of exec. Rio_writen(connfd, buffer, strlen(buffer)); //we use this as a counter to tell the client that we have sent everything over. Rio_writen(connfd, "exec\n", 5);// Same warning as above int ctr2 = 0; while(ctr2 < MAXLINE) { buffer[ctr2] = 0; ctr2++; } } } } } Close(connfd); exit(0); }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; int acc_count = 0; //if (strcmp(argv[2], "compute-0-29.local") == 0) //{ printf("%s listening\n", argv[2]); listenfd = Open_listenfd ("15618"); //} sleep(5); //int send_val = 5; printf("Hostname is %s\n", argv[2]); printf("Int got as %d\n", atoi(argv[1])); if (strcmp(argv[2], "compute-0-29.local") == 0) { while (1) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); //newfd = (int *) malloc (sizeof (int)); //newfd = connfd; // go serve this client! // pthread_create (&tid, NULL, doit, newfd); acc_count++; double send_double = 232.23; int retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } int len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); if (acc_count == 3) { printf("Accepted 3 connections.\n"); break; } } } else { int serverfd = Open_clientfd ("10.22.1.241", "15618"); printf("In host %s, serverfd is %d\n", argv[2], serverfd); double buf; int len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); buf = 99.104; int retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } } return 0; }
void* server(void* args){ int clientlen,connfd; char cmd[BUFFER_SIZE]; char filename[BUFFER_SIZE]; int piece_num; int piece_pos; int listenfd = Open_listenfd(myport); int optval = 1; rio_t client; struct sockaddr_in clientaddr; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&optval, sizeof(int)); char buf1[BUFFER_SIZE]; while(1) { // Get_piece clientlen = sizeof(clientaddr); /* accept a new connection from a client */ connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(connfd < 0) { perror("Server Accept Failed:"); break; } //char buf1[BUFFER_SIZE]; bzero(buf1, BUFFER_SIZE); if(recv(connfd, buf1,BUFFER_SIZE, 0) < 0) { perror("Server Recieve Data Failed:"); break; } //printf("the receive request buffer is %s\n ",buf1); // read client request about which file and which piece char num_temp[20]; char pos_temp[20]; sscanf(buf1, "%s %s %s %s", cmd,filename,num_temp,pos_temp); piece_num=atoi(num_temp); piece_pos=atoi(pos_temp); memset(buf1, 0, sizeof(buf1)); // split the file if(strcmp(cmd,"Get_piece")==0){ int size= getFileSize(filename); //printf("the file size is %d\n",size); //decide how many pieces to divide int piece_size = getPieceSize(size,piece_num); // divide the file char shell_cmd[BUFFER_SIZE]; sprintf(shell_cmd,"%s %s %d %s","split","-b",piece_size,filename); int dummy = system(shell_cmd); // get the specific piece name int temp = 0xaa + piece_pos-1; char temp2[10]; sprintf(temp2,"%x",temp); char piecename[100]; sprintf(piecename,"%s%s","x",temp2); //and send the requested file back to client char buffer[BUFFER_SIZE]; FILE *fp = fopen(piecename, "r"); if(NULL == fp) { printf("File:%s Not Found\n", piecename); } else { bzero(buffer, BUFFER_SIZE); int length = 0; while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) { if(send(connfd, buffer, length, 0) < 0) { printf("Send File:%s Failed./n", piecename); break; } bzero(buffer, BUFFER_SIZE); } fclose(fp); //printf("File:%s Transfer Successful!\n", piecename); } close(connfd); } if(piece_num!=0){ char *temp; char s[10] = "xaa"; for(int i=0;i<piece_num;i++){ char rec[MAXLINE]; if(i!=0){ sprintf(rec,"%s %s",temp,s); } else{ sprintf(rec,"%s",s); } s[2]=s[2]+1; temp=strdup(rec); } char act[MAXLINE]; sprintf(act,"%s %s","rm",temp); int dummy = system(act); } } }
int main(int argc, char *argv[]) { int listenfd, connfd, port, clientlen, numworkers; char *sched; struct sockaddr_in clientaddr; struct stat sbuf; pthread_mutex_init(&m, NULL); // lock for producer and consumer loops pthread_cond_init(&empty, NULL); // CV for master/producer to wait on pthread_cond_init(&fill, NULL); // CV for workers/consumers to wait on getargs(&port, &numworkers, &bufsize, &sched, argc, argv); // // CS537: Create some threads... // pthread_t threads[numworkers]; // array of worker threads int i; for (i = 0; i < numworkers; i++) { pthread_create(&threads[i], NULL, consumer, NULL); } listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); pthread_mutex_lock(&m); buff_t* newNode = malloc(sizeof(buff_t)); newNode->fd = connfd; Rio_readinitb(&((*newNode).rio), newNode->fd); Rio_readlineb(&((*newNode).rio), newNode->buf, MAXLINE); sscanf(newNode->buf, "%s %s %s", newNode->method, newNode->uri, newNode->version); requestReadhdrs(&((*newNode).rio)); newNode->is_static = requestParseURI(newNode->uri, newNode->filename, newNode->cgiargs); stat(newNode->filename, &sbuf); newNode->filesize = sbuf.st_size; newNode->filenamesize = strlen(newNode->filename); //newNode->cgiargs = cgiargs; while(numrequests == bufsize) { pthread_cond_wait(&empty, &m); } if (strcmp(sched, "FIFO") == 0) { fill_buff(newNode); } else if (strcmp(sched, "SFNF") == 0) { fill_buff2(newNode); } else if (strcmp(sched, "SFF") == 0) { fill_buff3(newNode); } pthread_cond_signal(&fill); pthread_mutex_unlock(&m); //fprintf(stderr, "numrequests: %d\n", numrequests); // // CS537: In general, don't handle the request in the main thread. // Save the relevant info in a buffer and have one of the worker threads // do the work. However, for SFF, you may have to do a little work // here (e.g., a stat() on the filename) ... // //requestHandle(connfd); //Close(connfd); } }
int main(int argc, char *argv[]) { int listenfd, connfd, port, threads, buffers, alg, clientlen; struct sockaddr_in clientaddr; struct timeval arrival; getargs(&port, &threads, &buffers, &alg, argc, argv); max = buffers; numfull = fillptr = useptr = 0; algorithm = alg; // SFF-BS if(alg > 0) { epochs = malloc((buffers / alg) * sizeof(*epochs)); } // FIFO or SFF else { buffer = malloc(buffers * sizeof(*buffer)); } cid = malloc(threads*sizeof(*cid)); int i; for(i = 0; i< threads; i++) { cid[i] = malloc(sizeof(pthread_t)); pthread_create(cid[i], NULL, consumer, NULL); } // // CS537: Create some threads... // listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); gettimeofday(&arrival, NULL); pthread_mutex_lock(&lock); while(numfull == max) { pthread_cond_wait(&empty, &lock); } request *req = malloc(sizeof(request)); //FIFO if(alg == -2) { buffer[fillptr] = req; fillptr = (fillptr+1) % max; } //SFF else if(alg == -1) { req->size = requestFileSize(connfd); buffer[fillptr] = req; fillptr++; if(fillptr > 1) { qsort(buffer, fillptr, sizeof(*buffer), requestcmp); } } //SFF-BD else { epoch *epo; if(epochs[fillptr] == NULL) { epo = malloc(sizeof(epoch)); epo->buffer = malloc(alg * sizeof(*epo->buffer)); epochs[fillptr] = epo; } else { epo = epochs[fillptr]; } req->size = requestFileSize(connfd); epo->buffer[epo->fillptr] = req; epo->fillptr++; epo->requests++; if(epo->fillptr > 1) { qsort(epo->buffer, epo->fillptr, sizeof(*epo->buffer), requestcmp); } if(epo->requests >= alg) { fillptr++; } } req->fd = connfd; req->arrival = ((arrival.tv_sec) * 1000 + arrival.tv_usec/1000.0) + 0.5; numfull++; pthread_cond_signal(&fill); pthread_mutex_unlock(&lock); } }
int main(int argc, char *argv[]) { int listenfd, connfd, port, clientlen, nThreads, bufferSize; int i; struct sockaddr_in clientaddr; getargs(&port, &nThreads, &bufferSize, argc, argv); // Initialize other local and global variables reqNum = 0; reqIndexFill = 0; reqIndexServe = 0; bs = bufferSize; // Allocate a buffer to hold the maximum number of requests the // server can handle at one time. reqBuffer = (Req**) malloc(sizeof(*reqBuffer) * bufferSize); // printf("req buffer size = %ld\n", sizeof(*reqBuffer)*bufferSize); // // CS537: Create some threads... // // Allocate a buffer to hold the maximum number of worker threads // the server can use. cBuffer = (pthread_t **) malloc(sizeof(*cBuffer) * nThreads); // printf("cBuffer size = %ld\n", sizeof(*cBuffer) * nThreads); // Allocate a pool of threads to be used to process requests as // they arrive. for(i = 0; i < nThreads; i++){ cBuffer[i] = (pthread_t*) malloc(sizeof(pthread_t)); pthread_create(cBuffer[i], NULL, serveRequest, NULL); } listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); // Busy wait while there is no more room in pthread_mutex_lock(&tLock); while(reqNum == bufferSize){ pthread_cond_wait(&tEmpty, &tLock); } // Allocate a new request Req *nReq = (Req*) malloc(sizeof(Req)); nReq->cfd = connfd; reqNum++; /* // First in first out implementation reqBuffer[reqIndexFill] = nReq; reqIndexFill = (reqIndexFill+1) % bufferSize; */ // Smallest file first implementation struct stat reqStat; fstat(connfd, &reqStat); nReq->cfd = connfd; nReq->reqSize = reqStat.st_size; reqBuffer[reqIndexFill] = nReq; reqIndexFill++; if(reqIndexFill > 1){ qsort(reqBuffer, reqIndexFill, sizeof(*reqBuffer), reqSizeComp); } // // CS537: In general, don't handle the request in the main thread. // Save the relevant info in a buffer and have one of the worker threads // do the work. // //requestHandle(connfd); pthread_cond_signal(&tFill); pthread_mutex_unlock(&tLock); // Close(connfd); } }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; char *common_port = "15618"; listenfd = Open_listenfd (common_port); sleep(5); total_workers = atoi(argv[3]); numchunks = atoi(argv[4]); numtrain = atoi(argv[5]); dimensions = atoi(argv[6]); prefix_input_file_name = std::string(argv[7]); prefix_label_file_name = std::string(argv[8]); printf("Hostname %s is listening on port %s with listenfd = %d\n", argv[1], common_port, listenfd); printf("Node is %s and Master is %s. Number of workers is %d\n", argv[1], argv[2], total_workers); printf("Number of shards (chunks) = %d\n", numchunks); printf("Number of traning points for each expert = %d, with D = %d\n", numtrain, dimensions); printf("Input file prefix: %s, Label file prefix = %s\n", prefix_input_file_name.c_str(), prefix_label_file_name.c_str()); if (strcmp(argv[1], argv[2]) == 0) { for (int i = 0; i < total_workers - 1; i++) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); worker_conn_fds.push_back(connfd); int new_worker_id = i + 1; Rio_writen (connfd, (void *)&new_worker_id, sizeof(int)); } } else { connfd = Open_clientfd (argv[2], common_port); printf("Host %s connected to master, connfd is %d\n", argv[1], connfd); Rio_readn (connfd, (void *)&worker_id, sizeof(int)); printf("Host %s got worker id as %d\n", argv[1], worker_id); } if (strcmp(argv[1], argv[2]) == 0) { printf("Master calling cg_solve()\n"); setup(numtrain, dimensions); BCM_log_hyperparams = new double[3]; Eigen::VectorXd initval(3); for(int i = 0 ; i < 3; i++){ initval[i] = 2.0; } set_loghyper_eigen_multinode(initval); double startime = CycleTimer::currentSeconds(); cg_solve(argv[1]); double endtime = CycleTimer::currentSeconds(); printf("TOTAL training time = %lf\n", endtime - startime); destruct_cublas_cusoler(); // testing_phase(numtrain,numtrain); } else { printf("Worker skipping cg_solve(), instead calling accept_commands\n"); setup(numtrain, dimensions); accept_commands(argv[1], connfd); } return 0; }