尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

LeetCode //C - 1195. Fizz Buzz Multithreaded

LeetCode //C - 1195. Fizz Buzz Multithreaded 1195. Fizz Buzz MultithreadedYou have the four functions:printFizz that prints the word “fizz” to the console,printBuzz that prints the word “buzz” to the console,printFizzBuzz that prints the word “fizzbuzz” to the console, andprintNumber that prints a given integer to the console.You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:Thread A: calls fizz() that should output the word “fizz”.Thread B: calls buzz() that should output the word “buzz”.Thread C: calls fizzbuzz() that should output the word “fizzbuzz”.Thread D: calls number() that should only output the integers.Modify the given class to output the series [1, 2, “fizz”, 4, “buzz”, …] where thei t h i^{th}ithtoken (1-indexed) of the series is:“fizzbuzz” if i is divisible by 3 and 5,“fizz” if i is divisible by 3 and not 5,“buzz” if i is divisible by 5 and not 3, ori if i is not divisible by 3 or 5.Implement the FizzBuzz class:FizzBuzz(int n) Initializes the object with the number n that represents the length of the sequence that should be printed.void fizz(printFizz) Calls printFizz to output “fizz”.void buzz(printBuzz) Calls printBuzz to output “buzz”.void fizzbuzz(printFizzBuzz) Calls printFizzBuzz to output “fizzbuzz”.void number(printNumber) Calls printnumber to output the numbers.Example 1:Input:n 15Output:[1,2,“fizz”,4,“buzz”,“fizz”,7,8,“fizz”,“buzz”,11,“fizz”,13,14,“fizzbuzz”]Example 2:Input:n 5Output:[1,2,“fizz”,4,“buzz”]Constraints:1 n 50From: LeetCodeLink: 1195. Fizz Buzz MultithreadedSolution:Ideas:Maintain a shared counter cur.Use four semaphores, one for each thread.Initially semNumber 1 because sequence starts at 1.After a thread prints its value, it increments cur and signals the semaphore corresponding to the next value.When cur n, wake all waiting threads so they can exit cleanly.Code:#includestdlib.h#includepthread.h#includesemaphore.htypedefstruct{intn;intcur;sem_tsemFizz;sem_tsemBuzz;sem_tsemFizzBuzz;sem_tsemNumber;pthread_mutex_tmutex;}FizzBuzz;FizzBuzz*fizzBuzzCreate(intn){FizzBuzz*obj(FizzBuzz*)malloc(sizeof(FizzBuzz));obj-nn;obj-cur1;sem_init(obj-semFizz,0,0);sem_init(obj-semBuzz,0,0);sem_init(obj-semFizzBuzz,0,0);sem_init(obj-semNumber,0,1);pthread_mutex_init(obj-mutex,NULL);returnobj;}// Dont change the following declarationsvoidprintNumber(inta);voidprintFizz();voidprintBuzz();voidprintFizzBuzz();staticvoidnextTurn(FizzBuzz*obj){if(obj-curobj-n){sem_post(obj-semFizz);sem_post(obj-semBuzz);sem_post(obj-semFizzBuzz);sem_post(obj-semNumber);return;}if(obj-cur%150)sem_post(obj-semFizzBuzz);elseif(obj-cur%30)sem_post(obj-semFizz);elseif(obj-cur%50)sem_post(obj-semBuzz);elsesem_post(obj-semNumber);}// printFizz() outputs fizz.voidfizz(FizzBuzz*obj){while(1){sem_wait(obj-semFizz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printFizz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// printBuzz() outputs buzz.voidbuzz(FizzBuzz*obj){while(1){sem_wait(obj-semBuzz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printBuzz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// printFizzBuzz() outputs fizzbuzz.voidfizzbuzz(FizzBuzz*obj){while(1){sem_wait(obj-semFizzBuzz);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printFizzBuzz();obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}// You may call global function void printNumber(int x)// to output x, where x is an integer.voidnumber(FizzBuzz*obj){while(1){sem_wait(obj-semNumber);pthread_mutex_lock(obj-mutex);if(obj-curobj-n){pthread_mutex_unlock(obj-mutex);break;}printNumber(obj-cur);obj-cur;nextTurn(obj);pthread_mutex_unlock(obj-mutex);}}voidfizzBuzzFree(FizzBuzz*obj){sem_destroy(obj-semFizz);sem_destroy(obj-semBuzz);sem_destroy(obj-semFizzBuzz);sem_destroy(obj-semNumber);pthread_mutex_destroy(obj-mutex);free(obj);}
返回列表