C4怎么使用信号量?
c4droid吧
全部回复
仅看楼主
level 13
savage200 楼主
我写过linux下的哲学家思考的问题,近日想移植到c4,发现不支持信号量,哪位大神知道,烦解惑一下,谢谢了!
2016年08月06日 18点08分 1
level 13
savage200 楼主
对了,还有多线程,我写是也是linux环境的,在c4也不行,大神给指点下开多线程!
2016年08月06日 18点08分 2
.
2016年08月07日 02点08分
2016年08月07日 02点08分
回复 savage200 :我也不太能玩
2016年08月07日 06点08分
level 11
信号量没用过,不清楚。至于开多线程,你可以试试sdl2,不过你的输出形式得改一下
2016年08月07日 03点08分 3
level 13
看着好刁...帮你顶帖
2016年08月07日 03点08分 4
level 13
基本上,你说说你会用到哪些头文件?线程的话pthread和std::thread都可以用啊。
<sys/xxx>的头文件也蛮多,你说的信号量是多线程中用到的东西么,多线程C4还是支持的,我不明白泪,你为啥要还到C4上写,,,,
2016年08月07日 07点08分 5
哦,抱歉没看标题,多线程的东西应该都支持
2016年08月07日 08点08分
pthread,std::thread有教程吗?
2016年08月07日 09点08分
楼下是我的声明
2016年08月07日 09点08分
@DXKite 帮我看看10楼
2016年08月09日 01点08分
level 13
savage200 楼主
////////////////////////////////////////
//File : PhilosopherDining.c
//Author : Savage
//About : philosopher dining problem
//Savage (C) 2007
//EM:[email protected]
////////////////////////////////////////
////////////////HEAD FILES//////////////
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <unistd.h>
#include <curses.h>
///////////////MACRO DEFINITION//////////////
#define N 5//the number of philosaphers
#define K 3+1//philosopher's state Kinds:3 + block time title num
#define THINKING 0//philosopher's thinking state
#define HUNGRY 1//philosopher's hungry state
#define EATING 2//philosopher's eating state
#define F ' '//representation of philosopher's state on screen
#define T -10//representation of philosopher's state on screen
///////GLOBE VARIABLE DECLARE AREA///////
double RunTime;//process execute_time
int state[N];//record philosophers's state
char scrstate[K-1][N];//record flag for screen
char LTitle[K][10]={"Thinking :"," Hungry :"," Eating :","WaitTime :"};//left title
int semid;//semaphore id
time_t start;//record rocking time
bool printbln[N];//boolean variable for contrl print block clocks
time_t blkclk[N];//record block time
//////////FUNCTIONS DECLARE AREA//////////
void P(int);//mutex semaphore P operate:semaphore - 1
void V(int);//mutex semaphore V operate:semaphore + 1
void initialize();//initialize semaphore ,philosophers' state,and so on
void OpenThread();//open thread for each philosopher
void philosopher(int);//philosopher's behavior
void iniscr();//initialize and start text graph mode
void exitscr();//exit text graph mode
void test(int);//To test the philosopher's state who is beside him
void ShowState();//Print the philosopher's state
void Printblkclk();//print philosapher blocked time
///////////////////MAIN PROCCESS/////////////////
main()
{
printf("*******Initializing...*******\n");
2016年08月07日 09点08分 6
这是linux环境的,C4中找不到对应的声明,目前移植受阻!
2016年08月07日 09点08分
@savage200 不知道你是怎么移植的,我看了一下include里面的文件,除了curses.h其他的都有,还有,你不会看看报错缺啥头文件?
2016年08月07日 23点08分
@DXKite curses.h头文件应该是个控制台图形库文件,非必需的,你可以自己弄,或者用:@李登纯 你看看能不能给他弄个适配还是啥子的,,,啊哈哈哈。。。。
2016年08月07日 23点08分
@DXKite 问题在10楼↓
2016年08月08日 19点08分
level 13
android有自己的一套IPC框架
2016年08月07日 13点08分 8
如何移植啊,没头绪
2016年08月07日 18点08分
难道要我自己写一套信号量的操作
2016年08月07日 18点08分
level 12
pthread和fork都能用 不知道你原来用什么实现的
2016年08月08日 06点08分 9
问题出在线程,在10楼
2016年08月09日 01点08分
level 13
savage200 楼主
问题出在开线程这↓
void OpenThread()
{
int i;
pthread_t id[N];
pthread_attr_t attr;
pthread_attr_init(&attr); //set philosapher thread's attribute
pthread_attr_setscope(&attr,PTHREAD_SCOPE_PROCESS); //No binding
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//detached
start=time(NULL); //Start rocking time;
srand(start);//initialize random
ShowState(); //Show the philosopher's begining state
for(i=0;i<N;i++)//Creat N thread for N philosopher
{
if(pthread_create(&id[i],&attr,(void *) philosopher,(void *) i)!=0)
{
printf("Create the %d thread error!\n",i);
exit(1);
}
pthread_join(id[i],NULL);
}
if(pthread_create(&id[i],&attr,(void *) Printblkclk,NULL)!=0)
{
printf("Create the print block clocks thread error!\n");
exit(1);
}
pthread_join(id[i],NULL);
}
2016年08月08日 19点08分 10
开线程的方法不对,学习中。。。
2016年08月08日 20点08分
设置线程属性时出错,报错无些函数
2016年08月09日 00点08分
@savage200 调用了未定义函数?那就换个嘛。
2016年08月09日 02点08分
1