Question: EECS 3 2 2 1 Assignment A 2 You have to work individually. You are not allowed to view or exchange documents with your peers.

EECS3221 Assignment A2
You have to work individually. You are not allowed to view or exchange documents with
your peers. We treat all sorts of cheating very seriously. Do not copy code from anywhere,
even as a "template''. No late submission will be accepted. All assignment work must be
done on the EECS red server. Your work will be graded based on: i) correctness of
programming logic; ii) clarity of your code and your coding style; iii) whether your code
follows the specifications. It is your responsibility to explain clearly every detail you do in
the code with appropriate comments to avoid any possible confusion in marking.
Your task is to write a C program that creates two groups of pthreads, an IN group and an
OUT group, to create an exact copy of a source file passed as a command-line argument.
The original main thread is not part of either group. The main() function should open the
source file, create/initialize a circular buffer, and create all IN and OUT threads. Then, the
main thread waits for all these threads to finish their work. All IN and OUT threads share
the circular buffer. Each buffer slot stores 2 pieces of information: one data byte read from
the source file and its offset in the source file.
typedef struct {
char data ;
off_t offset ;
} BufferItem ;
Each IN thread goes to sleep (use nanosleep) for some random time between 0 and
0.01 seconds upon being created. Then, it reads the next single byte from the file and
saves that byte and its offset in the file into the next available empty slot in the circular
buffer. Then, this IN thread goes to sleep (use nanosleep) for some random time
between 0 and 0.01 seconds and then goes back to read the next byte, until the end of file.
For better understanding, try inserting a nanosleep between the file reading and the
buffer writing parts of the code and compare the resulting logs.
Similarly, upon being created, each OUT thread sleeps (use nanosleep) for some
random time between 0 and 0.01 seconds and then reads a byte and its offset from the next
available nonempty buffer slot, and then writes the byte to that offset in the target file. Then,
it also goes to sleep (use nanosleep) for some random time between 0 and 0.01
seconds and then goes back to copy the next byte until nothing is left. For better
understanding, try inserting a nanosleep between the buffer reading and the file writing
parts of the code and compare the resulting logs.
Before finishing the main thread can either i) sleep for a few seconds to give time to the
other threads to finish their work (not guaranteed), or ii) use pthread_join and other
methods to make sure that all threads have finished their work (more difficult but may earn
you some extra credit).
Along the way, each thread writes some information to a log file, so that the execution of
the program can be traced.
Since all threads access common data, synchronization will be required. You may wish to
look at the man pages for pthread_create, pthread_mutex_init, sem_init
and other related pthread APIs.
Pay attention to the critical sections of code. Structure your code in such a way that no
thread will spend an extensive period of time in the critical section, i.e. you should make
your critical sections as small as possible. For example, an IN thread should not have one
critical section, where it does all of the following together: (a) read from the file, (b) write
to the log file, (c) write to the buffer; (d) write to the log file. Organize critical sections in a
way that avoids unnecessary blocking but guarantees the proper order of the records in the
log file. For instance, make sure that since a thread reads from a file no other record should
be allowed in the log file until the thread actually writes to the log file. Similarly, writer
threads should have separate critical sections and their reading from the buffer and writing
to the file should be timely recorded in the log file.
The program should be called copy.c and will be compiled with:
cc -Wall -o cpy copy.c -lpthread
It will be invoked as follows:
./cpy
is the number of IN threads to create. There should be at least 1.
is the number of OUT threads to create. There should be at least 1.
is the pathname of the file to be copied. It should exist and be readable.
is the name to be given to the copy. If a file with that name already exists, it
should be overwritten.
is the capacity, in terms of buffer slots, of the shared buffer. This should be

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!