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

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


Category

  김영대(2003-03-17 02:40:57, Hit : 4872, Vote : 1286
 [System V IPC] semop() function - 세마포어

/***************************************************
*   Creat  : 2001. 02. 10  (programed by Cori-Young)
*   Site: http://www.byoneself.co.kr
*   Update :
***************************************************/

#include    <stdio.h>
#include    <sys/types.h>
#include    <sys/ipc.h>
#include    <sys/sem.h>

static int      ask();
extern void     exit();
extern void     free();
extern char     *malloc();
extern void     perror();

static struct semid_ds  semid_ds;            /* status of semaphore set */

static char    error_mesg1[] = "semop: Can't allocate space--1n";
static char   error_mesg2[] = "semop: Can't allocate spaced--2";

main()
{
        register int    i;   /* work area */
        int    nsops;   /* number of operations to do */
        int    semid;   /* semid of semaphore set */
        struct sembuf    *sops;   /* ptr to operations to perform */

        (void) fprintf(stderr, "All numeric input must follow C conventions:n");
        (void) fprintf(stderr, "t0x... is interpreted as hexadecimal,n");
        (void) fprintf(stderr, "t0... is interpreted as octal,n");
        (void) fprintf(stderr, "totherwise, decimal.n");

        /* Loop until the invoker doesn't want to do anymore. */

        while (nsops = ask(&semid, &sops)) {

                  /* Initialize the array of operations to be performed.*/
                  for (i = 0; i < nsops; i++) {

                           (void) fprintf(stderr,"nEnter values for operation %d of %d.n", i + 1, nsops);
                           (void) fprintf(stderr, "sem_num(valid values are 0 <= sem_num < %d): ",semid_ds.sem_nsems);
                           (void) scanf("%hi", &sops[i].sem_num);
                           (void) fprintf(stderr, "sem_op: ");

                           (void) scanf("%hi", &sops[i].sem_op);

                           (void) fprintf(stderr, "Expected flags in sem_flg are:n");
                           (void) fprintf(stderr, "tIPC_NOWAIT =t%#6.6on", IPC_NOWAIT);
                           (void) fprintf(stderr, "tSEM_UNDO =t%#6.6on",   SEM_UNDO);
                           (void) fprintf(stderr, "sem_flg: ");
                           
                           (void) scanf("%hi", &sops[i].sem_flg);

                  }
                
                /* Recap the call to be made. */
                  (void) fprintf(stderr, "nsemop: Calling semop(%d, &sops, %d) with:", semid, nsops);

                  for (i = 0; i < nsops; i++)
                  {
                           (void) fprintf(stderr, "nsops[%d].sem_num = %d, ", i, sops[i].sem_num);
                           (void) fprintf(stderr, "sem_op = %d, ", sops[i].sem_op);
                           (void) fprintf(stderr, "sem_flg = %#on", sops[i].sem_flg);
                  }
                  /* Make the semop() call and report the results. */

                  if ((i = semop(semid, sops, nsops)) == -1) {
                           perror("semop: semop failed");
                  }
                  else {
                           (void) fprintf(stderr, "semop: semop returned %dn", i);
                  }
        }
}

static ask(semidp, sopsp)
int   *semidp;  /* pointer to semid (used only the first time) */
struct sembuf   **sopsp;
{
        static union semun     arg;  /* argument to semctl */
        int     i;  /* work area */
        static int     nsops = 0;  /* size of currently allocated sembuf array */

        static int     semid = -1;   /* semid supplied by user */
        static struct sembuf    *sops;     /* pointer to allocated array */

          if (semid < 0) {

                  /* First call; get semid from user and the current state of  the semaphore set. */
                  (void) fprintf(stderr, "Enter semid of the semaphore set you want to use: ");

                  (void) scanf("%i", &semid);

                  *semidp = semid;
                  arg.buf = &semid_ds;

                  if (semctl(semid, 0, IPC_STAT, arg) == -1)
                  {
                          perror("semop: semctl(IPC_STAT) failed");
                           /* Note that if semctl fails, semid_ds remains filled with zeros, so later test for number of semaphores will be zero. */

                           (void) fprintf(stderr, "Before and after values are not printed.n");
                  }
                  else {
                           if ((arg.array = (ushort *)malloc((unsigned)(sizeof(ushort) * semid_ds.sem_nsems))) == NULL) {
                            (void) fprintf(stderr, error_mesg1, semid_ds.sem_nsems);
                            exit(1);
                        }
                  }
        }
        /* Print current semaphore values. */

        if (semid_ds.sem_nsems) {

                  (void) fprintf(stderr,  "There are %d semaphores in the set.n", semid_ds.sem_nsems);

                  if (semctl(semid, 0, GETALL, arg) == -1) {
                           perror("semop: semctl(GETALL) failed");
                  }
                  else {
                           (void) fprintf(stderr, "Current semaphore values are:");

                           for (i = 0; i < semid_ds.sem_nsems; (void) fprintf(stderr, " %d", arg.array[i++]));
                                   (void) fprintf(stderr, "n");

                  }
        }

        /* Find out how many operations are going to be done in the next  call and allocate enough space to do it. */
        (void) fprintf(stderr,  "How many semaphore operations do you want %sn", "on the next call to semop()?");
        (void) fprintf(stderr, "Enter 0 or control-D to quit: ");

        i = 0;

        if (scanf("%i", &i) == EOF || i == 0)
                  exit(0);

        if (i > nsops) {

                  if (nsops)
                           free((char *)sops);

                  nsops = i;

                  if ((sops = (struct sembuf *)malloc((unsigned)(nsops * sizeof(struct sembuf)))) == NULL) {

                           (void) fprintf(stderr, error_mesg2, nsops);
                           exit(2);
                  }

        }

        *sopsp = sops;

        return (i);
}





62   [Unix/Linux] [강좌] 인텔 센트리노 무선랜 카드: ndiswrapper  김영대 2004/06/27 10841 1932
61   [Unix/Linux] [소스] top for System V Release 4, Intel or Sparc CPU  김영대 2004/02/20 6483 1121
60   [Unix/Linux] [소스] top for SunOS 5.x (Solaris 2.x)  김영대 2004/02/20 8737 1166
59   [Unix/Linux] [소스] String 해쉬(hash) 함수  김영대 2003/07/29 6202 1383
58   [Unix/Linux] [강좌] 웹로그분석을 위한 Webalizer + GDlib + PNGlib + Zlib 설치  김영대 2003/05/04 7026 1344
57   [Unix/Linux] [강좌] Apache + MySQL + PHP4 + Zend Optimizer 설치  김영대 2003/04/15 6082 1320
56   [Unix/Linux] [System V IPC] shmop() function - 공유메모리  김영대 2003/03/17 5640 1376
55   [Unix/Linux] [System V IPC] shmctl() function - 공유메모리  김영대 2003/03/17 5659 1526
54   [Unix/Linux] [System V IPC] shmget() function - 공유메모리  김영대 2003/03/17 5997 1504
  [Unix/Linux] [System V IPC] semop() function - 세마포어  김영대 2003/03/17 4872 1286
52   [Unix/Linux] [System V IPC] semctl() function - 세마포어  김영대 2003/03/17 4544 1253
51   [Unix/Linux] [System V IPC] semget() function - 세마포어  김영대 2003/03/17 5636 1508
50   [Unix/Linux] [System V IPC] msgget() function - 메세지큐  김영대 2003/03/17 5549 1423
49   [Unix/Linux] [System V IPC] msgctl() function - 메세지큐  김영대 2003/03/17 5636 1319
48   [Unix/Linux] [System V IPC] msgrcv() function - 메세지큐  김영대 2003/03/17 6052 1550
47   [Unix/Linux] [System V IPC] msgsnd() function - 메세지큐  김영대 2003/03/17 7395 1489
46   [Unix/Linux] [POSIX IPC] 생산자/소비자 - 세마포어  김영대 2003/03/17 7016 1452
45   [Unix/Linux] [POSIX IPC] mq_receive() function - 메세지큐  김영대 2003/03/17 6499 1589
44   [Unix/Linux] [POSIX IPC] mq_send() function - 메세지큐  김영대 2003/03/17 7626 1507
43   [Unix/Linux] [Thread] pthread_cond() function  김영대 2003/03/17 5782 1544

1 [2][3][4]
 

Copyright 1999-2017 Zeroboard / skin by zero