Dining philosophers and mutex initialization - c++

I am trying to solve dining philosophers problem.So I pretty much made entire code but the problem is that I can't initialize monitors(i've made pseudocode which I re-written in c++) so really I can't test the program. Can anyone help me and say what's the issue with initialization of monitor/mutex_init ?
I get error on line 18 and it goes like:
error: ‘int pthread_mutex_init’ redeclared as different kind of symbol
changing it into int pthread_mutex_init(&monitor,NULL); WON'T work !
by adding
int pthread_mutex_init(pthread_mutex_t *monitor, NULL);
I get error: expected identifier before ‘__null’
#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;
char v_filozofi[5]={'O'}; //vizualni prikaz filozofa
int stapic[5]={1}; //stapici za filozofe
int broj[5]; //shema koju sam mora sloziti da imam broj filozofa
pthread_t d_filozofi[5]; //dretve filozofa,philosopher's thread
pthread_cond_t red_uvjeta[5];
pthread_mutex_t monitor; //deklariramo monitor,tj mymutex
int pthread_mutex_init(*monitor,NULL);
void ispisi_stanje(int n){
for(int i = 0; i < 5 ;i++) cout<< v_filozofi[i];
cout<<"("<< n+1 << ")" <<endl;
}
void misliti(int n){
cout<<"Mislim " << endl;
sleep(4);
}
void jesti(int n){
pthread_mutex_lock(&monitor);
v_filozofi[n]='o';
while(stapic[n]==0 || stapic[n+1]%5==0){//gleda ima li lijevi i desni
//stapic na raspolaganju
pthread_cond_wait(&red_uvjeta[n],&monitor);
}
stapic[n] = stapic[(n+1)%5] = 0;
v_filozofi[n] = 'X';
ispisi_stanje(n);
pthread_mutex_unlock(&monitor);
sleep(2);
pthread_mutex_lock(&monitor);
v_filozofi[n] = 'O';
stapic[n] = stapic[(n+1)%5] = 1;
pthread_cond_signal(&red_uvjeta[(n-1)%5]);
pthread_cond_signal(&red_uvjeta[(n+1)%5]);
ispisi_stanje(n);
pthread_mutex_unlock(&monitor);
}
void * filozof(void *n){
int br_fil = *((int *)n);
while(1){
misliti(br_fil);
jesti(br_fil);
}
return 0;
}
//MAIN
int main(){
for(int i=0;i<5;i++){
broj[i] = i;
pthread_cond_init(&red_uvjeta[i],NULL);
}
for(int i=0;i<5;i++){
sleep(1);
pthread_create( &d_filozofi[i],NULL,filozof,&broj[i]);
}
for(int i=0;i<5;i++) pthread_join(d_filozofi[i],NULL);
pthread_mutex_destroy(&monitor);
return 0;
}

remove the line
int pthread_mutex_init(*monitor,NULL);
and instead do
pthread_mutex_init(&monitor,NULL);
at the beginning of your main function. (and check that it returns 0)
As it is you are declaring the function not calling it, and since it's already declared you get an error

This function expects a pointer, and why did you put "int" before the call? I think you confused a prototype (which is unecessary) and an actual call to the function.
int pthread_mutex_init(*monitor,NULL);
So:
pthread_mutex_init(&monitor,NULL);
http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_init.html

once take a look at this one nicely given
http://nitish712.blogspot.in/search/label/mutex
http://nitish712.blogspot.in/2012/09/classical-ipc-problems.html

Related

error - "no matching function for call to 'show2D'" while trying to call the function; why this error?

In the code that I have attached here, I have written the function called "show2D" and then I want to run this function inline 21 but it is showing an error there saying "no matching function for call to 'show2D'"
#include <iostream>
using namespace std;
void show2D(int variable[][20]){
int answer[4000];
int n=0;
for (int i=0; i<20;i++){
for(int j=0;j<20;j++){
if (variable[i][j]%2==1){
answer[n]=variable[i][j];
n++;
}
}
}
}
int main() {
int trailarr[2][2];
trailarr[0][0] = 0;
trailarr[0][1] = 1;
trailarr[1][0] = 2;
trailarr[1][1] = 3;
show2D(trailarr);
return 0;
}
Because trailarr is a [2][2] array and show2D expects an array of size variable[][20]. So the linker does not find any function called show2D that can accept a [2][2] array.

Weird symbols when reading data from a file?

I have a program that is meant to take data from 2 separate text files, place them in arrays, compare them to each other, and output certain results. However, when I read the data from the files and try to display the data, I get a lot of weird symbols before it finally displays all the data in the text files. Here is the code.
// Ch07-Exam Grader.cpp : Defines the entry point for the console application.
//
//Libraries
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes
void initialization(void);
void proccess(void);
void eoj(void);
void writeIt(void);
void readIt(void);
void calculate(void);
//Global Variables
ifstream student;
ifstream correct;
int main()
{
initialization();
return 0;
}
//This function opens the files and calls the function to send the data into the array
void initialization (void){
correct.open("CorrectAnswers.txt");
readIt();
student.open("StudentAnswers.txt");
readIt();
}
void proccess (char c[], char s[], int length){
int correctCount = 0;
int incorrectCount = 0;
for (int i = 0; i< length; i++){
if (s[i] == c[i]){
correctCount = correctCount + 1;
} else {
incorrectCount = incorrectCount + 1;
}
}
}
void eoj (void){
}
void writeIt (void){
}
//This function will take the data and place it into seperate arrays
void readIt (void){
char studentArray[20]; //Array to hold the student answers
char correctArray[20]; //Array to hold the correct answers
//Loops to place data to seperate arrays
for (int i = 0; !correct.eof(); i++){
correct >> correctArray[i];
}
for (int j = 0; !student.eof(); j++){
student >> studentArray[j];
}
for (int i = 0; i < 20; i++){
cout << studentArray[i] <<endl;
}
proccess(correctArray, studentArray, 20);
}
void calculate (void){
}
And this is the result:
Only the letters are a part of the text file.
Why do you call readIt() twice in initialization() function? It looks like readIt() expects that both files are open, but you open first file, call readIt(), open second file, call readIt() again. May be the reason of the problem in this defect?

parallel_pipeline not terminating

i am using parallel_pipeline function in my code.Sometimes when my condition is satisfied it stops the pipeline and sometimes it does not.When the flow control calls stop even after that it does not terminate instead it calls its next part and prints on console and then console output becomes like it is been staying in infinite loop and doing nothing.
Code is :
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <tbb/pipeline.h>
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/compat/thread>
#include <tbb/tbbmalloc_proxy.h>
#include <tbb/tick_count.h>
using namespace std;
using namespace tbb;
#define pi 3.141593
#define FILTER_LEN 265
double coeffs[ FILTER_LEN ] =
{
0.0033473431384214393,0.000032074683390218124,0.0033131082058404943,0.0024777666109278788,
-0.0008968429179843104,-0.0031973449396977684,-0.003430943381749411,-0.0029796565504781646,
-0.002770673157048994,-0.0022783059845596586,-0.0008531818129514857,0.001115432556294998,
0.0026079871108133294,0.003012423848769931,0.002461420635709332,0.0014154004589753215,
0.00025190669718400967,-0.0007608257014963959,-0.0013703600874774068,-0.0014133823230551277,
-0.0009759556503342884,-0.00039687498737139273,-0.00007527524701314324,-0.00024181463305012626,
-0.0008521761947454302,-0.00162618205097997,-0.002170446498273018,-0.002129903305507943,
-0.001333859049002249,0.00010700092934983156,0.0018039564602637683,0.0032107930896349583,
0.0038325849735515363,0.003416201274366522,0.002060848732332109,0.00017954815260431595,
-0.0016358832300944531,-0.0028402136847527387,-0.0031256650498727384,-0.0025374271571154713,
-0.001438370315670195,-0.00035115295209013755,0.0002606730012030533,0.0001969569787142967,
-0.00039635535951198597,-0.0010886127490608972,-0.0013530057243606405,-0.0008123200399262436,
0.0005730271959526784,0.0024419465938120906,0.004133717273258681,0.0049402122577746265,
0.0043879285604252714,0.002449549610687005,-0.00040283102645093463,-0.003337730734820209,
-0.0054508346511294775,-0.006093057767824609,-0.005117609782189977,-0.0029293645861970417,
-0.0003251033117661085,0.0018074390555649442,0.0028351284091668164,0.002623563404428517,
0.0015692864792199496,0.0004127664681096788,-0.00009249878881824428,0.0004690173244168184,
0.001964334172374759,0.0037256715492873485,0.004809640399145206,0.004395274594482053,
0.0021650921193604,-0.0014888595443799124,-0.005534807968511709,-0.008642334104607624,
-0.009668950651149259,-0.008104732391434574,-0.004299972815463919,0.0006184612821881392,
0.005136551428636121,0.007907786753766152,0.008241212326068366,0.00634786595941524,
0.003235610213062744,0.00028882736660937287,-0.001320994685952108,-0.0011237433853145615,
0.00044213409507615003,0.0022057106517524255,0.00277593527678719,0.0011909915058737617,
-0.0025807757230413447,-0.007497632882437637,-0.011739520895818884,-0.013377018279057393,
-0.011166543231844196,-0.005133056165990026,0.0032948631959114935,0.011673660427968408,
0.017376415708412904,0.018548938130314566,0.014811760899506572,0.007450782505155853,
-0.001019540069785369,-0.007805775815783898,-0.010898333714715424,-0.00985364043415772,
-0.005988406030111452,-0.001818560524968024,0.000028552677472614846,-0.0019938756495376363,
-0.007477684025727061,-0.013989430449615033,-0.017870518868849213,-0.015639422062597726,
-0.005624959109456065,0.010993528170353541,0.03001263681283932,0.04527492462846608,
0.050581340787164114,0.041949186532860346,0.019360612460662185,-0.012644336735920483,
-0.0458782599058412,-0.07073838953156347,-0.0791205623455818,-0.06709535677423759,
-0.03644544574795176,0.005505370370858695,0.04780486657828151,0.07898800597378192,
0.0904453420042807,0.07898800597378192,0.04780486657828151,0.005505370370858695,
-0.03644544574795176,-0.06709535677423759,-0.0791205623455818,-0.07073838953156347,
-0.0458782599058412,-0.012644336735920483,0.019360612460662185,0.041949186532860346,
0.050581340787164114,0.04527492462846608,0.03001263681283932,0.010993528170353541,
-0.005624959109456065,-0.015639422062597726,-0.017870518868849213,-0.013989430449615033,
-0.007477684025727061,-0.0019938756495376363,0.000028552677472614846,-0.001818560524968024,
-0.005988406030111452,-0.00985364043415772,-0.010898333714715424,-0.007805775815783898,
-0.001019540069785369,0.007450782505155853,0.014811760899506572,0.018548938130314566,
0.017376415708412904,0.011673660427968408,0.0032948631959114935,-0.005133056165990026,
-0.011166543231844196,-0.013377018279057393,-0.011739520895818884,-0.007497632882437637,
-0.0025807757230413447,0.0011909915058737617,0.00277593527678719,0.0022057106517524255,
0.00044213409507615003,-0.0011237433853145615,-0.001320994685952108,0.00028882736660937287,
0.003235610213062744,0.00634786595941524,0.008241212326068366,0.007907786753766152,
0.005136551428636121,0.0006184612821881392,-0.004299972815463919,-0.008104732391434574,
-0.009668950651149259,-0.008642334104607624,-0.005534807968511709,-0.0014888595443799124,
0.0021650921193604,0.004395274594482053,0.004809640399145206,0.0037256715492873485,
0.001964334172374759,0.0004690173244168184,-0.00009249878881824428,0.0004127664681096788,
0.0015692864792199496,0.002623563404428517,0.0028351284091668164,0.0018074390555649442,
-0.0003251033117661085,-0.0029293645861970417,-0.005117609782189977,-0.006093057767824609,
-0.0054508346511294775,-0.003337730734820209,-0.00040283102645093463,0.002449549610687005,
0.0043879285604252714,0.0049402122577746265,0.004133717273258681,0.0024419465938120906,
0.0005730271959526784,-0.0008123200399262436,-0.0013530057243606405,-0.0010886127490608972,
-0.00039635535951198597,0.0001969569787142967,0.0002606730012030533,-0.00035115295209013755,
-0.001438370315670195,-0.0025374271571154713,-0.0031256650498727384,-0.0028402136847527387,
-0.0016358832300944531,0.00017954815260431595,0.002060848732332109,0.003416201274366522,
0.0038325849735515363,0.0032107930896349583,0.0018039564602637683,0.00010700092934983156,
-0.001333859049002249,-0.002129903305507943,-0.002170446498273018,-0.00162618205097997,
-0.0008521761947454302,-0.00024181463305012626,-0.00007527524701314324,-0.00039687498737139273,
-0.0009759556503342884,-0.0014133823230551277,-0.0013703600874774068,-0.0007608257014963959,
0.00025190669718400967,0.0014154004589753215,0.002461420635709332,0.003012423848769931,
0.0026079871108133294,0.001115432556294998,-0.0008531818129514857,-0.0022783059845596586,
-0.002770673157048994,-0.0029796565504781646,-0.003430943381749411,-0.0031973449396977684,
-0.0008968429179843104,0.0024777666109278788,0.0033131082058404943,0.000032074683390218124,
0.0033473431384214393
};
class MyBuffer
{
public:
double *acc;
double *buffer;
int start,end;
static int j;
MyBuffer()
{
start=0;
end=0;
buffer=new double[150264];
acc=new double[150000];
fill_n(buffer,150264,0);
}
~MyBuffer()
{
delete[] buffer;
delete[] acc;
}
int startnumber()
{
return start;
}
int endnumber()
{
return end;
}
};
typedef concurrent_bounded_queue<MyBuffer> QueueMyBufferType;
QueueMyBufferType chunk_queue;
atomic<bool> stop_flag;
atomic<bool> stop_filter;
int MyBuffer::j=0;
int queueloopcount=30;
void input_function()
{
stop_flag = false;
cout<<"thread reached to call input function " <<endl;
ofstream o("testing sinewave.csv");
int counter=0;
while(counter<150000)
{
// cout<<"value of counter is \t" <<counter << endl;
MyBuffer *b=new MyBuffer;
b->start=(FILTER_LEN-1+(counter));
b->end=(5264+(counter));
// cout<<"value of b.start is and b.end is "<<b->start<<"\t" <<b->end<<endl;
for(int i =b->startnumber(); i <b->endnumber(); i++)
{
b->buffer[i] = sin(700 * (2 * pi) * (i / 5000.0));
o<<b->buffer[i]<<endl;
}
chunk_queue.push(*b);
counter+=5000;
// cout<<"value of queueloopcount is "<< queueloopcount << endl;
}
cout<<"all data is perfectly generated" <<endl;
}
int main()
{
int ntokens = 8;
thread inputfunc(input_function);
tick_count t1,t2;
ofstream o("filter700Hz.csv");
t1=tick_count::now();
bool stop_pipeline = false;
stop_filter=false;
inputfunc.join();
parallel_pipeline(ntokens,make_filter<void,MyBuffer*>
(
filter::parallel,[&](flow_control& fc)->MyBuffer*
{
if(queueloopcount==0)
{
fc.stop();
cout<<"pipeline stopped"<<endl;
}
else
{
MyBuffer *b=new MyBuffer;
chunk_queue.pop(*b);
{
cout<<"value of start and end popped is "<<b->startnumber()<<"\t"<<b->endnumber()<<endl;
queueloopcount--;
}
return b;
}
}
)&
make_filter<MyBuffer*, void>
(
filter::serial,[&](MyBuffer* b)
{
cout<<"value of second filter start is and end is \t "<< b->startnumber() << "\t" << b->endnumber() <<endl;
}
)
);
cout<<"now i am out" <<endl;
o.close();
t2=tick_count::now();
cout << "\n Time elapsed is \n\n" <<(t2-t1).seconds()<<endl;
return 0;
}
please help to find where code is wrong.
The Problem in this code is with filter i.e parallel in first stage that is causing problem to flow_control object which tries to pop and it is a blocking call due to which it blocks and solution to this is you should probably have a serial first filter that only create an empty MyBuffer* and stop the pipeline if no more work is due. Then have a parallel second filter that performs the real work and finally a serial (in-order) output stage.

