semctl函数

本文详细介绍了Linux系统中用于信号量操作的semctl函数,包括参数解析和使用示例。通过示例展示了如何创建、设置、获取信号量值以及删除信号量数组。同时,还展示了如何查询信号量数组的相关信息,如权限、时间戳和信号量计数。
摘要由CSDN通过智能技术生成

semctl函数

int semctl(int semid, int semnum, int cmd, …);
@semid:信号量数组标识
@semnum:要操作的信号量数组的信号量下标
@cmd:
IPC_STAT 查询此信号量数组的数据存入arg.buf(buf为struct semid_ds结构体指针)
IPC_RMID 删除指定semid的信号量数组
GETVAL 获取信号量的当前值,
SETVAL 设置信号量的值,初始化要用的命令

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

#if 0


struct semid_ds {
	struct ipc_perm	sem_perm;		/* permissions .. see ipc.h */
	__kernel_time_t	sem_otime;		/* last semop time */
	__kernel_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 short	sem_nsems;		/* no. of semaphores in array */
};

struct ipc_perm
{
	__kernel_key_t	key;
	__kernel_uid_t	uid;
	__kernel_gid_t	gid;
	__kernel_uid_t	cuid;
	__kernel_gid_t	cgid;
	__kernel_mode_t	mode; 
	unsigned short	seq;
};
	

union semun stat = {0};
struct semid_ds semds = {0};
stat.buf = &semds;
semctl(semid,0,IPC_STAT,stat);

#endif

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
								(Linux-specific) */
};

int main()
{
	key_t key = ftok(".",97);

	printf("key = %x\n",key);
	int semid = semget(key,1,IPC_CREAT | 0600);
	if(semid < 0)
	{
		perror("semget error");
		return -1;
	}
	
	union semun initval = {0};
	initval.val = 1;
	semctl(semid,0,SETVAL,initval);
	int curval = semctl(semid,0,GETVAL,0);	
	printf("curval = %d\n",curval);

	//p操作
	struct sembuf buf = {0};
	buf.sem_num = 0;
	buf.sem_op = -1;
	semop(semid,&buf,1);

	union semun stat = {0};
	struct semid_ds semds = {0};
	stat.buf = &semds;
	semctl(semid,0,IPC_STAT,stat);
	printf("uid = %d,mode = %o,sem_otime = %ld,sem_nsems = %ld\n",semds.sem_perm.uid,semds.sem_perm.mode,semds.sem_otime,semds.sem_nsems);
	printf("getuid() = %d,time() = %ld\n",getuid(),time(NULL));

	semctl(semid,0,IPC_RMID,0);	//删除信号量
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值