- 论坛徽章:
- 0
|
把我的代码贴出来吧,和大家分享一下
- /*****************************************************************
- *
- * Copyright (c) 2011
- *
- * FILE NAME: lock.c
- * FUNC DESC.: lock and unluck
- *
- * Version: 1.0
- * Auther: Boonie Chiong
- * Date: 2011-03-22
- *
- * Change logs:
- *
- ******************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/sem.h>
- #include "common.h"
- #include "lock.h"
-
- /**************************************************************************
- * function: sem_init
- * description: create and initial semaphore
- * input: int key
- int sem_num
- unsigned short int value_array
- * output: null
- * return: >0 SUCCEED other: FAILED
- **************************************************************************/
- int wea_api_sem_init(int key)
- {
- static unsigned short array_value = 1;
- int sem_id;
- union semun
- {
- int val;
- struct semid_ds * buf;
- ushort * array;
- }argument;
- argument.array = &array_value;
- /* Create the semaphore */
- if((sem_id = semget(key, 1, 0666 | IPC_CREAT))<0)
- {
- /* Always check system returns. */
- return FAILED;
- }
- /* What we actually get is an array of semaphores. The second
- * argument to semget() was the array dimension - in our case
- * 1. */
- if( semctl(sem_id, 0, SETALL, argument) < 0)
- {
- return FAILED;
- }
- if ((sem_id = semget(key, 0, 0)) < 0)
- {
- return FAILED;
- }
- return sem_id;
- }
- /****************************************************************
- * function: sem_open
- * discription: open semaphore
- * input: int key
- * output: null
- * return: 0: SUCCEED other: FAILED
- ****************************************************************/
- int wea_api_sem_open(int key)
- {
- int sem_id;
- if ((sem_id = semget(key, 0, 0)) < 0)
- {
- return FAILED;
- }
- return sem_id;
- }
- /****************************************************************
- * function: sem_lock
- * description: semaphore lock
- * input: int sem_id
- * int sem_num
- * output: null
- * return: 0:SUCCEED other: FAILED
- ****************************************************************/
- int wea_api_sem_lock(int sem_id)
- {
- // An "array" of one operation to perform on the semaphore.
- struct sembuf operations[1];
- int retval; // Return value from semop()
- /* Set up the sembuf structure. */
- /* Which semaphore in the semaphore array : */
- operations[0].sem_num = 0;
- /* Which operation? Subtract 1 from semaphore value : */
- operations[0].sem_op = -1;
- /* Set the flag so we will wait : */
- //operations[0].sem_flg = 0;
- operations[0].sem_flg = SEM_UNDO;
- /* So do the operation! */
- if((retval = semop(sem_id, operations, 1))!=0)
- {
- return FAILED;
- }
- return SUCCEED;
- }
- /******************************************************************
- * function: sem_unlock
- * description: semaphore unlock
- * input: int sem_id
- int sem_num
- * output: null
- * return: 0:SUCCEED other: FAILED
- ******************************************************************/
- int wea_api_sem_unlock(int sem_id)
- {
- struct sembuf operations[1];
- /* An "array" of one operation to perform on the semaphore. */
- int retval; /* Return value from semop() */
- /* Do a semaphore V-operation. */
- /*printf("Program sema about to do a V-operation. ");*/
- /* Set up the sembuf structure. */
- /* Which semaphore in the semaphore array : */
- operations[0].sem_num = 0;
- /* Which operation? Add 1 to semaphore value : */
- operations[0].sem_op = 1;
- /* Set the flag so we will wait : */
- //operations[0].sem_flg = 0;
- operations[0].sem_flg = SEM_UNDO;
- /* So do the operation! */
- if((retval = semop(sem_id, operations, 1))!=0)
- {
- return FAILED;
- }
- return SUCCEED;
- }
- /****************************************************************
- * function: sem_exit
- * description: semaphore exit
- * input: int sem_id
- * output: null
- * return: 0:SUCCEED other: FAILED
- ***************************************************************/
- int wea_api_sem_exit(int sem_id)
- {
- if(semctl(sem_id,0,IPC_RMID,(struct msquid_ds *)0) == -1)
- {
- return FAILED;
- }
- return SUCCEED;
- }
复制代码 |
|