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 fd1, fd2, *ptr1, *ptr2; pid_t childpid; struct stat stat; if (argc != 2) err_quit("Usage: test2 <name>"); Shm_unlink(Px_ipc_name(argv[1])); fd1 = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); Ftruncate(fd1, sizeof(int)); fd2 = Open("./shm", O_RDONLY); Fstat(fd2, &stat); if ((childpid = Fork()) == 0) { ptr2 = Mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0); ptr1 = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); printf("child: shm ptr = %p, motd ptr = %p\n", ptr1, ptr2); sleep(5); printf("shared memory integer = %d\n", *ptr1); exit(0); } //parent: map in reverse order from child ptr1 = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); ptr2 = Mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0); *ptr1 = 777; Waitpid(childpid, NULL, 0); exit(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); }
void encode_mmap() { int fd; int mmaplen; // handle share memory if(shm_unlink(Px_ipc_name(MMAP_SHM_NAME)) == -1) { CAP_DBG("shm_unlink error: %s", strerror(errno)); } mmaplen = sizeof(mmap_ring_t) + MMAP_NODE_NUM * sizeof(mmap_node_t); fd = Shm_open(Px_ipc_name(MMAP_SHM_NAME), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); CAP_DBG("shm_open create successfully\n"); Ftruncate(fd, mmaplen); h264_buf = Mmap(NULL, mmaplen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // initialize mmap memory after create memset(h264_buf, 0, mmaplen); x264_ring = (mmap_ring_t *)h264_buf; shm_init_mutex(); shm_init_cond(); x264_node = (mmap_node_t *)(h264_buf + sizeof(mmap_ring_t)); Close(fd); }
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); }
int main(int argc, char **argv) { int i, nloop; mqd_t mq1, mq2; char buff[MSGSIZE]; pid_t childpid; struct mq_attr attr; if (argc != 2) err_quit("usage: lat_pxmsg <#loops>"); nloop = atoi(argv[1]); attr.mq_maxmsg = MAXMSG; attr.mq_msgsize = MSGSIZE; mq1 = Mq_open(Px_ipc_name(NAME1), O_RDWR | O_CREAT, FILE_MODE, &attr); mq2 = Mq_open(Px_ipc_name(NAME2), O_RDWR | O_CREAT, FILE_MODE, &attr); if ( (childpid = Fork()) == 0) { for ( ; ; ) { /* child */ if (Mq_receive(mq1, buff, MSGSIZE, NULL) != 1) err_quit("mq_receive error"); Mq_send(mq2, buff, 1, 0); } exit(0); } /* 4parent */ doit(mq1, mq2); Start_time(); for (i = 0; i < nloop; i++) doit(mq1, mq2); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); Mq_close(mq1); Mq_close(mq2); Mq_unlink(Px_ipc_name(NAME1)); Mq_unlink(Px_ipc_name(NAME2)); 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); }
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: 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 != 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; }
/* 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); }
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); }