If you're going to write code, at least learn how to indent.
You don't have a hope in hell of working out what's going on if your code is all over the place.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int num, n;
int fp;
void *find_prime_number(void *name)
{
int *th = (int *) name;
//while(num <= n ){
int mynum;
while (pthread_mutex_lock(&mutex), mynum = num++, pthread_mutex_unlock(&mutex), mynum <= n) {
int check = 0;
for (int i = 2; i <= num / 2; i++) {
if (num <= n) {
if (num % i == 0) {
printf("Calc%d : %d NON è un numero primo \n", *th, num);
check = 1;
break;
}
}
else {
return 0;
}
}
if (num <= n) {
if (check == 0) {
printf("Calc%d : %d è un numero primo , lo invio al memorizzatore\n", *th, num);
write(fp, &num, sizeof(num));
}
} else {
return 0;
}
if (num <= n) {
num++;
}
}
}
int main(int argc, char *argv[])
{
char *fifo = "myfifo";
if (argc != 2) {
printf("Errore nell'immissione dei parametri \n ");
return -1;
}
int error;
n = atoi(argv[1]);
pthread_t t1, t2, t3;
pthread_t *calc_arr[3] = { &t1, &t2, &t3 }; //array per l'id dei thread
if (error = mkfifo(fifo, 0666) == -1) {
// printf("Errore nella pipe fifo \n");
perror(fifo);
if (errno != EEXIST) {
return -1;
}
}
if ((fp = open(fifo, O_RDONLY)) == -1) {
perror(fifo);
//if (errno != EEXIST)
//return -1 ;
return 1;
}
int thread[] = { 1, 2, 3 };
for (int i = 0; i < 3; i++) {
if (error = pthread_create(calc_arr[i], NULL, find_prime_number, &thread[i]) != 0) {
perror("Errore nella creazione dei thread");
exit(1);
}
}
for (int i = 0; i < 3; i++) {
if (error = pthread_join(*calc_arr[i], NULL) != 0) {
perror("Errore nel join dei thread ");
exit(1);
}
}
close(fp);
printf("Terminazione dei calcolatori \n");
return 0;
}
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
#define MAX_PID 8
int main()
{
int fd;
int bytesread;
char *myfifo = "myfifo";
char buf[MAX_BUF];
fd = open(myfifo, O_RDONLY);
while (1) {
if ((bytesread = read(fd, buf, MAX_BUF - 1)) > 0) {
buf[bytesread] = '\0';
printf("Received: %s\n", buf);
} else
break;
}
close(fd);
return 0;
}
> while (pthread_mutex_lock(&mutex), mynum = num++, pthread_mutex_unlock(&mutex), mynum <= n)
What are you doing!?
Abusing the comma operator for one thing.
Having lock and unlock in there just makes locking a complete waste of time. Your loop body isn't guarded at all.
All those 'else return 0;' calls in find_prime_number() pretty much made sure nothing happens.
Before you try and make a threaded version, at least make sure the single threaded version works.
You start single threaded, make it work, then make threads out of it.
You don't start hacking threads and then try and make the logic work.
> if (error = pthread_create(calc_arr[i], NULL, find_prime_number, &thread[i]) != 0)
Beware of operator precedence issues. You're not comparing the result of pthread_create with zero.
> pthread_t *calc_arr[3]
Why not just pthread_t calc_arr[3] ?
All those global variables need to go away, except for the mutex.
Most people associate 'fp' as being a FILE* (from fopen), and 'fd' as being an int descriptor from the likes of open.
Code:
typedef struct {
int fd; // not fp
int num; // pick a better name
int n; // pick a better name
} thread_param_t;
void *find_prime_number(void *name)
{
thread_param_t *params = (thread_param_t*) name;
// use params->member for your needs here
// important!
return NULL;
}
///
int main ( ) {
thread_param_t params[3];
pthread_t calc_arr[3];
for ( int i = 0 ; i < 3 ; i++ ) {
// setup params[i].member
pthread_create(&calc_arr[i], NULL, find_prime_number, ¶ms[i]);
}
}
This is also wrong.
Code:
if ((bytesread = read(fd, buf, MAX_BUF - 1)) > 0) {
buf[bytesread] = '\0';
printf("Received: %s\n", buf);
Except you're not writing printable data, only raw integers.
> write(fp, &num, sizeof(num));