Ejemplo n.º 1
0
int main(int argc,char *argv)
{
	pid_t pid;
	int i;
	int value;
    key_t key;
	int status;
	if((pid=fork())==-1)
	{
		perror("fork");
		exit(EXIT_FAILURE);
	}
	else if(pid==0)
	{	
		if((sem_id=semget((key_t)123456,1,IPC_CREAT|0770))==-1)
		{
			perror("semget");
			exit(EXIT_FAILURE);
		}
		if (!set_semvalue()) 
		{
			fprintf(stderr, "Failed to initialize semaphore\n");
			exit(EXIT_FAILURE);
		}
		
			value=get_semvalue();
			printf("this is child,the current value is %d\n",value);
			if(!semaphore_v())
			{
				fprintf(stderr, "Failed to v operator\n");
				exit(EXIT_FAILURE);
			}
			value=get_semvalue();
			printf("the child %d V operator,value=%d\n",i,value);
		
		printf("child exit success\n");
		exit(EXIT_SUCCESS);	
	}
	else	//parent
	{
		sleep(3);
		if((sem_id=semget((key_t)123456,1,IPC_CREAT|0770))==-1)
		{
			perror("semget");
			exit(EXIT_FAILURE);
		}

			value=get_semvalue();
			printf("this is parent ,the current value is %d\n",value);
		printf("the parent will remove the sem\n");
		if(semctl(sem_id,0, IPC_RMID,(struct msquid_ds*)0)==-1)
		{
			perror("semctl");
			exit(EXIT_FAILURE);
		}
		return 0;
	}
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	pid_t pid;
	int value;
	key_t key;
	
	if((pid=fork()) == -1){
		perror("fork");	
		exit(EXIT_FAILURE);
	}else if (pid == 0){
		if((sem_id=semget((key_t)123456, 1, 0770|IPC_CREAT)) == -1){
			perror("semget");	
			exit(EXIT_FAILURE);
		}
		if(!set_semvalue()){
			printf("failed initialized semaphore\n");
			exit(EXIT_FAILURE);	
		}
		value = get_semvalue();
		printf("child: the current value is %d\n",value);
		if(!semaphore_v()){
			printf("failed to v operator\n");
			exit(EXIT_FAILURE);
		}
		value = get_semvalue();
		printf("child: v operator, value=%d\n", value);
		printf("child: exit success\n");
		exit(EXIT_SUCCESS);	
	}else{
		sleep(3);
		if((sem_id = semget((key_t)123456, 1, 0770|IPC_CREAT)) == -1){
			perror("semget");
			exit(EXIT_FAILURE);	
		}
		value=get_semvalue();
		printf("parent: the current value is %d\n", value);
		printf("parent: will remove the sem\n");
		if(semctl(sem_id, 0, IPC_RMID, (struct msquid_ds *)0) == -1){
			perror("semclt");
			exit(EXIT_FAILURE);	
		}
	}
	return 0;
}
Ejemplo n.º 3
0
static int set_semvalue(void)
{
    union semun sem_union;
	int value;
    sem_union.val = 5;
    if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0);
    printf("set value success,");
	printf("init value is %d\n",get_semvalue());
	return(1);
}
Ejemplo n.º 4
0
static int set_semvalue(void)
{
	union semun sem_union;
	int value = 5;
	sem_union.val = value;
	if((semctl(sem_id, 0, SETVAL, sem_union)) == -1){  // set value 5
		return 0;	
	}
	printf("set value success\n");
	printf("init value is %d\n", get_semvalue());
	return 1;	
}
Ejemplo n.º 5
0
/************************************************************
 * function:  create or open the shared memory.
 * iscreated: 1, to create the shm; 0, only to open the shm.
 * mqsize:    the count of the items
 * itemsize:  the size of an item (bytes)
 * return:    0, correct; others, error number.
 ***********************************************************/
int shmmq_open(int iscreated, int mqsize, int itemsize)
{
	int nflag;
	int ret = 0;

	/* Initial semaphore */
	g_s_semid = get_semvalue((key_t)SEM_KEY);
	if (-1 == g_s_semid) {
		return ERROR_GET_SEMVALUE;
	}
	if (iscreated && -1 == set_semvalue(g_s_semid)) {
		return ERROR_SET_SEMVALUE;
	}

	if (-1 == semaphore_p(g_s_semid)) {
		return ERROR_SEMAPHORE_P;
	}
	while (1) 
	{
		if (iscreated) {
			nflag = 0666 | IPC_CREAT;
		} else {
			nflag = 0666;
		}

		g_s_mqsize = (mqsize <= 0) ? DEFAULT_MQ_SIZE : mqsize;
		g_s_itemsize = (itemsize <= 0) ? DEFAULT_ITEM_SIZE : itemsize;

		/* Initial shared memory */
		g_s_shmid = shmget((key_t)SHM_KEY, 
			g_s_mqsize * g_s_itemsize + DEFAULT_HEAD_SIZE,
			nflag);
		if (-1 == g_s_shmid) {
			ret = ERROR_SHMGET;
			break;
		}

		g_s_pbase = shmat(g_s_shmid, NULL, 0);
		if ((void *)-1 == g_s_pbase) {
			g_s_pbase = NULL;
			ret = ERROR_SHMAT;
			break;
		}

		g_s_phead = (shmmq_head*)g_s_pbase;
		g_s_pcontent = g_s_pbase + DEFAULT_HEAD_SIZE;

		/* Initial head of the shared memory  */
		if (iscreated) {
			g_s_phead->enq_pos = -1;
			g_s_phead->deq_pos = -1;
		}
		break;
	}
	if (ret && iscreated) {
		if (g_s_shmid >= 0) shmctl(g_s_shmid, IPC_RMID, 0);
		semaphore_v(g_s_semid);
		del_semvalue(g_s_semid);
		return ret;
	}

	if (-1 == semaphore_v(g_s_semid)) {
		return ERROR_SEMAPHORE_V;
	}

	return ret;
}