Example #1
0
/* include main */
int
main(int argc, char **argv)
{
	int			i, nthreads, count[MAXNTHREADS];
	pthread_t	tid_produce[MAXNTHREADS], tid_consume;

	if (argc != 3)
		err_quit("usage: prodcons4 <#items> <#threads>");
	nitems = min(atoi(argv[1]), MAXNITEMS);
	nthreads = min(atoi(argv[2]), MAXNTHREADS);

	Set_concurrency(nthreads + 1);
		/* 4create all producers and one consumer */
	for (i = 0; i < nthreads; i++) {
		count[i] = 0;
		Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
	}
	Pthread_create(&tid_consume, NULL, consume, NULL);

		/* wait for all producers and the consumer */
	for (i = 0; i < nthreads; i++) {
		Pthread_join(tid_produce[i], NULL);
		printf("count[%d] = %d\n", i, count[i]);	
	}
	Pthread_join(tid_consume, NULL);

	exit(0);
}
Example #2
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);
}
Example #3
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);
}
Example #4
0
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);
}
Example #5
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);
}
Example #6
0
int main(int argc, char *argv[])
{
  int i, nthreads, count[MAXNTHREADS];
  pthread_t tid_produce[MAXNTHREADS], tid_consume;
  
  if(argc != 3)
    err_quit("usage: prodcons2 <#items> <#threads>");
  nitems = min(atoi(argv[1]), MAXNITEMS);
  
  Set_concurrency(nthreads + 1);/*线程并发设置*/

	       /*创建线程生产者*/
  for(i = 0; i < nthreads; ++i){
    count[i] = 0;
    Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
  }
	       /*创建消费者线程*/
  Pthread_create(&tid_consume, NULL, comsume, NULL);
	       /*主线程等待子线程结束*/
  for(i = 0; i < nthreads; ++i){
  Pthread_join(tid_produce[i], NULL);
  printf ("count[%d] = %d\n",i, count[i]);
  }
  Pthread_join(tid_consume, NULL);

  return 0;
}
Example #7
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);
}
Example #8
0
int
main(int argc, char *argv[])
{                    
    if (argc != 2) {
	fprintf(stderr, "usage: main-first <loopcount>\n");
	exit(1);
    }
    max = atoi(argv[1]);

    pthread_t p1, p2;
    printf("main: begin [counter = %d] [%x]\n", counter, 
	   (unsigned int) &counter);

    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;;
    myarg args;
    args.a="A";
    args.b=lock;

    myarg args1;
    args1.a="B";
    args1.b=lock;

    Pthread_create(&p1, NULL, mythread, &args); 
    Pthread_create(&p2, NULL, mythread, &args1);
    // join waits for the threads to finish
    Pthread_join(p1, NULL); 
    Pthread_join(p2, NULL); 
    printf("main: done\n [counter: %d]\n [should: %d]\n", 
	   counter, max*2);
    return 0;
}
Example #9
0
/* include main */
int
main(int argc, char **argv)
{
	int			i, nproducers, nconsumers, countp[MAXNTHREADS], countc[MAXNTHREADS];
	pthread_t	tid_produce[MAXNTHREADS], tid_consume[MAXNTHREADS];

	if (argc != 4)
		err_quit("usage: prodcons6 <#items> <#producers> <#consumers>");
	nitems = min(atoi(argv[1]), MAXNITEMS);
	nproducers = min(atoi(argv[2]), MAXNTHREADS);
	nconsumers = min(atoi(argv[3]), MAXNTHREADS);

	Set_concurrency(nproducers + nconsumers);
		/* 4create all producers and one consumer */
	for (i = 0; i < nproducers; i++) {
		countp[i] = 0;
		Pthread_create(&tid_produce[i], NULL, produce, &countp[i]);
	}
	for (i = 0; i < nconsumers; i++) {
		countc[i] = 0;
		Pthread_create(&tid_consume[i], NULL, consume, &countc[i]);
	}

		/* wait for all producers and the consumer */
	for (i = 0; i < nproducers; i++) {
		Pthread_join(tid_produce[i], NULL);
		printf("countp[%d] = %d\n", i, countp[i]);	
	}
	for (i = 0; i < nconsumers; i++) {
		Pthread_join(tid_consume[i], NULL);
		printf("countc[%d] = %d\n", i, countc[i]);
	}

	exit(0);
}
Example #10
0
int
main(int argc, char *argv[])
{
    int     i, nthreads, count[MAXNITEMS];
    pthread_t     tid_produce[MAXNTHREADS], tid_consume;

    if (argc != 3)
        err_quit("usage: producons2 <#items> <#threads>");
    nitems = min(atoi(argv[1]), MAXNITEMS);
    nthreads= min(atoi(argv[2]), MAXNTHREADS);

    Set_concurrency(nthreads);

    for (i = 0; i < nthreads; i++) {
        count[i] = 0;
        Pthread_create(&tid_produce[i], NULL, produce, &count[i]); 
    }

    for (i = 0; i < nthreads; i++) {
        Pthread_join(tid_produce[i], NULL);
        printf("count[%d] = %d \n", i, count[i]);
    }

    Pthread_create(&tid_consume, NULL, consume, NULL);
    Pthread_join(tid_consume, NULL);

    return 0;
}
Example #11
0
int main(int argc, char **argv)
{
  int niters;
  pthread_t tid1, tid2;
  
  //check input argument
  if (argc != 2) {
    printf("usage:  %s <niters>\n", argv[0] );
    exit(0);
  }
  niters = atoi(argv[1]);
  
  //create threads and wait for them to finish
  Pthread_create(&tid1, NULL, thread, &niters);
  Pthread_create(&tid2, NULL, thread, &niters);
  Pthread_join(tid1, NULL);
  Pthread_join(tid2, NULL);
  
  //check result
  if (cnt != (2 * niters))
    printf("BOOM! cnt=%d\n", cnt);
  else 
    printf("OK cnt=%d\n", cnt);
  
  exit(0);
}
Example #12
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;
}
Example #13
0
int main(int argc, char **argv)
{
	int i, nthreads, count[MAXNTHREADS];
	pthread_t tid_produce[MAXNTHREADS], tid_consume;

	if (argc != 3)
		err_quit("Usage: prodcons2 <#items> <#threads>");
	nitems = min(atoi(argv[1]), MAXNITEMS);
    nthreads = min(atoi(argv[2]), MAXNTHREADS);
	
	Pthread_setconcurrency(nthreads);

	// start all the producer threads
	for (i = 0; i < nthreads; i++)
	{
		count[i] = 0;
		Pthread_create(&tid_produce[i], NULL, produce, &count[i]);
	}

	//wait for all the producer threads
	for (i = 0; i < nthreads; i++)
	{
		Pthread_join(tid_produce[i], NULL);
		printf("count[%d] = %d\n", i, count[i]);
	}

	//start then wait for the consumer thread
	Pthread_create(&tid_consume, NULL, consume, NULL);
	Pthread_join(tid_consume, NULL);

	exit(0);
}
Example #14
0
int main(int argc, char **argv)
{
    pthread_t	tidA, tidB;

    Pthread_create(&tidA, NULL, &doit, NULL);
    Pthread_create(&tidB, NULL, &doit, NULL);

    /* 4wait for both threads to terminate */
    Pthread_join(tidA, NULL);
    Pthread_join(tidB, NULL);

    exit(0);
}
int main(int argc, char *argv[]){
    if(argc!=2){
        fprintf(stderr, "parameter useage: theread<value>\n");
        exit(1);
    }
    loops = atoi(argv[1]);
    pthread_t p1,p2;
    printf("Initial value of Counter : %d\n",counter);
    Pthread_create(&p1,NULL,worker,NULL);
    Pthread_create(&p2,NULL,worker,NULL);
    Pthread_join(p1,NULL);
    Pthread_join(p2,NULL);
    printf("Final value of Counter : %d\n",counter);
    return 0;
}
Example #16
0
int main() 
{
    pthread_t tid1, tid2;

    Pthread_create(&tid1, NULL, count, NULL);
    Pthread_create(&tid2, NULL, count, NULL);
    Pthread_join(tid1, NULL);
    Pthread_join(tid2, NULL);

    if (cnt != (unsigned)NITERS*2)
	printf("BOOM! cnt=%d\n", cnt);
    else
	printf("OK cnt=%d\n", cnt);
    exit(0);
}
Example #17
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 #18
0
int main()                                    //line:conc:hello:main
{
    pthread_t tid;                            //line:conc:hello:tid
    Pthread_create(&tid, NULL, thread, NULL); //line:conc:hello:create
    Pthread_join(tid, NULL);                  //line:conc:hello:join
    exit(0);                                  //line:conc:hello:exit
}
Example #19
0
int
main(int argc, char **argv)
{
	int		i, nthreads;
	pthread_t	tid[MAXNTHREADS];

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

		/* 4obtain write lock */
	Rw_wrlock(&shared.rwlock);

		/* 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 write lock */
	Start_time();
	Rw_unlock(&shared.rwlock);

		/* 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 #20
0
void sendscl_tcps()
{
    int tcpfd;
    void *res;
    newconn_t *conn;

    thread_num = 0;

    tcpfd = tcp_listen(NULL, sendg.scalar_port, NULL);

    for(; ;) {
        conn = (newconn_t *)Malloc(sizeof(newconn_t));
        conn->fd = Accept(tcpfd, (SA *)&conn->cliaddr, &conn->len);

        // todo, can not support multi threads now, because
        // can not get data very correctly using multi-threads.
        // so only allow one thread to be existed .
        if(thread_num != 0) {
            Pthread_cancel(tid);
            Pthread_join(tid, &res);
            thread_num = 0;
            Close(last_connfd);
        }
        Pthread_create(&tid, NULL, sendscl_tcps_conn, conn);
        thread_num = 1;
        last_connfd = conn->fd;
    }
}
Example #21
0
/*
 * scheduler -
 */
void scheduler(int client_fd) {
    pthread_t tid;

    Thread_Input *thread_input = (Thread_Input*)malloc(sizeof(Thread_Input));
    thread_input->client_fd = client_fd;

    Pthread_create(&tid, NULL, request_handler, thread_input);
    Pthread_join(tid, NULL);
    //Pthread_detach(tid);
}
Example #22
0
int
main(int argc, char **argv)
{
	int n;
	pthread_t t, t1, t2;
	pthread_attr_t attr;

	Pthread_attr_init(&attr);

	Pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

	Pthread_create(&t, &attr, SumThread, NULL);
	Pthread_create(&t1, &attr, InsThread, NULL);
	Pthread_create(&t2, &attr, InsThread, NULL);

	Pthread_join(t1, NULL);
	Pthread_join(t2, NULL);
	return 0;
}
Example #23
0
/* 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);
}
Example #24
0
int
main( int argc, char **argv)
{
	pthread_t	tidA, tidB, tidC, tidD;

	Pthread_create( &tidA, NULL, &doit, NULL);
	Pthread_create( &tidB, NULL, &doit, NULL);
        Pthread_create( &tidC, NULL, &doit, NULL);
        Pthread_create( &tidD, NULL, &doit, NULL);

	/* wait for both threads to terminate */
	Pthread_join( tidA, NULL);
	Pthread_join( tidB, NULL);
        Pthread_join( tidC, NULL);
        Pthread_join( tidD, NULL);
        
        printf( "counter: %d\n", counter);

	exit(0);
}
Example #25
0
int main() 
{
    pthread_t tid[N];
    int i;

    for (i = 0; i < N; i++) 
	Pthread_create(&tid[i], NULL, thread, &i); //line:conc:race:createthread
    for (i = 0; i < N; i++) 
	Pthread_join(tid[i], NULL);
    exit(0);
}
Example #26
0
int
main(int argc, char *argv[])
{                    
    if (argc != 2) {
	fprintf(stderr, "usage: main-first <loopcount>\n");
	exit(1);
    }
    max = atoi(argv[1]);

    pthread_t p1, p2;
    printf("main: begin [balance = %d]\n", balance);
    Pthread_create(&p1, NULL, mythread, "A"); 
    Pthread_create(&p2, NULL, mythread, "B");
    // join waits for the threads to finish
    Pthread_join(p1, NULL); 
    Pthread_join(p2, NULL); 
    printf("main: done\n [balance: %d]\n [should: %d]\n", 
	   balance, max*2);
    return 0;
}
Example #27
0
int main()
{
	pthread_t tid1, tid2;

	Pthread_rwlock_wrlock(&rwlock);	/* parent read locks entire file */
	printf("%s: parent has write lock\n", gf_time());

	Pthread_create(&tid1, NULL, thread1, NULL);
	Pthread_create(&tid2, NULL, thread2, NULL);

	/* 4parent */
	sleep(5);
	Pthread_rwlock_unlock(&rwlock);
	printf("%s: parent releases write lock\n", gf_time());

	Pthread_join(tid1, NULL);
	Pthread_join(tid2, NULL);

	exit(0);
}
Example #28
0
int main() {
	pthread_t tid;

	printf("Create a peer thread\n");
	Pthread_create(&tid, NULL, thread, NULL);

	printf("Waiting for the peer thread exits\n");
	Pthread_join(tid, NULL);

	printf("Exit the main thread\n");
	exit(0);
}
Example #29
0
int main(int argc, char **argv)
{
	clock_t start_point, end_point;
	int niters = atoi(argv[1]);
	pthread_t tid1, tid2;

	start_point = clock();
	Pthread_create(&tid1, NULL, thread, &niters);
	Pthread_create(&tid2, NULL, thread, &niters);
	Pthread_join(tid1, NULL);
	Pthread_join(tid2, NULL);

	/* Check result */
	if (cnt != (2 * niters))
		printf("BOOM! cnt=%d\n", cnt);
	else
		printf("OK cnt=%d\n", cnt);
	end_point = clock();

	printf("Exe time : %f sec\n", ((double)(end_point - start_point)/CLOCKS_PER_SEC));

	exit(0);
}
Example #30
0
int main() 
{
    pthread_t tid[N];
    int i, *ptr;

    for (i = 0; i < N; i++) {
        ptr = Malloc(sizeof(int));                    //line:conc:norace:createthread1
        *ptr = i;                                     //line:conc:norace:createthread2
        Pthread_create(&tid[i], NULL, thread, ptr);   //line:conc:norace:createthread3
    } //line:conc:norace:endloop
    for (i = 0; i < N; i++) 
        Pthread_join(tid[i], NULL);
    exit(0);
}