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

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


Category

  ±è¿µ´ë(2003-03-17 01:35:29, Hit : 6176, Vote : 1633
 [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 6769 1736
41   [Unix/Linux] [System V IPC] shmop() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 6649 1621
40   [Unix/Linux] [System V IPC] msgctl() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6604 1745
39   [Unix/Linux] [Thread] pthread_mutexattr_init() function  ±è¿µ´ë 2003/03/17 6554 1972
38   [Unix/Linux] [System V IPC] msgget() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6500 1659
37   [Unix/Linux] [System V IPC] semctl() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 6272 1450
36   [Unix/Linux] [Thread] pthread_once() function  ±è¿µ´ë 2003/03/17 6267 1632
  [Unix/Linux] [Thread] »ý»êÀÚ/¼ÒºñÀÚ  ±è¿µ´ë 2003/03/17 6176 1633
34   [Unix/Linux] [Thread] pthread_cond_init() function  ±è¿µ´ë 2003/03/17 6130 1606
33   [Unix/Linux] [Thread] pthread_attr_destroy() function  ±è¿µ´ë 2003/03/17 6041 1595
32   [Unix/Linux] [Thread] pthread_attr_setdetachstate() function  ±è¿µ´ë 2003/03/17 6041 1663
31   [Unix/Linux] [Thread] pthread_mutex_init() function  ±è¿µ´ë 2003/03/17 6028 1494
30   [Unix/Linux] [Thread] pthread_detach() function  ±è¿µ´ë 2003/03/17 5971 1517
29   [Unix/Linux] [System V IPC] semop() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 5955 1492
28   [Unix/Linux] [Thread] pthread_attr_setinheritsched() function  ±è¿µ´ë 2003/03/17 5907 1556
27   [Unix/Linux] [Thread] pthread_key_create() function  ±è¿µ´ë 2003/03/17 5886 1452
26   [Unix/Linux] [Thread] pthread_create() function  ±è¿µ´ë 2003/03/17 5831 1456
25   [Unix/Linux] [Thread] pthread_muxtexcond() function  ±è¿µ´ë 2003/03/17 5818 1501
24   [Unix/Linux] [Thread] pthread_self() function  ±è¿µ´ë 2003/03/17 5713 1579
23   [Unix/Linux] [Thread] pthread_attr_getdetachstate() function  ±è¿µ´ë 2003/03/17 5645 1681

[1] 2 [3][4]
 

Copyright 1999-2025 Zeroboard / skin by zero