Home > Back-end >  In writing Linux environment producer/consumer problem met with some difficulties, you can help us h
In writing Linux environment producer/consumer problem met with some difficulties, you can help us h

Time:09-20

There are three producers and consumers, four buffer size of 10, for the sake of simplicity, producers are 5 this constant, but do not know why I can't run, I also don't know how to debug, under Linux code like this:


/producer/consumer problem
#include
#include
#include
#include

Int=0, in out=0;//in and out for producers and consumers to use in the buffer array subscript
Int buffer [10]=,0,0,0,0,0,0,0,0,0 {0};//buffer size of 10
Sem_t mutex, empty and full;//mutex mutex for buffer pool, the empty says the number of empty buffer, full said full buffer number


Int produce_an_item () {//for producers
Int I=5;
return i;
}

//producer
Void * producter (void * arg) {

While (1) {
Int I=produce_an_item ();//get data

Sem_wait (& amp; The empty);
Sem_wait (& amp; Mutex);
Printf (" producers read data % d \ n ", I);
Buffer [in]=I;//the data into the buffer
In=% (+ 1) in 10;//the subscript forward
Sem_post (& amp; Mutex);
Sem_post (& amp; Full);
}

}

//consumer
Void * consumer (void * arg) {
While (1) {
Sem_wait (& amp; Full);
Sem_wait (& amp; Mutex);
Int I=buffer [out].//I on behalf of the read data from a buffer
Printf (" consumers to take out the data for: % d \ n ", I);
Buffer [out]=0;//read after this set to 0
Out=(out + 1) % 10;
Sem_post (& amp; Mutex);
Sem_post (& amp; The empty);
}

}
Int main () {


//create the producer thread
Pthread_t p1, p2, p3;
Pthread_create (& amp; P1, NULL, producter, NULL);
Pthread_create (& amp; P2, NULL, producter, NULL);
Pthread_create (& amp; P3, NULL, producter, NULL);

//create the consumer process
Pthread_t c1, c2, c3 and c4.
Pthread_create (& amp; C1, NULL, consumer, NULL);
Pthread_create (& amp; C2, NULL, consumer, NULL);
Pthread_create (& amp; C3, NULL, consumer, NULL);
Pthread_create (& amp; C4, NULL, consumer, NULL);

//initialize the mutex
Sem_init (& amp; The mutex, 0, 1);
Sem_init (& amp; The empty, 0, 10);
Sem_init (& amp; Full, 0, 0);

//running thread

Pthread_join (p1, NULL);
Pthread_join (p2, NULL);
Pthread_join (p3, NULL);
Pthread_join (c1, NULL);
Pthread_join (c2, NULL);
Pthread_join (c3, NULL);
Pthread_join (c4, NULL);

return 0;

}

Programs often run up, don't know why, head all dizzy,, please help to see

CodePudding user response:

55555 people?

CodePudding user response:

Don't run is what meaning compilation errors or run the didn't reflect or other?
Under Linux can use GDB to debug

CodePudding user response:

refer to the second floor 5250 response:
don't run is what meaning to compile error or run the didn't reflect or other?
Under Linux can use GDB to debug the

Don't run that

Press enter after has been stuck in here is not running (pc3 program name), just press CTRL + c to the end, don't know where is in addition to the problems, like the one above can perform occasionally, but most of the time it is stuck,
I also tried using GDB debugging, but can't read threads run exactly where the

CodePudding user response:

See if there are other processes are in GDB or running

CodePudding user response:

5250 reference 4 floor response:
to see if there are other processes are run GDB or

Hey,, should be the problem of the program itself

CodePudding user response:

Program you write is death cycle, there is no normal withdrawal mechanism, press CTRL + c to terminate the program, the signal will not be released,
Hang wait for these to run again, process semaphore, so your situation, you restart the computer program can run again,

CodePudding user response:

Linux and Windows on a semaphore operation is not the same as
Linux has been created is not destroyed even if there is a program exits, can use the ipcs view
-Windows are created, if program exits, the semaphore is destroyed automatically
  • Related