Threads are created, then all exit before critical region - c

I need to write a program for class that creates 8 threads. 4 producer, and 4 consumer. The producers need to loop, and randomly send SIGUSR1 or SIGUSR2 to all consumer threads. Only 2 should register if they have received SIGUSR1 and the other 2 register SIGUSR2.
When I try to run it all threads are created, "prod ready" is printed by all 4 producer threads,"waiting 1" is printed by both threads, but "waiting 2" is printed 3 times then all threads exit. At the end of debugging it says that the process exits normally.
I need to use semaphores to control the critical regions. Any help would be great.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <signal.h>
#define NP 4
#define NC1 2
#define NC2 2
#define CNT 10
void handler1(int signum);
void handler2(int signum);
typedef struct {
int sent;
int received;
int buf[1];
int SIG1;
int SIG2;
sem_t con;
sem_t prod;
} sbuf_t;
sbuf_t buff;
void *producer() {
printf("prod ready \n");
int s;
while(1){
sem_wait(&buff.prod);
s=rand()%2;
if(s==1){
buff.sent++;
kill(0,SIGUSR1);
}
else if(s==2){
buff.sent++;
kill(0,SIGUSR2);
}sem_post(&buff.prod);
}
}
void *consumer1() {
signal(SIGUSR1, handler1);
printf("waiting 1\n");
while(1){
}
}
void *consumer2() {
signal(SIGUSR2, handler2);
printf("waiting 2\n");
while(1){
}
}
void handler1(int signum){
if(signum==SIGUSR1){
sem_wait(&buff.con);
printf("Caught 1\n");
buff.received++;
buff.SIG1++;
sem_post(&buff.con);
}
}
void handler2(int signum){
if(signum==SIGUSR2){
sem_wait(&buff.con);
printf("caught 2 \n");
buff.received++;
buff.SIG2++;
sem_post(&buff.con);
}
}
void main(){
pthread_t threads[9];
buff.SIG1=0;
buff.SIG2=0;
buff.sent=0;
buff.received=0;
int index;
sem_init(&buff.con, 0, 0);
sem_init(&buff.prod, 0, 0);
for (index = 0; index < NP; index++) {
pthread_create(&threads[index], NULL, producer,NULL);
}
for (index = 0;index < NC1;index++) {
pthread_create(&threads[index+4], NULL, consumer1,NULL);
}
for (index = 0;index < NC2;index++) {
pthread_create(&threads[index+6], NULL, consumer2,NULL);
}
}

In the main() function, the threads are being created,
then main() exits.
When main() exits, all the threads are also exited.
Suggest reading about such functions as: pthread_exit() and pthread_join()
Note: the following has an error in the handling of the signals and in the handling of the semaphores, but will demonstrate the use of pthread_join() and pthread_exit()
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <signal.h>
#define NP 4
#define NC1 2
#define NC2 2
#define CNT 10
#define MAX_THREADS (9)
void handler1(int signum);
void handler2(int signum);
struct sbuf_t
{
int sent;
int received;
int buf[1];
int SIG1;
int SIG2;
sem_t con1;
sem_t con2;
sem_t prod;
};
typedef struct sbuf_t myData;
myData buff;
void *producer( void *dummy )
{
(void)dummy;
printf("prod ready \n");
int s;
while(1)
{
sem_wait(&buff.prod);
s=rand()%2;
if( !s )
{
buff.sent++;
kill( 0, SIGUSR1 );
}
else // if( s )
{
buff.sent++;
kill( 0, SIGUSR2 );
}
//sem_post(&buff.prod);
}
pthread_exit( NULL );
} // end thread: producer
void *consumer1( void *dummy )
{
(void)dummy;
//signal(SIGUSR1, handler1);
printf("waiting 1\n");
while(1)
{
sem_wait( &buff.con1 );
// do something
//sem_post( &buff.prod );
}
pthread_exit( NULL );
} // end thread: consumer1
void *consumer2( void *dummy )
{
(void)dummy;
//signal(SIGUSR2, handler2);
printf("waiting 2\n");
while(1)
{
sem_wait( &buff.con2 );
// do something
//sem_post( &buff.prod );
}
pthread_exit( NULL );
} // end thread: consumer2
void handler(int signum)
{
sem_post(&buff.prod);
if(signum==SIGUSR1)
{
//sem_wait(&buff.con);
puts("Caught 1");
buff.received++;
buff.SIG1++;
sem_post(&buff.con1);
}
else if(signum==SIGUSR2)
{
//sem_wait(&buff.con);
puts("caught 2");
buff.received++;
buff.SIG2++;
sem_post(&buff.con2);
}
} // end signal handler: handler2
int main( void )
{
pthread_t threads[ MAX_THREADS ];
buff.SIG1=0;
buff.SIG2=0;
buff.sent=0;
buff.received=0;
int index;
sem_init(&buff.con1, 0, 0);
sem_init(&buff.con2, 0, 0);
sem_init(&buff.prod, 0, 0);
signal(SIGUSR2, handler);
signal(SIGUSR1, handler);
for (index = 0; index < NP; index++)
{
pthread_create(&threads[index], NULL, producer,NULL);
}
for (index = 0;index < NC1;index++)
{
pthread_create(&threads[index+4], NULL, consumer1,NULL);
}
for (index = 0;index < NC2;index++)
{
pthread_create(&threads[index+6], NULL, consumer2,NULL);
}
for( size_t x=0; x<MAX_THREADS; x++ )
{
pthread_join( threads[x], NULL );
}
} // end function: main
HOWEVER, the methods to handle signals to threads does not use kill() as that is for processes.
rather the code should use functions similar to::
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_signal(pthread_cond_t *cond)
The signaled thread should block on a pthread_cond_wait() call until another thread sends a signal using pthread_cond_signal(), with the same condition variable.
Considering the analogy with signals delivered to processes, this is a bit different because the signaled thread has already suspended its execution waiting for a signal, unlike a process that simply gets interrupted and goes on.

