According to your code snippet, all threads would execute one by one
serially i.e. first thread would be created, would execute "text" routine
and terminate; then second thread would be created, would execute "text"
routine and terminate and so on. Hence, multiple threads are not running in
parallel.
I guess, in order to run multiple threads in parallel, the code snippet
should call pthread_join in separate loop and use the semaphore as follows:
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 };
sem_t mySem;
int main()
{
int i;
pthread_t tid[7];
sem_init(&mySem, 0, 1);
for (i = 0; i < 7; i++)
{
pthread_create(&tid[i], NULL, text, (void*)&code[i]);
}
for (i = 0; i < 7; i++)
{
pthread_join(tid[i],NULL);
}
sem_destroy(&mySem);
return 0;
}
void *text(void *arg)
{
int n = *(int*)arg;
sem_wait(&mySem);
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;
}
sem_post(&mySem);
pthread_exit(0);
}
HTH
On Sun, Nov 25, 2012 at 10:47 PM, vIGNESH v <[email protected]> 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 <[email protected]> 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().
>>
>> --
>>
>>
>>
>
> --
>
>
>
--