From: blb@elt.com (Luke Braly)
Subject: See if you can run this
Date: 1997/05/12 [thread overview]
Message-ID: <33776eb4.19046817@news.elt.com> (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)
next reply other threads:[~1997-05-12 4:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-05-12 4:00 Luke Braly [this message]
1997-05-12 4:00 ` See if you can run this Andrew Gabriel
1997-05-13 4:00 ` Casper H.S. Dik - Network Security Engineer
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox