c Programming with pthreads

profilelazy808
OSassignment2.pdf

EECS3221 Fall 2020 Assignment 2

POSIX Threads

Due Date: Monday, November 16, 2020, 23:59.

A. Description of the Assignment A1. You are required to read and fully understand the first 4 chapters, that is, pages 1- 129, of the book "Programming with POSIX Threads" by (This book is currently on reserve at Steacie Science Library, Call Number QA 76.76 T55 B88 1997). A2. You are required to download the program "alarm_mutex.c" and the file "errors.h" and "README" from the directory /cs/course/3221E, and then try to compile and execute this program by following the instructions in the "README" file. (This program is explained in pages 52-58 of the book by David R. Butenhof). A3. You are required to make the following changes to the program "alarm_mutex.c" to produce a program named “New_Alarm_Mutex.c”. There are 2 different types of threads in “New_Alarm_Mutex.c”:

(a) A Main Thread; (b) Display Alarm Threads.

A3.1 Four types of “Alarm requests” in “New_Alarm_Mutex.c” Four types of “Alarm requests”: “Start_Alarm”, “Change_Alarm”, “Cancel_Alarm” and “View_Alarms“requests, are recognized and handled by “New_Alarm_Mutex.c”, as follows: (a) “Start_Alarm” requests with the following format: Alarm> Start_Alarm(Alarm_ID): Time Message

- “Start_Alarm” is a keyword. - “Alarm_ID” is a positive integer. - “Time” has the same meaning and syntax as in the program “alarm_mutex.c”. - “Message” has the same meaning and syntax as in the program “alarm_mutex.c”.

For example: Alarm> Start_Alarm(2345): 50 Will meet you at Grandma’s house at 6 pm (b) “Change_Alarm” requests with the following format: Alarm> Change_Alarm(Alarm_ID): Time Message

- “Change_Alarm” is a keyword. - “Alarm_ID” is a positive integer. - “Time” has the same meaning and syntax as in the program “alarm_mutex.c”. - “Message” has the same meaning and syntax as in the program “alarm_mutex.c”.

For example: Alarm> Change_Alarm(2345): 80 Will meet you at Grandma’s house later at 8 pm (c) “Cancel_Alarm” requests with the following format: Alarm> Cancel_Alarm(Alarm_ID): Time Message

- “Cancel_Alarm” is a keyword. - “Alarm_ID” is a positive integer.

For example: Alarm> Cancel_Alarm(2345) (d) “View_Alarms” requests with the following format: Alarm> View_Alarms

- “View_Alarms” is a keyword. For example: Alarm> View_Alarms If the user types in something other than one of the above four types of valid alarm requests, then an error message will be displayed, and the invalid request will be discarded. A3.2. The Main Thread in “New_Alarm_Mutex.c” The main thread in “New_Alarm_Mutex.c” will first determine whether the format of each alarm request is consistent with the formats specified above; otherwise the alarm request will be rejected with an error message. If a Message exceeds 128 characters, it will be truncated to 128 characters. A3.2.1. For each Start_Alarm request received, the main thread will insert the corresponding alarm with the specified Alarm_ID into the alarm list, in which all the alarms are placed in the order of their Alarm_IDs. Then the main thread will print: “Alarm( <alarm_id>) Inserted Into Alarm List at <insert_time>: <time message>”. For each newly inserted alarm in the alarm list, the main thread will do the following:

If either:

(a) the number of existing display alarm threads is 0, or (b) all of the existing display alarm threads have already assigned to each one of themselves a maximum number of 2 alarms (see A3.3.1 below),

Then create a new display alarm thread. Then the main thread will print: “New Display Alarm Thread <thread-id> Created at <create_time>: <time message>”. Note that above, and in each of the messages printed by the various threads below, <thread-id> is the thread identifier>; <insert_time>, <create_time>, <assign_time>, <cancel_time”, <view_time>, <remove_time>, <change_time>, <display_change_time>, <exit_time>, <print_time>, etc., are the actual times at which the corresponding action was performed by each thread; those times are all expressed as the number of seconds from the Unix Epoch Jan 1 1970 00:00; <time message> correspond to “Time” and “Message” as specified in A3.1 (a), (b) above. A3.2.2. For each Change_Alarm request received, the main thread will use the specified Time and Message values in the Change_Alarm request to replace the Time and Message values in the alarm with the specified Alarm_Id in the alarm list. Then the main thread will print: Alarm(<alarm_id>) Changed at <change_time>: <time message>”. A3.2.3. For each Cancel_Alarm request received, the main thread will remove the alarm with the specified Alarm_Id from the alarm list; then the main thread will print: Alarm(<alarm_id>) Canceled at <cancel_time>: <time message>”. A3.2.4. For each View_Alarms request received, the main thread will print out the following: - A list of all the current existing display alarm threads, together with the alarms that each display alarm thread has assigned to itself, in the following format: “View Alarms at <view_time>: <time>: 1. Display Thread <thread-id> Assigned: 1a. Alarm(<alarm_id>): Created at <create_time> Assigned at <assign_time> <time message> 1b. Alarm(<alarm_id>): Created at <create_time> Assigned at <assign_time> <time message>. (if a second alarm is assigned) 2. Display Thread <thread-id> Assigned: 2a. Alarm(<alarm_id>): Created at <create_time> Assigned at <assign_time> <time message> 2b. Alarm(<alarm_id>): Created at <create_time> Assigned at <assign_time> <time message>. (if a second alarm is assigned) …” .