mpi determinant, CODE: 11 segmentation fault

I'm new to mpi and I want to write a program in C++ that calculates the determinant of a matrix and in the beginning of trying to write it I get this error when trying to compile with mpi compiler.
#include "mpi.h"
#include <iostream>
#include <stdio.h>
#include "/usr/include/malloc.h"
using namespace std;
int matrix[3][3] = {{1,2,3},
{4,5,6},
{7,8,9}};
//**************
int** MakeMatrix (int** oldMatrix,int I,int J,int m,int n)
{
int** newMatrix = (int**)malloc(sizeof(int*)*J);
for(int i=0;i<J;i++)
{
newMatrix[i] = (int*)malloc(sizeof(int)*I);
}
for(int i=0;i<I-1;i++)
for(int j=0;j<J-1;j++)
if(i>=m)
if(j>=n)
{
newMatrix[i][j]=oldMatrix[i+1][j+1];
}
else
{
newMatrix[i][j]=oldMatrix[i+1][j];
}
else
{
newMatrix[i][j]=oldMatrix[i][j];
}
return newMatrix;
}
///////////////////////
int determinan (int** lmatrix,int I,int J)
{
if(I*J==1)
{
return lmatrix[I-1][J-1];
}
int minus = -1;
int sum = 0;
for(int j=0;j<J-1;j++)
{
for(int k=1;k<I+j+1;k++,minus*=minus);
sum += minus*lmatrix[0][j]*determinan(MakeMatrix(lmatrix,I,J,0,j),I-1,J-1);
}
return sum;
}
///*************
int main(int argc,char **argv)
{
//MPI_Init(&argc,&argv);
cout<<determinan((int**)matrix,3,3)<<'\n';
//MPI_Finalize();
return 0;
}
and here is the error message :
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
I am sure there are multiple problems. The immediate one is the type cast in:
cout<<determinan((int**)matrix,3,3)<<'\n';
^^^^^^^
This isn't a valid cast since matrix isn't an array of pointers.

Code Crashes Immediately After Running

Even at the bare minimum of 10 numbers to input, I get no errors but my code crashes immediately on running. I was also wondering, what should I do if I have a question similar to another question that I've already asked, but on another new problem?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime==true) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
}
}
I get no errors but the compiled code crashes immediately.
I changed it to
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a.push_back(2);
for (double i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
return a.size();
}
}
I addressed all of your problems. It still returns no errors and still crashes.
What makes you think you can do this?
vector<int> a;
a[1]=2;
vector<int> a;
a[1]=2;
You can't access a[1] until you've reserved space for it. You should probably use a.push_back(2) to append 2 to the end of a.
You have declared primer to return int, yet it returns nothing. Either make it void or return the number of primes.
i/a[ii]==floor(i/a[ii]) isn't going to do what you expect. i/a[ii] performs integer division. You should cast i to double before dividing.
if (prime==true) can be changed to simply if (prime), no need to compare a boolean to true.
Please improve your coding style. Use proper indentation and more commonly used variable names: i, j, k instead of i, ii, iii.
Here is another bug:
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
return a.size();
}
My understanding is that you can only return once from a function, main included. The execution will not loop here because of the return statement.
Did you really want a return statement inside a for loop?