System and device Programming LAB3

Source: Internet
Author: User

    1. Pthread_cond_wait (Buf->notfull,&buf->lock);

      Notfull is a pointer,so don ' t need to use &.

    2. If use int *p,needs to allocate space,but use int P[10],don ' t need

    3. Use GDB to detect segmentation fault, which usually due to malloc

    4. /*pthread_cond_init:initialize a condition variable:must be do before any thread uses the condition variable for the F Irst time*/
      Pthread_cond_init (B->notfull,null);

    5. 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-On-the-do 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. has 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 II threads then on one thread if buffer & Temp is updated so would it effect other thread?

I mean if there is both thread then does there would be is the 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 is allocated on the stack with each thread have its own Stack:these variables is private to each thread (They is not shared). (See this answer for more details.)

If you assign thread_data to a global pointer, for example, and other threads would be able to access Thread_data via the Globa L pointer.

Thread specific data (e.g. pthread_setspecific) is used to create variables that was global, but still specific to each th Read (not shared): They is thread-specific global variables.

You need the use of thread specific variables when you want global variables, but don ' t want to share them between threads.

Conclusion:

    1. Take care of the types declared in the structure.

    2. Watch carefully in the picture,like every office have its own urgent_q,

But answer_q are for each student,so needs to use in different ways:

Urgent_q[office_no],answer_q[info.id]

System and device Programming LAB3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.