feluna
feluna

Reputation: 75

How to Read/Write to a file at the same time with 2 threads?

I have a server/client application , with 2 different versions.In the first one client reads a text file and sends text data to the server , and server writes the received data to a new text file("received.txt") ,and when it finishes writing , it prints the text file to screen.

In the second version , client does the same thing , but i want the server to print data to screen at the same time it writes on the file "received.txt". You might wonder why am i doing such a thing. I want to speed up the process of writing to the file and printing the file back to the screen. I tought maybe if i use 2 threads , i might speed up the work.

I encountered with a problem though , (this is the first time that i use pthreads).Server terminates before it prints the data to the screen.I think it is about scheduling. While newly created thread is running and main thread blocked , the file has nothing to read , and then newly created thread terminates. Thats my guess about the problem. What i want to achieve here is to make this read/write simultenously , if its possible. If i'm doing this all wrong , or i cant make simultenous read/write write with threads , let me know :)

Here is the server(second version):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <time.h>

void error(char *msg) {
  perror(msg);
  exit(1);
}

//New Thread function
void *printData() {
  FILE *fp = fopen("received.txt" , "r");
  char buffer;

  time_t time1 = time(NULL);

  if( fp != NULL ) {
    while( ( buffer = fgetc(fp)) != NULL ) {
      printf("%c" , buffer);
    }
    fclose(fp);
  }

  time_t time2 = time(NULL);
  double diff = difftime(time2 , time1);
  printf("It took %.lf seconds.\n" , diff);
  pthread_exit(NULL);
}  

int main(int argc , char *argv[]) {
  int sockfd , newsockfd , port_no , cli_length , n; 
  char buffer[256];
  struct sockaddr_in server_addr , client_addr;
  FILE *fp;

  int thread_Created = 0;
  pthread_t thread;

  if(argc < 2) {
    fprintf(stderr , "ERROR , no port provided!");
    exit(1);
  }

  sockfd = socket(AF_INET , SOCK_STREAM , 0);

  if( sockfd < 0 )
    error("ERROR opening socket.");

  bzero( (char *) &server_addr , sizeof(server_addr) );
  port_no = atoi(argv[1]);

  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons( port_no );       
  server_addr.sin_addr.s_addr = INADDR_ANY;

  if( bind( sockfd , (struct sockaddr *) &server_addr , sizeof(server_addr)) < 0 ) {
    error("ERROR on binding.");
  }

  listen(sockfd , 5);

  cli_length = sizeof(&client_addr);
  newsockfd = accept( sockfd , (struct sockaddr *) &client_addr , &cli_length );
  if (newsockfd < 0 )
    error("ERROR on accept.");

  bzero(buffer , 256);

  if( (fp = fopen("received.txt" , "w")) != NULL ) {
    while( (n = read(newsockfd , buffer , 255)) != 0 ) {

      int i = 0;
      for(i = 0 ; i < 255 ; i++) {
        if( buffer[i] != '\0')
          fputc(buffer[i] , fp);
      }

      //Create a new thread to read the file and print the results
      if( !thread_Created ) {
        pthread_create( &thread , NULL , printData , NULL );
        thread_Created = 1;
      }

    }
  }

  fclose(fp);

  /* while(pthread_kill(thread , 0 ) == 0 ) { */
  /* } */
}

Upvotes: 0

Views: 2793

Answers (1)

Maxim
Maxim

Reputation: 1666

  1. Use mutex to lock "active write section"
  2. Use multiplexing for file IO. See pool or epol, select Linux system calls

Upvotes: 2

Related Questions