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

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


Category

  ±è¿µ´ë(2003-03-17 02:32:06, Hit : 7865, Vote : 5646
 [POSIX IPC] »ý»êÀÚ/¼ÒºñÀÚ - ¼¼¸¶Æ÷¾î

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

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

sem_t produced, consumed; /* semaphores */
int n;
main()
{
            pthread_t idprod, idcons; /* ids of threads */
            void *produce(void *);
            void *consume(void *);
            int loopcnt = 5;

            n = 0;
            
            if (sem_init(&consumed, 0, 0) < 0)
            {
                perror("sem_init");
                exit(1);
            }
            if (sem_init(&produced, 0, 1) < 0)
            {
                perror("sem_init");
                exit(1);
            }
            if (pthread_create(&idprod, NULL, produce, (void *)loopcnt) != 0)
            {
                perror("pthread_create");
                exit(1);
            }
            
            if (pthread_create(&idcons, NULL, consume, (void *)loopcnt) != 0)
            {
                perror("pthread_create");
                exit(1);
            }
            (void)pthread_join(idprod, NULL);
            (void)pthread_join(idcons, NULL);
            (void)sem_destroy(&produced);
            (void)sem_destroy(&consumed);
}        

void *produce(void *arg)
{
            int i, loopcnt;
            loopcnt = (int)arg;
            
            for (i=0; i<loopcnt; i++)
            {
                sem_wait(&consumed);
                n++;  /* increment n by 1 */
                sleep(1);
                sem_post(&produced);
        
            }
}      

void *consume(void *arg)
{
            int i, loopcnt;
            loopcnt = (int)arg;
            
            for (i=0; i<loopcnt; i++)
            {
                sem_wait(&produced);
                printf("n is %dn", n); /* print value of n */
                sem_post(&consumed);
    }
}





62   [Unix/Linux] [°­ÁÂ] ÀÎÅÚ ¼¾Æ®¸®³ë ¹«¼±·£ Ä«µå: ndiswrapper  ±è¿µ´ë 2004/06/27 12244 2143
61   [Unix/Linux] [¼Ò½º] top for System V Release 4, Intel or Sparc CPU  ±è¿µ´ë 2004/02/20 10461 1266
60   [Unix/Linux] [¼Ò½º] top for SunOS 5.x (Solaris 2.x)  ±è¿µ´ë 2004/02/20 15247 1314
59   [Unix/Linux] [¼Ò½º] String ÇØ½¬(hash) ÇÔ¼ö  ±è¿µ´ë 2003/07/29 7837 1540
58   [Unix/Linux] [°­ÁÂ] À¥·Î±×ºÐ¼®À» À§ÇÑ Webalizer + GDlib + PNGlib + Zlib ¼³Ä¡  ±è¿µ´ë 2003/05/04 8263 1493
57   [Unix/Linux] [°­ÁÂ] Apache + MySQL + PHP4 + Zend Optimizer ¼³Ä¡  ±è¿µ´ë 2003/04/15 8326 1451
56   [Unix/Linux] [System V IPC] shmop() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 6328 1521
55   [Unix/Linux] [System V IPC] shmctl() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 9569 1674
54   [Unix/Linux] [System V IPC] shmget() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 10837 1764
53   [Unix/Linux] [System V IPC] semop() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 5517 1405
52   [Unix/Linux] [System V IPC] semctl() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 5898 1388
51   [Unix/Linux] [System V IPC] semget() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 6467 1637
50   [Unix/Linux] [System V IPC] msgget() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6176 1555
49   [Unix/Linux] [System V IPC] msgctl() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6247 1448
48   [Unix/Linux] [System V IPC] msgrcv() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6772 1679
47   [Unix/Linux] [System V IPC] msgsnd() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 8097 1908
  [Unix/Linux] [POSIX IPC] »ý»êÀÚ/¼ÒºñÀÚ - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 7865 5646
45   [Unix/Linux] [POSIX IPC] mq_receive() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 7879 1801
44   [Unix/Linux] [POSIX IPC] mq_send() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 12065 1677
43   [Unix/Linux] [Thread] pthread_cond() function  ±è¿µ´ë 2003/03/17 6442 1675

1 [2][3][4]
 

Copyright 1999-2023 Zeroboard / skin by zero