www.pudn.com > LFYOS.zip > sem_al.c


#include"kernel.h" 
 
#define WRONG_PROCESS_NUMBER		-1 
#define WRONG_PROCESS_STATE 		-2 
#define WRONG_CAPABILITY		-3 
#define CREATE_TOO_MANY_SEMAPHORE	-4 
#define NO_SEMAPHORE			-5 
 
int allocate_semaphore(int process_id,int value, 
	struct capability *process_capability, 
	struct capability *semaphore_capability) 
{ 
	int sem_id; 
	struct process *p; 
	struct semaphore *s; 
	struct kernel_time my_time; 
 
	if((process_id<=0)||(process_id>=PROCESS_NUMBER)) 
		return WRONG_PROCESS_NUMBER; 
	p=&(os->process[process_id]); 
	if(!KERNEL_COMPARE_CAPABILITY(p->capability, 
			(*process_capability))) 
		return WRONG_CAPABILITY; 
	if(p->semaphore_number>=p->max_semaphore_number) 
		return CREATE_TOO_MANY_SEMAPHORE; 
	if((sem_id=os->process[0].semaphore_ring)<0) 
		return NO_SEMAPHORE; 
	s=&(os->semaphore[sem_id]); 
	s->value=value; 
	s->thread_ring=-1; 
	s->thread_id=(-1); 
	COPY_CAPABILITY((*semaphore_capability),(s->capability)); 
	move_semaphore(sem_id,process_id); 
 
	SET_MOST_TIME(my_time); 
	SET_MINIMAL_STEP(s->step); 
	s->v_value=(-1); 
	set_semaphore_time(sem_id,&my_time); 
 
	return sem_id; 
}