信号量

/************************************************/

/* sem_com.h */

#ifndef  SEM_COM_H
#define  SEM_COM_H                   //定义sem_com.h文件

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

union semun                    //定义联合体union semnn结构
{
 int val;
 struct semid_ds *buf;
 unsigned short *array;
};

int init_sem(int, int);          //函数申明
int del_sem(int);
int sem_p(int);
int sem_v(int);

#endif /* SEM_COM_H */

/*************************************************/

 

/* sem_com.c */

#include "sem_com.h"             //引用文件sem_com.h

int init_sem(int sem_id, int init_value)     //信号量初始化(赋值)函数
{
 union semun sem_union;
 sem_union.val = init_value;                //init_value为初始值

 if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
 {
  perror("Initialize semaphore");  
  return -1;
 }
 return 0;
}

int del_sem(int sem_id)                //系统中删除信号量的函数
{
 //union semun sem_union;
 //if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
 if (semctl(sem_id, 0, IPC_RMID, 0) == -1)
 {
  perror("Delete semaphore");
  return -1;
 }
}

int sem_p(int sem_id)                   //p操作函数
{
 struct sembuf sem_b;
 sem_b.sem_num = 0; /*id*/          //单个信号量的编号应该为0
 sem_b.sem_op = -1; /* P operation*/             //表示p操作
 sem_b.sem_flg = SEM_UNDO;      //系统自动释放将会在系统中残留的信号量
 
 if (semop(sem_id, &sem_b, 1) == -1) /*1:first struct*/
 {
  perror("P operation");
  return -1;
 }
 return 0;
}

int sem_v(int sem_id)           //v操作函数
{
 struct sembuf sem_b;
 
 sem_b.sem_num = 0; /* id */              //单个信号量的编号应该为0
 sem_b.sem_op = 1; /* V operation */      //表示为v操作
 sem_b.sem_flg = SEM_UNDO; /* It's tracks be follow, to automatical free for it*/  //系统自动释放将会在系统中残留的信号量

 if (semop(sem_id, &sem_b, 1) == -1)
 {
  perror("V operation");
  return -1;
 }
 return 0;
}
/*****************************************************************/

 

/* sem_fork.c */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define DELAY_TIME 3      //为了突出演示效果,等待几秒钟

int main(void)
{
 pid_t result;
 int sem_id;

 sem_id = semget(ftok(".", 'a'),  1, 0666|IPC_CREAT); /* create a semaphore set */ //创建一个信号量
 init_sem(sem_id, 1);

 /* store the child's PID in result */
 result = fork();     //调用fork()函数  创建子进程
 
 if(result ==  -1)
  perror("fork failed");

 else if(result == 0){ /* child */   //返回值为0代表子进程
  sem_p(sem_id);        //p操作

  /*** enter critical section ***/
  fprintf(stdout, "Child process will wait for some seconds.../n");
  sleep(DELAY_TIME);
  fprintf(stdout, "The returned value is %d"
   " in the child process(PID = %d)/n", result, getpid());
  /*** leave critical section ***/

  sem_v(sem_id);     //v操作
 }

 else if(result > 0){ /* parent */ //返回值大于0代表父进程

  sem_p(sem_id);      //p操作

  /*** enter critical section ***/
  fprintf(stdout, "The returned value is %d"
   " in the father process(PID = %d)/n", result, getpid());
  /*** leave critical section ***/

  sem_v(sem_id);       //v操作

  /*** delete the semaphore ***/
  wait(NULL);
  del_sem(sem_id);
 }
 
 exit(0);
}
/**************************************************************/

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值