Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sched.h>
- #include <signal.h>
- #include <stdlib.h>
- /*Asynchronous 3 threads through mutex and condition variable simple count example for sake of clearity it just incriment count and decrement */
- /*some unused variable */
- #define HIGHYPRIORTY 10
- static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER ;
- static pthread_cond_t cond = PTHREAD_COND_INITIALIZER ;
- static int count = 0 ;
- int done = 0 ;
- void *incremant(void *arg)
- {
- //int error = 0 ;
- pthread_mutex_lock(&lock);
- count+=100;
- pthread_mutex_unlock(&lock) ;
- ++done ;
- pthread_cond_signal(&cond) ;
- printf("\n00001\n");
- }
- void *decremeant(void *arg )
- {
- pthread_mutex_lock(&lock) ;
- count-- ;
- pthread_mutex_unlock(&lock);
- ++done ;
- pthread_cond_signal(&cond) ;
- printf("\n00002\n");
- }
- void *getcount(void *arg)
- {
- int error ;
- if((error = pthread_mutex_lock(&lock)))
- pthread_exit(NULL);
- while(done!=2)
- pthread_cond_wait(&cond ,&lock) ;
- printf("\n00003\n");
- pthread_mutex_unlock(&lock) ;
- return (void*)count;
- }
- void *(*routines[])(void*)={incremant,decremeant,getcount};
- pthread_attr_t *make_high_priority_attr(int priority)
- {
- pthread_attr_t *attr ;
- int error ;
- struct sched_param parm ;
- if((attr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t)))== NULL)
- {
- return NULL ;
- }
- if(!(error = pthread_attr_init(attr))&&!(error = pthread_attr_getschedparam(attr , &parm)))
- {
- parm.sched_priority = priority ;
- error = pthread_attr_setschedparam( attr , &parm ) ;
- }
- if(error)
- {
- free(attr) ;
- errno = error ;
- return NULL ;
- }
- return attr ;
- }
- int main()
- {
- pthread_attr_t *attr ;
- pthread_t thread[3] ;
- sigset_t mask ;
- sigset_t oldmask ;
- void *statuscount = NULL ;
- if((sigfillset(&mask)) == -1 )
- {
- perror("Warrning :unable to fill mask set") ;
- }
- if((sigprocmask(SIG_SETMASK, &mask, &oldmask))==-1)
- {
- perror("Unable to block all signals");
- return(-1) ;
- }
- if((attr=make_high_priority_attr(HIGHYPRIORTY))==NULL)
- {
- perror(strerror(errno)) ;
- return(-1) ;
- }
- if(pthread_attr_setscope(attr , PTHREAD_SCOPE_SYSTEM))
- {
- perror("Warrning: Unable Get system scope");
- }
- if(pthread_attr_setdetachstate(attr , PTHREAD_CREATE_JOINABLE))
- {
- perror("Unable to set detach state");
- return -1 ;
- }
- for(int i = 0; i < 3 ; i++)
- {
- if(pthread_create(&thread[i], attr ,(void *(*)(void*))*(routines+i),NULL))
- {
- fprintf(stderr,"Unable to creat thread%d",i) ;
- free(attr) ;
- return(-1) ;
- }
- printf("%d",i);
- }
- if((sigprocmask(SIG_SETMASK,&oldmask,NULL)) == -1)
- {
- perror("Warrning:Unable to reset Signal state") ;
- }
- for(int i = 0; i < 3 ;i++)
- {
- if(pthread_join(thread[i],&statuscount))
- {
- fprintf(stderr ,"unable to join thread%d",i) ;
- free(attr) ;
- return(-1) ;
- }
- }
- printf("\n\nCount=%d",((int)statuscount)) ;
- free(attr) ;
- return 0 ;
- }
Add Comment
Please, Sign In to add comment