For you example, you are creating your producers before your consumers. Also in your producer, you are using a random number generator to determine if you issue USR1 or USR2. Your description gave the impression you wanted two of each (versus some random mix totaling 4) which is what I see the code doing for you.

Related

How to implement futex_wait sysccall for pid

I have a code which puts threads to sleep using futex_wait syscall. how can i put a process to sleep using futex_wait syscall?
I understand this program, it creates the thread and put them on sleep and then calls the futex_Wake syscall to wake the thread and futex_wake should return the number of threads it has woken up
Sample Code:
#include <stdio.h>
#include <pthread.h>
#include <linux/futex.h>
#include <syscall.h>
#include <unistd.h>
#define NUM 2
int futex_addr;
int futex_wait(void* addr, int val1){
return syscall(SYS_futex,&futex_addr,val1, NULL, NULL, 0);
}
int futex_wake(void* addr, int n){
int msecs = 0;
int waked = 0;
while(1){
sleep(1);
msecs++;
waked = syscall(SYS_futex, addr, FUTEX_WAKE, n, NULL, NULL, 0);
if (waked == n)
break;
if (msecs > 100){
printf("Wake timedout\n");
return 0;
}
}
return waked;
}
void* thread_f(void* par) {
int id = (int) par;
/*go to sleep*/
futex_addr = 0;
int ret = futex_wait(&futex_addr,0);
printf("Futex_wait_returned %d\n", ret);
// printf("Thread %d starting to work!\n",id);
return NULL;
}
int main () {
pthread_t threads[NUM];
int i;
for (i=0;i<NUM;i++) {
pthread_create(&threads[i],NULL,thread_f,(void *)i);
}
printf("Everyone wait...\n");
sleep(1);
printf("Now go!\n");
/*wake threads*/
int ret = futex_wake(&futex_addr, NUM);
printf("No of futex_wake processes are %d\n", ret);
/*give the threads time to complete their tasks*/
sleep(1);
printf("Main is quitting...\n");
return 0;
}
I am quite new to it, so i want to understand what changes should i make to put a process to sleep using futex

Producer-Consumer solution runs but doesn't print

