* 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
* Re: See if you can run this
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
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Gabriel @ 1997-05-12 4:00 UTC (permalink / raw)
In article <33776eb4.19046817@news.elt.com>,
blb@elt.com (Luke Braly) writes:
> 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.
Recoding to use posix mutex instead, it works fine on Solaris.
The same would be true if recoded to use UI (a.k.a. Solaris)
threads and mutexs on Solaris.
--
Andrew Gabriel Andrew@cucumber.demon.co.uk
Consultant Software Engineer
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: See if you can run this
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
1 sibling, 0 replies; 3+ messages in thread
From: Casper H.S. Dik - Network Security Engineer @ 1997-05-13 4:00 UTC (permalink / raw)
blb@elt.com (Luke Braly) writes:
> 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.
>msemaphore *sem;
What's that?
There's no such thing in Solaris (so it won't even compile).
If you use pthreads, why not use POSIX semaphores?
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
^ 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