System and device programming lab3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了System and device programming lab3相关的知识,希望对你有一定的参考价值。
pthread_cond_wait(buf->notfull,&buf->lock);
notfull is a pointer,so don‘t need to use &.
if use int *p,needs to allocate space,but use int p[10],don‘t need
use gdb to detect segmentation fault, which usually due to malloc
/*pthread_cond_init: initialize a condition variable: must be done before any thread uses the condition variable for the first time*/
pthread_cond_init(b->notfull,NULL);check the urgent_Q first,which is for satisfying priority of serving urgent
service first
6.urgent_Q=(Buffer **)malloc(NUM_OFFICES*sizeof(Buffer *)); it‘s not Buffer
but Buffer*
7./*for(i=0;i<NUM_OFFICES;){
printf("i=%d\n",i);
//id[i]=rand()%5;
pjj=i;
i++;
pthread_create(&pj[i],NULL,office,&pjj);// &pj[i],not pj[i]
}*///this way will make order confused of value pjj which may have 1 2 3 3
so can use:
for(i=0;i<NUM_OFFICES;i++){
pjj[i]=i;
pthread_create(&pj[i],NULL,office,&pjj[i]);
}
8.For Buffer **answer_Q:
answer_Q=malloc(NUM_OFFICES*sizeof(Buffer *));
for(i=0;i<k;i++)
answer_Q[i]=B_init(MaxQue);
For Buffer *special_Q:
special_Q=B_init(MaxQue);
9. have declared some local variable in one function like this:
void* thread_function (void* parameter)
{
struct parameter * thread_data = (struct parameter *)parameter;
char buffer[20];
int temp;
}
Here if I have created two threads then in one thread if buffer & temp is updated so will it effect other thread ?
i mean if there are two thread then does there will be two copy of all local variable?
EDIT : then in which case i need to used thread specific data.? i mean pthread_setspecific & all such stuff
These variables are allocated on the stack, and each thread has its own stack: these variables are private to each thread (they are not shared). (See this answer for more details.)
If you assign thread_data to a global pointer, for example, other threads will be able to access thread_data via the global pointer.
Thread specific data (e.g. pthread_setspecific) is used to create variables that are global, but still specific to each thread (not shared): They are thread-specific global variables.
You need to use thread specific variables when you want global variables, but don‘t want to share them between threads.
conclusion:
take care of the types declared in the structure.
watch carefully in the picture,like every office has its own urgent_Q,
but answer_Q is for each student,so needs to use in different ways:
urgent_Q[office_no],answer_Q[info.id]
以上是关于System and device programming lab3的主要内容,如果未能解决你的问题,请参考以下文章
哪些 GPS 加密狗适用于 Windows 7 和 .Net 4.0 System.Device.Location 命名空间?
How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写
COMP 1043 Problem Solving and Programming
html 来自http://stackoverflow.com/questions/16504880/how-to-open-an-existing-program-in-client-device