Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Read/Write to a file at the same time with 2 threads?
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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 :)</p> <p>Here is the server(second version):</p> <pre><code>#include &lt;stdio.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;time.h&gt; 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 &lt; 2) { fprintf(stderr , "ERROR , no port provided!"); exit(1); } sockfd = socket(AF_INET , SOCK_STREAM , 0); if( sockfd &lt; 0 ) error("ERROR opening socket."); bzero( (char *) &amp;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 *) &amp;server_addr , sizeof(server_addr)) &lt; 0 ) { error("ERROR on binding."); } listen(sockfd , 5); cli_length = sizeof(&amp;client_addr); newsockfd = accept( sockfd , (struct sockaddr *) &amp;client_addr , &amp;cli_length ); if (newsockfd &lt; 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 &lt; 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( &amp;thread , NULL , printData , NULL ); thread_Created = 1; } } } fclose(fp); /* while(pthread_kill(thread , 0 ) == 0 ) { */ /* } */ } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload