Fork() and the Producer-Consumer Problem
This assignment explores malloc, makefile, modular programming, git, fork() and shell memory.
In class we saw examples of how to write producer-consumer algorithms using fork() and we saw
a critical section implementation using shell memory. For this assignment you will work with
one other person to solve a variation of the producer and consumer problem.
Both of you will submit the same solution algorithm to myCourses.
Do the following:
• Team work
o Divide the work between the two of you.
o Each of you will have their own private git. We will not implement a shared git.
o You will share your code using email once it is good and the other team member
will add your good code to their git. Your git needs to show these transactions for
you to get points for working together.
• Implementation
o Main.c will implement the distribution of work.
▪ It contains only the main() function
▪ It will fork the producer and consumer
o Producer.c will implement the producer algorithm
▪ It contains only the forked producer() function
▪ Producer reads one character at a time from a text file named mydata.txt.
You can populate that file with any information you like.
o Consumer.c will implement the consumer algorithm
▪ It contains only the forked consumer() function
▪ Consumer displays to the screen all the characters it receives from the
producer. The output must be in the same format (it should look the same)
as the file when displayed to the screen.
• Inter-process communication
o Shell variable TURN and DATA will be used to communicate between producer
and consumer. Producer will have TURN number 0 and consumer will have
TURN number 1. They will use the TURN shell memory in order to take turns
accessing the DATA shell variable. Main() initializes the TURN variable to 0
before it launches the clones.
o The DATA shell variable contains the single character producer wants to give to
consumer. Producer assigns a character to DATA then gives the TURN to the
consumer. The consumer extracts the value from DATA and gives the TURN back
to the producer.
o This continues until the producer is done. Then the programs must terminate.
McGill University COMP 206 School of Computer Science
Vybihal Assignment #6 Page 2 of 2
WHAT TO HAND IN
Submit your .c files, makefile, and gitlog.txt files.
The TA will compile and run your program on the Trottier computers.
HOW IT WILL BE GRADED
Points removed for bad practices:
• -1 for not following instructions
• -1 for not indenting, spacing, and/or commenting
• -1 for not using good variable names
This assignment is worth 20 points:
• +4 main()
• +2 Team work proof using git’s log file
• +4 Producer
• +4 Consumer
• +2 GIT usage
• +2 Correct makefile usage
• +2 Correct modular programming