{"id":250,"date":"2009-02-17T19:07:55","date_gmt":"2009-02-17T13:37:55","guid":{"rendered":"http:\/\/brainstorms.in\/?p=250"},"modified":"2009-06-24T22:20:22","modified_gmt":"2009-06-24T16:50:22","slug":"learning-pthreads","status":"publish","type":"post","link":"https:\/\/brainstorms.in\/?p=250","title":{"rendered":"Learning  Pthreads"},"content":{"rendered":"<p>I am beginning to enjoy the Prepare Future program on Linux internal and\u00a0\u00a0 driver development. Today&#8217;s class was about pthreads. I had some prior experience on pthreads library while at CUSAT.\u00a0\u00a0 It was refreshed today.<\/p>\n<p>Pthreads is a user space library for\u00a0 manipulating threads.<\/p>\n<p>Here is what we did today.\u00a0 First we wrote a small program which created a thread.<\/p>\n<p>It is here.<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>void * thread_function ( void *arg)<br \/>\n{<br \/>\nprintf(&#8220;In Thread\\n&#8221;);<br \/>\npthread_exit(&#8220;Success&#8221;);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\npthread_t tid;<br \/>\nvoid * ret_status;<br \/>\npthread_create( &amp;tid,NULL, &amp;thread_function,NULL);<\/p>\n<p>printf(&#8220;In Main\\n&#8221;);<\/p>\n<p>pthread_join(tid ,&amp;ret_status);<\/p>\n<p>return (0);<\/p>\n<p>}<\/p>\n<p>For compiling this I used.<\/p>\n<p>$gcc -D_REENTRANT -o pt1 pt1.c -l pthread<\/p>\n<p>Then\u00a0 i tried out \u00a0 semaphore and mutex in pthread library.<\/p>\n<p>The important semaphore operations are<\/p>\n<p>sem_init\u00a0 &#8211;\u00a0\u00a0 int sem_init(sem_t *sem, int pshared, unsigned int value);<\/p>\n<p>sem_ wait\u00a0 \u00a0 int sem_wait(sem_t *sem);<\/p>\n<p>sem_post\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 int sem_post(sem_t *sem);<\/p>\n<p>sem_destroy\u00a0\u00a0 int sem_destroy(sem_t *sem);<\/p>\n<p>I was forced to RTFM the manual. \ud83d\ude00<\/p>\n<p>I wrote this then.<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;semaphore.h&gt;<\/p>\n<p>int y=0;<br \/>\nsem_t sigsem;<\/p>\n<p>void * thread1 ( void *arg)<br \/>\n{<br \/>\nint x;<br \/>\nsem_wait(&amp;sigsem);<\/p>\n<p>x=y+3;<\/p>\n<p>printf(&#8220;In Thread1\\n&#8221;);<br \/>\nprintf(&#8221; x is %d\\n&#8221;,x);<\/p>\n<p>pthread_exit(&#8220;Success thread1 &#8220;);<br \/>\n}<\/p>\n<p>void * thread2 ( void *arg)<br \/>\n{<br \/>\ny=45;<br \/>\nsem_post(&amp;sigsem);<br \/>\nprintf(&#8220;In Thread2\\n&#8221;);<br \/>\nprintf(&#8221; y is %d\\n&#8221;,y);<br \/>\npthread_exit(&#8220;Success thread2&#8221;);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\npthread_t tid1,tid2;<br \/>\nvoid * ret_status;<\/p>\n<p>sem_init( &amp;sigsem,0,0);<\/p>\n<p>pthread_create( &amp;tid1,NULL, &amp;thread1,NULL);<br \/>\npthread_create( &amp;tid2,NULL, &amp;thread2,NULL);<br \/>\nprintf(&#8220;In Main\\n&#8221;);<\/p>\n<p>pthread_join(tid1 ,&amp;ret_status);<br \/>\npthread_join(tid2 ,&amp;ret_status);<\/p>\n<p>sem_destroy(&amp;sigsem);<\/p>\n<p>return (0);<\/p>\n<p>}<br \/>\nThis was followed by producer consumer problem as below.<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>\/\/sem_t sigsem;<br \/>\nint buffer[10];<\/p>\n<p>void * producer ( void *arg)<br \/>\n{<br \/>\nint i,index;<br \/>\nindex=0;<\/p>\n<p>for (i =0;i&lt;40 ;++i)<br \/>\n{<\/p>\n<p>printf(&#8220;Producer\u00a0 trying to produce an item&#8230;&#8230;&#8230;.\\n&#8221;);<\/p>\n<p>buffer[index] =i;<\/p>\n<p>printf (&#8220;producer put\u00a0 %d\u00a0 at buffer [%d]\\n&#8221;, i,index);<\/p>\n<p>index++;<br \/>\nif (index==10)<br \/>\nindex=0;<br \/>\n}<\/p>\n<p>pthread_exit(&#8220;Producer ends&#8221;);<br \/>\n}<\/p>\n<p>void * consumer ( void *arg)<br \/>\n{<br \/>\nint i,index=0;<br \/>\nfor (i=0;i&lt;40;++i)<br \/>\n{<br \/>\nprintf(&#8221; Consumer\u00a0 trying to consume\u00a0 an item &#8230;&#8230;&#8230;.\\n&#8221;);<br \/>\nprintf ( &#8221; Consumer ate\u00a0 %d from buffer[%d]\\n&#8221;, buffer [index] ,index);<\/p>\n<p>index++;<br \/>\nif (index==10)<br \/>\nindex=0;<br \/>\n}<\/p>\n<p>pthread_exit(&#8220;Consumer ends&#8221;);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\npthread_t tid1,tid2;<br \/>\nvoid * ret_status;<br \/>\npthread_create( &amp;tid1,NULL, &amp;producer,NULL);<br \/>\npthread_create( &amp;tid2,NULL, &amp;consumer,NULL);<\/p>\n<p>pthread_join(tid1 ,&amp;ret_status);<br \/>\npthread_join(tid2 ,&amp;ret_status);<\/p>\n<p>return (0);<\/p>\n<p>}<br \/>\nThe mutex in it\u00a0 was then rewritten with pthread_mutex functions. Again lot of RTFM.<\/p>\n<p>Then as an assignment we did the readers and writers problem.<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;semaphore.h&gt;<\/p>\n<p>sem_t\u00a0 mutex,rcmutex;<br \/>\nint reader_count=0;<br \/>\nint buffer=10;<\/p>\n<p>void reader_action(int r )<br \/>\n{<\/p>\n<p>printf ( &#8220;\\nReader %d\u00a0 buffer is %d &#8220;,r, buffer);<br \/>\n}<\/p>\n<p>void\u00a0 write_action (int i)<br \/>\n{<br \/>\nint j;<br \/>\nbuffer+=10;<br \/>\nfor (j=0;j&lt;10;++j) ; \/\/Waste some time<\/p>\n<p>printf ( &#8220;\\n Writing for %d time buffer is %d &#8220;,i, buffer);<\/p>\n<p>}<\/p>\n<p>void * reader1 ( void *arg)<br \/>\n{<\/p>\n<p>sem_wait(&amp;rcmutex);<br \/>\nreader_count++;<br \/>\nif(reader_count==1)<br \/>\nsem_wait(&amp;mutex);<br \/>\nsem_post(&amp;rcmutex);<\/p>\n<p>reader_action(1);<\/p>\n<p>sem_wait(&amp;rcmutex);<\/p>\n<p>reader_count&#8211;;<br \/>\nif ( reader_count==0)<br \/>\nsem_post(&amp;mutex);<\/p>\n<p>sem_post(&amp;rcmutex);<\/p>\n<p>pthread_exit(&#8220;Success reader1 &#8220;);<br \/>\n}<\/p>\n<p>void * reader2 ( void *arg)<br \/>\n{<\/p>\n<p>sem_wait(&amp;rcmutex);<br \/>\nreader_count++;<br \/>\nif(reader_count==1)<br \/>\nsem_wait(&amp;mutex);<br \/>\nsem_post(&amp;rcmutex);<\/p>\n<p>reader_action(2);<\/p>\n<p>sem_wait(&amp;rcmutex);<\/p>\n<p>reader_count&#8211;;<br \/>\nif ( reader_count==0)<br \/>\nsem_post(&amp;mutex);<\/p>\n<p>sem_post(&amp;rcmutex);<\/p>\n<p>pthread_exit(&#8220;Success reader2 &#8220;);<br \/>\n}<\/p>\n<p>void * writer ( void *arg)<br \/>\n{<br \/>\nint i;<br \/>\nfor (i=0 ; i&lt;10;++i)<br \/>\n{<\/p>\n<p>sem_wait (&amp;mutex);<br \/>\nwrite_action(i);<br \/>\nsem_post(&amp;mutex);<br \/>\n}<br \/>\npthread_exit(&#8220;Success writer&#8221;);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\npthread_t tid1,tid2,tid3,tid4,tid5,tid6;<br \/>\nvoid * ret_status;<\/p>\n<p>sem_init( &amp;mutex,0,1);<br \/>\nsem_init( &amp;rcmutex,0,1);<\/p>\n<p>pthread_create( &amp;tid1,NULL, &amp;reader1,NULL);<br \/>\npthread_create( &amp;tid2,NULL, &amp;writer,NULL);<br \/>\npthread_create( &amp;tid3,NULL, &amp;reader2,NULL);<br \/>\npthread_create( &amp;tid4,NULL, &amp;reader1,NULL);<br \/>\npthread_create( &amp;tid5,NULL, &amp;reader2,NULL);<br \/>\npthread_create( &amp;tid6,NULL, &amp;reader1,NULL);<\/p>\n<p>pthread_join(tid1 , &amp;ret_status);<br \/>\npthread_join(tid2 ,&amp;ret_status);<br \/>\npthread_join(tid3 ,&amp;ret_status);<br \/>\npthread_join(tid4 , &amp;ret_status);<br \/>\npthread_join(tid5 , &amp;ret_status);<br \/>\npthread_join(tid6 , &amp;ret_status);<\/p>\n<p>sem_destroy(&amp;mutex);<br \/>\nsem_destroy(&amp;rcmutex);<br \/>\nreturn (0);<\/p>\n<p>}<br \/>\nThe course is progressing smoothly. and looks interesting several fellow participants are also stimulating.<\/p>\n<p>I forgot to take the card reader, so I am unable to post photos.\u00a0 I had a walk around the JNTU campus in the morning and took several photos. May be I will beautify these blog enties with\u00a0 photos and proper code snippets ( and build \u00a0 proper pthread tutorial) \u00a0 when I return.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am beginning to enjoy the Prepare Future program on Linux internal and\u00a0\u00a0 driver development. Today&#8217;s class was about pthreads. I had some prior experience on pthreads library while at CUSAT.\u00a0\u00a0 It was refreshed today. Pthreads is a user space library for\u00a0 manipulating threads. Here is what we did today.\u00a0 First we wrote a small&hellip; <a class=\"more-link\" href=\"https:\/\/brainstorms.in\/?p=250\">Continue reading <span class=\"screen-reader-text\">Learning  Pthreads<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[52,53],"_links":{"self":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts\/250"}],"collection":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":3,"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/brainstorms.in\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/530"}],"wp:attachment":[{"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainstorms.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}