Date: Thu, 30 Nov 2000 18:54:26 +0200 From: "Dimitar V. Peikov" <mitko@rila.bg> To: hackers@freebsd.org Subject: pthreads, semaphores and wait Message-ID: <200011301654.eAUGsQ517235@earth.rila.bg>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
I've tryed to make some example on using pthreads and semaphores and found
that process became blocked if inside of critical section use wait, uwait or
nanowait finctions. In the attached file if change line : (#if 0) to (#if 1)
the program hangs. I've tested it even with gdb and the threads were blocked
after wait finction.
[-- Attachment #2 --]
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* pThread includes */
#include <pthread.h>
/* IPC semaphores includes */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
extern int errno;
int common = 0;
int common_shared_id = 0;
char *common_pointer = NULL;
int semaphore_id = 0;
int pthread_semaphores(char *s) {
int internal = common;
struct sembuf sem_buf;
int sem_val = 0;
/* Lock semaphore 0 */
sem_buf.sem_num = 0;
sem_buf.sem_op = -1;
sem_buf.sem_flg = 0;
if ((sem_val = semop(semaphore_id, &sem_buf, 1)) == -1) {
fprintf(stderr, "Error lock semaphore: %s\n", strerror(errno));
return -1;
}
/* Get current value of the first semaphore */
if ((sem_val = semctl(semaphore_id, 0, GETVAL)) == -1) {
fprintf(stderr, "Error allocating semaphore: %s\n", strerror(errno));
return -1;
}
/* Do something ... */
common_pointer = (char *) malloc(strlen(s) + 1);
strcpy(common_pointer, s);
fprintf(stderr, "Enter semaphore %u\n", common++);
#if 0
sleep(1);
#endif
fprintf(stderr, "Leave semaphore %u\n", internal);
sem_buf.sem_num = 0;
sem_buf.sem_op = 1;
sem_buf.sem_flg = 0;
if ((sem_val = semop(semaphore_id, &sem_buf, 1)) == -1) {
fprintf(stderr, "Error lock semaphore: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char **argv) {
pthread_t thr[4];
int retcode = 0;
int sem_val;
/* Example using semaphores */
/* Create semaphore */
if ((semaphore_id = semget(IPC_PRIVATE, 1, SEM_R | SEM_A)) == -1) {
fprintf(stderr, "Error allocating semaphore: %s\n", strerror(errno));
return -1;
}
/* Assign value to the first semaphore */
if ((sem_val = semctl(semaphore_id, 0, SETVAL, 1)) == -1) {
fprintf(stderr, "Error allocating semaphore: %s\n", strerror(errno));
return -1;
}
/* Get current value of the first semaphore */
if ((sem_val = semctl(semaphore_id, 0, GETVAL)) == -1) {
fprintf(stderr, "Error allocating semaphore: %s\n", strerror(errno));
return -1;
}
/* Create pthreads */
pthread_create(&thr[0], NULL, (void *(*)(void *))pthread_semaphores, argv[0]);
pthread_create(&thr[1], NULL, (void *(*)(void *))pthread_semaphores, argv[0]);
pthread_join(thr[0], (void *) &retcode);
/* Remove semaphore */
if (semctl(semaphore_id, IPC_RMID, NULL) == -1) {
fprintf(stderr, "Error removing semaphore: %s\n", strerror(errno));
return -1;
}
fprintf(stderr, "Done!\n");
return 0;
}
[-- Attachment #3 --]
Dimitar Peikov
Programmer Analyst
"We Build e-Business"
RILA Solutions
27 Building, Acad.G.Bonchev Str.
1113 Sofia, Bulgaria
home: (+359 2) 595495
phone: (+359 2) 9797320
phone: (+359 2) 9797300
fax: (+359 2) 9733355
http://www.rila.com
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200011301654.eAUGsQ517235>
