POSIX C Threads. Mutex example. Don't work as expected - c

I have a big problem, I can't figure out why mutexes in C don't work as I expect.
This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
pthread_t mythread;
pthread_mutex_t mymutex;
void *anotherFunc(void*)
{
pthread_mutex_lock(&mymutex);
for(int i = 0; i < 100; i++)
printf("anotherFunc\n");
pthread_mutex_unlock(&mymutex);
pthread_exit(NULL);
}
void *func(void*)
{
pthread_mutex_lock(&mymutex);
for(int i = 0; i < 100; i++)
printf("func\n");
pthread_mutex_unlock(&mymutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_mutex_init(&mymutex, NULL);
pthread_create(&mythread, NULL, func, NULL);
pthread_create(&mythread, NULL, anotherFunc, NULL);
pthread_mutex_destroy(&mymutex);
pthread_exit(NULL);
return EXIT_SUCCESS;
}
What I expect to happen is the program to print first 100 "func" messages and then 100 "anotherFunc" messages. What I expect is execution to reach func and lock the mutex. When the execution reaches anotherFunc, I expect to wait until func unlocks the mutex. But I get interfered messages like
func
func
func
anotherFunc
anotherFunc
anotherFunc
func
anotherFunc
I don't understand how this thing works. Please help!

pthread_create(&mythread, NULL, func, NULL);
pthread_create(&mythread, NULL, anotherFunc, NULL);
pthread_mutex_destroy(&mymutex);
You're destroying the mutex before the threads are done with it, so all bets are off. You'll probably want to pthread_join the 2 threads before destroying it.

I got few comiplation errors
I couldn't declare int i in for loop
Used an argument name arg as an argument for threads "func" and "anotherFunc"
I have used pthread_join before destroying the mutex.
In this way I am destroying my mutex "mymutex" after both threads "func" and "anotherFunc" have completed their execution
Also each threads now has their own thread id "mythread1" and "mythread2" so in this way I can use pthread_join() function for each thread
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
pthread_t mythread1, mythread2;
pthread_mutex_t mymutex;
void *anotherFunc(void *arg)
{
pthread_mutex_lock(&mymutex);
int i;
for(i = 0; i < 100; i++)
printf("anotherFunc\n");
pthread_mutex_unlock(&mymutex);
pthread_exit(NULL);
}
void *func(void *arg)
{
pthread_mutex_lock(&mymutex);
int i;
for(i = 0; i < 100; i++)
printf("func\n");
pthread_mutex_unlock(&mymutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_mutex_init(&mymutex, NULL);
pthread_create(&mythread1, NULL, func, NULL);
pthread_create(&mythread2, NULL, anotherFunc, NULL);
pthread_join(mythread1, NULL);
pthread_join(mythread2, NULL);
pthread_mutex_destroy(&mymutex);
return EXIT_SUCCESS;
}

Related

C thread parallel programming

I tried to make a parallel program that generates a random number with one thread and the other thread writes it.
Am I doing something wrong that messes with the performance/optimization? I ask it because it was very easy to write this program so I'm a little concerned that I am doing something wrong.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include "produceConsume.h"
#define NUM_THREAD 1
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int queue[1];
int queueCounter = 0;
void *producer(void *args)
{
while (1)
{
pthread_mutex_lock(&lock);
int n = rand() % 100;
queue[queueCounter] = n;
queueCounter++;
pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);
}
}
void *consumer(void *args)
{
while (1)
{
pthread_mutex_lock(&lock);
printf("%d\n", queue[queueCounter - 1]);
queueCounter--;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
sleep(1);
}
}
int main()
{
system("clear");
srand(time(NULL));
pthread_t th[NUM_THREAD], th2[NUM_THREAD];
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_create(&th[i], NULL, &producer, NULL);
pthread_create(&th2[i], NULL, &consumer, NULL);
}
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_join(th[i], NULL);
pthread_join(th2[i], NULL);
}
}
You don't need an array if you are going to use only one thread, in any case, you create two threads but only one is joined (leaking memory), instead:
pthread_t th1[NUM_THREAD]; // or simply pthread_t th1;
pthread_t th2[NUM_THREAD]; // or simply pthread_t th2;
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_create(&th1[i], NULL, &producer, NULL);
pthread_create(&th2[i], NULL, &consumer, NULL);
}
for (int i = 0; i < NUM_THREAD; i++)
{
pthread_join(th1[i], NULL);
pthread_join(th2[i], NULL);
}

Is pthread_join a must when using pthread in linux?

I an learning pthread and I have a few questions.
Here is my code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define NUM_THREADS 10
using namespace std;
void *PrintHello(void *threadid)
{
int* tid;
tid = (int*)threadid;
for(int i = 0; i < 5; i++){
printf("Hello, World (thread %d)\n", *tid);
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
int t;
int* valPt[NUM_THREADS];
for(t=0; t < NUM_THREADS; t++){
printf("In main: creating thread %d\n", t);
valPt[t] = new int();
*valPt[t] = t;
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)valPt[t]);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
The code runs well and I don't call pthread_join. So I want to know, is pthread_join a must?
Another issue, is:
valPt[t] = new int();
*valPt[t] = t;
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)valPt[t]);
equal to:
rc = pthread_create(&threads[t], NULL, PrintHello, &i);
It is not. But you need either pthread_exit() or pthread_join().
Here you called pthread_exit(), thats why the child threads continue execution even after the main thread terminates.
If there is any need for the main thread to wait till the child threads complete execution, you can use pthread_join().

segmentation fault in multithread program

I'm trying to write a program that uses 3 threads with a shared memory. the shared memory is an array with 101 values. the first value shared memory[0](initialized to 0) is status value which determines which operation should take place. the three threads do
The first one should fill the shared memory array with 100 random values. and set the status value to 1.
The second should print the product of the 100 random values (from index 1 to 100). and set the status value to 2.
The third should print the average of the 100 random variables. and set the status value to 0. so that thread one fill the shared memory with different random variables.
this is my code
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
unsigned int product=0;
float avg=0;
int* shared_memory;
int status=0;
void productAllThread();
void averageAllThread();
void *parentProcess();
void *prodAll();
void *avgAll();
void initializeArray();
int main(int argc, const char * argv[])
{
time_t t;
key_t key = 9876;
// Create shared memory area
int shm_id = shmget(key, sizeof(int)*101, IPC_CREAT | 0666);
// initialize the random variable
srand((unsigned) time(&t));
// Create shared memory
shared_memory=shmat(shm_id, NULL, 0);
//create threads
pthread_t tid1, tid2, tid3;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1, &attr, parentProcess, NULL);
pthread_create(&tid2, &attr, prodAll, NULL);
pthread_create(&tid3, &attr, avgAll, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
return 0;
}
void initializeArray() {
shared_memory[0]=0;
status=shared_memory[0];
int i= 0;
printf("Initial Array:{");
for(i=1; i<100; i++)
{
shared_memory[i]=rand()% 50;
printf("%d,", shared_memory[i]);
}
printf("}\n");
}
void *parentProcess()
{
while(1)
{
status=shared_memory[0];
if(status==0) {
// initialize array
initializeArray();
shared_memory[0]=1;
} else {
sleep(10);
}
}
}
void averageAllThread() {
while(1) {
status=shared_memory[0];
if(status==2)
{
avgAll();
wait(NULL);
printf("Avg:%.2f\n", avg);
shared_memory[0]=0;
} else {
sleep(5);
}
}
}
void productAllThread() {
while(1){
status=shared_memory[10];
if (status==1)
{
prodAll();
wait(NULL);
printf("Sum:%d\n",product);
shared_memory[0]=2;
} else {
sleep(5);
}
}
}
void *prodAll()
{
while(1){
int i=1;
product=0;
for(i=1; i<100; i++)
{
product=product+shared_memory[i];
}
}
}
void *avgAll()
{
while(1){
int i=0;
avg=0;
for(i=1; i<100; i++)
{
avg=avg+shared_memory[i];
}
avg=avg/100;
}
}
when I run it in the terminal, it gives me this error
"Segmentation fault: 11"
what might cause this type of errors? If this error is fixed will the program work fine to do the job I want it to do?
I found a few problems in your program:
You are calling the wrong functions to start your threads:
pthread_create(&tid1, &attr, parentProcess, NULL);
pthread_create(&tid2, &attr, prodAll, NULL);
pthread_create(&tid3, &attr, avgAll, NULL);
Should be:
pthread_create(&tid1, &attr, parentProcess, NULL);
pthread_create(&tid2, &attr, productAllThread, NULL);
pthread_create(&tid3, &attr, averageAllThread, NULL);
You have a few calls to wait() like this:
wait(NULL);
You should remove all of them.
The while loops in avgAll() and prodAll() should be removed since there are already while loops in the callers of those functions.
The call to srand() should be made from parentProcess() otherwise it might not affect the rand() calls in that thread.

