::: 강좌/소스/문서 :::

강좌/소스/문서 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  김영대(2003-03-17 01:33:50, Hit : 6709, Vote : 1491
 [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 MAXCOUNT 5

#define READER1  50000
#define READER2 100000
#define READER3      400000
#define READER4 800000
#define WRITER1  150000

typedef struct {
               pthread_mutex_t *mut;
               int writers;
               int readers;
               int waiting;
               pthread_cond_t *writeOK, *readOK;
} rwl;

rwl *initlock (void);
void readlock (rwl *lock, int d);
void writelock (rwl *lock, int d);
void readunlock (rwl *lock);
void writeunlock (rwl *lock);
void deletelock (rwl *lock);

typedef struct {
               rwl *lock;
               int id;
               long delay;
} rwargs;

rwargs *newRWargs (rwl *l, int i, long d);
void *reader (void *args);
void *writer (void *args);

static int data = 1;

int main ()
{
               pthread_t r1, r2, r3, r4, w1;
               rwargs *a1, *a2, *a3, *a4, *a5;
               rwl *lock;

               lock = initlock ();
               a1 = newRWargs (lock, 1, WRITER1);
               pthread_create (&w1, NULL, writer, a1);
               a2 = newRWargs (lock, 1, READER1);
               pthread_create (&r1, NULL, reader, a2);
               a3 = newRWargs (lock, 2, READER2);
               pthread_create (&r2, NULL, reader, a3);
               a4 = newRWargs (lock, 3, READER3);
               pthread_create (&r3, NULL, reader, a4);
               a5 = newRWargs (lock, 4, READER4);
               pthread_create (&r4, NULL, reader, a5);
               pthread_join (w1, NULL);
               pthread_join (r1, NULL);
               pthread_join (r2, NULL);
               pthread_join (r3, NULL);
               pthread_join (r4, NULL);
               free (a1); free (a2); free (a3); free (a4); free (a5);

               return 0;
}

rwargs *newRWargs (rwl *l, int i, long d)
{
               rwargs *args;

               args = (rwargs *)malloc (sizeof (rwargs));
               if (args == NULL) return (NULL);
               args->lock = l; args->id = i; args->delay = d;
               return (args);
}

void *reader (void *args)
{
               rwargs *a;
               int d;

               a = (rwargs *)args;

               do {
                               readlock (a->lock, a->id);
                               d = data;
                               usleep (a->delay);
                               readunlock (a->lock);
                               printf ("Reader %d : Data = %dn", a->id, d);
                               usleep (a->delay);
               } while (d != 0);
               printf ("Reader %d: Finished.n", a->id);

               return (NULL);
}

void *writer (void *args)
{
               rwargs *a;
               int i;

               a = (rwargs *)args;

               for (i = 2; i < MAXCOUNT; i++) {
                               writelock (a->lock, a->id);
                               data = i;
                               usleep (a->delay);
                               writeunlock (a->lock);
                               printf ("Writer %d: Wrote %dn", a->id, i);
                               usleep (a->delay);
               }
               printf ("Writer %d: Finishing...n", a->id);
               writelock (a->lock, a->id);
               data = 0;
               writeunlock (a->lock);
               printf ("Writer %d: Finished.n", a->id);

               return (NULL);
}

rwl *initlock (void)
{
               rwl *lock;

               lock = (rwl *)malloc (sizeof (rwl));
               if (lock == NULL) return (NULL);
               lock->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
               if (lock->mut == NULL) { free (lock); return (NULL); }
               lock->writeOK =
                               (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
               if (lock->writeOK == NULL) { free (lock->mut); free (lock);
                               return (NULL); }
               lock->readOK =
                               (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
               if (lock->writeOK == NULL) { free (lock->mut); free (lock->writeOK);
                               free (lock); return (NULL); }
              
               pthread_mutex_init (lock->mut, NULL);
               pthread_cond_init (lock->writeOK, NULL);
               pthread_cond_init (lock->readOK, NULL);
               lock->readers = 0;
               lock->writers = 0;
               lock->waiting = 0;

               return (lock);
}

void readlock (rwl *lock, int d)
{
               pthread_mutex_lock (lock->mut);
               if (lock->writers || lock->waiting) {
                               do {
                                              printf ("reader %d blocked.n", d);
                                              pthread_cond_wait (lock->readOK, lock->mut);
                                              printf ("reader %d unblocked.n", d);
                               } while (lock->writers);
               }
               lock->readers++;
               pthread_mutex_unlock (lock->mut);

               return;
}

void writelock (rwl *lock, int d)
{
               pthread_mutex_lock (lock->mut);
               lock->waiting++;
               while (lock->readers || lock->writers) {
                               printf ("writer %d blocked.n", d);
                               pthread_cond_wait (lock->writeOK, lock->mut);
                               printf ("writer %d unblocked.n", d);
               }
               lock->waiting--;
               lock->writers++;
               pthread_mutex_unlock (lock->mut);

               return;
}

void readunlock (rwl *lock)
{
               pthread_mutex_lock (lock->mut);
               lock->readers--;
               pthread_cond_signal (lock->writeOK);
               pthread_mutex_unlock (lock->mut);
}

void writeunlock (rwl *lock)
{
               pthread_mutex_lock (lock->mut);
               lock->writers--;
               pthread_cond_broadcast (lock->readOK);
               pthread_mutex_unlock (lock->mut);
}

void deletelock (rwl *lock)
{
               pthread_mutex_destroy (lock->mut);
               pthread_cond_destroy (lock->readOK);
               pthread_cond_destroy (lock->writeOK);
               free (lock);

               return;
}





62   [Unix/Linux] [소스] top for SunOS 5.x (Solaris 2.x)  김영대 2004/02/20 15380 1324
61   [Unix/Linux] [강좌] 인텔 센트리노 무선랜 카드: ndiswrapper  김영대 2004/06/27 12425 2178
60   [Unix/Linux] [POSIX IPC] mq_send() function - 메세지큐  김영대 2003/03/17 12261 1698
59   [Unix/Linux] [System V IPC] shmget() function - 공유메모리  김영대 2003/03/17 11057 1786
58   [Unix/Linux] [Thread] pthread_cond_timedwait() function  김영대 2003/03/17 11008 1859
57   [Unix/Linux] [Thread] pthread_setschedparam() function  김영대 2003/03/17 10777 1865
56   [Unix/Linux] [소스] top for System V Release 4, Intel or Sparc CPU  김영대 2004/02/20 10708 1283
55   [Unix/Linux] [System V IPC] shmctl() function - 공유메모리  김영대 2003/03/17 9774 1687
54   [Unix/Linux] [강좌] Apache + MySQL + PHP4 + Zend Optimizer 설치  김영대 2003/04/15 8566 1481
53   [Unix/Linux] [강좌] 웹로그분석을 위한 Webalizer + GDlib + PNGlib + Zlib 설치  김영대 2003/05/04 8419 1510
52   [Unix/Linux] [System V IPC] msgsnd() function - 메세지큐  김영대 2003/03/17 8254 1918
51   [Unix/Linux] [POSIX IPC] mq_receive() function - 메세지큐  김영대 2003/03/17 8163 1815
50   [Unix/Linux] [POSIX IPC] 생산자/소비자 - 세마포어  김영대 2003/03/17 8007 5676
49   [Unix/Linux] [Thread] pthread_cancel() function  김영대 2003/03/17 7987 3787
48   [Unix/Linux] [소스] String 해쉬(hash) 함수  김영대 2003/07/29 7942 1565
47   [Unix/Linux] [Thread] pthread_attr_getschedparam() function  김영대 2003/03/17 6949 8208
46   [Unix/Linux] [System V IPC] msgrcv() function - 메세지큐  김영대 2003/03/17 6927 1692
  [Unix/Linux] [Thread] 읽고/쓰기  김영대 2003/03/17 6709 1491
44   [Unix/Linux] [System V IPC] semget() function - 세마포어  김영대 2003/03/17 6622 1655
43   [Unix/Linux] [Thread] pthread_cond() function  김영대 2003/03/17 6561 1687

1 [2][3][4]
 

Copyright 1999-2024 Zeroboard / skin by zero