ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linuxϵͳ»·¾³½ø³Ì¼äͨÐÅ£ºÐźŵÆ

·¢²¼Ê±¼ä:2006-11-09 09:47:26À´Ô´:ºìÁª×÷Õß:Innovation
¡¡¡¡Ò»¡¢ÐźŵƸÅÊö

¡¡¡¡ÐźŵÆÓëÆäËû½ø³Ì¼äͨÐÅ·½Ê½²»´óÏàͬ£¬ËüÖ÷ÒªÌṩ¶Ô½ø³Ì¼ä¹²Ïí×ÊÔ´·ÃÎÊ¿ØÖÆ»úÖÆ¡£Ï൱ÓÚÄÚ´æÖеıêÖ¾£¬½ø³Ì¿ÉÒÔ¸ù¾ÝËüÅж¨ÊÇ·ñÄܹ»·ÃÎÊijЩ¹²Ïí×ÊÔ´£¬Í¬Ê±£¬½ø³ÌÒ²¿ÉÒÔÐ޸ĸñêÖ¾¡£³ýÁËÓÃÓÚ·ÃÎÊ¿ØÖÆÍ⣬»¹¿ÉÓÃÓÚ½ø³Ìͬ²½¡£ÐźŵÆÓÐÒÔÏÂÁ½ÖÖÀàÐÍ£º

¡¡¡¡¶þÖµÐźŵƣº×î¼òµ¥µÄÐźŵÆÐÎʽ£¬ÐźŵƵÄÖµÖ»ÄÜÈ¡0»ò1£¬ÀàËÆÓÚ»¥³âËø¡£

¡¡¡¡×¢£º¶þÖµÐźŵÆÄܹ»ÊµÏÖ»¥³âËøµÄ¹¦ÄÜ£¬µ«Á½ÕߵĹØ×¢ÄÚÈݲ»Í¬¡£ÐźŵÆÇ¿µ÷¹²Ïí×ÊÔ´£¬Ö»Òª¹²Ïí×ÊÔ´¿ÉÓã¬ÆäËû½ø³ÌͬÑù¿ÉÒÔÐÞ¸ÄÐźŵƵÄÖµ£»»¥³âËø¸üÇ¿µ÷½ø³Ì£¬Õ¼ÓÃ×ÊÔ´µÄ½ø³ÌʹÓÃÍê×ÊÔ´ºó£¬±ØÐëÓɽø³Ì±¾ÉíÀ´½âËø¡£

¡¡¡¡¼ÆËãÐźŵƣºÐźŵƵÄÖµ¿ÉÒÔÈ¡ÈÎÒâ·Ç¸ºÖµ£¨µ±È»ÊÜÄں˱¾ÉíµÄÔ¼Êø£©¡£

¡¡¡¡¶þ¡¢LinuxÐźŵÆ

¡¡¡¡linux¶ÔÐźŵƵÄÖ§³Ö×´¿öÓëÏûÏ¢¶ÓÁÐÒ»Ñù£¬ÔÚred had 8.0·¢Ðа汾ÖÐÖ§³ÖµÄÊÇϵͳVµÄÐźŵơ£Òò´Ë£¬±¾ÎĽ«Ö÷Òª½éÉÜϵͳVÐźŵƼ°ÆäÏàÓ¦API¡£ÔÚûÓÐÉùÃ÷µÄÇé¿öÏ£¬ÒÔÏÂÌÖÂÛÖÐÖ¸µÄ¶¼ÊÇϵͳVÐźŵơ£

¡¡¡¡×¢Ò⣬ͨ³£Ëù˵µÄϵͳVÐźŵÆÖ¸µÄÊǼÆÊýÐźŵƼ¯¡£

¡¡¡¡Èý¡¢ÐźŵÆÓëÄÚºË

¡¡¡¡1¡¢ÏµÍ³VÐźŵÆÊÇËæÄں˳ÖÐøµÄ£¬Ö»ÓÐÔÚÄÚºËÖØÆð»òÕßÏÔʾɾ³ýÒ»¸öÐźŵƼ¯Ê±£¬¸ÃÐźŵƼ¯²Å»áÕæÕý±»É¾³ý¡£Òò´ËϵͳÖмÇ¼ÐźŵƵÄÊý¾Ý½á¹¹£¨struct ipc_ids sem_ids£©Î»ÓÚÄÚºËÖУ¬ÏµÍ³ÖеÄËùÓÐÐźŵƶ¼¿ÉÒÔÔڽṹsem_idsÖÐÕÒµ½·ÃÎÊÈë¿Ú¡£

¡¡¡¡2¡¢ÏÂͼ˵Ã÷ÁËÄÚºËÓëÐźŵÆÊÇÔõÑù½¨Á¢ÆðÁªÏµµÄ£º
ÆäÖУºstruct ipc_ids sem_idsÊÇÄÚºËÖмÇ¼ÐźŵƵÄÈ«¾ÖÊý¾Ý½á¹¹£»ÃèÊöÒ»¸ö¾ßÌåµÄÐźŵƼ°ÆäÏà¹ØÐÅÏ¢¡£

[align=center][/align]