Pthread Mutex Segfault

I've never worked with pthreads before and am simply trying to familiarize myself with them. As such, I've written the following test code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int count = 0;
void *increment(void *tex);
int main(int argc, char **argv) {
pthread_t t1, t2, t3;
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
pthread_create(&t1, NULL, &increment, &mutex);
pthread_create(&t2, NULL, &increment, &mutex);
pthread_create(&t2, NULL, &increment, &mutex);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
printf("Value of count is: %d\n", count);
}
void *increment(void *tex) {
pthread_mutex_t *mutex = (pthread_mutex_t *) mutex;
for (int i = 0; i < 100; i++) {
pthread_mutex_lock(mutex);
count++;
pthread_mutex_unlock(mutex);
}
return NULL;
}
I'm compiling the code with GCC using the proper -pthread flag, yet, for whatever reason, any time that any of the threads reach the mutex locking line, segfault. Upon further investigation with GDB, I've discovered that the mutex pointer appears to be invalid inside of the increment function even though I initialized it in main, passed it in as the argument to pthread_create, and have called join on each thread to ensure that main is still in scope. I'm at a loss for why this is happening, and could use some help. Thanks!
You've got:
pthread_mutex_t *mutex = (pthread_mutex_t *) mutex;
what you need is:
pthread_mutex_t *mutex = (pthread_mutex_t *) tex;

Do sleep functions sleep all threads or just the one who call it?

I am programming with pthread on linux(Centos)? I wanna to threads sleep a short time to wait for something. I am trying to use sleep(), nanosleep(), or usleep() or maybe something can do that. I want to ask that: Do sleep functions sleep all threads or just the one who call it? Any advices or references would be appreciate.
void *start_routine () {
/* I just call sleep functions here */
sleep (1); /* sleep all threads or just the one who call it?
what about nanosleep(), usleep(), actually I
want the threads who call sleep function can
sleep with micro-seconds or mili-seconds.
*/
...
}
int main (int argc, char **argv) {
/* I just create threads here */
pthread_create (... ...);
...
return 0;
}
My test program:
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>
void *start_routine (void *j) {
unsigned long sum;
int i;
int jj;
jj = (int)j;
do {
sum = 1;
for (i=0; i<10000000; i++) {
sum = sum * (sum+i);
}
if (jj == 0) {
printf ("\033[22;33m[jj%d.%ld]\t", jj, sum);
sleep(1);
}
else {
printf ("\033[22;34m[jj%d.%ld]\t", jj, sum);
}
}while (1);
pthread_exit((void *)0);
}
int main(int argc, char *argv[])
{
cpu_set_t cpuset;
pthread_t thread[2];
int i;
i = 0;
CPU_ZERO(&cpuset);
CPU_SET(i, &cpuset);
pthread_create (&thread[0], NULL, start_routine, (void *)i);
pthread_setaffinity_np(thread[0], sizeof(cpu_set_t), &cpuset);
i = 1;
CPU_ZERO(&cpuset);
CPU_SET(i, &cpuset);
pthread_create (&thread[1], NULL, start_routine, (void *)i);
pthread_setaffinity_np(thread[1], sizeof(cpu_set_t), &cpuset);
pthread_exit (NULL);
}
The standard spells it:
The sleep() function shall cause the calling thread to be
suspended from execution until ....
The linux one is just as clear:
sleep() makes the calling thread sleep until...
There are however a few erroneous references which maintain otherwise. linux.die.net used to state sleep causes the process to wait.
Just the thread which calls the function.

Resources