Linux system programming 65 processes, inter-thread communication 3-semaphore array

NAME
       semget - get a System V semaphore set identifier :
创建一个新的信号量或获取一个已经存在的信号量的ID

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

/*
key:
If the creation and acquisition of the unrelated process is the same as the previous message queue.
If in the related process, after fork(), each child process can get the key value created by the parent process. At this time, you don’t care about the key value anymore. At this time, you can set it to IPC_PRIVATE, which means that the IPC is anonymous IPC and does not require ftok

nsems: Specify how many elements in the current semaphore array
semflg: other special requirements, when the key is IPC_PRIVATE, semflg is set to IPC_CREAT

*/

int semget(key_t key, int nsems, int semflg);


RETURN VALUE
       If successful, the return value will be the semaphore set identifier (a nonnegative integer), otherwise, -1 is returned, with errno indicating the error.

NAME
       semctl - System V semaphore control operations 
		对目标信号量执行各类操作,不过最常用的是删除它。
SYNOPSIS
       #include <sys/types.h>
       #include <sys/ipc.h>
       #include <sys/sem.h>

/*
semid: IPC ID
semnum: the subscript value of the target semaphore in the array
cmd: command

...
IPC_RMID:从系统中删除目标信号量集合
......
SETVAL:设置数组中下标为semnum的成员值,即设置某个信号量的资源量
......

最后参数:初始值,即资源总量


//设置ID为semid的信号量集合数组中 第1个信号量的资源总量为1
semctl(semid,0,SETVAL,1):

*/

   int semctl(int semid, int semnum, int cmd, ...);

RETURN VALUE
On failure, semctl() returns -1 with errno indicating the error.
Otherwise, the system call returns a nonnegative value depending on cmd as follows:


semop() operation semaphore collection

NAME
       semop, semtimedop - System V semaphore operations

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

	/*
semid:目标IPC ID
sops :结构体数组地址
nsops:每个结构体大小
*/
       int semop(int semid, struct sembuf *sops, size_t nsops);

The elements of this structure are of type struct sembuf, containing the following members:

		信号量编号,当使用单个信号量时候,为0
       unsigned short sem_num; 
       信号量操作,取值为-1,表示P操作。归还为+1,为释放操作
       short          sem_op;  
       short          sem_flg;  /* operation flags */

RETURN VALUE
If successful, semop() and semtimedop() return 0; otherwise they return -1 with errno indicating the error.

ERRORS
On failure, errno is set to one of the following:

   E2BIG  The argument nsops is greater than SEMOPM, the maximum number of operations allowed per system call.

   EACCES The calling process does not have the permissions required to perform the specified semaphore operations, and does not have the CAP_IPC_OWNER capability.

   EAGAIN 假错

   EFAULT An address specified in either the sops or the timeout argument isn't accessible.

   EFBIG  For some operation the value of sem_num is less than 0 or greater than or equal to the number of semaphores in the set.

   EIDRM  The semaphore set was removed.

   EINTR  While blocked in this system call, the thread caught a signal; see signal(7).

   EINVAL The semaphore set doesn't exist, or semid is less than zero, or nsops has a nonpositive value.

   ENOMEM The sem_flg of some operation specified SEM_UNDO and the system does not have enough memory to allocate the undo structure.

   ERANGE For some operation sem_op+semval is greater than SEMVMX, the implementation dependent maximum value for semval.

Experiment: semaphore use, 20 processes write the same file, there is only one semaphore in the semaphore set in this routine, and its function is similar to that of a mutex.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#define PROCNUM 20
#define FNAME "/home/mhr/Desktop/xitongbiancheng/super_io/out"
#define LINESIZE 1024
static 	int semid;

//取资源量
static void P(void)
{
	struct sembuf op;
	
	op.sem_num = 0;
	op.sem_op = -1;
	op.sem_flg = 0;

	while(semop(semid,&op,1) < 0)
	{
		if(errno != EINTR || error != EAGAIN)
		{
			perror("semop()");
			exit(1);
		}
	}
}

//归还资源量
static void V(void)
{
	struct sembuf op;
	
	op.sem_num = 0;
	op.sem_op = -1;
	op.sem_flg = 0;

	if(semop(semid,&op,1) < 0)
	{
		perror("semop()");
		exit(1);

	}
}

static void func_add(void)
{
	FILE *fp;
	int fd;
	char linebuf[LINESIZE];

	fp = fopen(FNAME,"r+");
	if(fp == NULL)
	{
		perror("fopen()");
		exit(1);
	}



P();//取资源量
	fgets(linebuf,LINESIZE,fp);
	fseek(fp,0,SEEK_SET);

	//sleep(1);	
	
	fprintf(fp,"%d\n",atoi(linebuf)+1);
	fflush(fp);
V();//归还资源量
	
	fclose(fp);
return;

} 

int main()
{
	int i,err;
	pid_t pid;

//创建信号量数组 IPC 返回值为id
	semid = semget(IPC_PRIVATE,1,IPC_CREAT);
	if(semid < 0)
	{
		perror("semget");
		exir(0);
	}

// 设置信号量集合中 第一个信号量的资源总量为1
	if(semctl(semid,0,SETVAL,1) < 0)
	{
		perror("semctl");
		exir(0);	
	}

	for(i = 0; i < PROCNUM; i++)
	{
		pid = fork();
		if(pid < 0)
		{
			perror("fork()");
			exit(1);
		}
		
		if(pid == 0)//Child 
		{
			func_add();
			exit(0);
		}

	}


	for(i = 0;i < PROCNUM; i++)
	{
		wait(NULL);
	}
	
	//从当前系统 删除该 信号量集合
	semctl(semid,0,IPC_RMID);

	exit(0);
	
}

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/114849643