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); }
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); }
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; }
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; }
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); }
int main(int argc, char **argv) { int c, flags; sem_t *sem; unsigned int value; flags = O_RDWR | O_CREAT; value = 1; while ((c = Getopt(argc, argv, "ei:")) != -1) { switch (c) { case 'e': flags |= O_EXCL; break; case 'i': value = atoi(optarg); break; } } if (optind != argc - 1) err_quit("Usage: create [-e] [-i arg] <name>"); sem = Sem_open(argv[optind], flags, FILE_MODE, value); Sem_close(sem); exit(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; }
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); }
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; }
int main(int argc, char **argv) { sem_t *sem; int val; if (argc != 2) err_quit("Usage: wait <name>"); sem = Sem_open(argv[1], 0); Sem_wait(sem); Sem_getvalue(sem, &val); printf("pid %ld has semaphore, value = %d\n", (long)getpid(), val); pause(); exit(0); }
int main(int argc, char *argv[]) { sem_t *sem; int val; if(argc != 2) err_quit("usage: semwait <name>"); sem = Sem_open(argv[1], 0);/*打开一个有名的信号量*/ Sem_wait(sem);/*测试sem是否大于0,是则减1,执行,否者阻塞*/ Sem_getvalue(sem, &val);/*获取信号的当前值*/ printf("pid %ld has semaphora value = %d\n", (long) getpid(), val); pause(); return 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); }
int main(int argc, char **argv) { int fd; struct shmstruct *ptr; if (argc != 3) err_quit("Usage: server <shmname> <semname>"); shm_unlink((const char *)Px_ipc_name(argv[1])); //create shm, set its size, map it, close descriptor fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); Ftruncate(fd, sizeof(struct shmstruct)); ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); sem_unlink((const char *)Px_ipc_name(argv[2])); mutex = Sem_open(Px_ipc_name(argv[2]), O_CREAT | O_EXCL, FILE_MODE, 1); Sem_close(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); }
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); }