Threads: Timeout lock - c

Program description:
I wrote a demo for conditional waiting. The program starts two threads and then waits for both to finish. One of the threads reads user-pressed keys from the keyboard, and if the q key is pressed, sets a condition variable, after that the thread ends.
How can I implement the second thread to run in an infinite loop a sequence in which it waits for the condition variable for up to 1 second, and if the set variable appears then it exits the infinite loop, and if it does not appear and expires in 1 second, it displays a * on the console and returns to standby?
#include<pthread.h>
#include<stdio.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c=PTHREAD_COND_INITIALIZER;
void *fun1()
{
pthread_mutex_lock(&m);
while (1)
{
char ch;
scanf("%c",&ch);
if(ch=='q')
{
pthread_mutex_unlock(&m);
pthread_cond_signal(&c);
pthread_exit(NULL);
}
}
}
void *fun2()
{
struct timespec to;
pthread_condattr_t attr;
pthread_condattr_init( &attr);
pthread_condattr_setclock( &attr, CLOCK_MONOTONIC);
pthread_cond_init( &c, &attr);
clock_gettime(CLOCK_MONOTONIC, &to);
to.tv_sec += 1;
while (1)
{
printf("hello world\n");
int rc=pthread_cond_timedwait(&c,&m,&to);
printf("%d",rc==ETIMEDOUT);
pthread_exit(NULL);
// if(rc==ETIMEDOUT)
// {
// }
// pthread_exit(NULL);
}
}
int main()
{
pthread_t id[2];
pthread_mutex_init(&m,NULL);
pthread_cond_init( &c, NULL);
pthread_mutex_lock(&m);
pthread_create(&id[0],NULL,fun1,NULL);
pthread_create(&id[1],NULL,fun2,NULL);
pthread_join(id[0],NULL);
pthread_join(id[1],NULL);
pthread_mutex_destroy(&m);
return 0;
}

The structure of the code looks at-best to be pure-guesswork. Pthread mutex and condition variable pairing has specific rolls and responsibilities.
The mutex protects some external 'predicate' data
The condition variable signals a change to 'predicate' data.
The management of both the mutex and the condition variable is completely wrong.
repeated initialization of the already-initialized objects.
acquiring, and never releasing the mutex in fun1
both functions have the wrong signature for proper pthread threads.
neither function provides state return values
Related, some pthread implementations do not support clock-selection. In those cases, even fixing the code, the condition variable cannot be configured for monotonic clock selection; realtime is the only alternative.
Fixing everything above,
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
bool quit = false;
void *fun1(void *pv)
{
while (1)
{
char ch;
scanf("%c", &ch);
if (ch == 'q')
{
// latch mutex before changing predicate
pthread_mutex_lock(&m);
quit = true;
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
break;
}
}
return pv;
}
void *fun2(void *pv)
{
pthread_mutex_lock(&m);
while (1)
{
printf("hello world\n");
struct timespec to;
clock_gettime(CLOCK_REALTIME, &to);
to.tv_sec += 1;
int rc = pthread_cond_timedwait(&c, &m, &to);
if (rc != ETIMEDOUT || quit == true)
break;
}
pthread_mutex_unlock(&m);
return pv;
}
int main()
{
pthread_t id[2];
pthread_create(id+0, NULL, fun1, NULL);
pthread_create(id+1, NULL, fun2, NULL);
pthread_join(id[0], NULL);
pthread_join(id[1], NULL);
return 0;
}
Pay close attention to how the mutex protects the predicate data (quit), and how that data is never checked, nor modified, without protection from that mutex by any thread. It's important.
Finally, note that fun1 (or fun2 ; pick one) is ultimately pointless in this code. Starting threads and waiting for them all to finish as the sole responsibility of main is a code smell that something can be done in main to avoid at least one of those threads. For example:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
bool quit = false;
void *fun2(void *pv)
{
pthread_mutex_lock(&m);
while (1)
{
printf("hello world\n");
struct timespec to;
clock_gettime(CLOCK_REALTIME, &to);
to.tv_sec += 1;
int rc = pthread_cond_timedwait(&c, &m, &to);
if (rc != ETIMEDOUT || quit == true)
break;
}
pthread_mutex_unlock(&m);
return pv;
}
int main()
{
pthread_t id;
pthread_create(&id, NULL, fun2, NULL);
while (1)
{
char ch;
scanf("%c", &ch);
if (ch == 'q')
{
// latch mutex before changing predicate
pthread_mutex_lock(&m);
quit = true;
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
break;
}
}
pthread_join(id, NULL);
return 0;
}
Does exactly the same thing as the previous code, but uses main as the driver for what fun1 was originally doing. Always consider this as an option worth pursuing if you can. Be kind to your scheduler; it will return the favor someday.

Related

How to synchronize 2 pthreads using mutexes and/or cond vars to create a "guess the number" minigame?

I am trying to make a "guess the number" minigame to get used to pthreads and synchronization. A thread sleeps for 10 seconds, while the other reads input and says whether the number is too big or too low. When the first thread "wakes up", the second thread should no longer read input, regardless if it were executing scanf. When the second thread has read the correct number, the first thread should no longer sleep and ignore the rest of its code.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
unsigned short quit = 0;
unsigned short won = 0;
void * engine(void * arg);
void * arbiter();
int main(int argc, char** argv) {
int n = rand() % 100;
pthread_t t1, t2;
pthread_create(&t1, NULL, arbiter, NULL);
pthread_create(&t2, NULL, engine, &n);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return (EXIT_SUCCESS);
}
void * arbiter(){
sleep(10);
quit = 1;
printf("Stop!\n");
}
void * engine(void * arg){
int n = *(int*)arg;
printf("%d\n", n);
while(!quit){
printf("N = ");
int a;
scanf("%d", &a);
if(a < n){
printf("Go higher!\n");
} else if(a > n){
printf("Go lower!\n");
} else{
printf("Bingo!\n");
won = 1;
}
}
}
I don't know how to use mutexes or cond var here, to make the threads work together. Now, when the first thread wakes up, the second will execute the last scanf. How should I implement this correctly and safe?

sem_init and pthread_mutex_init

I was writing 2 similar codes for printing odd and even numbers from given number set using mutex lock and semaphore. Both of the codes works fine.
But, while using mutex lock, even if I wont declare the pthread_mutex_init function, still the program executes with no issues. But that's not the case with semaphore. For this case, I have to declare sem_init in main() else the program execution gets stuck in sem_wait() (found after debugging).
So, how in the case of mutex lock, even without declaring init(), the program executes?
For reference, I am attaching the semaphore code.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
pthread_t tid[2];
unsigned int shared_data[] = {23,45,67,44,56,78,91,102};
unsigned int rc;
int len=(sizeof(shared_data)/sizeof(shared_data[0]));
int i=0;
void *even(void *arg) {
rc = sem_wait(&mutex);
int temp = rc;
if(rc)
printf("Semaphore failed\n");
do{
if(shared_data[i] %2 == 0) {
printf("Even: %d\n",shared_data[i]);
i++;
}
else
rc = sem_post(&mutex);
}while(i<len);
}
void *odd(void *arg) {
rc = sem_wait(&mutex);
if(rc)
printf("Semaphore failed\n");
do {
if(shared_data[i] %2 != 0) {
printf("Odd: %d\n",shared_data[i]);
i++;
}
else
rc = sem_post(&mutex);
}while(i<len);
}
int main() {
sem_init(&mutex, 0,1);
pthread_create(&tid[0], 0, &even, 0);
pthread_create(&tid[1], 0, &odd, 0);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
sem_destroy(&mutex);
return 0;
}
EDIT: Attaching the mutex lock code as well.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_t tid[2];
unsigned int shared_data []= {23,45,67,44,56,78,91,102};
pthread_mutex_t mutex;
unsigned int rc;
int len=(sizeof(shared_data)/sizeof(shared_data[0]));
int i=0;
void* PrintEvenNos(void *ptr)
{
rc = pthread_mutex_lock(&mutex);
if(rc)
printf("Mutex lock has failed\n");
do
{
if(shared_data[i]%2 == 0)
{
printf("Even:%d\n",shared_data[i]);
i++;
} else {
rc=pthread_mutex_unlock(&mutex);
}
} while(i<len);
}
void* PrintOddNos(void* ptr1)
{
rc = pthread_mutex_lock(&mutex);
if(rc)
printf("Mutex lock has failed\n");
do
{
if(shared_data[i]%2 != 0)
{
printf("Odd:%d\n",shared_data[i]);
i++;
} else {
rc=pthread_mutex_unlock(&mutex);
}
} while(i<len);
}
void main(void)
{
pthread_create(&tid[0],0,PrintEvenNos,0);
pthread_create(&tid[1],0,PrintOddNos,0);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
}
So, how in the case of mutex lock, even without declaring init(), the program executes?
This is undefined behavior, so there is no proper result. Per POSIX pthread_mutex_lock():
If mutex does not refer to an initialized mutex object, the behavior of pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock() is undefined.
"Appears to work" is one possible result of undefined behavior.
You have sem_init call for sem_t mutex;.
But pthread_mutex_init call is missing for pthread_mutex_t mutex;.
Both of the codes works fine.
No they don't; but first:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Is how you should have initialized your mutex. On your system, this value might be zero, which would be equivalent to what you have. Either way, the problem is your program is broken.
One of your threads (even, odd) acquires a lock. In the case of even, when i is 0,1,2,5 or 6; you unlock it, which would permit odd() to proceed. In the case of odd, when i is 3,4,5 or 7, you unlock it, which would permit even() to proceed. So in your logic, the lock does nothing at all.
Also, semaphores are counters; so when you release it 5 times, you are permitting the next 5 sem_waits to proceed. Simple mutexes are gates, so only the first unlock has any effect, the subsequent 4 are errors. You don't check the error status of the unlock, which is typically the one that uncovers logic errors.
fwiw, on macos, the pthread_mutex_lock()'s both report an error.

