Beispiel #1
0
int main(){

	int i=0;
	
	for(i=0;i<5;i++)
	{
		chopstick[i] = sem_creat();
	}

	i=0;

	pthread_t pthread[4];

	pthread_create(&pthread[0],NULL,philosopher0,NULL);
	pthread_create(&pthread[1],NULL,philosopher1,NULL);
	pthread_create(&pthread[2],NULL,philosopher2,NULL);
	pthread_create(&pthread[3],NULL,philosopher3,NULL);
	pthread_create(&pthread[4],NULL,philosopher4,NULL);


	i =0;
	while(i<5)
	{
		pthread_join(pthread[i],NULL);
		i++;
	}
	return 0;
}
Beispiel #2
0
int cgiMain(void) 
{    
	int ret;
	key_t key; 
	key_t key_cgi;
	int flag;

	int shm_id,sem_id, sem_id_cgi; 
	char *shm; 

	// 创建并关联共享内存
	key       = ftok("./vs_shm",0); 
	key_cgi  = ftok("./vs_shm_cgi",0); 
	shm_id  = shmget(key,SEGSIZE,0); 
	if(-1 == shm_id)
	{
		cgiHeaderContentType("text/html; charset=gb2312");
		puts("<html><head><title>share memeroy error</title><h1><font color='red'>create shared memory error</f><h1><html><head>"); 
		return -1; 
	} 

	shm = (char *)shmat(shm_id,0,0);
	if (-1 == (int)shm)
	{ 
		cgiHeaderContentType("text/html; charset=gb2312");
		puts(" attach shared memory error\n"); 
		return -1; 
	} 

	// 创建互斥锁
	sem_id     = sem_creat(key);                      // vs程序操作共享内存信号量
	sem_id_cgi = sem_creat(key_cgi);                  // 外部程序cgi操作共享内存信号量

	cgiFormInteger("flag", &flag, 1);
	if(1 == flag){				//getData
		cgi_rtv_get_action(shm);
		post_v(sem_id);             // 使能vs程序操作共享内存信号量
		wait_v(sem_id_cgi);

		ret = *(int *)shm;
		if(0 != ret )                          
		{
			char temp[32];
			sprintf(temp,"error is %d\n",ret);
			cgiHeaderContentType("text/html; charset=gb2312");
			puts("<html><head><title>rtv get error</title><h1><font color='red'>wrong username or password</f><h1>"); 
			puts(temp);
			return -1;    
		}
		
		cgi_rtv_puts(shm);

	}
	else if(0 == flag){
		cgi_rtv_set_action(shm);
		post_v(sem_id);             // 使能vs程序操作共享内存信号量
		wait_v(sem_id_cgi);

		ret = *(int *)shm;
		if(0 != ret )                          
		{
			char temp[32];
			sprintf(temp,"error is %d\n",ret);
			cgiHeaderContentType("text/html; charset=gb2312");
			puts("<html><head><title>login error</title><h1><font color='red'>wrong username or password</f><h1>"); 
			puts(temp);
			return -1;    
		}
		
		cgi_rtv_puts(shm);
	}

	shmdt(shm); 

	return 0; 
} 
Beispiel #3
0
int main(int argc, char *argv[])
{
	int procnum = 10;
	int loopnum = 100;
	int i = 0, j = 0;
	printf("please enter child process number:\n");
	scanf("%d", &procnum);
	printf("please enter child process test number:\n");
	scanf("%d", &loopnum);
	//create shm
	int ret = 0;
	int shmhdl = 0;
	ret = IPC_CreateShm(".", sizeof(int), &shmhdl);
	if (ret != 0) {
		printf("fun IPC_CreatShm() error:%d.\n", ret);
		return ret;
	}

	//create sem
	int semid = 0;
	ret = sem_creat(g_key, &semid);
	if (ret != 0) {
		if (ret == SEMERR_EEXIST) {
			ret = sem_open(g_key, &semid);
			if (ret != 0) {
				printf("fun sem_open() error, %d.\n", ret);
				return ret;
			}
		}
		else {
			return ret;
		}
	}
	int val = 0;
	ret = sem_getval(semid, &val);
	if (ret != 0) {
		printf("func sem_getval() err:%d.\n", ret);
		return ret;
	}
	printf("sem val is %d.\n", val);
//	pid_t pid;
//	for (i = 0; i < procnum; i++) {
//		pid = fork();
//		if (pid == 0) {
//			for (j = 0; j < loopnum; ++j) {
//				TestFunc(loopnum);
//			}
//			exit(0);
//		}
//	}
//	int childpid = 0;
//	while ((childpid = waitpid(-1, NULL, 0)) > 0) {
//		;
//	}
	pthread_t tidarr[200];
	for (; i < procnum; ++i) {
		pthread_create(&tidarr[i], NULL, thread_routine, NULL);
	}
	
	for (i = 0; i < procnum; ++i) {
		pthread_join(tidarr[i], NULL);
	}

	printf("parent process exit.\n");
	return 0;
}