I apologize for the format, but I am writing on a PhysTech server for windows on a virtual machine with Linux, so I can only show screenshots.

Brief formulation of the problem: Write a program that transfers a file between two created threads, using only mutex's as a means of synchronization. You cannot use other means of synchronization and busy-loop!

Requirements: ・ The first thread only reads information from the input file ・ The second thread only writes information to the output file

Compiling errors: Errors pop-up when compiling a program

The program itself: enter image description here enter image description here enter image description here

  • fread and fwrite are standard C functions; use other names. - Fat-Zer

1 answer 1

  • In the standard C library, the names fread and fwrite already "busy."

  • You are trying to apply the -> operator to an object of type struct buf , which is complete nonsense. This is what the open source compiler tells you. What is strange, you have an operator in some lines . , and in some of them, all of a sudden, for no reason at all -> . What have you been trying to achieve by this hodgepodge of . and -> ?

    Apparently, the function parameter is designed to be struct buf *ptr , not struct buf ptr .

  • POSIX does not allow direct copying of objects of type pthread_mutex_t

    Pthread_attr_t, pthread_cond_t, pthread_condattr_t, pthread_mutex_t, pthread_mutexattr_t, pthread_rwlock_t and pthread_rwlockattr_t.

    That is, your attempt to pass your struct buf by value and then work with the mutexes stored in the received copy is doomed to failure.

    After correcting the function parameter on the struct buf *ptr this problem will disappear, but it is also present in the main function: you there safely bind objects of the pthread_mutex_t type to each other.

  • The stream function must be of type void * (void *) . You pthread_create functions like void * (struct buf) in pthread_create . Your flow functions should look something like this.

     void *my_fwrite(void *param) { struct buf *ptr = param; // И далее уже работаем через `ptr` ... 
  • Your main function starts threads, and then immediately closes files and terminates execution. And who told you that by this time the streams had already done their work? You close files when they may still be needed by threads. Moreover, the completion of the main thread ( main ) will simply kill all other threads and the process.

    Your main function should wait for the threads to finish working and only then close the files and exit.