¡¡¡¡ÆäÖУ¬struct sem½á¹¹ÈçÏ£º

ÒýÓÃ:
struct sem{
int semval; // current value
int sempid // pid of last operation
}


¡¡¡¡´ÓÉÏͼ¿ÉÒÔ¿´³ö£¬È«¾ÖÊý¾Ý½á¹¹struct ipc_ids sem_ids¿ÉÒÔ·ÃÎʵ½struct kern_ipc_permµÄµÚÒ»¸ö³ÉÔ±£ºstruct kern_ipc_perm£»¶øÿ¸östruct kern_ipc_permÄܹ»Óë¾ßÌåµÄÐźŵÆÀàÐͳÉÔ±key£¬¶økeyÔòΨһȷ¶¨Ò»¸öÐźŵƼ¯£»Í¬Ê±£¬½á¹¹struct kern_ipc_permµÄ×îºóÒ»¸ö³ÉÔ±sem_nsemsÈ·¶¨Á˸ÃÐźŵÆÔÚÐźŵƼ¯ÖеÄ˳Ðò£¬ÕâÑùÄں˾ÍÄܹ»¼Ç¼ÿ¸öÐźŵƵÄÐÅÏ¢ÁË¡£kern_ipc_perm½á¹¹²Î¼û¡¶Linux»·¾³½ø³Ì¼äͨÐÅ£¨Èý£©£ºÏûÏ¢¶ÓÁС·¡£struct sem_array¼û¸½Â¼1¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 5 ÌõÆÀÂÛ

  1. rainofwind ÓÚ 2006-11-09 11:34:28·¢±í:

    :0L

  2. Innovation ÓÚ 2006-11-09 09:51:51·¢±í:

    ¡¡¡¡×¢£º¶ÁÕß¿ÉÒÔ³¢ÊÔÒ»ÏÂ×¢Ê͵ô³õʼ»¯²½Ö裬½ø³ÌÔÚÔËÐÐʱ»á³öÏÖºÎÖÖÇé¿ö£¨½ø³ÌÔÚÉêÇë×ÊԴʱ»á˯Ãߣ©£¬Í¬Ê±¿ÉÒÔÏñ³ÌÐò½áβ¸ø³öµÄ×¢ÊÍÄÇÑù£¬°Ñ¸Ã³ÌÐò±àÒë³ÉÁ½¸ö²»Í¬°æ±¾¡£ÏÂÃæÊDZ¾³ÌÐòµÄÔËÐнá¹û£¨²Ù×÷ϵͳredhat8.0£©£º

    ÒýÓÃ:
    owner's uid is 0
    owner's gid is 0
    creater's uid is 0
    creater's gid is 0
    the number of entries in semaphore map is 32000
    max number of semaphore identifiers is 128
    mas number of semaphores in system is 32000
    the number of undo structures system wide is 32000
    max number of semaphores per semid is 250
    max number of ops per semop call is 32
    max number of undo entries per process is 32
    the sizeof of struct sem_undo is 20
    the maximum semaphore value is 32767
    now free the resource
    remove sem ok


    ¡¡¡¡Summary£ºÐźŵÆÓëÆäËü½ø³Ì¼äͨÐÅ·½Ê½ÓÐËù²»Í¬£¬ËüÖ÷ÒªÓÃÓÚ½ø³Ì¼äͬ²½¡£Í¨³£Ëù˵µÄϵͳVÐźŵÆʵ¼ÊÉÏÊÇÒ»¸öÐźŵƵļ¯ºÏ£¬¿ÉÓÃÓÚ¶àÖÖ¹²Ïí×ÊÔ´µÄ½ø³Ì¼äͬ²½¡£Ã¿¸öÐźŵƶ¼ÓÐÒ»¸öÖµ£¬¿ÉÒÔÓÃÀ´±íʾµ±Ç°¸ÃÐźŵƴú±íµÄ¹²Ïí×ÊÔ´¿ÉÓã¨available£©ÊýÁ¿£¬Èç¹ûÒ»¸ö½ø³ÌÒªÉêÇë¹²Ïí×ÊÔ´£¬ÄÇô¾Í´ÓÐźŵÆÖµÖмõÈ¥ÒªÉêÇëµÄÊýÄ¿£¬Èç¹ûµ±Ç°Ã»ÓÐ×ã¹»µÄ¿ÉÓÃ×ÊÔ´£¬½ø³Ì¿ÉÒÔ˯Ãߵȴý£¬Ò²¿ÉÒÔÁ¢¼´·µ»Ø¡£µ±½ø³ÌÒªÉêÇë¶àÖÖ¹²Ïí×ÊԴʱ£¬linux¿ÉÒÔ±£Ö¤²Ù×÷µÄÔ­×ÓÐÔ£¬¼´ÒªÃ´ÉêÇëµ½ËùÓеĹ²Ïí×ÊÔ´£¬ÒªÃ´·ÅÆúËùÓÐ×ÊÔ´£¬ÕâÑùÄܹ»±£Ö¤¶à¸ö½ø³Ì²»»áÔì³É»¥Ëø¡£Linux¶ÔÐźŵÆÓи÷ÖÖ¸÷ÑùµÄÏÞÖÆ£¬³ÌÐòÖиø³öÁËÊä³ö½á¹û¡£ÁíÍ⣬Èç¹û¶ÁÕßÏë¶ÔÐźŵÆ×÷½øÒ»²½µÄÀí½â£¬½¨ÒéÔĶÁsem.hÔ´´úÂ룬¸ÃÎļþ²»³¤£¬µ«¸ø³öÁËÐźŵÆÏà¹ØµÄÖØÒªÊý¾Ý½á¹¹¡£

    ¡¡¡¡¸½Â¼1£º struct sem_arrayÈçÏ£º

    ÒýÓÃ:
    /*ϵͳÖеÄÿ¸öÐźŵƼ¯¶ÔÓ¦Ò»¸ösem_array ½á¹¹ */
    struct sem_array {
    struct kern_ipc_perm sem_perm; /* permissions .. see ipc.h */
    time_t sem_otime; /* last semop time */
    time_t sem_ctime; /* last change time */
    struct sem *sem_base; /* ptr to first semaphore in array */
    struct sem_queue *sem_pending; /* pending operations to be processed */
    struct sem_queue **sem_pending_last; /* last pending operation */
    struct sem_undo *undo; /* undo requests on this array */
    unsigned long sem_nsems; /* no. of semaphores in array */
    };


    ¡¡¡¡ÆäÖУ¬sem_queue½á¹¹ÈçÏ£º

    ÒýÓÃ:
    /* ϵͳÖÐÿ¸öÒòΪÐźŵƶø˯ÃߵĽø³Ì£¬¶¼¶ÔÓ¦Ò»¸ösem_queue½á¹¹*/
    struct sem_queue {
    struct sem_queue * next; /* next entry in the queue */
    struct sem_queue ** prev; /* previous entry in the queue, *(q->prev) == q */
    struct task_struct* sleeper; /* this process */
    struct sem_undo * undo; /* undo structure */
    int pid; /* process id of requesting process */
    int status; /* completion status of operation */
    struct sem_array * sma; /* semaphore array for operations */
    int id; /* internal sem id */
    struct sembuf * sops; /* array of pending operations */
    int nsops; /* number of operations */
    int alter; /* operation will alter semaphore */
    };


    ¡¡¡¡¸½Â¼2£ºunion semunÊÇϵͳµ÷ÓÃsemctlÖеÄÖØÒª²ÎÊý£º

    ÒýÓÃ:
    union semun {
    int val; /* value for SETVAL */
    struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
    unsigned short *array; /* array for GETALL & SETALL */
    struct seminfo *__buf; /* buffer for IPC_INFO */ //test!!
    void *__pad;
    };
    struct seminfo {
    int semmap;
    int semmni;
    int semmns;
    int semmnu;
    int semmsl;
    int semopm;
    int semume;
    int semusz;
    int semvmx;
    int semaem;
    };


    ¡¡¡¡²Î¿¼ÎÄÏ×£º

    ¡¡¡¡[1] UNIXÍøÂç±à³ÌµÚ¶þ¾í£º½ø³Ì¼äͨÐÅ£¬×÷ÕߣºW.Richard Stevens£¬ÒëÕߣºÑî¼ÌÕÅ£¬Ç廪´óѧ³ö°æÉç¡£¶ÔPOSIXÒÔ¼°ÏµÍ³VÐźŵƶ¼ÓвûÊö£¬¶ÔLinux»·¾³ÏµijÌÐò¿ª·¢Óм«´óµÄÆô·¢ÒâÒå¡£

    ¡¡¡¡[2] linuxÄÚºËÔ´´úÂëÇé¾°·ÖÎö£¨ÉÏ£©£¬Ã«µÂ²Ù¡¢ºúÏ£Ã÷Öø£¬Õã½­´óѧ³ö°æÉ磬¸ø³öÁËϵͳVÐźŵÆÏà¹ØµÄÔ´´úÂë·ÖÎö£¬ÓÈÆäÔÚ²ûÊö±£Ö¤²Ù×÷Ô­×ÓÐÔ·½Ã棬ÒÔ¼°²ûÊöundo±ê־λʱ£¬ÌÖÂ۵ĺÜÉî¿Ì¡£

    ¡¡¡¡[3]GNU/Linux±à³ÌÖ¸ÄÏ£¬µÚ¶þ°æ£¬Kurt WallµÈÖø£¬ÕÅ»ÔÒë

    ¡¡¡¡[4]semget¡¢semop¡¢semctlÊÖ²á

  3. Innovation ÓÚ 2006-11-09 09:50:44·¢±í:

    ¡¡¡¡Îå¡¢ÐźŵƵÄÏÞÖÆ

    ¡¡¡¡1¡¢ Ò»´Îϵͳµ÷ÓÃsemop¿Éͬʱ²Ù×÷µÄÐźŵÆÊýÄ¿SEMOPM£¬semopÖеIJÎÊýnsopsÈç¹û³¬¹ýÁËÕâ¸öÊýÄ¿£¬½«·µ»ØE2BIG´íÎó¡£SEMOPMµÄ´óСÌض¨Óëϵͳ£¬redhat 8.0Ϊ32¡£

    ¡¡¡¡2¡¢ ÐźŵƵÄ×î´óÊýÄ¿£ºSEMVMX£¬µ±ÉèÖÃÐźŵÆÖµ³¬¹ýÕâ¸öÏÞÖÆʱ£¬»á·µ»ØERANGE´íÎó¡£ÔÚredhat 8.0ÖиÃֵΪ32767¡£

    ¡¡¡¡3¡¢ ϵͳ·¶Î§ÄÚÐźŵƼ¯µÄ×î´óÊýÄ¿SEMMNIÒÔ¼°ÏµÍ³·¶Î§ÄÚÐźŵƵÄ×î´óÊýÄ¿SEMMNS¡£³¬¹ýÕâÁ½¸öÏÞÖƽ«·µ»ØENOSPC´íÎó¡£redhat 8.0ÖиÃֵΪ32000¡£

    ¡¡¡¡4¡¢ ÿ¸öÐźŵƼ¯ÖеÄ×î´óÐźŵÆÊýÄ¿SEMMSL£¬redhat 8.0ÖÐΪ250¡£ SEMOPMÒÔ¼°SEMVMXÊÇʹÓÃsemopµ÷ÓÃʱӦ¸Ã×¢ÒâµÄ£»SEMMNIÒÔ¼°SEMMNSÊǵ÷ÓÃsemgetʱӦ¸Ã×¢ÒâµÄ¡£SEMVMXͬʱҲÊÇsemctlµ÷ÓÃÓ¦¸Ã×¢ÒâµÄ¡£

    ¡¡¡¡Áù¡¢¾ºÕùÎÊÌâ

    ¡¡¡¡µÚÒ»¸ö´´½¨ÐźŵƵĽø³ÌͬʱҲ³õʼ»¯Ðźŵƣ¬ÕâÑù£¬ÏµÍ³µ÷ÓÃsemget°üº¬ÁËÁ½¸ö²½Ö裺´´½¨Ðźŵƣ»³õʼ»¯Ðźŵơ£ÓÉ´Ë¿ÉÄܵ¼ÖÂÒ»ÖÖ¾ºÕù״̬£ºµÚÒ»¸ö´´½¨ÐźŵƵĽø³ÌÔÚ³õʼ»¯ÐźŵÆʱ£¬µÚ¶þ¸ö½ø³ÌÓÖµ÷ÓÃsemget£¬²¢ÇÒ·¢ÏÖÐźŵÆÒѾ­´æÔÚ£¬´Ëʱ£¬µÚ¶þ¸ö½ø³Ì±ØÐë¾ßÓÐÅжÏÊÇ·ñÓнø³ÌÕýÔÚ¶ÔÐźŵƽøÐгõʼ»¯µÄÄÜÁ¦¡£Ôڲο¼ÎÄÏ×[1]ÖУ¬¸ø³öÁËÈƹýÕâÖÖ¾ºÕù״̬µÄ·½·¨£ºµ±semget´´½¨Ò»¸öеÄÐźŵÆʱ£¬Ðźŵƽṹsemid_dsµÄsem_otime³ÉÔ±³õʼ»¯ºóµÄֵΪ0¡£Òò´Ë£¬µÚ¶þ¸ö½ø³ÌÔڳɹ¦µ÷ÓÃsemgetºó£¬¿ÉÔÙ´ÎÒÔIPC_STATÃüÁîµ÷ÓÃsemctl£¬µÈ´ýsem_otime±äΪ·Ç0Öµ£¬´Ëʱ¿ÉÅжϸÃÐźŵÆÒѾ­³õʼ»¯Íê±Ï¡£ÏÂͼÃèÊöÁ˾ºÕù״̬²úÉú¼°½â¾ö·½·¨£º

    [align=center][/align]

    ¡¡¡¡Êµ¼ÊÉÏ£¬ÕâÖÖ½â¾ö·½·¨Ò²ÊÇ»ùÓÚÕâÑùÒ»¸ö¼Ù¶¨£ºµÚÒ»¸ö´´½¨ÐźŵƵĽø³Ì±ØÐëµ÷ÓÃsemop£¬ÕâÑùsem_otime²ÅÄܱäΪ·ÇÁãÖµ¡£ÁíÍ⣬ÒòΪµÚÒ»¸ö½ø³Ì¿ÉÄܲ»µ÷ÓÃsemop£¬»òÕßsemop²Ù×÷ÐèÒªºÜ³¤Ê±¼ä£¬µÚ¶þ¸ö½ø³Ì¿ÉÄÜÎÞÏÞÆڵȴýÏÂÈ¥£¬»òÕߵȴýºÜ³¤Ê±¼ä¡£

    ¡¡¡¡Æß¡¢ÐźŵÆÓ¦ÓÃʵÀý

    ¡¡¡¡±¾ÊµÀýÓÐÁ½¸öÄ¿µÄ£º1¡¢»ñÈ¡¸÷ÖÖÐźŵÆÐÅÏ¢£»2¡¢ÀûÓÃÐźŵÆʵÏÖ¹²Ïí×ÊÔ´µÄÉêÇëºÍÊÍ·Å¡£²¢ÔÚ³ÌÐòÖиø³öÁËÏêϸעÊÍ¡£

    ÒýÓÃ:
    #include
    #include
    #include
    #define SEM_PATH "/unix/my_sem"
    #define max_tries 3
    int semid;
    main()
    {
    int flag1,flag2,key,i,init_ok,tmperrno;
    struct semid_ds sem_info;
    struct seminfo sem_info2;
    union semun arg; //union semun£º Çë²Î¿¼¸½Â¼2
    struct sembuf askfor_res, free_res;
    flag1=IPC_CREAT|IPC_EXCL|00666;
    flag2=IPC_CREAT|00666;
    key=ftok(SEM_PATH,'a');
    //error handling for ftok here;
    init_ok=0;
    semid=semget(key,1,flag1);//create a semaphore set that only includes one semphore.
    if(semidsem_otime!=0){ i=max_tries; init_ok=1;}
    else sleep(1);
    }
    }
    if(!init_ok)
    // do some initializing, here we assume that the first process that creates the sem will
    // finish initialize the sem and run semop in max_tries*1 seconds. else it will not run
    // semop any more.
    {
    arg.val=1;
    if(semctl(semid,0,SETVAL,arg)==-1) perror("semctl setval error");
    }
    }
    else
    {perror("semget error, process exit"); exit(); }
    }
    else //semid>=0; do some initializing
    {
    arg.val=1;
    if(semctl(semid,0,SETVAL,arg)==-1)
    perror("semctl setval error");
    }
    //get some information about the semaphore and the limit of semaphore in redhat8.0
    arg.buf=&sem_info;
    if(semctl(semid, 0, IPC_STAT, arg)==-1)
    perror("semctl IPC STAT");
    printf("owner's uid is %d\n", arg.buf->sem_perm.uid);
    printf("owner's gid is %d\n", arg.buf->sem_perm.gid);
    printf("creater's uid is %d\n", arg.buf->sem_perm.cuid);
    printf("creater's gid is %d\n", arg.buf->sem_perm.cgid);
    arg.__buf=&sem_info2;
    if(semctl(semid,0,IPC_INFO,arg)==-1)
    perror("semctl IPC_INFO");
    printf("the number of entries in semaphore map is %d \n", arg.__buf->semmap);
    printf("max number of semaphore identifiers is %d \n", arg.__buf->semmni);
    printf("mas number of semaphores in system is %d \n", arg.__buf->semmns);
    printf("the number of undo structures system wide is %d \n", arg.__buf->semmnu);
    printf("max number of semaphores per semid is %d \n", arg.__buf->semmsl);
    printf("max number of ops per semop call is %d \n", arg.__buf->semopm);
    printf("max number of undo entries per process is %d \n", arg.__buf->semume);
    printf("the sizeof of struct sem_undo is %d \n", arg.__buf->semusz);
    printf("the maximum semaphore value is %d \n", arg.__buf->semvmx);

    //now ask for available resource:
    askfor_res.sem_num=0;
    askfor_res.sem_op=-1;
    askfor_res.sem_flg=SEM_UNDO;

    if(semop(semid,&askfor_res,1)==-1)//ask for resource
    perror("semop error");

    sleep(3); //do some handling on the sharing resource here, just sleep on it 3 seconds
    printf("now free the resource\n");

    //now free resource
    free_res.sem_num=0;
    free_res.sem_op=1;
    free_res.sem_flg=SEM_UNDO;
    if(semop(semid,&free_res,1)==-1)//free the resource.
    if(errno==EIDRM)
    printf("the semaphore set was removed\n");
    //you can comment out the codes below to compile a different version:
    if(semctl(semid, 0, IPC_RMID)==-1)
    perror("semctl IPC_RMID");
    else printf("remove sem ok\n");
    }

  4. Innovation ÓÚ 2006-11-09 09:49:47·¢±í:

    ¡¡¡¡²ÎÊýkeyÊÇÒ»¸ö¼üÖµ£¬ÓÉftok»ñµÃ£¬Î¨Ò»±êʶһ¸öÐźŵƼ¯£¬Ó÷¨Óëmsgget()ÖеÄkeyÏàͬ£»²ÎÊýnsemsÖ¸¶¨´ò¿ª»òÕßд´½¨µÄÐźŵƼ¯Öн«°üº¬ÐźŵƵÄÊýÄ¿£»semflg²ÎÊýÊÇһЩ±ê־λ¡£²ÎÊýkeyºÍsemflgµÄÈ¡Öµ£¬ÒÔ¼°ºÎʱ´ò¿ªÒÑÓÐÐźŵƼ¯»òÕß´´½¨Ò»¸öеÄÐźŵƼ¯Óëmsgget()ÖеĶÔÓ¦²¿·ÖÏàͬ£¬²»ÔÙÏéÊö¡£

    ¡¡¡¡¸Ãµ÷Ó÷µ»ØÓ뽡ֵkeyÏà¶ÔÓ¦µÄÐźŵƼ¯ÃèÊö×Ö¡£

    ¡¡¡¡µ÷Ó÷µ»Ø£º³É¹¦·µ»ØÐźŵƼ¯ÃèÊö×Ö£¬·ñÔò·µ»Ø-1¡£

    ×¢£ºÈç¹ûkeyËù´ú±íµÄÐźŵÆÒѾ­´æÔÚ£¬ÇÒsemgetÖ¸¶¨ÁËIPC_CREAT|IPC_EXCL±êÖ¾£¬ÄÇô¼´Ê¹²ÎÊýnsemsÓëÔ­À´ÐźŵƵÄÊýÄ¿²»µÈ£¬·µ»ØµÄÒ²ÊÇEEXIST´íÎó£»Èç¹ûsemgetÖ»Ö¸¶¨ÁËIPC_CREAT±êÖ¾£¬ÄÇô²ÎÊýnsems±ØÐëÓëÔ­À´µÄÖµÒ»Ö£¬ÔÚºóÃæ³ÌÐòʵÀýÖл¹Òª½øÒ»²½ËµÃ÷¡£

    ¡¡¡¡2£©int semop(int semid, struct sembuf *sops, unsigned nsops);

    ¡¡¡¡semidÊÇÐźŵƼ¯ID£¬sopsÖ¸ÏòÊý×éµÄÿһ¸ösembuf½á¹¹¶¼¿Ì»­Ò»¸öÔÚÌض¨ÐźŵÆÉϵIJÙ×÷¡£nsopsΪsopsÖ¸ÏòÊý×éµÄ´óС¡£

    ¡¡¡¡sembuf½á¹¹ÈçÏ£º

    ÒýÓÃ:
    struct sembuf {
    unsigned short sem_num; /* semaphore index in array */
    short sem_op; /* semaphore operation */
    short sem_flg; /* operation flags */
    };


    ¡¡¡¡sem_num¶ÔÓ¦Ðźż¯ÖеÄÐźŵƣ¬0¶ÔÓ¦µÚÒ»¸öÐźŵơ£sem_flg¿ÉÈ¡IPC_NOWAITÒÔ¼°SEM_UNDOÁ½¸ö±êÖ¾¡£Èç¹ûÉèÖÃÁËSEM_UNDO±êÖ¾£¬ÄÇôÔÚ½ø³Ì½áÊøʱ£¬ÏàÓ¦µÄ²Ù×÷½«±»È¡Ïû£¬ÕâÊDZȽÏÖØÒªµÄÒ»¸ö±ê־λ¡£Èç¹ûÉèÖÃÁ˸ñê־룬ÄÇôÔÚ½ø³ÌûÓÐÊͷŹ²Ïí×ÊÔ´¾ÍÍ˳öʱ£¬Äں˽«´úΪÊÍ·Å¡£Èç¹ûΪһ¸öÐźŵÆÉèÖÃÁ˸ñêÖ¾£¬Äں˶¼Òª·ÖÅäÒ»¸ösem_undo½á¹¹À´¼Ç¼Ëü£¬ÎªµÄÊÇÈ·±£ÒÔºó×ÊÔ´Äܹ»°²È«ÊÍ·Å¡£ÊÂʵÉÏ£¬Èç¹û½ø³ÌÍ˳öÁË£¬ÄÇôËüËùÕ¼ÓþÍÊÍ·ÅÁË£¬µ«ÐźŵÆֵȴûÓиı䣬´Ëʱ£¬ÐźŵÆÖµ·´Ó³µÄÒѾ­²»ÊÇ×ÊÔ´Õ¼ÓеÄʵ¼ÊÇé¿ö£¬ÔÚÕâÖÖÇé¿öÏ£¬ÎÊÌâµÄ½â¾ö¾Í¿¿ÄÚºËÀ´Íê³É¡£ÕâÓеãÏñ½©Ê¬½ø³Ì£¬½ø³ÌËäÈ»Í˳öÁË£¬×ÊÔ´Ò²¶¼ÊÍ·ÅÁË£¬µ«Äں˽ø³Ì±íÖÐÈÔÈ»ÓÐËüµÄ¼Ç¼£¬´Ëʱ¾ÍÐèÒª¸¸½ø³Ìµ÷ÓÃwaitpidÀ´½â¾öÎÊÌâÁË¡£

    ¡¡¡¡sem_opµÄÖµ´óÓÚ0£¬µÈÓÚ0ÒÔ¼°Ð¡ÓÚ0È·¶¨Á˶Ôsem_numÖ¸¶¨µÄÐźŵƽøÐеÄÈýÖÖ²Ù×÷¡£¾ßÌåÇë²Î¿¼linuxÏàÓ¦ÊÖ²áÒ³¡£

    ¡¡¡¡ÕâÀïÐèҪǿµ÷µÄÊÇsemopͬʱ²Ù×÷¶à¸öÐźŵƣ¬ÔÚʵ¼ÊÓ¦ÓÃÖУ¬¶ÔÓ¦¶àÖÖ×ÊÔ´µÄÉêÇë»òÊÍ·Å¡£semop±£Ö¤²Ù×÷µÄÔ­×ÓÐÔ£¬ÕâÒ»µãÓÈΪÖØÒª¡£ÓÈÆä¶ÔÓÚ¶àÖÖ×ÊÔ´µÄÉêÇëÀ´Ëµ£¬ÒªÃ´Ò»´ÎÐÔ»ñµÃËùÓÐ×ÊÔ´£¬ÒªÃ´·ÅÆúÉêÇ룬ҪôÔÚ²»Õ¼ÓÐÈκÎ×ÊÔ´Çé¿öϼÌÐøµÈ´ý£¬ÕâÑù£¬Ò»·½Ãæ±ÜÃâÁË×ÊÔ´µÄÀË·Ñ£»ÁíÒ»·½Ã棬±ÜÃâÁ˽ø³ÌÖ®¼äÓÉÓÚÉêÇë¹²Ïí×ÊÔ´Ôì³ÉËÀËø¡£

    ¡¡¡¡Ò²Ðí´Óʵ¼Êº¬ÒåÉϸüºÃÀí½âÕâЩ²Ù×÷£ºÐźŵƵĵ±Ç°Öµ¼Ç¼ÏàÓ¦×ÊÔ´Ä¿Ç°¿ÉÓÃÊýÄ¿£»sem_op>0¶ÔÓ¦ÏàÓ¦½ø³ÌÒªÊÍ·Åsem_opÊýÄ¿µÄ¹²Ïí×ÊÔ´£»sem_op=0¿ÉÒÔÓÃÓÚ¶Ô¹²Ïí×ÊÔ´ÊÇ·ñÒÑÓÃÍêµÄ²âÊÔ£»sem_op

    ¡¡¡¡3) int semctl(int semid£¬int semnum£¬int cmd£¬union semun arg)¸Ãϵͳµ÷ÓÃʵÏÖ¶ÔÐźŵƵĸ÷ÖÖ¿ØÖƲÙ×÷£¬²ÎÊýsemidÖ¸¶¨ÐźŵƼ¯£¬²ÎÊýcmdÖ¸¶¨¾ßÌåµÄ²Ù×÷ÀàÐÍ£»²ÎÊýsemnumÖ¸¶¨¶ÔÄĸöÐźŵƲÙ×÷£¬Ö»¶Ô¼¸¸öÌØÊâµÄcmd²Ù×÷ÓÐÒâÒ壻argÓÃÓÚÉèÖûò·µ»ØÐźŵÆÐÅÏ¢¡£

    ¡¡¡¡¸Ãϵͳµ÷ÓÃÏêϸÐÅÏ¢Çë²Î¼ûÆäÊÖ²áÒ³£¬ÕâÀïÖ»¸ø³ö²ÎÊýcmdËùÄÜÖ¸¶¨µÄ²Ù×÷¡£

    ¡¡¡¡IPC_STAT

    ¡¡¡¡»ñÈ¡ÐźŵÆÐÅÏ¢£¬ÐÅÏ¢ÓÉarg.buf·µ»Ø£»

    ¡¡¡¡IPC_SET

    ¡¡¡¡ÉèÖÃÐźŵÆÐÅÏ¢£¬´ýÉèÖÃÐÅÏ¢±£´æÔÚarg.bufÖУ¨ÔÚmanpageÖиø³öÁË¿ÉÒÔÉèÖÃÄÄЩÐÅÏ¢£©£»

    ¡¡¡¡GETALL

    ¡¡¡¡·µ»ØËùÓÐÐźŵƵÄÖµ£¬½á¹û±£´æÔÚarg.arrayÖУ¬²ÎÊýsennum±»ºöÂÔ£»

    ¡¡¡¡GETNCNT

    ¡¡¡¡·µ»ØµÈ´ýsemnumËù´ú±íÐźŵƵÄÖµÔö¼ÓµÄ½ø³ÌÊý£¬Ï൱ÓÚÄ¿Ç°ÓжàÉÙ½ø³ÌÔڵȴýsemnum´ú±íµÄÐźŵÆËù´ú±íµÄ¹²Ïí×ÊÔ´£»

    ¡¡¡¡GETPID

    ¡¡¡¡·µ»Ø×îºóÒ»¸ö¶ÔsemnumËù´ú±íÐźŵÆÖ´ÐÐsemop²Ù×÷µÄ½ø³ÌID£»

    ¡¡¡¡GETVAL

    ¡¡¡¡·µ»ØsemnumËù´ú±íÐźŵƵÄÖµ£»

    ¡¡¡¡GETZCNT

    ¡¡¡¡·µ»ØµÈ´ýsemnumËù´ú±íÐźŵƵÄÖµ±ä³É0µÄ½ø³ÌÊý£»

    ¡¡¡¡SETALL

    ¡¡¡¡Í¨¹ýarg.array¸üÐÂËùÓÐÐźŵƵÄÖµ£»Í¬Ê±£¬¸üÐÂÓë±¾Ðźż¯Ïà¹ØµÄsemid_ds½á¹¹µÄsem_ctime³ÉÔ±£»

    ¡¡¡¡SETVAL

    ¡¡¡¡ÉèÖÃsemnumËù´ú±íÐźŵƵÄֵΪarg.val£»

    ¡¡¡¡µ÷Ó÷µ»Ø£ºµ÷ÓÃʧ°Ü·µ»Ø-1£¬³É¹¦·µ»ØÓëcmdÏà¹Ø£º

    ÒýÓÃ:
    Cmd
    return value
    GETNCNT
    Semncnt
    GETPID
    Sempid
    GETVAL
    Semval
    GETZCNT
    Semzcnt

  5. Innovation ÓÚ 2006-11-09 09:48:36·¢±í:

    ¡¡¡¡ËÄ¡¢²Ù×÷ÐźŵÆ

    ¡¡¡¡¶ÔÏûÏ¢¶ÓÁеIJÙ×÷ÎÞ·ÇÓÐÏÂÃæÈýÖÖÀàÐÍ£º

    ¡¡¡¡1¡¢ ´ò¿ª»ò´´½¨ÐźŵÆ

    ¡¡¡¡ÓëÏûÏ¢¶ÓÁеĴ´½¨¼°´ò¿ª»ù±¾Ïàͬ£¬²»ÔÙÏêÊö¡£

    ¡¡¡¡2¡¢ ÐźŵÆÖµ²Ù×÷

    ¡¡¡¡linux¿ÉÒÔÔö¼Ó»ò¼õСÐźŵƵÄÖµ£¬ÏàÓ¦ÓÚ¶Ô¹²Ïí×ÊÔ´µÄÊͷźÍÕ¼ÓС£¾ßÌå²Î¼ûºóÃæµÄsemopϵͳµ÷Óá£

    ¡¡¡¡3¡¢ »ñµÃ»òÉèÖÃÐźŵÆÊôÐÔ£º

    ¡¡¡¡ÏµÍ³ÖеÄÿһ¸öÐźŵƼ¯¶¼¶ÔÓ¦Ò»¸östruct sem_array½á¹¹£¬¸Ã½á¹¹¼Ç¼ÁËÐźŵƼ¯µÄ¸÷ÖÖÐÅÏ¢£¬´æÔÚÓÚϵͳ¿Õ¼ä¡£ÎªÁËÉèÖᢻñµÃ¸ÃÐźŵƼ¯µÄ¸÷ÖÖÐÅÏ¢¼°ÊôÐÔ£¬ÔÚÓû§¿Õ¼äÓÐÒ»¸öÖØÒªµÄÁªºÏ½á¹¹ÓëÖ®¶ÔÓ¦£¬¼´union semun¡£

    [align=center][/align]

    ¡¡¡¡ÁªºÏsemunÊý¾Ý½á¹¹¸÷³ÉÔ±ÒâÒå²Î¼û¸½Â¼2

    ¡¡¡¡ÐźŵÆAPI

    ¡¡¡¡1¡¢ÎļþÃûµ½¼üÖµ#include

    ¡¡¡¡#include
    ¡¡¡¡key_t ftok (char*pathname, char proj)£»

    ¡¡¡¡Ëü·µ»ØÓë·¾¶pathnameÏà¶ÔÓ¦µÄÒ»¸ö¼üÖµ£¬¾ßÌåÓ÷¨Çë²Î¿¼¡¶Linux»·¾³½ø³Ì¼äͨÐÅ£¨Èý£©£ºÏûÏ¢¶ÓÁС·¡£

    ¡¡¡¡2¡¢ linuxÌØÓеÄipc()µ÷Óãº

    ¡¡¡¡int ipc(unsigned int call, int first, int second, int third, void *ptr, long fifth);

    ¡¡¡¡²ÎÊýcallÈ¡²»Í¬ÖµÊ±£¬¶ÔÓ¦ÐźŵƵÄÈý¸öϵͳµ÷Óãº

    ¡¡¡¡µ±callΪSEMOPʱ£¬¶ÔÓ¦int semop(int semid, struct sembuf *sops, unsigned nsops)µ÷Óã»
    ¡¡¡¡µ±callΪSEMGETʱ£¬¶ÔÓ¦int semget(key_t key, int nsems, int semflg)µ÷Óã»
    ¡¡¡¡µ±callΪSEMCTLʱ£¬¶ÔÓ¦int semctl(int semid£¬int semnum£¬int cmd£¬union semun arg)µ÷Óã»

    ¡¡¡¡ÕâЩµ÷Óý«ÔÚºóÃæ²ûÊö¡£

    ¡¡¡¡×¢£º±¾È˲»Ö÷ÕŲÉÓÃϵͳµ÷ÓÃipc()£¬¶ø¸üÇãÏòÓÚ²ÉÓÃϵͳV»òÕßPOSIX½ø³Ì¼äͨÐÅAPI¡£Ô­ÒòÒÑÔÚLinux»·¾³½ø³Ì¼äͨÐÅ£¨Èý£©£ºÏûÏ¢¶ÓÁÐÖиø³ö¡£

    ¡¡¡¡3¡¢ÏµÍ³VÐźŵÆAPI

    ¡¡¡¡ÏµÍ³VÏûÏ¢¶ÓÁÐAPIÖ»ÓÐÈý¸ö£¬Ê¹ÓÃʱÐèÒª°üÀ¨¼¸¸öÍ·Îļþ£º

    ÒýÓÃ:
    #include
    #include
    #include
    1£©int semget(key_t key, int nsems, int semflg)