C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-14-2006, 10:35 PM   #1
Registered User
 
Join Date: Nov 2006
Posts: 10
Post semaphore functions

this is my c code regarding threads ....
Code:
#include<stdio.h>
#include<pthread.h>
//#include<semaphores.h>


void *function1()
{
        printf("thread numbers(1-10): %ld\n",pthread_self());
}

void *function2()
{
        printf("thread number 11 : %ld\n",pthread_self());
}

int main()
{
        int i;
        pthread_t thread[10],thread11;
        for(i=0;i<10;i++)
        {
           pthread_create(&thread[i],NULL,&function1,NULL);
        }
        pthread_create(&thread11,NULL,&function2,NULL);
}
thread 11 should execute after all the 10 threads have completed their process

i want to use semaphore fuctions instead thread_join ...... can anyone help

in implementing this.....


thanks in advance..

pthread
pthread is offline   Reply With Quote
Old 11-15-2006, 01:31 AM   #2
Registered User
 
Join Date: Nov 2006
Posts: 176
Code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

union semun {
            int val;
            struct semid_ds *buf;
            unsigned short  *array;
};

int semset_id;          /*set id*/
struct sembuf sem_op;   /*struct for operations*/

void *function1(void *arg)
{
        printf("thread numbers(1-10)\n");

        /* increase semaphore counter */
        sem_op.sem_num = 0;
        sem_op.sem_op = 1;
        sem_op.sem_flg = 0;
        semop(semset_id, &sem_op, 1);

        pthread_exit(NULL);
}

void *function2(void *arg)
{

        /* wait till semaphore value is 10 */
        sem_op.sem_num = 0;
        sem_op.sem_op = -10;
        sem_op.sem_flg = 0;
        semop(semset_id, &sem_op, 1);

        printf("thread number 11\n");

        pthread_exit(NULL);
}

int main()
{
        int i;
        pthread_t thread[10],thread11;
        union semun sem_val;            /* sem value */
        void *ret_val;

        semset_id = semget(IPC_PRIVATE, 1, 0600);       /* create set */
        if (semset_id == -1)
        {
                perror("Crap");
                exit(1);
        }

        sem_val.val = 0;
        semctl(semset_id, 0, SETVAL, sem_val);

        if (pthread_create(&thread11,NULL,function2,NULL) != 0)
        {
                perror("Create 11");
                exit(1);
        }

        for(i=0;i<10;i++)
        {
                if (pthread_create(&thread[i],NULL,function1,NULL) != 0)
                {
                        perror("Create");
                        exit(1);
                }

        }

        for (i = 0; i < 10; i++)
                pthread_join(thread[i], &ret_val);

        pthread_join(thread11, &ret_val);

        return 1;
}
theres probably a better way to do this, I haven't used semaphores for awhile (maybe without the globals), but this does work
so should get you on the way

output:
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread numbers(1-10)
thread number 11

whether the 11th is created before the other 10 or after
sl4nted is offline   Reply With Quote
Old 11-15-2006, 02:27 AM   #3
Registered User
 
Join Date: Nov 2006
Posts: 10
Thumbs up

thanx sl4nted.......
pthread is offline   Reply With Quote
Old 11-15-2006, 02:55 AM   #4
Registered User
 
Join Date: Nov 2006
Posts: 10
is there any other solution for that code ..... is it possible by using sem_wait ,sem_post,sem_init functions .....


thanx

pthread
pthread is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Void Functions Help bethanne41 C++ Programming 1 05-09-2005 05:30 PM
Functions and Classes - What did I do wrong? redmage C++ Programming 5 04-11-2005 11:50 AM
calling functions within functions edd1986 C Programming 3 03-29-2005 03:35 AM
Factory Functions HOWTO GuardianDevil Windows Programming 1 05-01-2004 01:41 PM
Shell functions on Win XP geek@02 Windows Programming 6 04-19-2004 05:39 AM


All times are GMT -6. The time now is 01:07 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22