Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <pthread.h>
#include <stdio.h>
int sum;
void *runner(void *param); /* threads call this function */
int main(int argc, char *argv[])
{
pthread_t tid; /* the thread identifier */
pthread_attr_t attr; /* set of thread attributes */
if(argc != 2) {
fprintf(stderr, "usage: a.out <integer value>\n");
return -1;
}
if(atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >= 0\n", atoi(argv[1]));
return -1;
}
/* get the default attributes */
pthread_attr_init(&attr);
/* create the thread */
pthread_create(&tid, &attr, runner, argv[1]);
/* wait for the thread to exit */
pthread_join(tid, NULL);
printf("sum = %d\n", sum);
}
/* The thread will begin control in this function */
void *runner(void *param)
{
int i, upper = atoi(param);
sum = 0;
for(i=1; i <= upper; i++) sum += i;
pthread_exit(0);
}
This is my code following book.
Make child thread and allocate the runner function to cumulative the values in array.
But gcc Multithread_Pthread.c isn't work and gcc -pthread Multithread_Pthread.c are also work not.
What is the reason and solution?
Oh I forget the result of each command.
Use the gcc Multithread_Pthread.c error has occurred.
Use the gcc -pthread Multithread_Pthread.c usage: a.out
is the result.
Try this:
$ gcc Multithread_Pthread.c -lpthread
To complement Ren's answer: you should try to compile object files before linking them instead of building the whole program with a single command. Try this:
gcc -c Multithread_Pthread.c
This will correctly create a "Multithread_Pthread.o" object file, proving that "pthread.h" was found in system header files.
Next is command to try linking:
gcc Multithread_Pthread.o
You will get the same message error, proving you this is a linking error.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 months ago.
Improve this question
I've took an example of the simple OpenMP code.
#include <stdio.h>
#include <omp.h>
int main(int argc, char** argv){
int i;
int thread_id;
omp_set_num_threads(4);
#pragma omp parallel
{
thread_id = omp_get_thread_num();
for( int i = 0; i < omp_get_num_threads(); i++){
if(i == omp_get_thread_num()){
printf("Hello from process: %d\n", thread_id);
}
#pragma omp barrier
}
}
return 0;
}
Compiled it with gcc -fopenmp omp_test_hello.c -o a.exe.
I did not change or set any environment variables (I've decided that internal directives in code should work similarly). When I execute file I get the following output:
Hello from process: 0
Hello from process: 3
Hello from process: 3
Hello from process: 3
After that, the execution of the program does not stop, so it seems that something blocks it from termination.
I've tried even more simple example of the code without the barrier and for loop. It does not terminates similarly, though the output include "signals" from all threads:
int main(int argc, char** argv){
int i;
int thread_id;
omp_set_num_threads(4);
#pragma omp parallel
{
thread_id = omp_get_thread_num();
printf("Hello from process: %d\n", thread_id);
}
return 0;
}
Output:
Hello from process: 2
Hello from process: 1
Hello from process: 0
Hello from process: 3
I've managed to test these examples also on Linux and get same results.
So what could be the problem?
Problem solved by reinstalling mingw compiler.
I want to modify the multithread program on the Linux operating system using this Pthread API.
#include <pthread.h>
#include <stdio.h>
int sum;
void *runner(void *param);
int main(int argc, char *argv[]) {
pthread_t tid
pthread_attr_t attr;
if (argc != 2) {
fprintf(stderr, "usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >=0\n", atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_join(tid, NULL);
printf("sum = %d\n", sum);
}
void *runner(void *param);
{
int i, upper = atoi(param);
sum = 0;
for (i = 1; i <= upper; i++)
sum += i;
pthread exit(0);
}
I want to change that program into a program that has 2 threads that work together to add a number. But i don't know how to change it, Thanks again for any help that can be offered. I am sorry,because I'm not good at explaining.
first there is 3 errors : the pthread tid declaration has no ";", then there is one at the end of your runner()* function declaration, and last but not least, a underscore is missing on the last line pthread_exit(0)
beware ahah
ok for vars :
pthread_t tid;
pthread_t tid2;
pthread_attr_t attr;
pthread_attr_t attr2;
and in the code after the ifs, add this :
pthread_attr_init(&attr);
pthread_attr_init(&attr2);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_create(&tid2, &attr2, runner, argv[2]); // not sure for argv[2]?
not sure for argv[2], it depends if it's 2 different numbers?
pthread_join are no use, they are here only for pausing threads, i think that if you want them to work in parallel, you need to only do "pthread_create" and they should work in parallel (but was i saw on my CS class on parallel programming 3 years ago, it will never be "real real" parallel because only the OS can control this and you need to be some kind of a super root to be able to really control the threads
I mean
it won't be faster because it will not be real parallel prog
I'm not exactly sure what you want, but a really quick and dirty solution based on the existing code is below. I'm assuming you just want two thread to sum a single variable to the input.
An explanation of what's going on: I had to fix some minor syntax issues you have in your code, one big one being the semicolon at the end of the runner function definition. I added a mutex to define a critical section in the runner's for loop. It makes sure only 1 thread can update the sum. I'm assuming you want the sum to equal the input, so we just increment it by 1 and check before incrementing whether the value is still below. Like I said, it's quite quick and dirty, not really the ideal solution. To create two threads, we just call the thread create function twice in main.
See https://computing.llnl.gov/tutorials/pthreads/#Mutexes for more important about mutexes and the pthread library.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int sum = 0; // set it once globally
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void *runner(void *param);
int main(int argc, char *argv[]) {
pthread_t tid1, tid2;
pthread_attr_t attr;
if (argc != 2) {
fprintf(stderr, "usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >=0\n", atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid1, &attr, runner, argv[1]);
pthread_create(&tid2, &attr, runner, argv[1]);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("sum = %d\n", sum);
}
void *runner(void *param) {
int i, upper = atoi(param);
// sum = 0;
int t = pthread_self();
for (i = 1; i <= upper; i++) {
pthread_mutex_lock(&mtx);
if (sum < upper) {
printf("%d incrementing\n", t);
sum += 1;
}
pthread_mutex_unlock(&mtx);
}
pthread_exit(0);
}
Compile with cc -o main main.c -pthread.
The code below is a sample provided by the book in my Operating Systems course.
When compiling it I get the error shown below it.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
int main(int argc, char *argv[])
{
int i, policy;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
if(pthread_attr_getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy.\n");
else{
if(policy == SCHED_OTHER)
printf("SCHED_OTHER\n");
else if(policy == SCHED_RR)
printf("SCHED_RR\n");
else if(policy == SCHED_FIFO)
printf("SCHED_FIFO\n");
}
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
fprintf(stderr, "Unable to set policy.\n");
/* create the threads */
for(i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i], &attr, runner, NULL);
/* now join on each thread */
for(i = 0; i < NUM_THREADS; i++)
pthread_join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
/* do some work... */
pthread_exit(0);
}
I compiled it using this command...
gcc linux_scheduling.c -o scheduling
However, I get this error.
linux_scheduling.c:32:34: error: 'runner' undeclared (first use in this function)
pthread_create(&tid[i], &attr, runner, NULL);
^
linux_scheduling.c:32:34: note: each undeclared identifier is report only once for each function it appears in
I tried adding -pthread:
gcc linux_scheduling.c -o scheduling -pthread
but the error remains.
Thanks for your help!
You have the correct compiling command:
gcc linux_scheduling.c -o scheduling -pthread
but you need to put:
void *runner(void *param);
ahead of the start of main to declare it:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *runner(void *param);
int main(int argc, char *argv[])
{
...
Declare prototype of runner or if you don't want to declare then define the function before main. This is because main is referring the function and gives you such errors
i am using LINUX 10.04 i think this is not a problem,anyway i have a
Weird Error.
To me all looks perfect.
So what is the problem?
Sorry for this format type.i am new here.
//COMPILE with: gcc -g -Wall -pthread pthread_ex_book_pg193.c -lpthread -o MYthread
/* the program is simple.We create two threads One is for the main () and th esecond with the pthread_create().
The second thread calls a function runner() to calculate a sum and when it finishes it returns to the main thread */
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
int sum;
void *runner(void *argv[]);
int main (int argc,char *argv[]){
pthread_t tid; //thread id
pthread_attr_t attr;//thread attributes
if (argc!=2){
fprintf(stderr,"usage: a.out<integer value> \n");
return -1;
}
if (atoi(argv[1]) < 0){
fprintf(stderr, "%d must be >=0\n ",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("sum = %d \n",sum);
}
void *runner(void *param){
int i;
int upper = atoi(param);
sum=0;
for (i=1;i<=upper;i++){
sum=sum+i;
pthread_exit(0);
}
}
Change
void *runner(void *argv[]);
to
void *runner(void *argv);
(1) Your forward declaration of runner clashes with your later usage; (2) the proper signature of a thread's start function is void* f(void*) - IOW it takes a single pointer to void not an array of pointers to void.
I am trying to create threads with the pthread library. Compilation is fine with
gcc -o -pthread file file.c
but when I run the code, I get a segmentation fault. I am not sure what the problem is. I tried to execute the code that was given in the textbook to try and learn but I am lost right now. Can anyone help? The code is below... very basic, yes but please hlep me out.
#include<stdio.h>
#include<pthread.h>
int sum;
void *runner(void *param);
int main (int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
//printf("Am I here..?\n");
if (argc!=2)
{
fprintf(stderr, "usage: a.out ...\n");
return -1;
}
if (atoi(argv[1] < 0))
{
fprintf(stderr, "%d must be >= 0\n", atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_join(tid, NULL);
printf("sum = %d\n", sum);
}
void *runner(void *param)
{
extern int sum;
int i, upper=atoi(param);
sum=0;
for(i=1; i<= upper; i++)
sum+=i;
pthread_exit(0);
}
Please turn on, and examine carefully, your compiler's warnings.
You're not including stdlib.h, so atoi is undeclared, and you're not returning anything from runner but you've declared it as returning a void*.
But the main problem is this line:
if (atoi(argv[1] < 0))
argv[1] < 0 will evaluate to 0 or 1, which are not what you want as an argument to atoi. What you wanted is:
if (atoi(argv[1]) < 0)
It's more than likely that your compiler would have indicated all these problems if the right warnings were enabled.
You have misplaced the bracket:
Change:
if (atoi(argv[1] < 0))
to:
if (atoi(argv[1]) < 0)