This is my current code for the Producer-Consumer problem. I compiled it and ran it but nothing is printed. The command line takes in 3 arguments: Sleep time, producer threads, consumer threads. I've tried setting the values as 5, 1, 1 respectively, the sleep timer works but I'm unsure about the rest.
Code for buffer.h:
typedef int buffer_item;
#define BUFFER_SIZE 5
Code for buffer.c:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include "buffer.h"
buffer_item buffer[BUFFER_SIZE];
void *producer(void *param);
void *consumer(void *param);
pthread_mutex_t mutex;
sem_t empty;
sem_t full;
int insert_item(buffer_item item)
{
do
{
wait(empty);
wait(mutex);
signal(mutex);
signal(full);
}while(1);
return 0;
}
int remove_item(buffer_item *item)
{
do
{
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
}while(1);
return 0;
}
int main(int argc, char *argv[])
{
int sleepTime;
int producerThreads;
int consumerThreads;
int counter_1;
int counter_2;
if(argc != 4)
{
return -1;
}
sleepTime = atoi(argv[1]);
producerThreads = atoi(argv[2]);
consumerThreads = atoi(argv[3]);
srand((unsigned)time(NULL));
for(counter_1 = 0; counter_1 < producerThreads; counter_1++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, producer, NULL);
}
for(counter_2 = 0; counter_2 < consumerThreads; counter_2++)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, consumer, NULL);
}
sleep(sleepTime);
return 0;
}
void *producer(void *param)
{
buffer_item item;
int randomTime;
int counter_1 = 0;
while(1)
{
randomTime = rand() % 1000 + 1;
sleep(randomTime);
item = rand();
if(insert_item(item))
{
fprintf(stderr, "Error.");
}
else
{
printf("Producer ID: %lu, Produced Item: %d\n", pthread_self(), item);
printf("The buffer now contains %d items\n", counter_1);
++counter_1;
}
}
}
void *consumer(void *param)
{
buffer_item item;
int randomTime;
int counter_2 = 0;
while(1)
{
randomTime = rand() % 1000 + 1;
sleep(randomTime);
if(insert_item(item))
{
fprintf(stderr, "Error.");
}
else
{
printf("Consumer ID: %lu, Consumed Item: %d\n", pthread_self(), item);
printf("The buffer now contains %d items\n", counter_2);
++counter_2;
}
}
}
So far I've tried declaring the tid separately, skipping sleep and join the threads, but it still doesn't print.
Your code can't possibly run, indeed it doesn't even compile.
Here's a list of issues that need to be addressed:
wait should be sem_wait
signal should be sem_post for semaphores
int sem_wait(sem_t *sem); and int sem_post(sem_t *sem); take the pointer to a semaphore
sem_wait(mutex) and sem_post(mutex) give something like "incompatible type for argument 1 of sem_wait", I guess you want to acquire and release the lock on the mutex like pthread_mutex_lock(&mutex) and pthread_mutex_unlock(&mutex)
in the consumer if(insert_item(item)): item is used uninitialized
still in the consumer you use insert_item instead of remove_item
Coming to the main question "I compiled it and ran it but nothing is printed", it doesn't print anything because producer and consumer call, respectively, insert_item and remove_item and are trapped inside infinite loops (e.g. while(1))

Mutex Deadlock (pthread_mutex)

