Example #1
0
/* include consume */
void *
consume(void *arg)
{
	int		i;

	for ( ; ; ) {
		Sem_wait(&shared.nstored);	/* wait for at least 1 stored item */
		Sem_wait(&shared.mutex);

		if (shared.nget >= nitems) {
			Sem_post(&shared.nstored);
			Sem_post(&shared.mutex);
			return(NULL);			/* all done */
		}

		i = shared.nget % NBUFF;
		if (shared.buff[i] != shared.ngetval)
			printf("error: buff[%d] = %d\n", i, shared.buff[i]);
		shared.nget++;
		shared.ngetval++;

		Sem_post(&shared.mutex);
		Sem_post(&shared.nempty);	/* 1 more empty slot */
		*((int *) arg) += 1;
	}
}
Example #2
0
int main(int argc, char *argv[])
{
  int i, nloop;
  sem_t *mutex;
  
  if(argc != 2)
    err_quit("usage: incr1  <#loops>");
  nloop = atoi(argv[1]);

  /*创建并初始化信号量*/
  mutex = Sem_open(Px_ipc_name(SEM_NAME), O_CREAT | O_EXCL, FILE_MODE, 1);
  Sem_unlink(Px_ipc_name(SEM_NAME));
  /*把标准输出设置为非缓冲区*/
  setbuf(stdout, NULL);
	    
  if(Fork() == 0){/*child*/
    for(i = 0; i < nloop; ++i){
	Sem_wait(mutex);
	printf("child: %d\n", count++);
	Sem_post(mutex);
	}
    exit(0);
   }
	     
  for(i = 0; i < nloop; ++i){/*parent*/
	Sem_wait(mutex);
	printf ("parent: %d\n", count++);
	Sem_post(mutex);
      }	     
  exit(0);
}
Example #3
0
int main(int argc, char *argv[])
{
  int fd, i, nloop, zero = 0;
  int *ptr;
  sem_t *mutex;
  
  if(argc != 3)
    err_quit("usage: incr2 <pathname> <#loops>");
  
  fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);/*打开文件用于读写,不存在则创建它*/
  Write(fd, &zero, sizeof(int));/*写一个值为0的值保存到文件*/
  /*调用mmap把刚打开的文件映射到本进程的内存空间中*/
  ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  Close(fd);
  
  mutex = Sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1);
  Sem_unlink(SEM_NAME);
  
  setbuf(stdout, NULL);/*把标准输出设置为非缓冲区*/
  if(Fork() == 0){
    for(i = 0; i < nloop; ++i){
      Sem_wait(mutex);
      printf ("child:%d\n",(*ptr)++);
      Sem_post(mutex);
    }
    exit(0);
  }
  
  for(i = 0; i < nloop; ++i){
    Sem_wait(mutex);
    printf ("parent:%d\n",(*ptr)++);
    Sem_post(mutex);
  }
  return 0;
}
Example #4
0
void  *consume(void * arg)
{
	int i;

	for(i=0;;){
		Sem_wait(&shared.nstored);
		Sem_wait(&shared.mutex);
		Sem_post(&shared.mutex);
		
		if(shared.buff[i].n==0)
		{
			return NULL;
		}
		Write(STDOUT_FILENO, shared.buff[i].data, shared.buff[i].n);

		if(++i==NBUFF)
			i=0;

		Sem_post(&shared.nempty);

	}



}
Example #5
0
int main(int argc, char *argv[])
{
  int fd, i, nloop;
  struct shared *ptr;
  
  if(argc != 3)
    err_quit("usage: incr3 <pathname> <#loop>");
  nloop = atoi(argv[2]);
  
  /*映射到内存*/
  fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);
  Write(fd, &shared, sizeof(struct shared));
  ptr = Mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, 
	     MAP_SHARED, fd, 0);
  Close(fd);
  
  Sem_init(&ptr->mutex, 1, 1);/*初始化信号量*/
  
  setbuf(stdout, NULL);
  if(Fork() == 0){
    for(i = 0; i < nloop; ++i){
      Sem_wait(&ptr->mutex);
      printf("child:%d\n", ptr->count++);
      Sem_post(&ptr->mutex);
    }
    exit(0);
  }
  for(i = 0; i < nloop; ++i){
    Sem_wait(&ptr->mutex);
    printf("parent: %d\n", ptr->count++);
    Sem_post(&ptr->mutex);
  }
  return 0;
}
Example #6
0
int
main(int argc, char **argv)
{
	int		fd, index, lastnoverflow, temp;
	long	offset;
	struct shmstruct	*ptr;

	if (argc != 2)
		err_quit("usage: server2 <name>");

		/* 4create shm, set its size, map it, close descriptor */
	shm_unlink(Px_ipc_name(argv[1]));		/* OK if this fails */
	fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE);
	ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
			   MAP_SHARED, fd, 0);
	Ftruncate(fd, sizeof(struct shmstruct));
	Close(fd);

		/* 4initialize the array of offsets */
	for (index = 0; index < NMESG; index++)
		ptr->msgoff[index] = index * MESGSIZE;

		/* 4initialize the semaphores in shared memory */
	Sem_init(&ptr->mutex, 1, 1);
	Sem_init(&ptr->nempty, 1, NMESG);
	Sem_init(&ptr->nstored, 1, 0);
	Sem_init(&ptr->noverflowmutex, 1, 1);

		/* 4this program is the consumer */
	index = 0;
	lastnoverflow = 0;
	for ( ; ; ) {
		Sem_wait(&ptr->nstored);
		Sem_wait(&ptr->mutex);
		offset = ptr->msgoff[index];
		printf("index = %d: %s\n", index, &ptr->msgdata[offset]);
		if (++index >= NMESG)
			index = 0;				/* circular buffer */
		Sem_post(&ptr->mutex);
		Sem_post(&ptr->nempty);

		Sem_wait(&ptr->noverflowmutex);
		temp = ptr->noverflow;		/* don't printf while mutex held */
		Sem_post(&ptr->noverflowmutex);
		if (temp != lastnoverflow) {
			printf("noverflow = %d\n", temp);
			lastnoverflow = temp;
		}
	}

	exit(0);
}
Example #7
0
/*生产者*/
void *produce(void *arg)
{
  int i;
  
  for(i = 0; i < nitems; ++i){
    Sem_wait(shared.mutex);/*测试mutex大于0?mutex--:阻塞  这是一个原子操作*/
    Sem_wait(shared.nempty);/*测试nempty大于0?nempty--:阻塞*/
    shared.buff[i % NBUFF] = i;/*向数组中放i*/
    Sem_post(shared.mutex);/*测试mutex等于0?mutex++:阻塞*/
    Sem_post(shared.nstored);
  }
  return(NULL);
}
Example #8
0
void *produce(void *arg)
{
	int i;

	for (i = 0; i < nitems; i++)
	{
		Sem_wait(shared.nempty); // wait for at least 1 empty slot
		Sem_wait(shared.mutex);
		shared.buff[i % NBUFF] = i; // store i into circular buffer
		Sem_post(shared.mutex);
		Sem_post(shared.nstored); // 1 more stored item
	}
	return (NULL);
}
Example #9
0
void *consume(void *arg)
{
  int i;
  
  for(i = 0; i < nitems; ++i){
    Sem_wait(shared.nstored);
    Sem_wait(shared.mutex);
    if(shared.buff[i % NBUFF] != i)
      printf ("buff[%d] = %d\n",i, shared.buff[i % NBUFF]);
    Sem_post(shared.mutex);
    Sem_post(shared.nempty);
  }
  return(NULL);
}
Example #10
0
void consume(void *arg)
{
	int i;

	for (i = 0; i < nitems; i++)
	{
		Sem_wait(shared.nstored); // wait for at least 1 stored item
		Sem_wait(shared.mutex);
		if (shared.buff[i % NBUFF] == i)
			printf("buff[%d] = %d\n", i, shared.buff[i % NBUFF]);
		Sem_post(shared.mutex);
		Sem_post(shared.nempty); // 1 more empty slot
	}
	return (NULL);
}
Example #11
0
int
main(int argc, char *argv[])
{
    int fd,i,nloop,zero = 0;
    int *ptr;
    sem_t *mutex;

    if (argc != 3) {
        err_quit("usage: incr2 <pathname> <#loops> \n");
        return -1;
    }
    nloop = atoi(argv[2]);

    fd = Open(argv[1],O_RDWR | O_CREAT,00666);
    Write(fd, &zero,sizeof(int));
    ptr = (int *)Mmap(
                      NULL,
                      sizeof(int),
                      PROT_READ | PROT_WRITE,
                      MAP_SHARED,
                      fd,
                      0
                    );

    close(fd);

    mutex =Sem_open(SEM_NAME,O_CREAT | O_EXCL,00666,1);
    Sem_unlink(SEM_NAME);

    setbuf(stdout,NULL);

    if (Fork() == 0) {
        for (i=0; i<nloop; i++) {
            Sem_wait(mutex);
            printf("child: %d \n",(*ptr)++);
            Sem_post(mutex);
        }
    }


    for (i=0; i<nloop; i++) {
        Sem_wait(mutex);
        printf("parent: %d \n",(*ptr)++);
        Sem_post(mutex);
    }

    return 0;
}
Example #12
0
int
main(int argc, char **argv)
{
	int		i, nthreads;
	pthread_t	tid[MAXNTHREADS];

	if (argc != 3)
		err_quit("usage: incr_pxsem1 <#loops> <#threads>");
	nloop = atoi(argv[1]);
	nthreads = min(atoi(argv[2]), MAXNTHREADS);

		/* 4initialize memory-based semaphore to 0 */
	Sem_init(&shared.mutex, 0, 0);

		/* 4create all the threads */
	Set_concurrency(nthreads);
	for (i = 0; i < nthreads; i++) {
		Pthread_create(&tid[i], NULL, incr, NULL);
	}
		/* 4start the timer and release the semaphore */
	Start_time();
	Sem_post(&shared.mutex);

		/* 4wait for all the threads */
	for (i = 0; i < nthreads; i++) {
		Pthread_join(tid[i], NULL);
	}
	printf("microseconds: %.0f usec\n", Stop_time());
	if (shared.counter != nloop * nthreads)
		printf("error: counter = %ld\n", shared.counter);

	exit(0);
}
Example #13
0
/*
 *  ======== doCmd ========
 */
