Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* See if  you can run this
@ 1997-05-12  4:00 Luke Braly
  1997-05-12  4:00 ` Andrew Gabriel
  1997-05-13  4:00 ` Casper H.S. Dik - Network Security Engineer
  0 siblings, 2 replies; 3+ messages in thread
From: Luke Braly @ 1997-05-12  4:00 UTC (permalink / raw)



 If you are able to multi-thread on your Unix implementation, I'd be
interested to see if you can run the program below.  It uses msem_
type semaphores in conjunction with threads.  A colleague of mine says
it won't work on some Unix systems because the process (as opposed to
a thread) will be locked, thus causing the program to hang if two
threads in the same process lock the same semaphore. However, it works
fine under AIX. 

The command to compile and link under AIX is:
xlC_r  -lpthreads msemthr.cpp.
(xlC_r is the thread-compatible version of AIX's C++ compiler.)

The output should be:
thread - 0
thread - 1
thread - 2
thread - 3

(Note: you may have to replace the "pthread_create" call if your Unix
system uses a different threading mechanism.)


//THE PROGRAM:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>

msemaphore *sem;        

void* threadfunc(void *p) {
	
	int n = *(int*)p;
	msem_lock(sem,0);
	printf("thread - %d\n",n);      
	sleep(1);
	msem_unlock(sem,0);
	return (NULL);
}

main() {                

	pthread_t ThreadID;
	int n,m;
	sem = (msemaphore*) mmap(NULL,sizeof(msemaphore),
		   PROT_READ | PROT_WRITE,
		   MAP_SHARED | MAP_ANONYMOUS | MAP_VARIABLE, -1, 0 );
	sem = msem_init(sem,MSEM_UNLOCKED);

	int X[4];
	for (n=0; n<4; n++) {
		X[n] = n;
		pthread_create(&ThreadID, NULL, threadfunc, &(X[n]));
	}
	
	sleep(7);
}


//(END)



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-05-13  4:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-05-12  4:00 See if you can run this Luke Braly
1997-05-12  4:00 ` Andrew Gabriel
1997-05-13  4:00 ` Casper H.S. Dik - Network Security Engineer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox