Trouble using pthread_mutex_lock - c

I just started at college to study a little bit about threads, and it seems that I don't quite get the hang of it.
I wanted for my code to get the arguments and check if they are either even or prime numbers, and if they are, to print them. Additionally make the sum of each one of these kind of numbers.
This is the code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define something 10
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
int snrp=0;
int sprim=0;
void * verif(void* argv){
pthread_mutex_lock(&lock);
int x=*(int*)argv;
if (x%2==0){
printf("%d is even\n",x);
snrp+=x;
}
else{
int ok=1;
int d;
if(x<1)
ok=0;
for(d=3;d*d<x;d+=2)
if(x%d==0)
ok=0;
if(ok==0)
return NULL;
printf("%d is prime\n",x);
sprim+=x;
}
pthread_mutex_unlock(&lock);
sleep(1);
return NULL;
}
int main(int argc,char* argv[]){
pthread_t threads[something];
int i,n;
for(i=1;i<argc;i+=1){
n=atoi(argv[i]);
if(pthread_create(&threads[i],NULL,verif,(void*) &n))
printf("Error");
}
for(i=1;i<argc;i+=1)
pthread_join(threads[i],NULL);
printf("Even numbers sum is %d \n",snrp);
printf("Prime numbers sum is %d \n",sprim);
pthread_mutex_destroy(&lock);
return 0;
}
If I use for example the arguments 2,3,5 I get the output:
5 is prime
5 is prime
5 is prime
Even numbers sum is 0
Prime numbers sum is 15
Can someone please explain why?

