pthread locking and unlocking.


 
Thread Tools Search this Thread
Top Forums Programming pthread locking and unlocking.
# 1  
Old 07-27-2012
pthread locking and unlocking.

When more than one thread is waiting for a locked mutex, which thread will be granted the lock first after it is released.

In our application we want to implement FIFO thread scheduling policy i.e. Thread should get lock which one requested first for it.
AIX have SCHED_FIFO thread scheduling policy but did not worked, i have added code snap shots below with output.

Plz guys help us ...!!


Code:
#include <pthread.h>
#include <stdio.h>
#define NTHREADS 13 

 
pthread_attr_t attr;
pthread_mutex_t mym;
pthread_once_t             OnceInit = PTHREAD_ONCE_INIT;
int once = 1;
void *dowork(void *threadid)
{
	pthread_t ThreadID = thread_self();
	printf("\n ---------GOING TO LOCK ==[%u]",ThreadID);
	if( pthread_mutex_lock (&mym ) != 0)
	{
		printf("\n Error in Locking Mutex");
		exit(1);
	}
	printf("\n ------- GOT IT MAN----[%u]",ThreadID);
	if( once == 1)
	{
		++once;	
		sleep(1);
	}
	else
	sleep(1);

	printf("\n ---------GOING TO UNLOCK ==[%u]",ThreadID);

	if(  pthread_mutex_unlock ( &mym ) != 0)
	{
		printf("\n Error in UnLocking Mutex");
		exit(1);
	}
	printf("\n ------------BYE---------[%u]",ThreadID);
   	pthread_exit(NULL);
}

void init_routine ()
{
	if(  pthread_mutex_init (&mym,NULL ) != 0)
	printf("Error in intialising Mutex "),exit(1);
} 

int main(int argc, char *argv[])
{
	pthread_attr_t tattr;
	int policy;
	
   pthread_t threads[NTHREADS];
   size_t stacksize;
   int rc;
   long *t;
   long i=0;
   t = (long *)malloc(sizeof(long));
 	 pthread_once (&OnceInit,init_routine);

   pthread_attr_init(&attr);
   pthread_attr_getschedpolicy(&attr,&policy);
	printf("\n BEFORE POLICY-------[%d]",policy);
   /*if(pthread_attr_setschedpolicy(&attr,SCHED_FIFO)!= 0)
	perror("Error is:");   */
   pthread_attr_getschedpolicy(&attr,&policy);
	printf("\n AFTER POLICY-------[%d]",policy);

   for(i=0; i<NTHREADS; i++){
      rc = pthread_create(&threads[i], &attr, dowork, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
		 
         exit(-1);
      }
   }
   sleep(10);
   pthread_exit(NULL);
}

---------------------------------------------------------------------------------------------
OUTPUT without thread policy

Code:
BEFORE POLICY-------[0]
 AFTER POLICY-------[0]
 ---------GOING TO LOCK ==[1111421]
 ------- GOT IT MAN----[1111421]
 ---------GOING TO LOCK ==[465893]
 ---------GOING TO LOCK ==[167661]
 ---------GOING TO LOCK ==[1132113]
 ---------GOING TO LOCK ==[951301]
 ---------GOING TO LOCK ==[1554371]
 ---------GOING TO LOCK ==[852815]
 ---------GOING TO LOCK ==[170119]
 ---------GOING TO LOCK ==[98149]
 ---------GOING TO LOCK ==[495807]
 ---------GOING TO LOCK ==[1330225]
 ---------GOING TO LOCK ==[438407]
 ---------GOING TO LOCK ==[731879]
 ---------GOING TO UNLOCK ==[1111421]
 ------------BYE---------[1111421]
 ------- GOT IT MAN----[167661]
 ---------GOING TO UNLOCK ==[167661]
 ------------BYE---------[167661]
 ------- GOT IT MAN----[1132113]
 ---------GOING TO UNLOCK ==[1132113]
 ------------BYE---------[1132113]
 ------- GOT IT MAN----[951301]
 ---------GOING TO UNLOCK ==[951301]
 ------------BYE---------[951301]
 ------- GOT IT MAN----[1554371]
 ---------GOING TO UNLOCK ==[1554371]
 ------------BYE---------[1554371]
 ------- GOT IT MAN----[852815]
 ---------GOING TO UNLOCK ==[852815]
 ------------BYE---------[852815]
 ------- GOT IT MAN----[170119]
 ---------GOING TO UNLOCK ==[170119]
 ------------BYE---------[170119]
 ------- GOT IT MAN----[98149]
 ---------GOING TO UNLOCK ==[98149]
 ------------BYE---------[98149]
 ------- GOT IT MAN----[495807]
 ---------GOING TO UNLOCK ==[495807]
 ------------BYE---------[495807]
 ------- GOT IT MAN----[1330225]
 ---------GOING TO UNLOCK ==[1330225]
 ------------BYE---------[1330225]
 ------- GOT IT MAN----[438407]
 ---------GOING TO UNLOCK ==[438407]
 ------------BYE---------[438407]
 ------- GOT IT MAN----[731879]
 ---------GOING TO UNLOCK ==[731879]
 ------------BYE---------[731879]
 ------- GOT IT MAN----[465893]
 ---------GOING TO UNLOCK ==[465893]
 ------------BYE---------[465893]

