Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <errno.h>
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include "restart.h"
  10. #include "uici.h"
  11. #include "nqueens_library.c"
  12.  
  13. static pthread_mutex_t lock;
  14. int main(int argc, char *argv[]) {
  15.    int bytescopied;
  16.    char client[MAX_CANON];
  17.    int communfd;
  18.    int listenfd;
  19.    u_port_t portnumber;
  20.    pid_t child;
  21.    thread_info_t info;
  22.    int solution, totalSolutions;
  23.    
  24.    if (argc != 2) {
  25.       fprintf(stderr, "Usage: %s port\n", argv[0]);
  26.       return 1;
  27.    }
  28.    
  29.    //mutex init
  30.    if (pthread_mutex_init(&lock, NULL) != 0)
  31.    {
  32.        printf("\n mutex init failed\n");
  33.        return 1;
  34.     }
  35.  
  36.     portnumber = (u_port_t) atoi(argv[1]);
  37.     if ((listenfd = u_open(portnumber)) == -1) {
  38.        perror("Failed to create listening endpoint");
  39.        return 1;
  40.     }
  41.  
  42.     fprintf(stderr, "[%ld]: Waiting for connection on port %d\n",
  43.      (long)getpid(), (int)portnumber);
  44.  
  45.     for ( ; ;  ) {
  46.       if ((communfd = u_accept(listenfd, client, MAX_CANON)) == -1) {
  47.        perror("Failed to accept connection");
  48.        continue;
  49.       }
  50.  
  51.      fprintf(stderr, "[%ld]:connected to %s\n", (long)getpid(), client);
  52.      
  53.      // fork a process
  54.      if ((child = fork()) == -1)
  55.         perror("Failed to fork a child");
  56.      
  57.      if (child == 0) {  /* child code */
  58.         if (r_close(listenfd) == -1) {
  59.            fprintf(stderr, "[%ld]:failed to close listenfd: %s\n", (long)getpid(), strerror(errno));
  60.            return 1;
  61.         }
  62.  
  63.         bytescopied = r_read(communfd, &info, sizeof(thread_info_t));
  64.         solution = generate_n_queens_server_one(info.n, info.first, communfd);
  65.  
  66.         //pthread_mutex_lock(&lock);
  67.         info.result = solution;
  68.         pthread_mutex_lock(&lock);
  69.         //fprintf(stderr, "\nn = %d, first = %d\n", info.n, info.first);
  70.         fprintf(stderr, "\nSolutions: %d\n", solution);
  71.         fprintf(stderr, "[%ld]:received %d bytes\n", (long)getpid(), bytescopied);
  72.         //r_write(communfd, &solution, sizeof(int));
  73.         pthread_mutex_unlock(&lock);
  74.  
  75.         //added this
  76.  
  77.         return 0;
  78.      }
  79.    if (r_close(communfd) == -1)                             /* parent code */
  80.       fprintf(stderr, "[%ld]:failed to close communfd: %s\n", (long)getpid(), strerror(errno));
  81.  //  while (r_waitpid(-1, NULL, WNOHANG) > 0);  /* clean up zombies */
  82.  
  83.    }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement