Note that there are some explanatory texts on larger screens.

plurals
  1. POhow the system call read and write behave and why the threads cannot work?
    primarykey
    data
    text
    <p>fifo.3 source code:</p> <pre><code> #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;fcntl.h&gt; #include &lt;limits.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;pthread.h&gt; #include &lt;time.h&gt; #define FIFO_NAME "/tmp/my_fifo" #define BUFFER_SIZE PIPE_BUF //4096 #define TEN_MEG (1024 * 1024 * 1) void* thread_tick(void* arg) { int count =0; while(count &lt; 4){ printf("hello, world!\n"); sleep(1); count++; } } void* thread_write(void* arg) { int pipe_fd; int res; int bytes_sent = 0; char buffer[BUFFER_SIZE ]; int count=0; if (access(FIFO_NAME, F_OK) == -1) { res = mkfifo(FIFO_NAME, 0777); if (res != 0) { fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); exit(EXIT_FAILURE); } } while(count &lt; 10){ printf("write: Process %d opening FIFO O_WRONLY\n", getpid()); pipe_fd = open(FIFO_NAME, O_WRONLY); printf("write: Process %d result %d \n", getpid(), pipe_fd); if (pipe_fd != -1) { while(bytes_sent &lt; TEN_MEG) { res = write(pipe_fd, buffer, BUFFER_SIZE); if (res == -1) { fprintf(stderr, "Write error on pipe\n"); exit(EXIT_FAILURE); } bytes_sent += res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("write: Process %d finished , count =%d\n", getpid(),count); count++; } } void CreateThread(void* (*start_routine)(void*), void* arg,int stacksize, int priority) { pthread_t app_thread; pthread_attr_t thread_attr; int res; int max_priority; int min_priority; struct sched_param scheduling_value; res = pthread_attr_init(&amp;thread_attr); if (res != 0) { perror("Attribute creation failed\n"); exit(EXIT_FAILURE); } res = pthread_attr_setdetachstate(&amp;thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } res = pthread_attr_setstacksize(&amp;thread_attr, stacksize); if (res != 0) { perror("Set stack size failed\n"); exit(EXIT_FAILURE); } res = pthread_attr_setschedpolicy(&amp;thread_attr, SCHED_RR); if (res != 0) { perror("Setting schedpolicy failed"); exit(EXIT_FAILURE); } max_priority = sched_get_priority_max(SCHED_RR); min_priority = sched_get_priority_min(SCHED_RR); scheduling_value.sched_priority = priority; res = pthread_attr_setschedparam(&amp;thread_attr, &amp;scheduling_value); if (res != 0) { perror("Setting schedpolicy failed"); exit(EXIT_FAILURE); } res = pthread_create(&amp;app_thread, &amp;thread_attr, (*start_routine), arg); if(res != 0){ perror("Thread creation failed\n"); exit(EXIT_FAILURE); } pthread_attr_destroy(&amp;thread_attr); //res = pthread_join(app_thread ,0 ); //return app_thread; } int main() { CreateThread(thread_write, 0, 50000, 99); CreateThread(thread_tick, 0, 50000, 98); // pthread_join(w,0 ); // pthread_join(t ,0 ); return 0; } </code></pre> <p>fifo.4 source code :</p> <pre><code> #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;fcntl.h&gt; #include &lt;limits.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #define FIFO_NAME "/tmp/my_fifo" #define BUFFER_SIZE PIPE_BUF //4096 int main() { int pipe_fd; int res; char buffer[BUFFER_SIZE ]; int bytes_read = 0; int count = 0; memset(buffer, '\0', sizeof(buffer)); while(count &lt; 10){ printf("read: Process %d opening FIFO O_RDONLY\n", getpid()); pipe_fd = open(FIFO_NAME, O_RDONLY); printf("read: Process %d result %d\n", getpid(), pipe_fd); if (pipe_fd != -1) { do { res = read(pipe_fd, buffer, BUFFER_SIZE); bytes_read += res; } while (res &gt; 0); (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("read: Process %d finished, %d bytes read , count =%d\n", getpid(), bytes_read,count); count++; } return 0; } </code></pre> <p>this is the first time I post code on Stack overflow, so it is in a mess. Above are two C source code. fifo3.c has two thread and thread_write is to write data to named fifo. fifo4.c is to read data from named fifo.</p> <p>my question:</p> <p>1) how does the read(pipe_fd, buffer, BUFFER_SIZE) behave when write() is writing data to fifo? If read() can not read data, SHOULD not read() return 0 and then exit, why read() would wait write() to finish write data??? of course, how does write() behave when read() is reading?</p> <p>2) in fifo3.c , I create two threads, when I create them detached , the program can not run !!! but joinable, they could run correctly !!I do not know why! In theory, they both could function right.</p>
    singulars
    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.
    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