| 首页 | 学习 | 计算机 | 小说 | 动漫 | 论文 | 军事 | 科技 | 教育 | 哲学 | 历史 | 英语 | 笑话 | 
您现在的位置: 【书斋】 >> 计算机 >> 程序编程 >> 计算机正文 用户登录 新用户注册
信号量使用方法的例子           ★★
信号量使用方法的例子
Unix semaphore example
/* semabinit.c - initialize a semaphore for use by programs sema and semb */

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

/* The semaphore key is an arbitrary long integer which serves as an
external identifier by which the semaphore is known to any program
that wishes to use it. */

#define KEY (1492)

void main()
{
int id; /* Number by which the semaphore is known within a program */

/* The next thing is an argument to the semctl() function. Semctl()
does various things to the semaphore depending on which arguments
are passed. We will use it to make sure that the value of the
semaphore is initially 0. */

union semun {
int val;
struct semid_ds *buf;
ushort * array;
} argument;

argument.val = 0;

/* Create the semaphore with external key KEY if it doesn't already
exists. Give permissions to the world. */

id = semget(KEY, 1, 0666 | IPC_CREAT);

/* Always check system returns. */

if(id < 0)
{
fprintf(stderr, "Unable to obtain semaphore. ");
exit(0);
}

/* What we actually get is an array of semaphores. The second
argument to semget() was the array dimension - in our case
1. */

/* Set the value of the number 0 semaphore in semaphore array
# id to the value 0. */

if( semctl(id, 0, SETVAL, argument) < 0)
{
fprintf( stderr, "Cannot set semaphore value. ");
}
else
{
fprintf(stderr, "Semaphore %d initialized. ", KEY);
}
}

/* Semaphore example program a (sema.c) */
/* We have two programs, sema and semb. Semb may be initiated at any
time, but will be forced to wait until sema is executed. Sema and
semb do not have to be executed by the same user! */

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

#define KEY (1492)
/* This is the external name by which the semaphore is known to any
program that wishes to access it. */

void main()
{
int id; /* Internal identifier of the semaphore. */
struct sembuf operations[1];
/* An "array" of one operation to perform on the semaphore. */

int retval; /* Return value from semop() */

/* Get the index for the semaphore with external name KEY. */
id = semget(KEY, 1, 0666);
if(id < 0)
/* Semaphore does not exist. */
{
fprintf(stderr, "Program sema cannot find semaphore, exiting. ");
exit(0);
}

/* 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;

/* So do the operation! */
retval = semop(id, operations, 1);

if(retval == 0)
{
printf("Successful V-operation by program sema. ");
}
else
{
printf("sema: V-operation did not succeed. ");
perror("REASON");
}
}

/* Think carefully about what the V-operation does. If sema is executed
twice, then semb can execute twice. */

/* Semaphore example program b (semb.c) */
/* We have two programs, sema and semb. Semb may be initiated at any
time, but will be forced to wait until sema is executed. Sema and
semb do not have to be executed by the same user! */

/* HOW TO TEST:
Execute semb &
The & is important - otherwise you would have have to move to

[1] [2] 下一页  

a different terminal to execute sema.

Then execute sema.
*/

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

#define KEY (1492)
/* This is the external name by which the semaphore is known to any
program that wishes to access it. */

void main()
{
int id; /* Internal identifier of the semaphore. */
struct sembuf operations[1];
/* An "array" of one operation to perform on the semaphore. */

int retval; /* Return value from semop() */

/* Get the index for the semaphore with external name KEY. */
id = semget(KEY, 1, 0666);
if(id < 0)
/* Semaphore does not exist. */
{
fprintf(stderr, "Program semb cannot find semaphore, exiting. ");
exit(0);
}

/* Do a semaphore P-operation. */
printf("Program semb about to do a P-operation. ");
printf("Process id is %d ", getpid());

/* 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;

/* So do the operation! */
retval = semop(id, operations, 1);

if(retval == 0)
{
printf("Successful P-operation by program semb. ");
printf("Process id is %d ", getpid());
}
else
{
printf("semb: P-operation did not succeed. ");
}
}

/* Think carefully about what the V-operation does. If sema is executed
twice, then semb can execute twice. *

上一页  [1] [2] 

[1]

在百度搜索:信号量使用方法的例子
  • 上一个计算机:

  • 下一个计算机:
  • 相 关 文 章
  • 基于Linux的网络数据帧捕

  • 使用UDP协议的echo核心守

  • C语言编程的排序方法

  • 创建和使用库:静态、共

  • C语言图像处理方法

  • 通用子目录文件显示方法

  • C语言创建自己的设备(一

  • Linux 下面使用 mtrace 

  • 在C语言下使用I/O端

  • 使用C中自带的驱动去改变

  • Linux 守护进程的编程方

  • C语言中使用环境变量的技

  • Socket编程例子:TCP She

  • 信号(signal)介绍

  • Linux下C语言编程--信号

  • 创建和使用库:静态、共

  • 在MySQL数据库中使用C执

  • 使用 GDB 调试 Linux 软

  • 关于输入环状图形的三种

  • 文件加锁的例题示范

  • 数组(一维数组的使用)

  • 二叉树的几种运算方法

  • 哈希表处理冲突的方法及

  • 哈希表的概念作用及意义

  • 二维数组的实现方法

  • C语言常用的三种排序方法

  • 排序的各种方法

  • 排序及查找方法

  • 堆排序算法的过程演示

  • 小型的文本编辑器(使用