Question
Please help Below is the code used // Please change the following comment part into you name // #0#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
Please help
Below is the code used
// Please change the following comment part into you name
// #0#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// Chengfei
// Wang
// #0#END# DO NOT MODIFIE THIS COMMENT LINE!
#include
#include
// This file used for course 3500 operating system class project 3 assignment
// Autor: Chengfei Wang, All rights reserved.
// This macro defines how many units of data will be generated in total,
// For simplicity, we use an integer to represent one unit of data,
// therefore, this project actuall will generate at most 10 integers.
#define MAX 10
// We need the mutex to make sure that every time only one thread access the buffer
// , could be the consumer thread or the producer thread.
pthread_mutex_t the_mutex;
// A blocked thread waits for the "traffic lights" before it make an action.
// The condition varibles are the "traffic lights" to tell the theads
// whether the job is done and resource, the buffer in this case, available
// or not. The following two condition varibles "condc" and "condp" are
// the traffic lights for consumer thread and producer thread.
pthread_cond_t condc, condp;
// To simplify the simulation, we use the integer varible as a buffer
// The buffer stores one uint of data, that is, a integer.
int buffer = 0;
void *producer(void *ptr)
{
int i;
for(i=1; i
{
// Step 1, use the function to lock the mutex, therefore, only the
// producer thread use the buffer after that. You do not want to enter
// the "dressing room" when someone is using it. If the mutex has already
// been locked, that means someone is using the "dressing room", the thread will
// wait. Also, when you start to use the room, you need to lock the door so
// other people knows someone is in the room.
pthread_mutex_lock(&the_mutex);
// Step 2, if the buffer is NOT empty, that means buffer hold some data
// the thread need to wait for traffic light "condp" before output to the buffer,
while(buffer !=0) pthread_cond_wait(&condp, &the_mutex);
// Step 3, in this simulation, the producer generate one integer,
// which is one unit of data assigned to the buffer.
buffer = i*7;
// Step 4, print out message tell what item the producer genetate.
printf("producer produce item %d ", buffer);
// Step 5, since the buffer is only one unit, it is full now.
// We need to stop, and notify the other thread, the consumer
// thread that you are ready to go. So we use the following funtion
// to change the traffic light for consumer thread to "green".
pthread_cond_signal(&condc);
// Step 6, after we give green light to the other thread,
// we now unlock the mutex so the "dressing room" is available.
pthread_mutex_unlock(&the_mutex);
}
// Step 7, exit the thread, return with 0, which means "success"
pthread_exit(0);
}
void *consumer(void *ptr)
{
int i;
int get_data;
for(i=1; i
{
// Lock the_mutex before you start, see step 1 example in producer
// #1#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #1#END# DO NOT MODIFIE THIS COMMENT LINE!
// When the buffer IS empty, nothing to consume, so we have to wait,
// see step 2 example in producer
// #2#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #2#END# DO NOT MODIFIE THIS COMMENT LINE!
// Assign the buffer data to the varible get_data
// #3#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #3#END# DO NOT MODIFIE THIS COMMENT LINE!
// Prints out the content of what you get in get_data
printf("consumer consume item %d ", get_data);
// Clear the buffer by assign value 0 to the buffer
// #4#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #4#END# DO NOT MODIFIE THIS COMMENT LINE!
// Notify the condition varible of producer that good to go
// see step 5 example in producer
// #5#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #5#END# DO NOT MODIFIE THIS COMMENT LINE!
// Unlock the mutex, see step 6 example in producer
// #6#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #6#END# DO NOT MODIFIE THIS COMMENT LINE!
}
// exit the thread, reurn with 0, which means "success", see step 7 in producer
// #7#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
// #7#END# DO NOT MODIFIE THIS COMMENT LINE!
}
int main(int argc, char *argv[])
{
// change your the following id into your banner id
// #8#BEGIN# DO NOT MODIFIE THIS COMMENT LINE!
int banner_id = 903900281;
// #8#END# DO NOT MODIFIE THIS COMMENT LINE!
printf("Banner id: %d ", banner_id);
pthread_t pro, con;
pthread_mutex_init(&the_mutex, 0); // create the mutex used by thread
pthread_cond_init(&condc,0); // intialize the condition varible for consumer
pthread_cond_init(&condp,0); // initalize the condition varible for producer
pthread_create(&con, 0, consumer, 0); // create thread map to function consumer
pthread_create(&pro, 0, producer, 0); // create thread map to function producer
pthread_join(pro,0); // put the producer thread into the system
pthread_join(con,0); // put the consummer thread into the system
// clean up, destroy the mutex and two threads
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
pthread_mutex_destroy(&the_mutex);
return 0;
}
COMP 3500 Introduction to Operating Systems Project 3 - Producer / Consumer Model Points Possible: 100 due: 11:59 pm Mar 1h, 2019 There should be no collaboration among students. A student shouldn't share any project code with any other student. Collaborations among students in any form will be treated as a serious violation of the University's academic integrity code 1. Complete the source code to simulate producer/consumer problem 2. Understand the basics of the POSIX thread library 3. Build a binary program on a Linux machine 4. Run the program and record the result of the simulation ui Each student should independently accomplish this project assignment. You may discuss with other students to solve the coding problems To embark on this project, you may choose one of the following four options Important! Option 1: For Mac and Linux users, use SSH to connect to a remote Linux server. Please read files in "tutorial" on Canvas for details Important! Option 2: For Window 10 users, Please read "enviro_tutorial", "Putty Tutorial" and "Win subsystem Linux" for details o o Important! Read the I/O format specification carefully and follow. It is very important to your final grade of this project! . You are highly recommended to use a Linux operating system 1. Introduction to producer and consumer model Producer and consumer model is a model to schedule how concurrent processes and thread:s access the resources. It contains 1. Producer: one or multiple processes/threads that produce data or release hardware resource Consumer: the one process/threads that take in data or use hardware resource to do the computation 2. A producer could also be relatively a consumer to the output of another producer, vice versa 3. Buffer: the destination to store the output from producer or resources and later accessed by another consumer Other concepts involved in our project 4. POSIX thread: threads mechanism that satisfies POSIX standard (most operating system) 5. Mutex: a "lock" that guarantee that only one person has access In this project, we use POSIX threads. The "pthread" is a POSIX thread library written in C and provides the basic functions. To avoid conflicts, we use C only in this project. To simplify our simulation, we assume there are only 2 POSIX threads. One is the consumer, the other is the producer. The producer generates 1 unit data each time to the buffer, and the consumer takes 1 unit data from the buffer each time. The size of the buffer is 1. One unit data is just one integer. The producer generates integers 7, 14, 21.. into the buffer and consumer read them out from the buffer 2. Follow the Format Specification (10 Points) In the source file "Firstname_Lastname.cpp", you will find first four comment lines // #0#BEGIN# DO NOT MODIFY THIS COMMENT LINE! / Firstname // Lastname // ##END# DO NOT MODIFY THIS COMMENT LINE! Your first task is modifying the two lines between the beginning line and end line. Change them into your first name and last name. Remember the strings are case-sensitive, so capitalize the first letter of the word and follow exactly the syntax of the example You can see lots of similar code blocks in the file. You are free and supposed to fill your answer between those special beginning and ending comment lines. You can insert and edit multiple lines between special comment lines in anyways, however(Important!), as the comment indicated, do not modify the special begin and comment lines themselves! Let's do the second task. Scroll down to the bottom of the file and find those lines (press "shift + g" if you are using vi/vim) // #8#BEGIN# DO NOT MODIFY THIS COMMENT LINE! banner id903900281; // #8#END# D0 NOT MODIFY THIS COMMENT LINE! Look at your student ID card, check your banner ID. Again, change the "banner_id" value to your own ID. Your unique student id will be compiled into the program, and the input of the experiment also uniquely depends on your ID Warning: Since every student has a unique id number, the later compiled binary file is also unique. Copy binary file from other students will be easily detected! 3. Complete Source Code (70 Points) Read the source code and rest comments, try to understand the function of each line of code Try to understand the basic usage of pthread library function from the example code of producer and the main function Follow the instructions in the comments and insert proper code into the rest 7 blocks to implement a producer/consumer model. (Only C file acceptable in this project) 4. Run and Record Simulation Result (10 Points) Compile your source code into a binary program. For example, use following command to include the pthread library: $ gcc Firstname_Lastname.c -o Firstname_Lastname -1pthread After you compile the c source code successfully, please use the script command to record the running result of the program Firstname Lastname $ script Firstname Lastname.script Script started, file is Firstname_Lastname.script ./Firstname Lastname After you run the program, you will have the following results Banner id: 939281 producer produce item 7 consumer consume 1tem producer produce item 14 consumer consume item 14 producer produce item 21 consumer consume item 21 producer produce item 28 consumer consume item 28 producer produce item 35 consumer consume item 35 producer produce item 42 consumer consume item 42 producer produce item 49 consumer consume item 49 producer produce item 56 consumer consume item 56 producer produce item 63 consumer consume item 63 producer produce item 70 consumer consume item 70 You should have the same result except for the difference in the banner ID. Then exit recording and save it into typescript file "Firstname_Lastname.script" by the following command: $ exit exit Script done, file is Firstname_Lastname.script Warning: Since every student has a unique id number, the result of the simulation is also unique. Copy simulation results from other students will be easily detected! 5. Deliverables (10 Points) Since you have generated multiple script files, please save all the script files in one directory. Create a folder "Firstname_Lastname" first. Please make sure the folder name correct. You can use the command mv to rename the folder: $ mv folder-name Firstname_Lastname Make sure all the three files are into this folder. You should have those following files inside the folder: 1. commands recording file: Firstname_Lastname.script 2. executable binary file: Firstname_Lastname 3. source code file: Firstname_Lastname.c Achieve all the folder into a single tarred and compressed file with a tar command. tar -zevf Firstname_Lastname.tar.gz Firstname_Lastname You need to submit one tarred file with a format: Firstname_Lastname.tar.gz Grading Criteria: 1. Follow the format specification: 10% Do not break the special comments. a. b. Input your name properly (mark #0). c, input your banner id properly (mark #8). 2. Complete the source code: 70% Each blank worth 10, total 70 (mark #1 ~ #7). b. a. See detailed specification for each blank in the source code file. 3. Compiling and running result: 10% a. Compile the code successfully. b. Record the running results. 4, Deliverables: 10% Contains all the files. Naming all the files properly. a. b. You will be given three business days to read and respond to the comments and grades of your homework or project assignment. The TA may use this opportunity to address any concern and question you have. The TA also may ask for additional information from you regarding your homework or project COMP 3500 Introduction to Operating Systems Project 3 - Producer / Consumer Model Points Possible: 100 due: 11:59 pm Mar 1h, 2019 There should be no collaboration among students. A student shouldn't share any project code with any other student. Collaborations among students in any form will be treated as a serious violation of the University's academic integrity code 1. Complete the source code to simulate producer/consumer problem 2. Understand the basics of the POSIX thread library 3. Build a binary program on a Linux machine 4. Run the program and record the result of the simulation ui Each student should independently accomplish this project assignment. You may discuss with other students to solve the coding problems To embark on this project, you may choose one of the following four options Important! Option 1: For Mac and Linux users, use SSH to connect to a remote Linux server. Please read files in "tutorial" on Canvas for details Important! Option 2: For Window 10 users, Please read "enviro_tutorial", "Putty Tutorial" and "Win subsystem Linux" for details o o Important! Read the I/O format specification carefully and follow. It is very important to your final grade of this project! . You are highly recommended to use a Linux operating system 1. Introduction to producer and consumer model Producer and consumer model is a model to schedule how concurrent processes and thread:s access the resources. It contains 1. Producer: one or multiple processes/threads that produce data or release hardware resource Consumer: the one process/threads that take in data or use hardware resource to do the computation 2. A producer could also be relatively a consumer to the output of another producer, vice versa 3. Buffer: the destination to store the output from producer or resources and later accessed by another consumer Other concepts involved in our project 4. POSIX thread: threads mechanism that satisfies POSIX standard (most operating system) 5. Mutex: a "lock" that guarantee that only one person has access In this project, we use POSIX threads. The "pthread" is a POSIX thread library written in C and provides the basic functions. To avoid conflicts, we use C only in this project. To simplify our simulation, we assume there are only 2 POSIX threads. One is the consumer, the other is the producer. The producer generates 1 unit data each time to the buffer, and the consumer takes 1 unit data from the buffer each time. The size of the buffer is 1. One unit data is just one integer. The producer generates integers 7, 14, 21.. into the buffer and consumer read them out from the buffer 2. Follow the Format Specification (10 Points) In the source file "Firstname_Lastname.cpp", you will find first four comment lines // #0#BEGIN# DO NOT MODIFY THIS COMMENT LINE! / Firstname // Lastname // ##END# DO NOT MODIFY THIS COMMENT LINE! Your first task is modifying the two lines between the beginning line and end line. Change them into your first name and last name. Remember the strings are case-sensitive, so capitalize the first letter of the word and follow exactly the syntax of the example You can see lots of similar code blocks in the file. You are free and supposed to fill your answer between those special beginning and ending comment lines. You can insert and edit multiple lines between special comment lines in anyways, however(Important!), as the comment indicated, do not modify the special begin and comment lines themselves! Let's do the second task. Scroll down to the bottom of the file and find those lines (press "shift + g" if you are using vi/vim) // #8#BEGIN# DO NOT MODIFY THIS COMMENT LINE! banner id903900281; // #8#END# D0 NOT MODIFY THIS COMMENT LINE! Look at your student ID card, check your banner ID. Again, change the "banner_id" value to your own ID. Your unique student id will be compiled into the program, and the input of the experiment also uniquely depends on your ID Warning: Since every student has a unique id number, the later compiled binary file is also unique. Copy binary file from other students will be easily detected! 3. Complete Source Code (70 Points) Read the source code and rest comments, try to understand the function of each line of code Try to understand the basic usage of pthread library function from the example code of producer and the main function Follow the instructions in the comments and insert proper code into the rest 7 blocks to implement a producer/consumer model. (Only C file acceptable in this project) 4. Run and Record Simulation Result (10 Points) Compile your source code into a binary program. For example, use following command to include the pthread library: $ gcc Firstname_Lastname.c -o Firstname_Lastname -1pthread After you compile the c source code successfully, please use the script command to record the running result of the program Firstname Lastname $ script Firstname Lastname.script Script started, file is Firstname_Lastname.script ./Firstname Lastname After you run the program, you will have the following results Banner id: 939281 producer produce item 7 consumer consume 1tem producer produce item 14 consumer consume item 14 producer produce item 21 consumer consume item 21 producer produce item 28 consumer consume item 28 producer produce item 35 consumer consume item 35 producer produce item 42 consumer consume item 42 producer produce item 49 consumer consume item 49 producer produce item 56 consumer consume item 56 producer produce item 63 consumer consume item 63 producer produce item 70 consumer consume item 70 You should have the same result except for the difference in the banner ID. Then exit recording and save it into typescript file "Firstname_Lastname.script" by the following command: $ exit exit Script done, file is Firstname_Lastname.script Warning: Since every student has a unique id number, the result of the simulation is also unique. Copy simulation results from other students will be easily detected! 5. Deliverables (10 Points) Since you have generated multiple script files, please save all the script files in one directory. Create a folder "Firstname_Lastname" first. Please make sure the folder name correct. You can use the command mv to rename the folder: $ mv folder-name Firstname_Lastname Make sure all the three files are into this folder. You should have those following files inside the folder: 1. commands recording file: Firstname_Lastname.script 2. executable binary file: Firstname_Lastname 3. source code file: Firstname_Lastname.c Achieve all the folder into a single tarred and compressed file with a tar command. tar -zevf Firstname_Lastname.tar.gz Firstname_Lastname You need to submit one tarred file with a format: Firstname_Lastname.tar.gz Grading Criteria: 1. Follow the format specification: 10% Do not break the special comments. a. b. Input your name properly (mark #0). c, input your banner id properly (mark #8). 2. Complete the source code: 70% Each blank worth 10, total 70 (mark #1 ~ #7). b. a. See detailed specification for each blank in the source code file. 3. Compiling and running result: 10% a. Compile the code successfully. b. Record the running results. 4, Deliverables: 10% Contains all the files. Naming all the files properly. a. b. You will be given three business days to read and respond to the comments and grades of your homework or project assignment. The TA may use this opportunity to address any concern and question you have. The TA also may ask for additional information from you regarding your homework or projectStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started