Пример #1
0
int
main(int argc, char **argv)
{
	pthread_t	tid_produce, tid_consume;

	if (argc != 1)
		err_quit("usage: deadlock <#items>");
	nitems = atoi(argv[1]);

		/* 4create three semaphores */
	shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL,
							FILE_MODE, 1);
	shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL,
							 FILE_MODE, NBUFF);
	shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL,
							  FILE_MODE, 0);

	Set_concurrency(2);
	Pthread_create(&tid_produce, NULL, produce, NULL);
	Pthread_create(&tid_consume, NULL, consume, NULL);

	Pthread_join(tid_produce, NULL);
	Pthread_join(tid_consume, NULL);

	Sem_unlink(Px_ipc_name(SEM_MUTEX));
	Sem_unlink(Px_ipc_name(SEM_NEMPTY));
	Sem_unlink(Px_ipc_name(SEM_NSTORED));
	exit(0);
}
Пример #2
0
int main(int argc, char **argv)
{
	pthread_t tid_produce, tid_consume;

	if (argc != 2)
		err_quit("Usage: pc <#items>");
	nitems = atoi(argv[1]);

	//create three semaphore
	shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1);
	shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF);
	shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0);

	//create one producer thread and one consumer thread
	Pthread_setconcurrency(2);
	Pthread_create(&tid_produce, NULL, produce, NULL);
	Pthread_create(&tid_consume, NULL, consume, NULL);

	//wait for the two threads
	Pthread_join(tid_produce, NULL);
	Pthread_join(tid_consume, NULL);

	//remove the semaphores
	Sem_unlink(Px_ipc_name(SEM_MUTEX));
	Sem_unlink(Px_ipc_name(SEM_NEMPTY));
	Sem_unlink(Px_ipc_name(SEM_NSTORED));
	exit(0);
}
Пример #3
0
int main(int argc, char *argv[])
{
  pthread_t tid_produce, tid_consume;
  
  if(argc != 2)
    err_quit("usage: prodcons1 <#items>");
  nitems = atoi(argv[1]);
  
  /*创建信号量*/
  shared.mutex = Sem_open(Px_ipc_name(SEM_MUTEX), O_CREAT | O_EXCL, FILE_MODE, 1);
  shared.nempty = Sem_open(Px_ipc_name(SEM_NEMPTY), O_CREAT | O_EXCL, FILE_MODE, NBUFF);
  shared.nstored = Sem_open(Px_ipc_name(SEM_NSTORED), O_CREAT | O_EXCL, FILE_MODE, 0);

  Set_concurrency(2);/*线程并发处理*/
  /*创建两个线程*/
  Pthread_create(&tid_produce, NULL, produce, NULL);
  Pthread_create(&tid_consume, NULL, consume, NULL);
  
  /*主线程等待两个线程*/
  Pthread_join(tid_produce, NULL);
  Pthread_join(tid_consume, NULL);

  /*释放信号量*/
  Sem_unlink(Px_ipc_name(SEM_MUTEX));
  Sem_unlink(Px_ipc_name(SEM_NEMPTY));
  Sem_unlink(Px_ipc_name(SEM_NSTORED));
  return 0;
}
Пример #4
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);
}
Пример #5
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;
}
Пример #6
0
int main(int argc, char **argv)
{
	if (argc != 2)
		err_quit("Usage: unlink <name>");

	Sem_unlink(argv[1]);
	exit(0);
}
Пример #7
0
int main(void) {
	sem_t * sem;
	sem = sem_open("/tmp/test0", O_CREAT | O_RDWR, FILE_MODE, 1);
	if (sem == SEM_FAILED) {
		printf("sem_open failed (%s)\n", strerror(errno));
		return 1;
	}

	Sem_unlink("/tmp/test0");
	Sem_close(sem);

	return 0;
}
Пример #8
0
int 
main(int argc ,char ** argv)
{
	int i,count[MAXTHREAD];
	
	pthread_t propids[MAXTHREAD],conpids[5];
	

	if((shared.pro_sem=(PROSEM,
	sem_open(O_RDWR|O_CREAT,FILE_MODE,0)))==SEM_FAILED)
		goto err1;
	if((shared.con_sem=sem_open(CONSEM,
		O_RDWR|O_CREAT,FILE_MODE,0))==SEM_FAILED)
		goto err2;

	nthread=min(atoi(argv[1]),MAXTHREAD);
	nitem=min(atoi(argv[2]));

	for(i=0;i<nthread;i++){
		count[i]=0;
		pthread_create(&propids[i],NULL,produce,&count[i]);

	}

	for(i=0;i<5;i++)
	{
		pthread_create(&conpids[i],NULL,consume,NULL);
	}



       	for(i=0;i<nthread;i++){
		pthread_join(propids[i],NULL);
		if(i<5)
			pthread_join(propids[i],NULL);

	  }






err2:
	Sem_unlink(shared.pro_sem);
err1:
	err_quit("sem_open error");


}
Пример #9
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;
}
Пример #10
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);
}
/* 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);
}
Пример #12
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);
}