static Int doCmd(Int cmdId, Processor_Handle proc)
{
    Int result;

    GT_2trace(curTrace, GT_ENTER, "doCmd> Enter (cmdId=%d, proc=0x%x)\n",
            cmdId, proc);

    //pthread_mutex_lock(&dcmd.gate);
    Lock_acquire(dcmd.gate);

    dcmd.proc = proc;
    dcmd.cmdId = cmdId;
    //pthread_cond_signal(&dcmd.cmdPresent);
    Sem_post(dcmd.cmdPresent);

    //while (dcmd.reply == NONE) {
    //    pthread_cond_wait(&dcmd.replyPresent, &dcmd.gate);
    //}
    Sem_pend(dcmd.replyPresent, Sem_FOREVER);

    result = dcmd.reply;
    dcmd.reply = NONE;

    //pthread_mutex_unlock(&dcmd.gate);
    Lock_release(dcmd.gate);

    GT_1trace(curTrace, GT_ENTER, "doCmd> Exit (result=%d)\n", result);

    return (result);
}
Example #14
0
int main(int argc, char *argv[])
{
    int fd, i, nloop;
    pid_t pid;
    struct shmstruct *ptr;

    if(argc != 4)
        err_quit("usage: client1 <shmname> <semname> <#loop>");
    nloop = atoi(argv[3]);

    fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR, FILE_MODE);
    ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
               MAP_SHARED, fd, 0);
    Close(fd);

    mutex = Sem_open(Px_ipc_name(argv[2]), 0);

    pid = getpid();
    for(i = 0; i < nloop; ++i) {
        Sem_wait(mutex);
        printf("pid %ld:%d\n", (long)pid, ptr->count++);
        Sem_post(mutex);
    }
    return 0;
}
Example #15
0
int main(int argc, char **argv)
{
	int fd, i, nloop;
	int *ptr;
	sem_t *mutex;

	if (argc != 2)
	{
		err_quit("usage: incr_dev_zero <#loops>");
	}

	nloop = atoi(argv[1]);
	
	/* open /dev/zero, map into memory */
	fd = Open("/dev/zero", O_RDWR);
	ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	Close(fd);

	/* create, initialize, and unlink semaphore */
	mutex = Sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1);
	Sem_unlink(SEM_NAME);

	// stdout is unbuffered
	setbuf(stdout, NULL);
	if (Fork() == 0)
	{ // child process
		for (i = 0;i < nloop; ++i)
		{
			Sem_wait(mutex);
			printf("child:%d\n", (*ptr)++);
			Sem_post(mutex);
			sleep(1);
		}
		exit(0);
	}

	for (i = 0;i < nloop; ++i)
	{
		Sem_wait(mutex);
		printf("parent:%d\n", (*ptr)++);
		Sem_post(mutex);
		sleep(1);
	}

	exit(0);
}
Example #16
0
/* include consume */
void *
consume(void *arg)
{
	int		i;

	for (i = 0; i < nitems; i++) {
		Sem_wait(&shared.nstored);		/* wait for at least 1 stored item */
		Sem_wait(&shared.mutex);

		if (shared.buff[i % NBUFF] != i)
			printf("error: buff[%d] = %d\n", i, shared.buff[i % NBUFF]);

		Sem_post(&shared.mutex);
		Sem_post(&shared.nempty);		/* 1 more empty slot */
	}
	return(NULL);
}
Example #17
0
static int lprintf(log_t *log, unsigned int level, int err_flg,
		int t_err_code, const char *fmt, va_list ap)
{
	int fd;
	time_t now;
	char date[50];
	static char line[LOGLINE_MAX];
	static char processnum[10];
	int len;
	int errno_save;
	static char *levels[10] = {
		"[(bad)]", "[debug]", "[info]", "[warn]",
		"[error]", "[emerg]", "[fatal]"};

	if (NULL == log) return -1;

	fd = log->fd;
	if (!(log->flags&LOG_NODATE)) {
		now = time(NULL);
		strcpy(date, ctime(&now));
		date[strlen(date) - 6] = ' ';
		date[strlen(date) - 5] = '\0';
	}

	if (!(log->flags&LOG_NOPID))
		sprintf(processnum, "(PID:%ld) ", (long) getpid());

	snprintf(line, sizeof(line), "%s%s%s",
			log->flags&LOG_NODATE ? "" : date,
			log->flags&LOG_NOLVL ? "" : 
			(level > FATAL ? levels[0] : levels[level]),
			log->flags&LOG_NOPID ? "" : processnum);
	len = strlen(line);

	vsnprintf(line+len, sizeof(line) - len, fmt, ap);

	len = strlen(line);
	if (1 == err_flg) {
		errno_save = errno;
		snprintf(line+len, sizeof(line) - len,
				": %s", strerror(errno_save));
	}
	else if (2 == err_flg)
		snprintf(line+len, sizeof(line) - len,
				": %s", strerror(t_err_code));

	if (!(log->flags&LOG_NOLF))
		strcat(line, "\n");

	Sem_wait(&log->sem);
	Writen(fd, line, strlen(line));
	if (EMERG == level && (log->flags&LOG_STDERR))
		fprintf(stderr, "%s\n", line);
	Sem_post(&log->sem);

	return len;
} /* end lprintf */
Example #18
0
/* include prodcons */
void *
produce(void *arg)
{
	int		i;

	for (i = 0; i < nitems; i++) {
		printf("prod: calling sem_wait(nempty)\n");
		Sem_wait(shared.nempty);	/* wait for at least 1 empty slot */
		printf("prod: got sem_wait(nempty)\n");
		printf("prod: calling sem_wait(mutex)\n");
		Sem_wait(shared.mutex);
		printf("prod: got sem_wait(mutex), storing %d\n", i);
		shared.buff[i % NBUFF] = i;	/* store i into circular buffer */
		Sem_post(shared.mutex);
		Sem_post(shared.nstored);	/* 1 more stored item */
	}
	return(NULL);
}
Example #19
0
int
main(int argc, char **argv)
{
	int		fd, i, nloop, nusec;
	pid_t	pid;
	char	mesg[MESGSIZE];
	long	offset;
	struct shmstruct	*ptr;

	if (argc != 4)
		err_quit("usage: client2 <name> <#loops> <#usec>");
	nloop = atoi(argv[2]);
	nusec = atoi(argv[3]);

		/* 4open and map shared memory that server must create */
	fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR, FILE_MODE);
	ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
			   MAP_SHARED, fd, 0);
	Close(fd);

	pid = getpid();
	for (i = 0; i < nloop; i++) {
		Sleep_us(nusec);
		snprintf(mesg, MESGSIZE, "pid %ld: message %d", (long) pid, i);

		if (sem_trywait(&ptr->nempty) == -1) {
			if (errno == EAGAIN) {
				Sem_wait(&ptr->noverflowmutex);
				ptr->noverflow++;
				Sem_post(&ptr->noverflowmutex);
				continue;
			} else
				err_sys("sem_trywait error");
		}
		Sem_wait(&ptr->mutex);
		offset = ptr->msgoff[ptr->nput];
		if (++(ptr->nput) >= NMESG)
			ptr->nput = 0;		/* circular buffer */
		Sem_post(&ptr->mutex);
		strcpy(&ptr->msgdata[offset], mesg);
		Sem_post(&ptr->nstored);
	}
	exit(0);
}
Example #20
0
void *
consume(void *arg)
{
	int		i;

	for (i = 0; i < nitems; i++) {
		printf("cons: calling sem_wait(mutex)\n");
		Sem_wait(shared.mutex);
		printf("cons: got sem_wait(mutex)\n");
		printf("cons: calling sem_wait(nstored)\n");
		Sem_wait(shared.nstored);		/* wait for at least 1 stored item */
		printf("cons: got sem_wait(nstored)\n");
		printf("cons: fetched %d\n", shared.buff[i % NBUFF]);
		Sem_post(shared.mutex);
		Sem_post(shared.nempty);		/* 1 more empty slot */
	}
	printf("\n");
	return(NULL);
}
Example #21
0
/* include incr */
void *
incr(void *arg)
{
	int		i;

	for (i = 0; i < nloop; i++) {
		Sem_wait(&shared.mutex);
		shared.counter++;
		Sem_post(&shared.mutex);
	}
	return(NULL);
}
Example #22
0
void *
consume(void *arg)
{
	int		i;

	for (i = 0; ; ) {
		Sem_wait(&shared.nstored);		/* wait for at least 1 stored item */

		Sem_wait(&shared.mutex);
			/* 4critical region */
		Sem_post(&shared.mutex);

		if (shared.buff[i].n == 0)
			return(NULL);
		Write(STDOUT_FILENO, shared.buff[i].data, shared.buff[i].n);
		if (++i >= NBUFF)
			i = 0;					/* circular buffer */

		Sem_post(&shared.nempty);		/* 1 more empty slot */
	}
}
Example #23
0
void *produce(void *arg)
{
	int i;

	for (i = 0; ; ) {
		Sem_wait(&shared.nempty);
		Sem_wait(&shared.mutex);

		Sem_post(&shared.mutex);

		shared.buff[i].n = Read(fd, shared.buff[i].data, BUFFSIZE);
		if (shared.buff[i].n == 0) {
			Sem_post(&shared.nstored);
			return (NULL);
		}
		if (++i >= NBUFF)
			i = 0;

		Sem_post(&shared.nstored);
	}
}
Example #24
0
/* include produce */
void *
produce(void *arg)
{
	for ( ; ; ) {
		Sem_wait(&shared.nempty);	/* wait for at least 1 empty slot */
		Sem_wait(&shared.mutex);

		if (shared.nput >= nitems) {
			Sem_post(&shared.nempty);
			Sem_post(&shared.mutex);
			return(NULL);			/* all done */
		}

		shared.buff[shared.nput % NBUFF] = shared.nputval;
		shared.nput++;
		shared.nputval++;

		Sem_post(&shared.mutex);
		Sem_post(&shared.nstored);	/* 1 more stored item */
		*((int *) arg) += 1;
	}
}
Example #25
0
int
main(int argc, char **argv)
{
	int		fd, i, nloop;
	struct shared 	*ptr;

	if (argc != 3)
		err_quit("usage: incr3 <pathname> <#loops>");
	nloop = atoi(argv[2]);

		/* 4open file, initialize to 0, map into memory */
	fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);
	Write(fd, &shared, sizeof(struct shared));
	ptr = Mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE,
			   MAP_SHARED, fd, 0);
	Close(fd);

		/* 4initialize semaphore that is shared between processes */
	Sem_init(&ptr->mutex, 1, 1);

	setbuf(stdout, NULL);	/* stdout is unbuffered */
	if (Fork() == 0) {		/* child */
		for (i = 0; i < nloop; i++) {
			Sem_wait(&ptr->mutex);
			printf("child: %d\n", ptr->count++);
			Sem_post(&ptr->mutex);
		}
		exit(0);
	}

		/* 4parent */
	for (i = 0; i < nloop; i++) {
		Sem_wait(&ptr->mutex);
		printf("parent: %d\n", ptr->count++);
		Sem_post(&ptr->mutex);
	}
	exit(0);
}
/* include diff */
int
main(int argc, char **argv)
{
    int		i, nloop;
    int		*ptr;
    sem_t	*mutex;

    if (argc != 2)
        err_quit("usage: incr_map_anon <#loops>");
    nloop = atoi(argv[1]);

    /* 4map into memory */
    ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
               MAP_SHARED | MAP_ANON, -1, 0);
    /* end diff */

    /* 4create, initialize, and unlink semaphore */
    mutex = Sem_open(Px_ipc_name(SEM_NAME), O_CREAT | O_EXCL, FILE_MODE, 1);
    Sem_unlink(Px_ipc_name(SEM_NAME));

    setbuf(stdout, NULL);	/* stdout is unbuffered */
    if (Fork() == 0) {		/* child */
        for (i = 0; i < nloop; i++) {
            Sem_wait(mutex);
            printf("child: %d\n", (*ptr)++);
            Sem_post(mutex);
        }
        exit(0);
    }

    /* 4parent */
    for (i = 0; i < nloop; i++) {
        Sem_wait(mutex);
        printf("parent: %d\n", (*ptr)++);
        Sem_post(mutex);
    }
    exit(0);
}
Example #27
0
/* include prodcons */
void *
produce(void *arg)
{
	int		i;

	for (i = 0; ; ) {
		Sem_wait(&shared.nempty);	/* wait for at least 1 empty slot */

		Sem_wait(&shared.mutex);
			/* 4critical region */
		Sem_post(&shared.mutex);

		shared.buff[i].n = Read(fd, shared.buff[i].data, BUFFSIZE);
		if (shared.buff[i].n == 0) {
			Sem_post(&shared.nstored);	/* 1 more stored item */
			return(NULL);
		}
		if (++i >= NBUFF)
			i = 0;					/* circular buffer */

		Sem_post(&shared.nstored);	/* 1 more stored item */
	}
}
Example #28
0
int main(int argc, char **argv)
{
	int fd, i, nloop, zero = 0;
	int *ptr;
	sem_t *mutex;

	if (argc != 3)
		err_quit("Usage: incr1 <pathname> <#loops>");
	nloop = atoi(argv[2]);

	//open file, initialize to 0, map into memory
	fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);
	Write(fd, &zero, sizeof(int));
	ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	Close(fd);

	//create, initialize and unlink semaphore
	mutex = Sem_open(Px_ipc_name(SEM_NAME), O_CREAT | O_EXCL, FILE_MODE, 1);
	Sem_unlink(Px_ipc_name(SEM_NAME));

	setbuf(stdout, NULL);
	if (Fork() == 0) {
		for (i = 0; i < nloop; i++) {
			Sem_wait(mutex);
			printf("child: %d\n", (*ptr)++);
			Sem_post(mutex);
		}
		exit(0);
	}

	for (i = 0; i < nloop; i++) {
		Sem_wait(mutex);
		printf("parent: %d\n", (*ptr)++);
		Sem_post(mutex);
	}
	exit(0);
}
Example #29
0
int main(int argc, char **argv)
{
    sem_t *sem;
    int val;

    if (argc != 2)
        err_quit("Usage: post <name>");

    sem = Sem_open(argv[1], 0);
    Sem_post(sem);
    Sem_getvalue(sem, &val);
    printf("value = %d\n", val);

    exit(0);
}
Example #30
0
/*
 *  ======== putReply ========
 */
static Void putReply(Int status)
{
    //pthread_mutex_lock(&dcmd.gate);

    dcmd.reply = status;

    //pthread_cond_signal(&dcmd.replyPresent);
    Sem_post(dcmd.replyPresent);

    /* GT_2trace(curTrace, GT_ENTER, "putReply(0x%x): proc = 0x%lx\n",
     *   status, dcmd.proc);
     */

    //pthread_mutex_unlock(&dcmd.gate);
}