semaphore functions

This is a discussion on semaphore functions within the Linux Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #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

  2. #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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    Thumbs up

    thanx sl4nted.......

  4. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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