{"id":3187,"date":"2023-02-20T04:54:31","date_gmt":"2023-02-20T04:54:31","guid":{"rendered":"https:\/\/www.goodacademic.com\/blog\/questions\/mplement-a-solution-to-the-critical-section-problem-for-2-threads-using-mutex-locks\/"},"modified":"2023-02-20T04:54:31","modified_gmt":"2023-02-20T04:54:31","slug":"mplement-a-solution-to-the-critical-section-problem-for-2-threads-using-mutex-locks","status":"publish","type":"questions","link":"https:\/\/www.goodacademic.com\/blog\/questions\/mplement-a-solution-to-the-critical-section-problem-for-2-threads-using-mutex-locks\/","title":{"rendered":"mplement a solution to the critical section problem for 2 threads using mutex locks"},"content":{"rendered":"<div class=\"col-sm-12 messageContent\">\n<p>in C programming<\/p>\n<p>Implement a solution to the critical section problem for 2 threads using mutex locks. Specifically, in <strong>pthreads<\/strong> using <strong>pthread_<\/strong><strong>mutex_<\/strong><strong>trylock <\/strong>and<strong> pthread_mutex_unlock<\/strong>.<\/p>\n<p>both two threads need to <strong>concurrently<\/strong> increment a <strong>shared variable<\/strong> called <em><strong>counter<\/strong> <\/em>by one <strong>2,000,000<\/strong> times starting from <strong>zero<\/strong>. all global memory is shared among threads of a process. Both threads need to count to 2,000,000 which means the <strong>only<\/strong> <strong>correct<\/strong> overall count is <strong>4,000,000<\/strong>.<\/p>\n<p>the shared variable count should be defined as follows:<\/p>\n<p><em><strong>struct shared_data<\/strong><\/em><br \/><em><strong> {<\/strong><\/em><br \/><em><strong> int value; \/* shared variable to store result*\/<\/strong><\/em><br \/><em><strong> };<\/strong><\/em><\/p>\n<p><em><strong>struct shared_data *counter;<\/strong><\/em><\/p>\n<p>remember to allocate memory to your shared data as follows:<\/p>\n<p><em><strong>counter = (struct shared_data *) malloc(sizeof(struct shared_data));<\/strong><\/em><\/p>\n<p><strong>Thread1<\/strong> needs to be designed in a way that every time it sees (<strong>counter\u00e2\u02c6\u2019&gt; value<\/strong><strong>%100) == 0<\/strong> it increments counter\u00e2\u02c6\u2019&gt; value by <strong>100<\/strong> (<strong>bonus<\/strong>). That counts for <strong>100 individual updates<\/strong>. You also need to keep track of how many times the thread got the bonus and <strong>report<\/strong> it in its <strong>remainder<\/strong> section.<\/p>\n<p><strong>Thread2<\/strong> does not get any bonus, it only increments <em>counter-&gt;value<\/em> by <strong>1<\/strong>.<\/p>\n<p>you should <strong>prevent<\/strong> each thread from counting to more than 2,000,000.<\/p>\n<p>remember the parent process needs to <strong>wait<\/strong> for its 2 threads to join.<strong><\/strong><\/p>\n<p>in the main body of your code add the following lines :<\/p>\n<p>\/* Required to schedule thread independently. Otherwise, use NULL in place of attr. *\/<br \/><strong> pthread_attr_init(&amp;attr[0]);<\/strong><br \/><strong> pthread_attr_setscope(&amp;attr[0], PTHREAD_SCOPE_SYSTEM); <\/strong>\/* system-wide contention *\/ <br \/> \/* end to schedule thread independently *\/<\/p>\n<p>remember to initialize the mutex using <strong>pthread_mutex_init .<\/strong><\/p>\n<p>Make comments indicating the following sections in your code:<\/p>\n<ul>\n<li><strong>entry section<\/strong><\/li>\n<li><strong>critical section<\/strong><\/li>\n<li><strong>exit section<\/strong><\/li>\n<li><strong>remainder section<\/strong><strong><\/strong><\/li>\n<\/ul>\n<p><strong>Expected output:<\/strong><\/p>\n<p>both threads need to report the <strong>number of updates<\/strong> done at the end of their <strong>remainder<\/strong> sections.<\/p>\n<p>both threads need to report the <strong>current value <\/strong>of the shared variable<strong> counter<\/strong> at the end of their <strong>remainder<\/strong> sections.<\/p>\n<p>thread1 needs to report the number of times it got the bonus at the end of its <strong>remainder<\/strong> sections.<\/p>\n<p><strong><em>Im thread1, I did 2000000 updates and I got the bonus for 16089 times, counter = 2554849<\/em><\/strong><br \/><strong><em>Im thread2, I did 2000000 updates, counter = 4000000<\/em><\/strong><br \/><strong><em>from parent counter = 4000000<\/em><\/strong><\/p>\n<p>Use the POSIX implementation of threads. You will need to look at the <strong>pthread_<\/strong><strong>create<\/strong>, <strong>pthread_<\/strong><strong>join<\/strong> and threads manual pages. A tutorial and the man pages are here:<\/p>\n<p><a href=\"http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialPosixThreads.html\" target=\"_blank\" class=\"external\" rel=\"noreferrer noopener\">http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialPosixThreads.html<span class=\"external_link_icon\"><br \/>\n    <svg viewbox=\"\">\n     <path><\/path>\n    <\/svg><span class=\"screenreader-only\">Links to an external site.<\/span><\/span><\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>in C programming Implement a solution to the critical section problem for 2 threads using mutex locks. Specifically, in pthreads using pthread_mutex_trylock and pthread_mutex_unlock. both two threads need to concurrently increment a shared variable called counter by one 2,000,000 times starting from zero. all global memory is shared among threads of a process. Both threads [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":[],"disciplines":[733],"paper_types":[],"tagged":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions\/3187"}],"collection":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions"}],"about":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/types\/questions"}],"author":[{"embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/comments?post=3187"}],"version-history":[{"count":0,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/questions\/3187\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/media?parent=3187"}],"wp:term":[{"taxonomy":"disciplines","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/disciplines?post=3187"},{"taxonomy":"paper_types","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/paper_types?post=3187"},{"taxonomy":"tagged","embeddable":true,"href":"https:\/\/www.goodacademic.com\/blog\/wp-json\/wp\/v2\/tagged?post=3187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}