信号量使用方法的比如-C / C++-优质IT资源分享社区

admin
管理员
管理员
  • UID1
  • 粉丝29
  • 关注4
  • 发帖数581
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:268回复:0

  信号量使用方法的比如

楼主#
更多 发布于:2016-05-22 14:59

Unix semaphore example

/* semabinit.c - initialize a semaphore for use by

programs sema and semb */

#include

#include

#include

#include

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

#include

#include

#include

#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;

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

a different terminal to execute sema.

Then execute sema.

*/

#include

#include

#include

#include

#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;

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

优质IT资源分享社区为你提供此文。

本站有大量优质C、C++教程视频,资料等资源,包含C,C++基础教程,高级进阶教程等等,教程视频资源涵盖传智播客,极客学院,达内,北大青鸟,猎豹网校等等IT职业培训机构的培训教学视频,价值巨大。欢迎点击下方链接查看。

C、C++教程视频

优质IT资源分享社区(www.itziyuan.top)
一个免费,自由,开放,共享,平等,互助的优质IT资源分享网站。
专注免费分享各大IT培训机构最新培训教学视频,为你的IT学习助力!

!!!回帖受限制请看点击这里!!!
!!!资源失效请在此版块发帖说明!!!

[PS:按 CTRL+D收藏本站网址~]

——“优质IT资源分享社区”管理员专用签名~

本版相似帖子

游客