::: °­ÁÂ/¼Ò½º/¹®¼­ :::

°­ÁÂ/¼Ò½º/¹®¼­ ¼º°Ý¿¡ ¸ÂÁö ¾Ê´Â ±¤°í,ºñ¹æ,Áú¹®ÀÇ ±ÛÀº Áï½Ã »èÁ¦Çϸç
³»¿ëÀ» º¹»çÇÏ¿© »ç¿ëÇÒ °æ¿ì ¹Ýµå½Ã ÀÌ°÷(http://www.howto.pe.kr)À» Ãâó·Î ¸í½ÃÇÏ¿© ÁÖ¼¼¿ä


Category

  ±è¿µ´ë(2003-03-17 01:35:29, Hit : 6053, Vote : 1548
 [Thread] »ý»êÀÚ/¼ÒºñÀÚ

/***************************************************
*--------------------------------------------------
*   Creat  : 2001. 04. 10  (programed by Cori-Young )
*   Site: http://www.byoneself.co.kr
*   Update :
*--------------------------------------------------
*   Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware:   sun4u
* OS version:         5.7
* Processor type:     sparc
* Hardware:           SUNW,Ultra-60
***************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>

#define QUEUESIZE 10
#define LOOP 20

void *producer (void *args);
void *consumer (void *args);

typedef struct {
               int buf[QUEUESIZE];
               long head, tail;
              int full, empty;
               pthread_mutex_t *mut;
               pthread_cond_t *notFull, *notEmpty;
} queue;

queue *queueInit (void);
void queueDelete (queue *q);
void queueAdd (queue *q, int in);
void queueDel (queue *q, int *out);

int main ()
{
               queue *fifo;
               pthread_t pro, con;

               fifo = queueInit ();
               if (fifo ==  NULL) {
                              fprintf (stderr, "main: Queue Init failed.n");
                              exit (1);
               }
               pthread_create (&pro, NULL, producer, fifo);
               pthread_create (&con, NULL, consumer, fifo);
               pthread_join (pro, NULL);
               pthread_join (con, NULL);
               queueDelete (fifo);

               return 0;
}

void *producer (void *q)
{
               queue *fifo;
               int i;

               fifo = (queue *)q;

               for (i = 0; i < LOOP; i++) {
                              pthread_mutex_lock (fifo->mut);
                              while (fifo->full) {
                                       printf ("producer: queue FULL.n");
                                       pthread_cond_wait (fifo->notFull, fifo->mut);
                              }
                              queueAdd (fifo, i);
                              pthread_mutex_unlock (fifo->mut);
                              pthread_cond_signal (fifo->notEmpty);
                              usleep (100000);
               }
               for (i = 0; i < LOOP; i++) {
                              pthread_mutex_lock (fifo->mut);
                              while (fifo->full) {
                                       printf ("producer: queue FULL.n");
                                       pthread_cond_wait (fifo->notFull, fifo->mut);
                              }
                              queueAdd (fifo, i);
                              pthread_mutex_unlock (fifo->mut);
                              pthread_cond_signal (fifo->notEmpty);
                              usleep (200000);
               }
               return (NULL);
}

void *consumer (void *q)
{
               queue *fifo;
               int i, d;

               fifo = (queue *)q;

               for (i = 0; i < LOOP; i++) {
                              pthread_mutex_lock (fifo->mut);
                              while (fifo->empty) {
                                       printf ("consumer: queue EMPTY.n");
                                       pthread_cond_wait (fifo->notEmpty, fifo->mut);
                              }
                              queueDel (fifo, &d);
                              pthread_mutex_unlock (fifo->mut);
                              pthread_cond_signal (fifo->notFull);
                              printf ("consumer: recieved %d.n", d);
                              usleep(200000);
               }
               for (i = 0; i < LOOP; i++) {
                              pthread_mutex_lock (fifo->mut);
                              while (fifo->empty) {
                                       printf ("consumer: queue EMPTY.n");
                                       pthread_cond_wait (fifo->notEmpty, fifo->mut);
                              }
                              queueDel (fifo, &d);
                              pthread_mutex_unlock (fifo->mut);
                              pthread_cond_signal (fifo->notFull);
                              printf ("consumer: recieved %d.n", d);
                              usleep (50000);
               }
               return (NULL);
}

#ifdef 0
typedef struct {
               int buf[QUEUESIZE];
               long head, tail;
              int full, empty;
               pthread_mutex_t *mut;
               pthread_cond_t *notFull, *notEmpty;
} queue;
#endif

queue *queueInit (void)
{
               queue *q;

               q = (queue *)malloc (sizeof (queue));
               if (q == NULL) return (NULL);

               q->empty = 1;
               q->full = 0;
               q->head = 0;
               q->tail = 0;
               q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
               pthread_mutex_init (q->mut, NULL);
               q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
               pthread_cond_init (q->notFull, NULL);
               q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
               pthread_cond_init (q->notEmpty, NULL);
              
               return (q);
}

void queueDelete (queue *q)
{
               pthread_mutex_destroy (q->mut);
               free (q->mut);  
               pthread_cond_destroy (q->notFull);
               free (q->notFull);
               pthread_cond_destroy (q->notEmpty);
               free (q->notEmpty);
               free (q);
}

void queueAdd (queue *q, int in)
{
               q->buf[q->tail] = in;
               q->tail++;
               if (q->tail == QUEUESIZE)
                              q->tail = 0;
               if (q->tail == q->head)
                              q->full = 1;
               q->empty = 0;

               return;
}

void queueDel (queue *q, int *out)
{
               *out = q->buf[q->head];

               q->head++;
               if (q->head == QUEUESIZE)
                              q->head = 0;
               if (q->head == q->tail)
                              q->empty = 1;
               q->full = 0;

               return;
}





42   [Unix/Linux] [Thread] pthread_cond() function  ±è¿µ´ë 2003/03/17 6626 1688
41   [Unix/Linux] [System V IPC] shmop() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 6520 1580
40   [Unix/Linux] [System V IPC] msgctl() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6474 1701
39   [Unix/Linux] [Thread] pthread_mutexattr_init() function  ±è¿µ´ë 2003/03/17 6445 1937
38   [Unix/Linux] [System V IPC] msgget() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6377 1593
37   [Unix/Linux] [System V IPC] semctl() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 6134 1411
36   [Unix/Linux] [Thread] pthread_once() function  ±è¿µ´ë 2003/03/17 6118 1602
  [Unix/Linux] [Thread] »ý»êÀÚ/¼ÒºñÀÚ  ±è¿µ´ë 2003/03/17 6053 1548
34   [Unix/Linux] [Thread] pthread_cond_init() function  ±è¿µ´ë 2003/03/17 6010 1567
33   [Unix/Linux] [Thread] pthread_mutex_init() function  ±è¿µ´ë 2003/03/17 5920 1457
32   [Unix/Linux] [Thread] pthread_attr_setdetachstate() function  ±è¿µ´ë 2003/03/17 5919 1619
31   [Unix/Linux] [Thread] pthread_attr_destroy() function  ±è¿µ´ë 2003/03/17 5905 1556
30   [Unix/Linux] [Thread] pthread_detach() function  ±è¿µ´ë 2003/03/17 5828 1470
29   [Unix/Linux] [Thread] pthread_attr_setinheritsched() function  ±è¿µ´ë 2003/03/17 5808 1535
28   [Unix/Linux] [System V IPC] semop() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 5770 1436
27   [Unix/Linux] [Thread] pthread_key_create() function  ±è¿µ´ë 2003/03/17 5762 1419
26   [Unix/Linux] [Thread] pthread_muxtexcond() function  ±è¿µ´ë 2003/03/17 5725 1475
25   [Unix/Linux] [Thread] pthread_create() function  ±è¿µ´ë 2003/03/17 5682 1415
24   [Unix/Linux] [Thread] pthread_self() function  ±è¿µ´ë 2003/03/17 5588 1547
23   [Unix/Linux] [Thread] pthread_attr_getdetachstate() function  ±è¿µ´ë 2003/03/17 5533 1642

[1] 2 [3][4]
 

Copyright 1999-2024 Zeroboard / skin by zero