pthread cleanup function is not getting called

i am trying to use pthread clean up function to release the mutex which the cancelled thread is holding
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define SHOW_TECH_CMD_MAX_EXEC_TIME 5 //in secs
pthread_mutex_t waitMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t testMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t waitCond = PTHREAD_COND_INITIALIZER;
void mutex_cleanup_handler(void *arg )
{
printf("cleanup \n");
pthread_mutex_unlock( &testMutex );
}
void *execute_on_thread(void *arg);
void *execute_on_thread(void *arg)
{
pthread_cleanup_push(mutex_cleanup_handler, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_mutex_lock( &testMutex );
while(1)
{
printf(".");
}
pthread_mutex_lock( &waitMutex );
pthread_cond_signal( &waitCond );
pthread_mutex_unlock( &waitMutex );
pthread_cleanup_pop(1); // even with 1 behavior remains the same
return (void *) 0;
}
int main( )
{
pthread_t tid;
struct timespec ts;
int error;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 5;
pthread_create(&tid,NULL,execute_on_thread,NULL);
pthread_mutex_lock(&waitMutex);
error = pthread_cond_timedwait(&waitCond, &waitMutex,&ts);
pthread_mutex_unlock(&waitMutex);
printf("come here 1\n");
if(error == ETIMEDOUT)
{
printf("come here 2\n");
error = pthread_cancel(tid);
if(error != 0)
{
printf("come here 3\n");
}
}
}
cleanup function is not getting called itself
the thread is getting cleared properly , but the cleanup function is not being called
As Man says:
The pthread_cleanup_pop() function removes the routine at the top of
the stack of clean-up handlers, and optionally executes it if execute
is nonzero.
Then you have to write:
pthread_cleanup_pop(1);
Take note that your thread function definition is incorrect
EDIT
Using your code, the correction below works.
if(error == ETIMEDOUT)
{
printf("come here 2\n");
error = pthread_cancel(tid);
pthread_join(tid, NULL);
if(error != 0)
{
printf("come here 3\n");
}
You must wait the thread completion before end of your application.
In your code, after thread cancel request, the application ends immediately.

Multithreaded semaphore program

I've spent quite a few hours on trying to figure this one out and I'm completly stuck. The program is supposed to start 6 threads. Where some threads start where others end. Right now, I'm trying to get one single thread (thread 0) to execute. The caps lock commenting shows where I have added code and done my mistakes. My main struggle here is dealing with the pointers. Could anyone give me any pointers (ha..ha.. :c )?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define SHARED 1
sem_t sem[6];
struct threadargs
{
int id; /* thread number */
int sec; /* how many sec to sleep */
int signal[6]; /* which threads to signal when done */
};
void *tfunc(void *arg)
{
int i;
struct threadargs *targs=arg;
sem_wait(sem); //WAIT FOR OWN SEMAPHORE
printf("Thread %d is running\n", targs->id);
sleep(targs->sec);
printf("Thread %d is completed and may wake others..\n", targs->id);
for(i=0; i<6; i++) //ITERATE OVER signal_ARRAY &
{ //WAKE THREAD NUMBER i IF
if(targs->signal[i] == 1) //signal[i] IS 1
pthread_cond_signal(&sem[i]);
}
}
int main(void)
{
int i, j;
struct threadargs *targs[6];
pthread_t tid[6];
for(i=0; i<6; i++)
{
targs[i] = (struct threadargs*) malloc(sizeof(struct threadargs));
for(j=0; j<6; j++)
{ targs[i]->signal[j]=0; }
}
targs[0]->id=1;
targs[0]->sec=1;
targs[0]->signal[1]=1;
targs[0]->signal[4]=1;
sem[0] = 0; //INITIALIZE THREAD'S SEMAPHORE TO 0 or 1
pthread_create(targs[0], NULL, tfunc, NULL) // START THREAD
for(i=0; i<6; i++)
pthread_join(tid[i], NULL);
return 0;
}
Alright. First things first, I do recommend taking a second look at your coding style. It is of course highly subjective and I won't say yours is bad, but it took me a while to figure it out (if you really want to know, I recommend the Linux coding style for C/C++ code).
Lets get on with your problem. As far as I can see, the main issue seems that you're basically comparing pointers to apples with pointers to banana's (in other words, you're using the wrong pointer type in the wrong place).
To make sure that calls to functions and the like are correct, make sure to look up the API documentation for functions that are new to you (examples: pthread_create, sem_init, sem_wait, sem_post, pthread_cond_signal).
As you can see, pthread_cond_signal doesn't take a sem_t* as argument, and therefore you can't pass one to it and expect it to work. Below you'll find an example program showing how semaphores are used.
First, a new thread is created which will be put in waiting state instantly. As soon as the main tread finished counting from 0 to 150, it will post ('unlock') the semaphore and allowing the second thread to finish its execution.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
static sem_t sem_thread_one;
static pthread_t thread_one_data;
static int x;
static void *tfunc(void *arg)
{
sem_wait(&sem_thread_one);
printf("Thread 1 is running. The value of x is %i\n", x);
return NULL;
}
int main(int argc, char **argv)
{
sem_init(&sem_thread_one, 0 /* don't share between processes */, 0);
if(pthread_create(&thread_one_data, NULL, &tfunc, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
while(x < 150) {
x++;
}
sem_post(&sem_thread_one);
if(pthread_join(thread_one_data, NULL)) {
fprintf(stderr, "Could not join threads, exiting!\n");
return -EXIT_FAILURE;
}
sem_destroy(&sem_thread_one);
printf("Program ran succesfully!\n");
return -EXIT_SUCCESS;
}
Save in a file sem.c and compile & link using:
gcc -Wall -Os -pthread -o sem_test sem.c
Now a second example, but now using pthread_cond_t. The functionality of the program is somewhat similar, it waits for a counter to reach a certain number.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
static pthread_t thread_one_data, thread_two_data;
static volatile int x, y, idx = 10;
static int count = 1;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
static void *cond_test_wait(void *arg)
{
pthread_mutex_lock(&mutex);
while(count < 10) {
printf("Waiting for `count < 10' to become true\n");
pthread_cond_wait(&condition, &mutex);
}
pthread_mutex_unlock(&mutex);
printf("Test wait thread finished. Value of count: %i\n", count);
return NULL;
}
static void *cond_test_signal(void *arg)
{
while(count < 10) {
pthread_mutex_lock(&mutex);
pthread_cond_signal(&condition);
/* do more intelligent things here */
count++;
pthread_mutex_unlock(&mutex);
}
printf("Test signal thread finished\n");
return NULL;
}
int main(int argc, char **argv)
{
if(pthread_create(&thread_one_data, NULL, &cond_test_wait, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
if(pthread_create(&thread_two_data, NULL, &cond_test_signal, NULL)) {
fprintf(stderr, "Could not create thread, exiting!\n");
return -EXIT_FAILURE;
}
pthread_join(thread_one_data, NULL);
pthread_join(thread_two_data, NULL);
pthread_cond_destroy(&condition);
pthread_mutex_destroy(&mutex);
printf("Program ran succesfully!\n");
return -EXIT_SUCCESS;
}
Save in a file cond.c and compile & link using:
gcc -o cond -pthread -Os -Wall cond.c
Do note how neat condition work in this example. You can use them to wait until any expression (= condition) becomes true. After the condition becomes true normal execution continue's.
If you need any more help, don't hesitate to ask in the comments. Good luck combining the above examples to fix up your program.

Sem_wait() not blocking after first true condition

I am learning to use semaphores and below is a small scenario which I've tried to implement. Its behaving weird in a way. After sem_wait() gets unblocked first time, its not getting blocked again and keeps on looping, not getting why. Is this the right way or right scenario to use semaphore?
EDIT: I just realized that if I uncomment the sleep after sem_post, it works fine. .Reason being it was repeatedly doing sem_post() before thread could do coin=0 I believe. But is it right to use sleep this way with semaphores. I believe this would be considered a bad practice?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define MAX_MSG_LEN 256
sem_t sem1;
sem_t sem2;
int coin=0;
void *thrdFun1(void *arg);
void *thrdFun2(void *arg);
void toggleCase(char *buf, int cnt);
int main()
{
pthread_t thrd1;
char argmsg1[] = "Thread1: Waiting to deliver\n";
int thNum;
int res;
res = sem_init(&sem1, 0,0);
// res = sem_init(&sem2, 0,0);
res = pthread_create(&thrd1, NULL, thrdFun1, argmsg1);
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1);
// sleep(1);
}
}
return 0;
}
void *thrdFun1(void *arg)
{
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1);
printf("Delivered...\n");
coin=0;
sleep(1);
}
}
Semaphores are used to control Critical-Section Access. In this case, critical section would be the output shell. The thread may or may not start promptly when the pthread_create() is called. Also, sem_wait() will decrease the value of sem1 with each call. Thus, when you include sleep(1) in the thrdFun1 function, there may be undefined behaviour :)
You need to remove sleep(1); from function thrdFun1. After this there will not be any need of sleep(1) in main.
You realized it right, it is repeatedly doing sem_post() before thread could do coin=0 when removing all sleeps.
To solve this you could use second semaphore (you already tried it seems) like below,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#define MAX_MSG_LEN 256
sem_t sem1;
sem_t sem2;
int coin=0;
void *thrdFun1(void *arg);
void *thrdFun2(void *arg);
void toggleCase(char *buf, int cnt);
int main()
{
pthread_t thrd1;
char argmsg1[] = "Thread1: Waiting to deliver\n";
int thNum;
int res;
res = sem_init(&sem1, 0,0);
res = sem_init(&sem2, 0,0);
res = pthread_create(&thrd1, NULL, thrdFun1, argmsg1);
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1); // Coin is spun
sem_wait(&sem2); // Wait till it is caught
}
}
return 0;
}
void *thrdFun1(void *arg)
{
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1); // Wait to spin the coin
coin=0;
sem_post(&sem2); // inform as caught
printf("Delivered...\n");
}
}
there is a chance that "Thread1: Waiting to deliver" this string can get printed many times.
What you are trying to achieve is looks like producer-consumer problem.
You required two semaphore to achive this.
main function:
while(1)
{
if (coin==0)
{
printf("no coin: please enter coin\n");
scanf("%d",&coin);
}
else
{
sem_post(&sem1);
sem_wait(&sem2)
}
}
in thread function
while(1)
{
printf("I'm %s\n",(char *)arg);
sem_wait(&sem1);
printf("Delivered...\n");
coin=0;
sem_post(&sem2)
}

Resources