I can't understand why in this code I get a deadlock.
I have defined this mutexes:
mutex3: = 0 (LOCKED)
mutex2: = 1 (UNLOCKED)
I have 2 processes: Father and son. Each has N threads that I pass by argument. The child's threads contend with the father's threads a "resource" (typical producer / consumer problem).
Thanks in advance!
I suppose the problem is here:
void * func_padre(void * n)
{
while(1)
{
pthread_mutex_lock(&mutex2);
printf("Scrivi messaggio: ");
fflush(stdout);
scanf("%[^\n]",(char *)addr_mem);
getchar();
pthread_mutex_unlock(&mutex3);
}
}
void * func_figlio(void * n)
{
while(1)
{
pthread_mutex_lock(&mutex3);
write(fd,addr_mem,4096);
lseek(fd,0,SEEK_SET);
memset(addr_mem,0,4096);
pthread_mutex_unlock(&mutex2);
}
}
CODE:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
void * addr_mem;
pthread_mutex_t mutex2,mutex3;
int fd;
void * func_padre(void * n)
{
while(1)
{
pthread_mutex_lock(&mutex2);
printf("Scrivi messaggio: ");
fflush(stdout);
scanf("%[^\n]",(char *)addr_mem);
getchar();
pthread_mutex_unlock(&mutex3);
}
}
void * func_figlio(void * n)
{
while(1)
{
pthread_mutex_lock(&mutex3);
write(fd,addr_mem,4096);
lseek(fd,0,SEEK_SET);
memset(addr_mem,0,4096);
pthread_mutex_unlock(&mutex2);
}
}
int main(int argc, char*argv[])
{
if(argc<2)
{
printf("POCHI PARAMETRI\n");
fflush(stdout);
exit(-1);
}
int val=strtol(argv[2],0,10);
fd = open(argv[1],O_CREAT|O_RDWR,0666);
lseek(fd,0,SEEK_SET);//USELESS
///SHARED MEMORY
int id_mem;
id_mem = shmget(6543,4096,IPC_CREAT|0666);
addr_mem = shmat(id_mem,NULL,0);
/////////////////////
/// MUTEX
pthread_mutex_init(&mutex2,NULL);
pthread_mutex_init(&mutex3,NULL);
pthread_mutex_lock(&mutex3);
/////////////////////
pthread_t tid;
int i=0;
int pid;
pid = fork();
if(pid==0)
{
//CHILD
for(i=0; i<val; i++)
pthread_create(&tid,NULL,func_figlio,NULL);
while(1)
{
pthread_mutex_lock(&mutex3);
write(fd,addr_mem,4096);
memset(addr_mem,0,4096);
pthread_mutex_unlock(&mutex2);
}
}
else
{
//FATHER
for(i=0; i<val; i++)
pthread_create(&tid,NULL,func_padre,NULL);
while(1)
{
pthread_mutex_lock(&mutex2);
printf("Scrivi messaggio: ");
fflush(stdout);
scanf("%[^\n]",(char *)addr_mem);
getchar();
pthread_mutex_unlock(&mutex3);
}
}
}
Your problem is not a deadlock, it's just buggy code. A thread cannot unlock a mutex it did not lock. Your while loops all unlock mutexes they do not hold and never unlock the mutexes they do hold.
suggest:
have global variable that is cycled through a limited set of values, say: 0, 1, 0 ...
Use the mutex to stop other threads from progressing, I.E.
When a call (in thread) returns from locking the mutex,
then check
if the value in the global variable is for 'this' thread.
then
process the thread activities,
increment the global variable,
endif
endif
unlock the mutex
call nanosleep() to delay for a while to allow other threads to run

Question about consumer and producer threads