#Darkmer
void * verif(void* argv){
pthread_mutex_lock(&lock);
int x=*(int*)argv;
printf("%p stores %d",argv,n); // Add this line. in your code.
You will understand, every time you are sending same address, thus same n ( which is 5 in your sample). Why this happened? that happened because before the thread used n, you parallely changed it in main function with next command line argument.
This happened, because in main program, you used same 'n' for all three parameters. Instead, postpone the atoi function and use it inside verif(). pass argv directly from command line arguments. I can do that pthread_create(..) function change for you, but that is your home work.
good luck.

Your verif function can return without releasing the lock here:
if(ok==0)
return NULL;
That will leave the lock held forever and any other thread that tries to acquire it will wait forever.
Also, this is wrong:
for(i=1;i<argc;i+=1){
n=atoi(argv[i]);
if(pthread_create(&threads[i],NULL,verif,(void*) &n))
printf("Error");
}
You pass the thread the address of n, but what is it supposed to do with that address? The value of n is modified in this code with no synchronization, so the new thread cannot legally access it. Instead of passing the thread the address of n, pass it the value of n.

You send the same parameter for your thread.
int main(int argc,char* argv[]){
pthread_t threads[something];
int i;
int n[3] = {2, 3, 5};
for(i=0;i<3;i+=1)
{
if(pthread_create(&threads[i],NULL,verif,(void*) &n[i]))
printf("Error");
}
for(i=0;i<3;i+=1)
pthread_join(threads[i],NULL);
printf("Even numbers sum is %d \n",snrp);
printf("Prime numbers sum is %d \n",sprim);
pthread_mutex_destroy(&lock);
return 0;
}
Notice: pthread_create(&threads[i],NULL,verif,(void*) &n[i])
Here is the link:
http://linux.die.net/man/3/pthread_create

Related

Some problem in package function(Wrapper function )in C .How do i package a founction and let the callee founction return multipe values or structure

There are some problems thatconfuse me:
The callee function needs return a structure, but there is not a structure statment in caller function.
If i have to write the declaration in the calling function,it can not be called packaging function.
If i return a structure pointer by callee function, but the structure is in the stack of the called function and will be destroyed after the end, which is not safe. Sometimes i get some warning or even wrong!
I have a limited ideas but it not good. I put the structure into the heap by malloc and return the void*pointer. But this gave birth to a new problem :after each call to this function, in the caller, I cannot release the heap through the free() function,the complier can not identify variable name of structure pointer. I think it verey dangerous. I want when the callee function quit,it can be released by itself.
This is the first time I came to this website to ask questions and I just came into contact with c language,If there is something stupid please point it out.
I have to write the structure declaration outside. This program for judging prime number, and I want to package the founction "judging_number". I do not want to write the structure declaration when I want to call the founction "judging_number".
Please give me some help, I would be very grateful.
Sorry, this is my fault. I compiled it with clang++, I saved it as *.cpp, but I wrote c code in it.
What I mean is, can I put the declaration in the called function to realize the function modularization, how can I not declare a structure before calling the function? Is there any way I can not write a declaration. Like use founction in stdio.h.It is as convenient as using the functions of the standard library. Only need to write a line of function call and pass parameters, the called function can return multiple results.
#include <stdio.h>
struct boundary{
int L;
int R;
};boundary *range;
int *get_number()
{
int *nPtr = (int *)malloc(sizeof(int));
do
{
printf("Please enter a vaild number for judging prime number.Or enter the number 1 to quit.\r\n");
scanf("%d", nPtr);
if (*nPtr == 1)
{
exit(1);
}
} while (*nPtr < 1);
printf("The object is %d\r\n", *nPtr);
return nPtr;
}
int judg_number(int N,boundary range){
if (N%range.L==0&&N!=2){
printf("The number %d is a composite number.\r\n", N);
}
else{
printf("The number %d is a prime number.\r\n", N);
}
return 0;
}
boundary* get_range(int N){
boundary *Ptr = (boundary *)malloc(sizeof(boundary));
*Ptr = {2,N-1};
printf("The range is between %d and %d .\r\n", Ptr->L, Ptr->R);
return Ptr;
}
int main(int argc,char**argv,char**env){
int*N;
while(1){
N=get_number();
range=get_range(*N);
judg_number(*N, *range);
free(N);
free(range);
}
getchar();
getchar();
return 0;
}
You dont need dynamic memory allocation here. If you want to retun an int, retun an int. If you want to retun a stuct return a struct.
You probably want this:
#include <stdio.h>
#include <stdlib.h>
struct boundary {
int L;
int R;
};
struct boundary range;
int get_number()
{
int n;
do
{
printf("Please enter a vaild number for judging prime number.Or enter the number 1 to quit.\r\n");
scanf("%d", &n);
if (n == 1)
{
exit(1);
}
} while (n < 1);
printf("The object is %d\r\n", n);
return n;
}
int judg_number(int N, struct boundary range) {
if (N % range.L == 0 && N != 2) {
printf("The number %d is a composite number.\r\n", N);
}
else {
printf("The number %d is a prime number.\r\n", N);
}
return 0;
}
struct boundary get_range(int N) {
struct boundary b = { 2, N - 1 };
printf("The range is between %d and %d .\r\n", b.L, b.R);
return b;
}
int main(int argc, char** argv, char** env) {
int N;
while (1) {
N = get_number();
range = get_range(N);
judg_number(N, range);
}
getchar();
getchar();
return 0;
}
BTW:
boundary get_range(int N) {... is invalid in C but valid in C++. In C it should be struct boundary get_range(int N) {...

Is it possible to set a global variable or a define from the user input at launch time?

I'm writing a simulation program where the user is suppose to enter an int to define the number of people involved in the simulation, and I need to use the same input to set a pair of global variables.
I tried the following and other version of it:
#define N_PEOPLE get_user_input()
pthread_t th_ppl[N_PEOPLE];
pthread_mutex_t m_ppl[N_PEOPLE];
int get_user_input() {
int input;
scanf("%d", &input);
return (input);
}
int main(int argc, int **argv) {
get_user_input();
int m_id[N_PEOPLE];
printf("%d", m_id);
}
The rest of the code isn't important yet, but basically I'm trying to catch the user input at launch so I can create an array of thread and an other of mutexes, but when I compile I get errors line 3,4: "error: variably modified 'th_ppl' at file scope pthread_t th_ppl[N_PEOPLE]". - same for the m_ppl.
I kinda get why I have this error, because I launch the program with my variables unset yet so I tried differents approach, like using another global to store the int or to directly using the return of the function in the global, like:
phtread_t th_ppl[get_user_input()];
but I always get the same error, and I dont seem to be able to work around this problem yet. So I'd like to know if I'm just doing it wrong or if I should just completely change my strategy here.
You cannot construct global variable size arrays as posted. You should read the number of people from the user, allocate the global arrays with calloc, and define th_ppl and m_ppl as pointers.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int N_PEOPLE;
pthread_t *th_ppl;
pthread_mutex_t *m_ppl;
int get_user_input() {
int input;
if (scanf("%d", &input) != 1)
return 0;
return input;
}
int main(int argc, int **argv) {
N_PEOPLE = get_user_input();
if (N_PEOPLE <= 0) {
printf("invalid people number: %d\n", N_PEOPLE);
exit(1);
}
th_ppl = calloc(sizeof(*th_ppl), N_PEOPLE);
m_ppl = calloc(sizeof(*m_ppl), N_PEOPLE);
if (th_ppl == NULL || m_ppl == NULL) {
printf("cannot allocate memory for %d people\n", N_PEOPLE);
exit(1);
}
printf("ready for %d people", N_PEOPLE);
...
}

C program for counting the number of occurrences of a word in a file using threads getting segmentation fault error

So i have the following problem: Implement a program that gets as arguments a file name followed by words. For each word, create a separate thread that counts its appearances in the given file.Print out the sum of the appearances of all words.
I'm also not sure if my code is really formatted correctly. I'm trying to figure out how to get each word to be counted in the given text file. I am trying to test out this code but I am getting a couple errors from it, the biggest being a segmentation error. If you have any pointers or advice or help, please let me know.
my code is:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions
int global_sum = 0;
typedef struct{
char* word;
char* filename;
}MyStruct;
void *count(void*str)
{
MyStruct *struc;
struc = (MyStruct*)str;
const char *myfile = struc->filename;
FILE *f;
int count=0, j;
char buf[50], read[100];
// myfile[strlen(myfile)-1]='\0';
if(!(f=fopen(myfile,"rt"))){
printf("Wrong file name");
}
else
printf("File opened successfully\n");
for(j=0; fgets(read, 10, f)!=NULL; j++){
if (strcmp(read[j],struc->word)==0)
count++;
}
printf("the no of words is: %d \n",count);
pthread_mutex_lock(&mtx); // lock the mutex, to prevent other threads from accessing global_sum
global_sum += count; // add thread's count result to global_sum
pthread_mutex_unlock(&mtx); // unlock the mutex, to allow other threads to access the variable
}
int main(int argc, char* argv[]) {
int i;
MyStruct str;
pthread_mutex_init(&mtx, NULL); // initialize mutex
pthread_t threads[argc-1]; // declare threads array
for (i=0;i<argc-2;i++){
str.filename = argv[1];
str.word = argv[i+2];
pthread_create(&threads[i], NULL, count, &str);
}
for (i = 0; i < argc-1; ++i)
pthread_join(threads[i], NULL);
printf("The global sum is %d.\n", global_sum); // print global sum
pthread_mutex_destroy(&mtx); // destroy the mutex
return 0;
}
The second good bit of advice for today is to look carefully at the warnings that your compiler gives you and take heed of them. strcmp(read[j],struc->word) Your compiler must have warned you that that is wrong. You are passing a single char as the first parameter instead of const char *. That'll almost certainly result in a seg fault. – kaylum

Threads receiving the same ID in C

Here is a block of code that creates a number of threads provided by the user, each thread then generates a random number and calculates its squareroot. I cannot figure out why the threads are getting the same ID, line 64 is the culprit as it is where the threads are being created. I suspect that there is something happening in the loop that is causing the threads to all be generated at the same time.
////////////////////////////////////////////////
//
//
// Zach
//
//
//
//
////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
void *squareroot(void *num1)
{
int *id = (int *)num1;
int incoming = rand()/100;
float *outgoing = (float *)malloc(sizeof(float));
printf("%d \n", *id);
printf("%d\n", incoming);
*outgoing = 5.000;
//Calculate the square root of the number passed to the function
*outgoing = sqrt(incoming);
return outgoing;
}
int main(int argc, char* argv[])//testing funcion
{
srand(time(NULL));
int i, j;
int *temp = (int *)malloc(sizeof(int));
if (argc != 2)
{
printf ("ERROR: Enter a number\n");
return 1;
}
int loop = atoi(argv[1]); //grabbing the integer supplied by user
pthread_t thread_id[loop];
void *exit_status;
float *thread_result;
for(i = 0; i < loop; i++)
{
pthread_create(&thread_id[i], NULL, squareroot, &i);
}
for(j = 0; j < loop; j++)
{
pthread_join(thread_id[j], &exit_status);
thread_result = (float *)exit_status;
printf("%f\n", *thread_result);
}
}
I think what is happening is that your loop finishes creating all the threads (or at least some of them) before any of the threads actually run and extract their unique id.
Because you're passing a pointer to i, when each thread finally gets around to checking its parameter, i is already finished... Or at least partway through. The danger is that multiple threads might see the same value for i. It's even worse that you never copy the value out of that pointer - you always dereference it. That means it might change in the middle of your thread's execution.
What you should do instead is pretend it is a pointer:
pthread_create(&thread_id[i], NULL, squareroot, (void*)i);
And in your thread function:
int id = (int)num1;
This works because the pointer is passed by value. Whatever value you provide is the value that goes into the thread function. Previously it didn't work because you passed a pointer to a value that could change in another thread.
PS: Don't forget to free the result from each thread in your loop at the end. At the moment you're not cleaning up memory that you allocated.

Trying to write a function that get names from the user

I tried to write a function, that get a number of candidates betwen 10 to 60,000,
and gets a name for each candidate...
This is what I wrote:
/********** Headers **********/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
/********** Consts **********/
const number_candidates;
/********** Functions **********/
void get_candidates(char *arr[256][256])
{
int counter = 1, i = 0, j =0;
int temp = 0;
printf ("Please enter the number of candidates: \n");
scanf ("%d", &temp);
while (temp < 10 && temp > 60000)
{
printf ("Please enter the number of candidates: \n");
scanf ("%d", temp);
}
const number_candidates = temp;
while (counter < number_candidates + 1)
{
printf ("Please enter the %d name: \n", counter);
fgets (arr, 256, stdin);
counter++;
}
}
int main(int argc, char *argv[])
{
int i = 0;
char can_names[256][256];
get_candidates(&can_names);
system("PAUSE");
return 0;
}
There is an error in getting the names into the arr...
You should avoid using arguments like this one: char *arr[256][256] ... what's the point of it? You should think about what your function will do. You want it to load names of candidates right? So you could define struct candidate with an attribute name within it:
typedef struct candidate{
char name[256];
} Candidate;
Another thing: why are you passing an address of your array to this function? You just want your array to be filled with data, you won't work with an array itself, thus it's enough to pass an array, not an address of it.
Then prototype of your function could be changed to void get_candidates(Candidate* candidates) which is much easier to read. And look how simple can usage of this function become:
Candidate candidates[256];
get_candidates(candidates);
And last thing: before you write function like that, try something simpler first (to find out what's happening there).
Here's an example:
#include <stdio.h>
typedef struct candidate{
char name[256];
} Candidate;
void get_candidates(Candidate* candidates){
scanf("%255s", candidates[4].name);
}
int main(int argc, char *argv[]){
Candidate candidates[256];
get_candidates(candidates);
printf("%s\n", candidates[4].name);
return 0;
}
In case you don't know the count of candidates before calling get_candidates, then it's better to change the prototype of this function to Candidate* get_candidates() so that it's clear that this function creates an array:
// caller is responsible for calling free on return value
Candidate* get_candidates(){
Candidate* candidates;
int count = 50; // here you found out the count
candidates = malloc(count*sizeof(Candidate));
fgets(candidates[4].name, 255, stdin);
return candidates;
}
int main(int argc, char *argv[]){
Candidate* candidates = get_candidates();
printf("%s\n", candidates[4].name);
free(candidates);
return 0;
}
You should call:
counter = 0;...fgets (arr[counter], 256, stdin);
You need walk one step for each loop.
Have a look at the documentation for scanf which indicates that variables need to be passed as a pointer as you did the first time you call scanf. Then have a look at your second call to scanf...
You're currently only assigning names to the first string in your array over and over again. Look at that while loop and in particular, how you're passing in the 'arr' variable. Have a look here for some inspiration.
To print out all of the names you need to loop over the array. You can find some examples of printing a list of strings here and here.
A few things are wrong:
First, you need space for 60000 names, yet you only allocate enough for 256. OK, we change
char can_names[256][256];
to
char can_names[60000][256];
and get... a segmentation fault, probably. That's because the array is using too much stack space. Change it to
static char can_names[60000][256];
so it's not on the stack.
Second, there's no need to take the address of the array - it's already passed as a pointer. Your function call changes to
get_candidates(can_names);
and the function signature is
void get_candidates(char arr[60000][256])
Third, you need a loop to read the entries one at a time. A for loop is easier to read:
for (counter = 0; counter < number_candidates; counter++)
{
printf ("Please enter the %d name: \n", counter);
fgets (arr[counter], 256, stdin);
}
Fourth, the condition
while (temp < 10 && temp > 60000)
should be
while (temp < 10 || temp > 60000)
(how can a number be both less than 10 and greater than 60000?) Once this is fixed, you can remove the initial read of temp since the loop will run at least once. Note that if you type a letter instead of a number now, the program will go into an infinite loop (it will repeatedly read the letter). Fixing this is left as an exercise.
Fifth, you don't need any headers except stdio.h. Also, the i and j variables are unused.
Edit: missed the scanf error. scanf takes addresses as parameters. it makes sense too: scanf needs somewhere to store a value, it doesn't care about the current value. So the call to scanf should be:
scanf ("%d", &temp);
Don't know if this would help but have a look at the code below.
It works dynamically ie. it allocated memory for desired no of candidates rather than assuming 60,000 of them.
/*
* Write a function, that get a number of candidates betwen 10 to 60,000, and gets a name for each candidate
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
int main(int argc, char*argv[]){
int i,n;
char **s;
printf("Enter the total number of candidates\n");
scanf("%d",&n);
//error condition
if(n<10 || n>60000){
printf("Sorry number of candidates should be between 10 to 60,000\n");
return -1;
}
//allocate memory
s = malloc(sizeof(char*)*n);
//get the data
for(i=0;i<n;i++){
s[i] = calloc(MAX,sizeof(char));
printf("Enter the candidate number %d's name:\n",i+1);
//fgets(s[i],MAX,stdin);
scanf("%s",s[i]);
}
//Display the data
printf("\nDetails of all the Candidates\n\n");
for(i=0;i<n;i++){
printf("Candidate number %d's name:%s\n",i+1,s[i]);
}
//Free the memory
for(i=0;i<n;i++){
free(s[i]);
}
free(s);
return 0;
}
I had a problem with fgets it was skipping the first candidate info. Any help would be appreciated.. I tried flush(stdin) but did not solve the issue.

Resources