int main(int argc,char **argv) { pthread_t produce_tid,consume_tid; if(argc!=2) err_quit("usage: mycat <pathname> "); if((fd=open(argv[1],O_RDONLY))==-1) err_sys("open error"); Sem_init(&shared.mutex,0,1); Sem_init(&shared.nempty,0,NBUFF); Sem_init(&shared.nstored,0,0); Set_concurrency(2); Pthread_create(&produce_tid,NULL,produce,NULL); Pthread_create(&consume_tid,NULL,consume,NULL); Pthread_join(produce_tid,NULL); Pthread_join(consume_tid,NULL); Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main(int argc, char **argv) { pthread_t tid_produce, tid_consume; if (argc != 2) err_quit("usage: mycat2 <pathname>"); fd = Open(argv[1], O_RDONLY); /* 4initialize three semaphores */ Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); /* 4one producer thread, one consumer thread */ Set_concurrency(2); Pthread_create(&tid_produce, NULL, produce, NULL); /* reader thread */ Pthread_create(&tid_consume, NULL, consume, NULL); /* writer thread */ Pthread_join(tid_produce, NULL); Pthread_join(tid_consume, NULL); Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main(int argc, char **argv) { pthread_t tid_produce, tid_consume; if (argc != 2) err_quit("Usage: buffer <pathname>"); fd = Open(argv[1], O_RDONLY); Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); Pthread_setconcurrency(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_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
/* Create an empty, bounded, shared FIFO buffer with n slots */ void sbuf_init(sbuf_t *sp, int n){ sp->buf = Calloc(n, sizeof(int)); sp->n = n; /* Buffer holds max of n items */ sp->front = sp->rear = 0; /* Empty buffer iff front == rear */ Sem_init(&sp->mutex, 0, 1); /* Binary semaphore for locking */ Sem_init(&sp->slots, 0, n); /* Initially, buf has n empty slots */ Sem_init(&sp->items, 0, 0); /* Initially, buf has zero data items */ }
T Chan_new(void) { T c; NEW(c); Sem_init(&c->send, 1); Sem_init(&c->recv, 0); Sem_init(&c->sync, 0); return c; }
void cache_init(){ int i; caches = (struct cache_block *)(malloc(cache_cnt*sizeof(struct cache_block))); for (i=0;i<cache_cnt;i++){ caches[i].timestamp = 0; caches[i].url[0] = '\0'; caches[i].response[0] = '\0'; caches[i].readcnt = 0; Sem_init(&caches[i].mutex, 0, 1); Sem_init(&caches[i].w, 0, 1); } }
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); }
/* After intiall error checks, bind and listen to the provided port * start some helper threads, then accept connections and place them in * the shared buffer for a helper thread to process. */ int main(int argc, char** argv) { int i, listenport, listenfd, connfd; unsigned clientlen; sockaddr_in clientaddr; pthread_t tid; if(argc < 2){ printf("usage: %s <port number to bind and listen>\n", argv[0]); exit(1); } Sem_init(&w, 0, 1); num_entries = 0; cache = NULL; listenport = atoi(argv[1]); sbuf_init(&sbuf, SBUFSIZE); listenfd = Open_listenfd(listenport); clientlen = sizeof(clientaddr); for(i = 0; i < NTHREADS; i++) /* prethreading, creating worker threads */ Pthread_create(&tid, NULL, thread, &clientaddr); while(1){ connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen); sbuf_insert(&sbuf, connfd); // put in buffer } return 0; }
T *Sem_new(int count) { T *s; NEW(s); Sem_init(s, count); return s; }
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; }
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); }
/* * ======== CERuntime_init ======== */ Void CERuntime_init(Void) { GT_init(); /* allow user to over-ride via CE_TRACE. */ GT_set(Global_getenv("CE_TRACE")); Global_init(); Sem_init(); SemMP_init(); Memory_init(); Queue_init(); Comm_init(); Thread_init(); Processor_init(); LockMP_init(); /* Must be called before DMAN3_init() */ Algorithm_init(); XdmUtils_init(); Lock_init(); Engine_init(); Server_init(); }
/****************************************************************************** * Dmai_init ******************************************************************************/ Void Dmai_init() { if (ti_sdo_dmai_GTMask.modName == NULL) { GT_init(); Global_init(); Memory_init(); Sem_init(); GT_create(&ti_sdo_dmai_GTMask, Dmai_gtname); if (Dmai_debugLogLevel != Dmai_LogLevel_Notset) { switch (Dmai_debugLogLevel) { case 0: GT_set("ti.sdo.dmai="); break; case 1: GT_set("ti.sdo.dmai=67"); break; case 2: GT_set("ti.sdo.dmai=01234567"); break; default: /* Invalid parameter. Do nothing. */ break; } Dmai_dbg1("Dmai log level set to %d.\n", Dmai_debugLogLevel); } } return; }
void init_cache() { /* initialize semaphores */ Sem_init(&conn_ex, 0, 1); Sem_init(&client_ex, 0, 1); Sem_init(&conn_wait, 0, 1); Sem_init(&client_wait, 0, 1); conn_num = 0; client_num = 0; /* initialize cache */ my_cache->size = 0; my_cache->count = 0; my_cache->front = NULL; my_cache->rear = NULL; }
/* include main */ int main(int argc, char **argv) { int i, prodcount[MAXNTHREADS], conscount[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS]; if (argc != 4) err_quit("usage: prodcons4 <#items> <#producers> <#consumers>"); nitems = atoi(argv[1]); nproducers = min(atoi(argv[2]), MAXNTHREADS); nconsumers = min(atoi(argv[3]), MAXNTHREADS); /* 4initialize three semaphores */ Sem_init(&shared.mutex, 0, 1); Sem_init(&shared.nempty, 0, NBUFF); Sem_init(&shared.nstored, 0, 0); /* 4create all producers and all consumers */ Set_concurrency(nproducers + nconsumers); for (i = 0; i < nproducers; i++) { prodcount[i] = 0; Pthread_create(&tid_produce[i], NULL, produce, &prodcount[i]); } for (i = 0; i < nconsumers; i++) { conscount[i] = 0; Pthread_create(&tid_consume[i], NULL, consume, &conscount[i]); } /* 4wait for all producers and all consumers */ for (i = 0; i < nproducers; i++) { Pthread_join(tid_produce[i], NULL); printf("producer count[%d] = %d\n", i, prodcount[i]); } for (i = 0; i < nconsumers; i++) { Pthread_join(tid_consume[i], NULL); printf("consumer count[%d] = %d\n", i, conscount[i]); } Sem_destroy(&shared.mutex); Sem_destroy(&shared.nempty); Sem_destroy(&shared.nstored); exit(0); }
int main() { printf(1, "Combining 2 hydrogen and 1 oxygen\n"); printf(1, "1 water molecule should be created\n\n"); Sem_init(&h, 0); Sem_init(&o, 0); Sem_init(&l, 1); thread_create(hReady,(void*)&water); thread_create(hReady,(void*)&water); thread_create(oReady,(void*)&water); //main waits for threads to exit while(wait() >= 0); exit(); }
int main() { Sem_init(&t, 3); //part_one(); part_two(); exit(); }
/*Intialize the pre-set header content and lock for the cache_list*/ void proxy_init(){ int i; int lengthSet[5] = {86,73,32,19,25}; /* Initial locks to be 1 */ Sem_init(&mutex,0,1); Sem_init(&w,0,1); /* Initial Cache space */ Cache_init(); /* Initial pre-set header */ headerSet = (char**)malloc(5*sizeof(char*)); for(i = 0 ; i < 5 ;i++){ headerSet[i] = (char*)malloc(lengthSet[i]*sizeof(char)); } strcpy(headerSet[0],"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n"); strcpy(headerSet[1],"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"); strcpy(headerSet[2],"Accept-Encoding: gzip, deflate\r\n"); strcpy(headerSet[3],"Connection: close\r\n"); strcpy(headerSet[4],"Proxy-Connection: close\r\n"); }
int main(int argc, char **argv) { int listenfd; char hostname[MAXLINE], port[MAXLINE]; /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } listenfd = Open_listenfd(argv[1]); Sem_init(&list_lock, 0, 1); // init cache's head node head = (struct cache_block*) malloc(sizeof(struct cache_block)); init_cache(head); // block sigpipe Signal(SIGPIPE, SIG_IGN); while (1) { pthread_t tid; // allocate space for a thread arg thread_args* args_ptr = (thread_args*) malloc(sizeof(thread_args)); if (!args_ptr) { printf("malloc failure\n"); continue; } socklen_t clientlen = sizeof(struct sockaddr_storage); printf("Preparing to connect with clients...\n"); args_ptr->fd = Accept(listenfd, (SA *)&args_ptr->socket_addr, &clientlen); if (getnameinfo((SA *) &args_ptr->socket_addr, clientlen, hostname, MAXLINE, port, MAXLINE, 0) != 0) { printf("getnameinfo failure\n"); continue; } printf("Accepted connection from (%s, %s)\n", hostname, port); if (pthread_create(&tid, NULL, thread, args_ptr) != 0) { printf("pthread_create error\n"); continue; } } }
void insert_into_cache(struct cache_header *cache, size_t size, char* data, char* tag){ pthread_rwlock_wrlock(&lock); struct cache_block *new_block = malloc(sizeof(struct cache_block)); if (cache->total_size + size > MAX_CACHE_SIZE) remove_from_cache(cache,size); new_block->next = cache->start; cache->start = new_block; cache->total_size += size; new_block->size = size; new_block->data = malloc(size); memcpy(new_block->data, data, size); new_block->tag = malloc(strlen(tag) + 1); strcpy(new_block->tag,tag); new_block->timestamp = ++cache->currTime; Sem_init(&new_block->mutex, 0, 1); pthread_rwlock_unlock(&lock); }
int main(int argc, char **argv) { int fd = 0, stat, nconflicts; long i, j, nproc; sem_t *ptr; pid_t pid; ssize_t n; if (argc != 2) err_quit("usage: locksvsemrace1 <#processes>"); nproc = atol(argv[1]); Pipe(pipefd); ptr = My_shm(sizeof(sem_t)); /* create memory-based semaphore */ Sem_init(ptr, 1, 0); for (j = 0; j < nproc; j++) { if (Fork() == 0) { /* 4child */ Sem_wait(ptr); /* wait for parent to start children */ for (i = 0; i < 10; i++) { my_lock(fd); /* lock the file */ my_unlock(fd); /* unlock the file */ } exit(0); } /* parent loops around, creating next child */ } for (j = 0; j < nproc; j++) Sem_post(ptr); /* start all the children */ /* now just wait for all the children to finish */ while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) ; Close(pipefd[1]); nconflicts = 0; while ( (n = Read(pipefd[0], &stat, 1)) > 0) nconflicts += n; printf("%d conflicts\n", nconflicts); exit(0); }
int main(int argc, char *argv[]){ int m = 5; int preempt; preempt = Thread_init(1, NULL); assert(preempt == 1); if (argc >= 2){ m = atoi(argv[1]); } n = 0; // increment n unsafely { int i; for (i = 0; i < m; i++){ Thread_new(unsafe, &n, 0, NULL); // 创建m个线程 } Thread_join(NULL); } Fmt_print("%d == %d\n", n, NBUMP * m); n = 0; // increment n safely { int i; struct args args; Sem_T mutex; Sem_init(&mutex, 1); args.mutex = &mutex; args.ip = &n; for (i = 0; i < m; i++){ Thread_new(safe, &args, sizeof(args), NULL); } Thread_join(NULL); } Fmt_print("%d == %d\n", n, NBUMP * m); Thread_exit(EXIT_SUCCESS); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int i, j; int m = argc > 1 ? atoi(argv[1]) : 4; int n = argc > 2 ? atoi(argv[2]) : 3; Thread_init(); Sem_init(&mutex, 1); for (j = 0; j < n; j++) { struct args args; args.mutex = &mutex; args.ip = &y; for (i = 0; i < m; i++) Thread_new((int (*)(void *))incr, &args, sizeof args, NULL); Thread_join(0); if (y != m*10000) printf("%d:%d\n", j, y); y = 0; } Thread_exit(0); return 0; }
void cache_erase(int index) { //初始化所有值 cache[index].turn=0; cache[index].url[0]='\0'; cache[index].data[0]='\0'; Sem_init(&cache[index].t_mutex,0,1); Sem_init(&cache[index].t_w,0,1); cache[index].t_readcnt=0; Sem_init(&cache[index].url_mutex,0,1); Sem_init(&cache[index].url_w,0,1); cache[index].url_readcnt=0; Sem_init(&cache[index].mutex,0,1); Sem_init(&cache[index].w,0,1); cache[index].readcnt=0; }
int main(int argc, char **argv) { int i, nprocs; pid_t childpid[MAXNPROC]; if (argc != 3) err_quit("usage: incr_pxsem9 <#loops> <#processes>"); nloop = atoi(argv[1]); nprocs = min(atoi(argv[2]), MAXNPROC); /* 4get shared memory for parent and children */ shared = My_shm(sizeof(struct shared)); /* 4initialize memory-based semaphore to 0 */ Sem_init(&mutex, 1, 0); /* 4create all the children */ for (i = 0; i < nprocs; i++) { if ( (childpid[i] = Fork()) == 0) { incr(NULL); exit(0); } } /* 4parent: start the timer and release the semaphore */ Start_time(); Sem_post(&mutex); /* 4wait for all the children */ for (i = 0; i < nprocs; i++) { Waitpid(childpid[i], NULL, 0); } printf("microseconds: %.0f usec\n", Stop_time()); if (shared->counter != nloop * nprocs) printf("error: counter = %ld\n", shared->counter); exit(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); }
static void init_echo_cnt(void) { Sem_init(&mutex, 0, 1); byte_cnt = 0; }
int main(int argc, char **argv) { int listenfd, port; struct sockaddr_in clientaddr; socklen_t clientlen; pthread_t tid; int err = 0; memset(fd_q, -1, sizeof(int)*8); /* init mutex, default attri */ err = pthread_mutex_init(&restrict_mutex, NULL); if (err != 0) fprintf(stderr, "pthread_mutex_init failed: %s\n", strerror(err)); Sem_init(&mutex, 0, 1); Sem_init(&mutex_q, 0 , 1); /* Check command line args */ if (argc != 2) { fprintf(stderr, "proxy usage: %s <port>\n", argv[0]); exit(1); } port = atoi(argv[1]); init_cache(); /* ignore SIGPIPE to ignore broken error*/ Signal(SIGPIPE, SIG_IGN); //for (i = 0; i < MAX_THREADS; i++) //Pthread_create(&tid, NULL, proxy_thread, NULL); /* open proxy listen port */ listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); /*thread timing issue could happen between accept and execuing thread Normal: A:fd1->t1(fd1)->A:fd2->t2(fd2) Abnormal: A:fd1->A:fd2->t1(fd2)->t2(fd2) Use Malloc to avoid var race condition */ int *connfd = Malloc(sizeof(int)); *connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //insert_fd_q(connfd); //async task queue Pthread_create(&tid, NULL, proxy_thread, connfd); } Close(listenfd); deinit_cache(); /*destroy mutex*/ sem_destroy(&mutex); sem_destroy(&mutex_q); pthread_mutex_destroy(&restrict_mutex); if (err != 0) fprintf(stderr, "pthread_mutex_destroy failed: %s\n", strerror(err)); return 0; }
/* * ======== CERuntime_init ======== */ Void CERuntime_init(Void) { extern Void IPC_generatedInit(); GT_init(); /* if CE_DEBUG is set, turn on tracing and DSP auto trace collection */ if (Global_getenv("CE_DEBUG") != NULL) { extern Bool Engine_alwaysCollectDspTrace; extern String Engine_ceDebugDspTraceMask; Engine_alwaysCollectDspTrace = TRUE; if (Global_getenv("CE_DEBUG")[0] == '1') { GT_set("*+67,CE-3,GT_time=0,GT_prefix=1235"); Engine_ceDebugDspTraceMask = "*+67,GT_prefix=1235,GT_time=3"; } else if (Global_getenv("CE_DEBUG")[0] == '2') { GT_set( "*+01234567,CE-3,ti.sdo.ce.osal.SemMP=67,OG=467,OM=4567,OC=67,GT_time=0,GT_prefix=1235"); Engine_ceDebugDspTraceMask = "*+01234567,CR=67,ti.sdo.fc.dman3-2,ti.sdo.fc.dskt2-2,GT_prefix=1235,GT_time=3"; } else { GT_set("*+01234567,CE-3,GT_time=0,GT_prefix=12345"); Engine_ceDebugDspTraceMask = "*+01234567,GT_prefix=12345,GT_time=3"; } } if (Global_getenv("CE_CHECK") != NULL) { extern Bool VISA_checked; /* * Currently just change _this_ processor's value... perhaps we should * enable remote processors as well? */ if (Global_getenv("CE_CHECK")[0] == '1') { VISA_checked = TRUE; /* turn on all GT_7CLASS trace (errors) */ GT_set("*+7"); } else if (Global_getenv("CE_CHECK")[0] == '0') { VISA_checked = FALSE; } else { /* leave it whatever it was... maybe we should drop a warning? */ } } /* allow user to over-ride via CE_TRACE. */ GT_set(Global_getenv("CE_TRACE")); IPC_generatedInit(); Global_init(); Sem_init(); SemMP_init(); Memory_init(); Queue_init(); Comm_init(); Thread_init(); Processor_init(); LockMP_init(); /* Must be called before DMAN3_init() */ Algorithm_init(); XdmUtils_init(); Lock_init(); Engine_init(); Server_init(); }