A3.3. The Display Alarm Threads in “New_Alarm_Mutex.c” After each display alarm thread in “New_Alarm_Mutex.c” has been created by the main thread, it will do the following: A3.3.1. For each display alarm thread that has not yet assigned to itself a maximum of 2 alarms, whenever there exists any new alarm in the alarm list that has not been assigned to any display alarm thread, each display alarm thread will attempt to assign that alarm to itself. If a display alarm thread succeeds in assigning an alarm to itself, it will print: “Display Thread <thread-id> Assigned Alarm(<alarm_id>) at <assign_time>: <time message>”. A3.3.2. For each alarm in the alarm list that has been assigned to this display alarm thread, if the expiry time of that alarm has been reached, or if that alarm has been canceled, then the display alarm thread will remove that alarm from the alarm list, and the display alarm thread will stop printing the message in that alarm Then the display alarm thread will print: “Display Alarm Thread <thread-id> Removed Alarm( <alarm_id> at <remove_time>: <time message>”. A3.3.3. For each alarm in the alarm list that has been assigned to this display alarm thread, if the expiry time of that alarm has been reached, or if that alarm has been canceled and the display alarm thread does not have any more alarms assigned to it, then the display alarm thread will first print: “Display Alarm Thread <thread-id> Exiting at <exit_time>”; Then the display alarm thread will exit from the system. A3.3.4. For each alarm that has been assigned to this display alarm thread, the display alarm thread will periodically print, every 5 seconds, the following: “Alarm (<alarm_id>) Printed by Alarm Display Thread <thread-id>at <print_time>: <time message> ”. A3.3.4. For each newly changed alarm in the alarm list, the display alarm thread that assigned to itself that alarm will start periodically printing, every 5 seconds, the new message in that newly changed alarm. When the display alarm thread first starts printing a new changed message it will print: “Display Thread <thread-id> Starts to Print Changed Message at <display_change_time>: <time message>”. B. Platform on Which The Programs Are to be Implemented

The programs should to be implemented using the ANSI C programming language and using the Linux system at York. You should use POSIX system calls or POSIX functions whenever possible. C. Additional Requirements (a) You must make sure that your code has very detailed comments. (b) You must make sure that your code compiles correctly with your Make file. (c) You must make sure that your code does not generate segmentation faults. (d) You must make sure that your code is able to handle incorrect input. (e) You must describe in detail any problems or difficulties that you had encountered, and how you solved or were able to overcome those problems or difficulties in the report. Failure to satisfy the additional requirements above will result in a very low mark for the assignment. D. What to Hand In Each group is required to hand in both a hard copy and an electronic copy of the following: 1. A written report that identifies and addresses all the important aspects and issues in the design and implementation of the programs for the problem described above. 2. The C source programs. 3. A “Test_output” file containing the output of any testing your group has done. 4. A “makefile” file to make it easier for the marker to compile and run your group’s program. 5. A “README” file explaining how to compile and run your group’s program. Each group is required to use the utility "submit" to submit the electronic version of the above 5 files plus the “errors.h” file to the course directory /cs/course/3221M/submit/a2 (The file “errors.h” should be included among the files submitted so that the marker can test whether your group’s programs can compile and run correctly or not.)

E. Evaluation of the Assignment 1. The report part of your group’s assignment (50%) will be evaluated according to: (a) Whether all important design and implementation aspects and issues of your group’s programs related to the problem above have been identified and appropriately addressed. (b) How well you have justified your design decisions. (c) The quality of your design. (d) How well you have designed and explained the testing. (e) The clarity, and readability of the report. 2. The program and testing part of your assignment (50%) will be evaluated according to: (a) The quality of the design and implementation of your programs. (b) The quality of the testing of your programs. (c) Whether your programs satisfy the Additional Requirements in section C above. F. Notes Please note that the requirements specified in section A. Description of the Assignment above, are the minimum requirements that must be satisfied by your program. Obviously, there are many other possible details of the alarm system that have been left unspecified. It is your responsibility to make appropriate design and implementation choices concerning the unspecified details of the alarm system, and justify those decisions in your report.