I'm having an issue with my sleep cycles in the ProducerConsumer.h file. If I comment out the sleep(rnum) the program will access the line printf("producer produced %d\n", item); If it's not commented out it will never access that line and I cant figure out why.
Here is my code it's in 3 separate files I would be grateful of any help thank you.
buffer.h:
typedef int buffer_item;
#define BUFFER_SIZE 5
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
ProducerConsumer.h:
#include <stdlib.h> /* required for rand() */
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <iostream>
#include "buffer.h"
#define RAND_DIVISOR 100000000;
#define TRUE 1
int counter;
buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t full, empty;
/* Producer Thread */
void *producer(void *param) {
buffer_item item;
while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);
printf("producer produced %d\n", item);
/* generate a random number */
item = rand();
/* acquire the empty lock */
sem_wait(&empty);
/* acquire the mutex lock */
pthread_mutex_lock(&mutex);
if(insert_item(item)) {
fprintf(stderr, " Producer report error condition\n");
} else {
printf("producer produced %d\n", item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal full */
sem_post(&full);
}
}
/* Consumer Thread */
void *consumer(void *param) {
buffer_item item;
while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);
/* aquire the full lock */
sem_wait(&full);
/* aquire the mutex lock */
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
fprintf(stderr, "Consumer report error condition\n");
} else {
printf("consumer consumed %d\n", item);`enter code here`
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal empty */
sem_post(&empty);
}
}
int insert_item(buffer_item item) {
if(counter < BUFFER_SIZE) {
buffer[counter] = item;
counter++;
return 0;
} else {
return -1;
}
}
int remove_item(buffer_item *item) {
if(counter > 0) {
*item = buffer[(counter-1)];
counter--;
return 0;
} else {
return -1;
}
}
main.cpp:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include "ProducerConsumer.h"
#include "buffer.h"
pthread_t tid;
pthread_attr_t attr;
void initializeData()
{
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
pthread_attr_init(&attr);
counter = 0;
}
int main() {
int i;
int numProd = 5; /* Number of producer threads */
int numCons = 5; /* Number of consumer threads */
initializeData();
/* Create the producer threads */
for(i = 0; i < numProd; i++) {
/* Create the thread */
pthread_create(&tid,&attr,producer,NULL);
}
/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid,&attr,consumer,NULL);
}
void *producer(void *param);
void *consumer(void *param);
/* Exit the program */
printf("Exit the program\n");
exit(0);
}
The RAND_MAX value is likely at least 32767, causing the the following line:
int rNum = rand() / RAND_DIVISOR;// defined as 100000000
to most always set rNum == 0.
Change statement to get a non-zero value:
int rNum = 1 + rand() % 10;// yields 1-10
Also, call srand() one time, and before using rand(). eg:
void *producer(void *param) {
buffer_item item;
srand((unsigned) time(&t));
...

No signals sent (POSIX and C)

I have written the code below, and I want the following:
In every 5 secs the parent gets a random int and puts into into the shared mem, then sends a signal to its child.
The child calculates how many trams are needed to carry that amount of passengers (as you can see there are defs for TRAM_CAP etc) and then the child puts the new tram count into the shared mem.
Finally, after it is informed by a signal, the parent prints the count of passengers and trams.
My problem is that no signals are sent but I dont know why.
I searched and googled million times but nothing... The timer is working but nothing else (just the first "tram amount calculation")
Btw this is the second C code I have written in my life :S so I am a newbie.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "fcntl.h"
#include "errno.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
#include <sys/time.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <math.h>
#define MEM_KEY 2003
#define INTERVAL 2
#define NEW_STAT SIGUSR1
#define NEW_TRAM_CNT SIGUSR2
#define MAX_PASSENGER 1000
#define TRAM_CAP 60
// "slots" in shared memory
#define PASS_CNT 0
#define TRAM_CNT 1
int get_pass_stat();
void handle_pass_stat_generation (int sig);
void handle_exit_func (int sig);
void set_pass_stat_generation();
void handle_new_tram_cnt_set(int sig);
void handle_new_stat_arrived(int sig);
void set_data(int which, int to);
int get_data(int which);
static pid_t ppid;
static pid_t chpid;
int main()
{
int shmid = shmget((key_t) MEM_KEY, sizeof(int) * 2, IPC_CREAT | 0666);
int* segptr = shmat(shmid, 0, 0);
segptr[PASS_CNT] = 0;
segptr[TRAM_CNT] = 0;
// seed for rand
srand((unsigned)time(NULL));
//
int pid=fork();
if(pid == 0) // child
{
signal(NEW_STAT, handle_new_stat_arrived);
while(1) sleep(30);
}else if(pid > 0) // parent
{
chpid = pid;
ppid = getpid();
signal(NEW_TRAM_CNT, handle_new_tram_cnt_set);
signal(SIGINT,handle_exit_func);
set_pass_stat_generation(pid);
while(1) sleep(30);
}else
{
perror("Fork error.");
exit(1);
};
return 0;
}
int get_pass_stat()
{
return rand() % MAX_PASSENGER + 1;
}
void handle_pass_stat_generation (int sig)
{
int st = get_pass_stat();
set_data(PASS_CNT, st);
kill( chpid, NEW_STAT );
printf("%i is the pass. cnt %i is the tram cnt. \n", st, get_data(TRAM_CNT));
// set_pass_stat_generation();
}
void handle_exit_func (int sig)
{
printf("\nBye Bye!!!\n");
exit(0);
}
void set_pass_stat_generation()
{
struct itimerval tval;
tval.it_interval.tv_sec = INTERVAL;
tval.it_interval.tv_usec = 0;
tval.it_value.tv_sec = INTERVAL; /* 5 seconds timer */
tval.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &tval,0);
signal(SIGALRM, handle_pass_stat_generation); /* set the Alarm signal capture */
}
void handle_new_stat_arrived(int sig)
{
int tram_count = get_data(TRAM_CNT);
int passenger_count = get_data(PASS_CNT);
if( (TRAM_CAP * tram_count < passenger_count) ||
(TRAM_CAP * (tram_count-1) > passenger_count) ||
(passenger_count == 0) )
{
int new_cnt = (int) ceil((double)passenger_count / (double)TRAM_CAP);
set_data(TRAM_CNT, new_cnt);
kill( ppid, NEW_TRAM_CNT );
}
signal(NEW_STAT, handle_new_stat_arrived);
}
void handle_new_tram_cnt_set(int sig)
{
signal(NEW_TRAM_CNT,handle_new_tram_cnt_set);
}
void set_data(int which, int to)
{
int shmid = shmget((key_t) MEM_KEY, 0, 0);
int* segptr = shmat(shmid, 0, 0);
segptr[which] = to;
}
int get_data(int which)
{
int shmid = shmget((key_t) MEM_KEY, 0, 0);
int* segptr = shmat(shmid, 0, 0);
return segptr[which];
}

Resources