C代写:CS3270 Critical Section

练习Unix中的Semaphore用法,对Critical Section实行保护,避免发生进程间的并发错误。

Purpose

In this assignment, you will write a complete C program that exploits mutual exclusion with threads.
Time: Your copy of the program and other required files must be zipped and submitted on Canvas by 11:00AM on the due date.

Assignment

This assignment will help you better-understand the use of semaphore for critical section. Your task is to write a C program, on BGUnix (under our class directory, cs3270a), which provides mutual exclusion for the critical section. This is be an individual assignment.

Details

Your task is to explore how to create and use multiple threads using pthread and provide mutual exclusion using a binary semaphore.

  • Create a work directory, named Prog5, under the class directory.
  • Copy sem-tmp.c file from the lib directory (i.e., under the class directory) to your work directory.
    • You can use cp command to make a copy of a file into a certain directory.
    • You can use pwd command to check the path to the current directory.
  • Rename sem-tmp.c file to sem-ex.c file
    • You can use mv command to rename a file name.
  • Revise the code to provide mutual exclusion for the critical section using proper use of a binary semaphore.
    • Critical section is given in the handler( ) function.
    • POSIX threads (pthreads) are used in the code.
    • Explore how pthread_create( ), pthread_join( ) and other pthread functions are used.
    • Semaphore functions: sem_init( ), sem_wait( ), sem_post( ), sem_destroy( )
    • You are allowed to use references to see how the pthread and semaphore functions work (e.g., use the manual on Unix: for instance, manpthread_create)
    • You need to explain how the semaphore functions and the pthread functions work in a MS Word document. You should not be just copying from the manual.
  • Document your program properly. Program description, program structure/architecture, key functions/methods, key variables should be explained. (Note that the program already have many of the documentation written. However, you will need to add more.) How to run the program and any other feature about your program should also be described.

Softcopy submission

  • Your softcopy must be in Prog5 directory under our class directory (cs3270a) on BGUnix by 11:00AM on the due date.
  • You will need to submit your program source files and a MS Word file which contains (1) program source code, (2) the screen capture of the outputs with 3 thread and 5 thread, (3) explanation of pthread and semaphore functions, as ONE zipped (NOT gzipped) file by 11:00AM on the due date.
  • Please note that submissions with wrong formats/files/names will be discarded.

Grade Distribution

  • Not submitted with the required formats/files/names 0 point (no grading)
  • Program description and documentation including the compiling and running instruction: 3 points
  • Correct use of semaphore functions for the critical section: 5 points
  • Good explanation of the semaphore functions: 2 points
  • Good explanation of the pthread functions: 2 points
  • Reasonable screen captured output: 3 points