-----------------------------------------------------------------------------------
OUTPUT with thread scheduling policy
After setting FIFO policy

Code:
[ BEFORE POLICY-------[0]
 AFTER POLICY-------[1]
 ---------GOING TO LOCK ==[40339]
 ------- GOT IT MAN----[40339]
 ---------GOING TO LOCK ==[721475]
 ---------GOING TO LOCK ==[1174377]
 ---------GOING TO LOCK ==[449527]
 ---------GOING TO LOCK ==[98151]
 ---------GOING TO LOCK ==[495809]
 ---------GOING TO LOCK ==[1330227]
 ---------GOING TO LOCK ==[438409]
 ---------GOING TO LOCK ==[731881]
 ---------GOING TO LOCK ==[161917]
 ---------GOING TO LOCK ==[1111423]
 ---------GOING TO LOCK ==[745475]
 ---------GOING TO LOCK ==[1180995]
 ---------GOING TO UNLOCK ==[40339]
 ------------BYE---------[40339]
 ------- GOT IT MAN----[1174377]
 ---------GOING TO UNLOCK ==[1174377]
 ------------BYE---------[1174377]
 ------- GOT IT MAN----[449527]
 ---------GOING TO UNLOCK ==[449527]
 ------------BYE---------[449527]
 ------- GOT IT MAN----[98151]
 ---------GOING TO UNLOCK ==[98151]
 ------------BYE---------[98151]
 ------- GOT IT MAN----[495809]
 ---------GOING TO UNLOCK ==[495809]
 ------------BYE---------[495809]
 ------- GOT IT MAN----[1330227]
 ---------GOING TO UNLOCK ==[1330227]
 ------------BYE---------[1330227]
 ------- GOT IT MAN----[438409]
 ---------GOING TO UNLOCK ==[438409]
 ------------BYE---------[438409]
 ------- GOT IT MAN----[731881]
 ---------GOING TO UNLOCK ==[731881]
 ------------BYE---------[731881]
 ------- GOT IT MAN----[161917]
 ---------GOING TO UNLOCK ==[161917]
 ------------BYE---------[161917]
 ------- GOT IT MAN----[1111423]
 ---------GOING TO UNLOCK ==[1111423]
 ------------BYE---------[1111423]
 ------- GOT IT MAN----[745475]
 ---------GOING TO UNLOCK ==[745475]
 ------------BYE---------[745475]
 ------- GOT IT MAN----[1180995]
 ---------GOING TO UNLOCK ==[1180995]
 ------------BYE---------[1180995]
 ------- GOT IT MAN----[721475]
 ---------GOING TO UNLOCK ==[721475]
 ------------BYE---------721475]

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 07-27-2012 at 03:34 PM..
# 2  
Old 07-27-2012
The order they print isn't really related to the order they lock, for two reasons:

1) Every system call is a context switch. By printing, your thread is giving others the opportunity to go first.

2) You are using buffered printing. Use fprintf(stderr, ...); if you want it to go directly to the terminal, right now, no buffering.

If you put a sleep() in your thread-creating loop, print to stderr, and only print when you've locked the mutex, the order will probably look more sane.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Programming

pthread()

I have a while loop like so: while (counter (file1)); how can I pass that into a pthread_create()? I was thinking ... while(pthread_create(&path, NULL, counter, file)); is that right? (1 Reply)
Discussion started by: l flipboi l
1 Replies

3. UNIX for Advanced & Expert Users

pthread

I am so confused about the user threads and kernel threads.Suppose I created a thread using pthread create call in Linux ,whether it will be a user thread or kernel thread.If it user thread,then how its map to kernel thread. I heard about the M:1,M:N,1:1 mapping methods.Which method linux is... (1 Reply)
Discussion started by: sujith4u87
1 Replies

4. Programming

Error with Pthread

problem solved edited, sorry (1 Reply)
Discussion started by: joey
1 Replies

5. UNIX for Dummies Questions & Answers

a pthread problem

Hello, I run my pthread code on Linux with 4 processors. However, the speed up is only 2 times. The code is about solving equation (G+s(i)C)z(i)=B*us(i), i=1,...,n. Here G,C are m*m matrix, B*us(i) is a m*1 vector and s(i) are n different numbers. I need to solve the equation n times to... (2 Replies)
Discussion started by: mgig
2 Replies

6. Programming

pthread.h

hallo 2 al can anyone pls tell me where and how can i find and install the pthread.h lib ? thx :cool: (2 Replies)
Discussion started by: XinU*
2 Replies

7. Programming

pthread

consider if the thread routine returns any void pointer while calling pthread_join, the thread resources are freed and the thread will be terminated when the main thread is exit ,that is my assumption whether it is true how do we find whether the thread is alive or terminated how do we find... (0 Replies)
Discussion started by: MKSRaja
0 Replies

8. UNIX for Dummies Questions & Answers

Unlocking KDE

Currently we have KDE installed on Solaris 9. The problem is though when a user locks the machine I (as root) can not unlock the machine. What do I have to do to make it so that root user can unlock the machine when a user locks it? Cheers Woofie :-) (1 Reply)
Discussion started by: woofie
1 Replies

9. Programming

More about Pthread

Can someone point to a link where I can get good info about pthread? thanx.. :) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question