title: What causes a Server to read the last message of a Client over and over again?
body: <p>Given that code for a server, a really unexplainable bug appears in the job_read function.</p>
  
  <p>If this server is connecting with clients, everything seems to work fine - although there might be a handful of other bugs. If a client connects, a new thread will be started, which then reads the input of the client. If the client sends a message this message will be spread to all other clients. The client is run through the terminal. If you stop the client with STRG + C everything will work just fine, but not if you crash it when you exit the terminal.  </p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define BUFLEN 255
  #define MAX_CONNECTIONS 128
  #define MAX_NAME_LENGTH 30
  
  struct JOB_INFO {
      int socket_ids[MAX_CONNECTIONS];
      bool end_of_program;
      int open_cnncts;
      pthread_mutex_t socketids_changingMutex;
  };
  
  struct SERVER_INFO {
      struct sockaddr_in serv_add;
      struct sockaddr_in cli_adr;
      socklen_t cli_len;
      pthread_t write_thread;
  };
  //Global Variables
  FILE* plog_file;
  
  void delete_socket(int socketid, struct JOB_INFO* pjob_data);
  void* job_read(void*);
  void* job_write(void*);
  void error(const char* msg);
  void setUpFileStream(int argc, char* argv[]);
  int setUpConnections(struct SERVER_INFO* pserver_data, char* port_numc);
  void enterConnectingLoop(struct SERVER_INFO* pserver_data, struct JOB_INFO* pjob_data, int sockfd);
  
  void error(const char* msg){
      perror(msg);
      exit(1);
  }
  
  int main(int argc, char* argv[]) {
      setUpFileStream(argc, argv);
  
      int sock_fd;
      struct JOB_INFO job_data;
      job_data.end_of_program = false;
      job_data.open_cnncts = 0;
  
      struct SERVER_INFO server_data;
      //Create mutex
      if(pthread_mutex_init(&amp;(job_data.socketids_changingMutex), NULL) &lt; 0){
          error("Could not initialize Mutex");
      }
      //Initialzing threads and create write_thread
      pthread_create(&amp;server_data.write_thread, NULL, job_write, (void*)&amp;job_data);
  
      //Setup for connections
      sock_fd = setUpConnections(&amp;server_data, argv[1]);
      fprintf(plog_file,"Listening...");
      fflush(stdout); //For unknown reason this is needed.
      enterConnectingLoop(&amp;server_data, &amp;job_data, sock_fd);
  
      job_data.end_of_program = true;
      close(sock_fd);
  
      pthread_join(server_data.write_thread, NULL);
      pthread_mutex_destroy(&amp;job_data.socketids_changingMutex);
      return EXIT_SUCCESS;
  }
  
  void* job_read(void * p){
  
      char** pc = (char**)p; //allow pointer arithmetic
      int new_sock_fd = *((int*) (pc[1]));  //Casting
      struct JOB_INFO* pjob_data = ((struct JOB_INFO*) (pc[0])); //Casting
  
      ssize_t n; //Error catching variable
      ssize_t m; //Error catching variable
      char buffer[BUFLEN];
      char name[MAX_NAME_LENGTH];
      sprintf(name, "Client %d: ", new_sock_fd);
      fprintf(plog_file, "Reading from %s\n", name);
  
      while(!pjob_data-&gt;end_of_program){
          n = read(new_sock_fd, buffer, BUFLEN-1);
          if ( n &gt; 0 ) buffer[n] = '\0';  // add a \0 on success
          if(n&lt;0){
              printf("Buffer: %s", buffer);
              error("Reading Failed");
          }
          pthread_mutex_lock(&amp;pjob_data-&gt;socketids_changingMutex);
          if(n == 0){
              delete_socket(new_sock_fd, pjob_data);
              pthread_exit(NULL);
          }
  
          for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
              if(pjob_data-&gt;socket_ids[i] == new_sock_fd){
                  continue;
              }
              m = write(pjob_data-&gt;socket_ids[i], name, strlen(name));
              if((m &lt; 0)){
                  error("Writing name failed");
              }
              n = write(pjob_data-&gt;socket_ids[i], buffer, strlen(buffer));
              if((n &lt; 0)){
                  error("Writing message failed");
              }
          }
          pthread_mutex_unlock(&amp;pjob_data-&gt;socketids_changingMutex);
          printf("%s%s", name, buffer);
          fflush(stdout); //For unknown reason this is needed.
      }
      delete_socket(new_sock_fd, pjob_data);
      free(p);
      pthread_exit( NULL );
  }
  
  void* job_write(void* args){
      struct JOB_INFO* pjob_data = (struct JOB_INFO*)(args);
      fprintf(plog_file, "Started writing thread...\n");
      ssize_t n; //Error catching variable
      ssize_t m; //Error catching variable
      char buffer[BUFLEN];
      char* name = "Server: \0";
  
      while(!pjob_data-&gt;end_of_program) {
          fgets(buffer, BUFLEN, stdin);
  
          pthread_mutex_lock(&amp;pjob_data-&gt;socketids_changingMutex);
          for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
              m = write(pjob_data-&gt;socket_ids[i], name, strlen(name));
              if((m &lt; 0)){
                  error("Writing name failed");
              }
              n = write(pjob_data-&gt;socket_ids[i], buffer, strlen(buffer));
              if((n &lt; 0)){
                  error("Writing message failed");
              }
          }
          pthread_mutex_unlock(&amp;pjob_data-&gt;socketids_changingMutex);
          if(strcmp("Bye\n", buffer) == 0){
              exit(EXIT_SUCCESS);
          }
      }
      free(args);
      pjob_data-&gt;end_of_program = true;
      pthread_exit( NULL );
  }
  
  void enterConnectingLoop(struct SERVER_INFO* pserver_data, struct JOB_INFO* pjob_data, int sockfd){
      listen(sockfd, MAX_CONNECTIONS);
  
      for(pjob_data-&gt;open_cnncts = 0; (!pjob_data-&gt;end_of_program); /*mutex needs to be set*/ ){
          void** p = malloc(2*sizeof(void*));
          p[0] = (void*)pjob_data;
          p[1] = (void*)&amp;(pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]);
          pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts] =
                  accept(sockfd, (struct sockaddr*) &amp;pserver_data-&gt;cli_adr, &amp;pserver_data-&gt;cli_len);
          pthread_mutex_lock(&amp;(pjob_data-&gt;socketids_changingMutex)); //Note you cannot mutex the accept()
          printf("SOCKETFD %d\n", (pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]));
          fprintf(plog_file,"Client connected.\n");
          pthread_t thread;
          pthread_create(&amp;thread , NULL, job_read, p);
          pjob_data-&gt;open_cnncts++;
          pthread_mutex_unlock(&amp;(pjob_data-&gt;socketids_changingMutex));
      }
  }
  
  void delete_socket(int socketid, struct JOB_INFO* pjob_data){
      bool found = false;
      for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
          if(found){
              pjob_data-&gt;socket_ids[i-1] = pjob_data-&gt;socket_ids[i];
          }
          if(pjob_data-&gt;socket_ids[i] == socketid){
              close(socketid);
              found = true;
          }
      }
      if(found){
          pjob_data-&gt;open_cnncts--;
      }
  }
  
  inline void setUpFileStream(int argc, char* argv[]){
      printf("Server started...\n");
      if(argc &lt; 2){
          fprintf(stderr, "You must provide a port number");
          exit(EXIT_FAILURE);
      }
      if(argc == 3){
          plog_file = fopen(argv[2], "w");
      } else {
          plog_file = fopen("logfile.txt", "w");
      }
      plog_file = stdout;
      stderr = plog_file;
  }
  
  inline int setUpConnections(struct SERVER_INFO* pserver_data, char* port_numc){
      pserver_data-&gt;cli_len = sizeof(pserver_data-&gt;cli_adr);
      memset(&amp;pserver_data-&gt;serv_add, 0, sizeof((pserver_data-&gt;serv_add)));
  
      uint16_t port_num = (uint16_t)atoi(port_numc);
      pserver_data-&gt;serv_add.sin_family = AF_INET;
      pserver_data-&gt;serv_add.sin_addr.s_addr = INADDR_ANY;
      pserver_data-&gt;serv_add.sin_port = htons(port_num);
  
      int sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd &lt; 0){
          error("Error opening socket.");
      }
  
      if(bind(sockfd, (struct sockaddr*) (&amp;pserver_data-&gt;serv_add), sizeof(pserver_data-&gt;serv_add)) &lt; 0){
          error("Binding failed.");
      }
      return sockfd;
  }", buffer) == 0){
              exit(EXIT_SUCCESS);
          }
      }
      free(args);
      pjob_data-&gt;end_of_program = true;
      pthread_exit( NULL );
  }
  
  void enterConnectingLoop(struct SERVER_INFO* pserver_data, struct JOB_INFO* pjob_data, int sockfd){
      listen(sockfd, MAX_CONNECTIONS);
  
      for(pjob_data-&gt;open_cnncts = 0; (!pjob_data-&gt;end_of_program); /*mutex needs to be set*/ ){
          void** p = malloc(2*sizeof(void*));
          p[0] = (void*)pjob_data;
          p[1] = (void*)&amp;(pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]);
          pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts] =
                  accept(sockfd, (struct sockaddr*) &amp;pserver_data-&gt;cli_adr, &amp;pserver_data-&gt;cli_len);
          pthread_mutex_lock(&amp;(pjob_data-&gt;socketids_changingMutex)); //Note you cannot mutex the accept()
          printf("SOCKETFD %d\n", (pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]));
          fprintf(plog_file,"Client connected.\n");
          pthread_t thread;
          pthread_create(&amp;thread , NULL, job_read, p);
          pjob_data-&gt;open_cnncts++;
          pthread_mutex_unlock(&amp;(pjob_data-&gt;socketids_changingMutex));
      }
  }
  
  void delete_socket(int socketid, struct JOB_INFO* pjob_data){
      bool found = false;
      for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
          if(found){
              pjob_data-&gt;socket_ids[i-1] = pjob_data-&gt;socket_ids[i];
          }
          if(pjob_data-&gt;socket_ids[i] == socketid){
              close(socketid);
              found = true;
          }
      }
      if(found){
          pjob_data-&gt;open_cnncts--;
      }
  }
  
  inline void setUpFileStream(int argc, char* argv[]){
      printf("Server started...\n");
      if(argc &lt; 2){
          fprintf(stderr, "You must provide a port number");
          exit(EXIT_FAILURE);
      }
      if(argc == 3){
          plog_file = fopen(argv[2], "w");
      } else {
          plog_file = fopen("logfile.txt", "w");
      }
      plog_file = stdout;
      stderr = plog_file;
  }
  
  inline int setUpConnections(struct SERVER_INFO* pserver_data, char* port_numc){
      pserver_data-&gt;cli_len = sizeof(pserver_data-&gt;cli_adr);
      memset(&amp;pserver_data-&gt;serv_add, 0, sizeof((pserver_data-&gt;serv_add)));
  
      uint16_t port_num = (uint16_t)atoi(port_numc);
      pserver_data-&gt;serv_add.sin_family = AF_INET;
      pserver_data-&gt;serv_add.sin_addr.s_addr = INADDR_ANY;
      pserver_data-&gt;serv_add.sin_port = htons(port_num);
  
      int sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd &lt; 0){
          error("Error opening socket.");
      }
  
      if(bind(sockfd, (struct sockaddr*) (&amp;pserver_data-&gt;serv_add), sizeof(pserver_data-&gt;serv_add)) &lt; 0){
          error("Binding failed.");
      }
      return sockfd;
  }
  </code></pre>
  
  <p>Within the job function, the Server seems to print the last message of the Client over and over again.</p>
  
  <pre><code>void* job_read(void * p){
  
      char** pc = (char**)p; //allow pointer arithmetic
      int new_sock_fd = *((int*) (pc[1]));  //Casting
      struct JOB_INFO* pjob_data = ((struct JOB_INFO*) (pc[0])); //Casting
  
      ssize_t n; //Error catching variable
      ssize_t m; //Error catching variable
      char buffer[BUFLEN];
      char name[MAX_NAME_LENGTH];
      sprintf(name, "Client %d: ", new_sock_fd);
      fprintf(plog_file, "Reading from %s\n", name);
  
      while(!pjob_data-&gt;end_of_program){
          n = read(new_sock_fd, buffer, BUFLEN-1);
          if ( n &gt; 0 ) buffer[n] = '\0';  // add a \0 on success
          if(n&lt;0){
              printf("Buffer: %s", buffer);
              error("Reading Failed");
          }
          pthread_mutex_lock(&amp;pjob_data-&gt;socketids_changingMutex);
          if(n == 0){
              delete_socket(new_sock_fd, pjob_data);
              pthread_exit(NULL);
          }
  
          for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
              if(pjob_data-&gt;socket_ids[i] == new_sock_fd){
                  continue;
              }
              m = write(pjob_data-&gt;socket_ids[i], name, strlen(name));
              if((m &lt; 0)){
                  error("Writing name failed");
              }
              n = write(pjob_data-&gt;socket_ids[i], buffer, strlen(buffer));
              if((n &lt; 0)){
                  error("Writing message failed");
              }
          }
          pthread_mutex_unlock(&amp;pjob_data-&gt;socketids_changingMutex);
          printf("%s%s", name, buffer);
          fflush(stdout); //For unknown reason this is needed.
      }
      delete_socket(new_sock_fd, pjob_data);
      free(p);
      pthread_exit( NULL );
  }
  </code></pre>
  
  <p>There are two things. Within this function, the name variable seems to disappear from time to time.  She does not get printed nor sent. But as far as I know, the name variable is always initialized.</p>
  
  <p><strong>Reproduce the bug:</strong></p>
  
  <p>Compile Server and Client(code below). 
  Start Server and open a port: </p>
  
  <pre><code>./Server 9999
  </code></pre>
  
  <p>Start Client:</p>
  
  <pre><code>./Client 127.0.0.1 9999
  </code></pre>
  
  <p>Type something in the client.
  Exit the client by closing the terminal.
  Server behaves faulty.</p>
  
  <p>Thank you!</p>
  
  <p>Client Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define BUFLEN 255
  
  bool endprogram = false;
  int sockfd;
  
  void error(const char* msg){
      perror(msg);
      exit(1);
  }
  
  void* job_read(void* p){
      char buffer[BUFLEN];
      while(!endprogram){
          bzero(buffer, BUFLEN);
          ssize_t n = read(sockfd, buffer, (BUFLEN));
          if(n &lt; 0){
              error("Error on reading");
          }
          printf("%s", buffer);
          int i = strncmp("Bye", buffer, 3);
          if(i == 0){
              endprogram = true;
              pthread_exit(NULL);
          }
      }
     pthread_exit(NULL);
  }
  
  int main(int argc, const char * argv[]) {
      pthread_t readt;
  
      int16_t portnum;
      struct sockaddr_in serveraddr;
      struct hostent* server;
  
      if(argc &lt; 3){
          perror("You shall provide a port and a ip adress");
      }
      portnum = atoi(argv[2]);
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd &lt; 0){
          error("Error opening socket");
      }
  
      server = gethostbyname(argv[1]);
      if(!server){
          error("No such host");
      }
  
      bzero((char*)&amp;serveraddr, sizeof(serveraddr));
      serveraddr.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr_list[0], (char *)&amp;serveraddr.sin_addr.s_addr, sizeof(server-&gt;h_length));
      serveraddr.sin_port = htons(portnum);
  
      if(connect(sockfd, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr))&lt;0){
          error("Connection failed");
      }
  
      pthread_create(&amp;readt, NULL, &amp;job_read, NULL);
  
      ssize_t n;
      char buffer[BUFLEN];
      while(!endprogram){
          fgets(buffer, BUFLEN, stdin);
          n = write(sockfd, buffer, strlen(buffer));
          if(n &lt; 0){
              error("Error on writing");
          }
          n = strcmp(buffer, "Bye");
          if(n == 0){
              endprogram = true;
          }
      }
      pthread_join(readt, NULL);
      close(sockfd);
      return 0;
  }
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>It is really hard to reduce the problem since I need all these methods to either set the problem up or to maintain core-functionality. However, this is the first reducement (uncompilable), where the bug might be.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define BUFLEN 255
  #define MAX_CONNECTIONS 128
  #define MAX_NAME_LENGTH 30
  
  struct JOB_INFO {
      int socket_ids[MAX_CONNECTIONS];
      bool end_of_program;
      int open_cnncts;
      pthread_mutex_t socketids_changingMutex;
  };
  
  struct SERVER_INFO {
      struct sockaddr_in serv_add;
      struct sockaddr_in cli_adr;
      socklen_t cli_len;
      pthread_t write_thread;
  };
  //Global Variables
  FILE* plog_file;
  
  void delete_socket(int socketid, struct JOB_INFO* pjob_data);
  void* job_read(void*);
  void error(const char* msg);
  void enterConnectingLoop(struct SERVER_INFO* pserver_data, struct JOB_INFO* pjob_data, int sockfd);
  
  int main(int argc, char* argv[]) {
  
      int sock_fd;
      struct JOB_INFO job_data;
      job_data.end_of_program = false;
      job_data.open_cnncts = 0;
  
      struct SERVER_INFO server_data;
      enterConnectingLoop(&amp;server_data, &amp;job_data, sock_fd);
  
      job_data.end_of_program = true;
      close(sock_fd);
  
      pthread_join(server_data.write_thread, NULL);
      pthread_mutex_destroy(&amp;job_data.socketids_changingMutex);
      return EXIT_SUCCESS;
  }
  
  void* job_read(void * p){
  
      char** pc = (char**)p; //allow pointer arithmetic
      int new_sock_fd = *((int*) (pc[1]));  //Casting
      struct JOB_INFO* pjob_data = ((struct JOB_INFO*) (pc[0])); //Casting
  
      ssize_t n; //Error catching variable
      ssize_t m; //Error catching variable
      char buffer[BUFLEN];
      char name[MAX_NAME_LENGTH];
      sprintf(name, "Client %d: ", new_sock_fd);
      fprintf(plog_file, "Reading from %s\n", name);
  
      while(!pjob_data-&gt;end_of_program){
          n = read(new_sock_fd, buffer, BUFLEN-1);
          if ( n &gt; 0 ) buffer[n] = '\0';  // add a \0 on success
          if(n&lt;0){
              printf("Buffer: %s", buffer);
              error("Reading Failed");
          }
          pthread_mutex_lock(&amp;pjob_data-&gt;socketids_changingMutex);
          if(n == 0){
              delete_socket(new_sock_fd, pjob_data);
              pthread_exit(NULL);
          }
  
          for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
              if(pjob_data-&gt;socket_ids[i] == new_sock_fd){
                  continue;
              }
              m = write(pjob_data-&gt;socket_ids[i], name, strlen(name));
              if((m &lt; 0)){
                  error("Writing name failed");
              }
              n = write(pjob_data-&gt;socket_ids[i], buffer, strlen(buffer));
              if((n &lt; 0)){
                  error("Writing message failed");
              }
          }
          printf("%s%s", name, buffer);
          pthread_mutex_unlock(&amp;pjob_data-&gt;socketids_changingMutex);
          fflush(stdout); //For unknown reason this is needed.
      }
      delete_socket(new_sock_fd, pjob_data);
      free(p);
      pthread_exit( NULL );
  }
  
  void enterConnectingLoop(struct SERVER_INFO* pserver_data, struct JOB_INFO* pjob_data, int sockfd){
      listen(sockfd, MAX_CONNECTIONS);
  
      for(pjob_data-&gt;open_cnncts = 0; (!pjob_data-&gt;end_of_program); /*mutex needs to be set*/ ){
          void** p = malloc(2*sizeof(void*));
          p[0] = (void*)pjob_data;
          p[1] = (void*)&amp;(pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]);
          pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts] =
                  accept(sockfd, (struct sockaddr*) &amp;pserver_data-&gt;cli_adr, &amp;pserver_data-&gt;cli_len);
          pthread_mutex_lock(&amp;(pjob_data-&gt;socketids_changingMutex)); //Note you cannot mutex the accept()
          printf("SOCKETFD %d\n", (pjob_data-&gt;socket_ids[pjob_data-&gt;open_cnncts]));
          fprintf(plog_file,"Client connected.\n");
          pthread_t thread;
          pthread_create(&amp;thread , NULL, job_read, p);
          pjob_data-&gt;open_cnncts++;
          pthread_mutex_unlock(&amp;(pjob_data-&gt;socketids_changingMutex));
      }
  }
  
  void delete_socket(int socketid, struct JOB_INFO* pjob_data){
      bool found = false;
      for(int i = 0; i &lt; pjob_data-&gt;open_cnncts; i++){
          if(found){
              pjob_data-&gt;socket_ids[i-1] = pjob_data-&gt;socket_ids[i];
          }
          if(pjob_data-&gt;socket_ids[i] == socketid){
              close(socketid);
              found = true;
          }
      }
      if(found){
          pjob_data-&gt;open_cnncts--;
      }
  }
  </code></pre>
  
  <p>I will try to set up a program which will do the same thing on a pc using input and output file and thereby surrounding this whole inet-setup-monster...</p>
  
  <p><strong>EDIT EDIT:</strong></p>
  
  <p>If the same thing happens outside if an IDE, I get an coredump - instead of stupid output: similar to this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  void* job_read(void* p){
      sleep(1000); //delay to ensure job_write is waiting
      FILE* pfile = ((FILE**)p)[0];
      while(1){
          char buffer[20];
          fgets(buffer, 19, pfile);
          printf("&amp;s", buffer);
      }
  
  }
  
  void* job_write(void* p){
      FILE* pfile = ((FILE**)p)[0];
      fputs("hi\n", pfile);
      sleep(1200); //delay to ensure job_write is waiting
      pthread_exit(NULL); //FUCK THE OPEN CONNECTION
  }
  
  int main(int argc, char* argv[]) {
      (void)argv; //Compiler things
      (void)argc;
      printf("HERE");
      FILE* in = fopen("in.txt", "w");
      pthread_t firstread; //Simulate all threads in actual IO: Server
      pthread_t firstwrite; //Client
  
      FILE** p = malloc(sizeof(FILE*));
      p[0] = in;
  
      pthread_create(&amp;firstwrite, NULL, job_write, (void*)&amp;p);
      pthread_create(&amp;firstread, NULL, job_read, (void*)&amp;p);
  
      pthread_join(firstwrite, NULL);
  }
  </code></pre>
  
  <p>I guess this might be the solution to my actual problem. My assumption is that I try to write a file which no longer exists or something like this. however, I guess it is because of a closed file causing the bug</p>
  
  <p><strong>EDIT EDIT EDIT</strong></p>
  
  <p>I can safely say, that the problem is caused by the job_read function.
  As commented, a smart user realzed that if the terminal quits it will take some time, before the actual process gets killed. This might cause one of the errors.</p>