Hi Jagannath,
     pthread_join function call will suspend the execution of the current
thread till the new thread (whose id you provide to this function) has
completed.

So in your example when you say pthread_join(tid[i],NULL), the current
thread (ie the main thread) will remain suspended till thread created by
you in the previous line pthread_create (with thread id tid[i]) completes.

Regards,
Sachin


On Mon, Nov 26, 2012 at 9:46 AM, Jagannath Prasad Das
<jpdasi...@gmail.com>wrote:

> @Naveen: I didn't understand your point . Can you please throw more light
> on join operation
>
> On Sun, Nov 25, 2012 at 10:47 PM, vIGNESH v <v.v.07121...@gmail.com>wrote:
>
>> You can use mutex instead of semaphores if you need to execute only one
>> case at a time.
>>
>> FROM,
>>
>> V.VIGNESH.
>> M.Sc Theoretical Computer Science.
>> PSG College of Technology
>>
>>
>>
>> On 25 November 2012 22:37, jagannath <jpdasi...@gmail.com> wrote:
>>
>>>  Folks, I have one pthread question.I know that its not the right place
>>> but i thought this is the right place to post this. Code snippet:
>>> #include <stdlib.h>
>>> #include <stdio.h>
>>>
>>> #include <unistd.h> /* defines _POSIX_THREADS if pthreads are available
>>> */
>>> #ifdef _POSIX_THREADS
>>> # include <pthread.h>
>>> #endif
>>>
>>> #include <semaphore.h>
>>>
>>> void *text(void *arg);
>>>
>>> int code[] = { 4, 6, 3, 1, 5, 0, 2 };
>>>
>>> int main()
>>> {
>>> int i;
>>> pthread_t tid[7];
>>>
>>> for (i = 0; i < 7; i++)
>>> {
>>> pthread_create(&tid[i], NULL, text, (void*)&code[i]);
>>> pthread_join(tid[i],NULL);
>>> }
>>>
>>> return 0;
>>> }
>>>
>>> void *text(void *arg)
>>> {
>>> int n = *(int*)arg;
>>>
>>> switch (n)
>>> {
>>> case 0:
>>> printf("A semaphore S is an integer-valued variable which can take only
>>> non-negative\n");
>>> printf("values. Exactly two operations are defined on a
>>> semaphore:\n\n");
>>> break;
>>>
>>> case 1:
>>> printf("Signal(S): If there are processes that have been suspended on
>>> this semaphore,\n");
>>> printf(" wake one of them, else S := S+1.\n\n");
>>> break;
>>>
>>> case 2:
>>> printf("Wait(S): If S>0 then S:=S-1, else suspend the execution of this
>>> process.\n");
>>> printf(" The process is said to be suspended on the semaphore S.\n\n");
>>> break;
>>>
>>> case 3:
>>> printf("The semaphore has the following properties:\n\n");
>>> break;
>>>
>>> case 4:
>>> printf("1. Signal(S) and Wait(S) are atomic instructions. In particular,
>>> no\n");
>>> printf(" instructions can be interleaved between the test that S>0 and
>>> the\n");
>>> printf(" decrement of S or the suspension of the calling process.\n\n");
>>> break;
>>>
>>> case 5:
>>> printf("2. A semaphore must be given an non-negative initial
>>> value.\n\n");
>>> break;
>>>
>>> case 6:
>>> printf("3. The Signal(S) operation must waken one of the suspended
>>> processes. The\n");
>>> printf(" definition does not specify which process will be
>>> awakened.\n\n");
>>> break;
>>> }
>>>
>>> pthread_exit(0);
>>> }
>>>
>>> The threads are not synchronized and therefore the text output is
>>> garbled. How to add semaphores of POSIX to this program to synchronize the
>>> threads.?
>>>
>>> primitives to rejig your memory:
>>>
>>> sem_init(), sem_wait(),sem_post(),sem_destroy().
>>>
>>> --
>>>
>>>
>>>
>>
>>  --
>>
>>
>>
>
>  --
>
>
>



-- 
Regards,
Sachin Maheshwari
Cell phone: +91.7259917298

"If we admit that human life can be ruled by reason; the possibility of
life is destroyed." - Alexander Supertramp

-- 


Reply via email to