I am just a beginner in Programming using C.For my college project I want to create a multi-threaded server application to which multiple clients can connect and transfer there data which can be saved in a database.
After going through many tutorials I got confused about how to create multiple threads using pthread_create.
Somewhere it was done like:
pthread_t thr;
pthread_create( &thr, NULL , connection_handler , (void*)&conn_desc);
and somewhere it was like
pthread_t thr[10];
pthread_create( thr[i++], NULL , connection_handler , (void*)&conn_desc);
I tried by implementing both in my application and seems to be working fine. Which approach of the above two is correct which I should follow.
sorry for bad english and description.
Both are equivalent. There's no "right" or "wrong" approach here.
Typically, you would see the latter when creating multiple threads, so an array of thread identifiers (pthread_t) are used.
In your code snippets, both create just a single thread. So if you want to create only one thread, you don't need an array. But this is just like declaring any variable(s) that you didn't use. It's harmless.
In fact, if you don't need the thread ID for any purpose, (for joining or changing attributes etc.), you can create multiple threads using a single thread_t variable without using an array.
The following
pthread_t thr;
size_t i;
for(i=0;i<10;i++) {
pthread_create( &thr, NULL , connection_handler , &conn_desc);
}
would work just fine. Note that the cast to void* is unnecessary (last argument to pthread_create()). Any data pointer can be implicitly converted to void *.
Sample Example of multiple thread :
#include<iostream>
#include<cstdlib>
#include<pthread.h>
using namespace std;
#define NUM_THREADS 5
struct thread_data
{
int thread_id;
char *message;
};
void *PrintHello(void *threadarg)
{
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
cout << "Thread ID : " << my_data->thread_id ;
cout << " Message : " << my_data->message << endl;
pthread_exit(NULL);
}
int main ()
{
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc, i;
for( i=0; i < NUM_THREADS; i++ )
{
cout <<"main() : creating thread, " << i << endl;
td[i].thread_id = i;
td[i].message = "This is message";
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)&td[i]);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
The first one you provided creates a single thread.
The second one (if looped) has the potential to spawn 10 threads.
Basically the pthread_t type is a handler for the thread so if you have an array of 10 of them then you can have 10 threads.
For everyone, use a thread list because it will not be free if you call once pthread_join().
Code exemple :
int run_threads(void)
{
int listLength = 5;
pthread_t thread[listLength];
//Create your threads(allocation).
for (int i = 0; i != listLength; i++) {
if (pthread_create(&thread[i], NULL, &routine_function, NULL) != 0) {
printf("ERROR : pthread create failed.\n");
return (0);
}
}
//Call pthread_join() for all threads (they will get free) and all threads
//will terminate at this position.
for (int i = 0; i != listLength; i++) {
if (pthread_join(thread[i], NULL) != 0) {
printf("ERROR : pthread join failed.\n");
return (0);
}
}
//return 1 when all threads are terminated.
return (1);
}
Related
I'm very new in C and Linux and English is not my mother language. Sorry in advance.
I'm working on a school project which is implementing thread api and I made the thread_create() function using clone().
The problem is that when I call thread_create(&tid1, NULL, (void *)Testcase1, 0); ,
it creates a new thread but TestCase1 also includes thread_create and it doesn't seem create another thread. Let me explain with my code below:
int foo(void* arg){
printf("Hii");
return 0;
}
int thread_create(thread_t *thread, thread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
void* stack;
stack= malloc( STACK_SIZE );
pid_t pid;
if( stack==0)
{
perror( "malloc : could not allocate stack" );
exit( 1 );
}
pid = clone( &foo ,( char* )stack+STACK_SIZE,SIGCHLD|CLONE_VM|CLONE_SIGHAND|CLONE_FS|CLONE_FILES,0 );
if(pid == -1)
{
perror("clone");
exit(2);
}
kill(pid, SIGSTOP);
Thread* newTCB = (Thread*)malloc(sizeof(Thread));
newTCB->stackSize = malloc(STACK_SIZE);
newTCB->pid = pid;
newTCB->status = THREAD_STATUS_READY;
rEnqueue(newTCB);
rPrintqueue();
free(stack);
printf("Child thread returned and stack freed.\n");
return 0;
}
And this is my test code below:
thread_create(&tid1, NULL, (void*)TestCase1, 0);
TestCase1() below:
int Tc1ThreadProc(int param)
{
int tid = 0;
int count = 0;
tid = thread_self();
count = 3;
while (count > 0)
{
/* sleep for 1 seconds */
sleep(2);
printf("Tc1ThreadProc: my thread id (%d), arg is (%d)\n", tid, param);
count--;
}
}
void TestCase1(void)
{
thread_t tid[TOTAL_THREAD_NUM];
thread_create(&tid[0], NULL, (void*)Tc1ThreadProc, (int*)1);
thread_create(&tid[1], NULL, (void*)Tc1ThreadProc, (int*)2);
thread_create(&tid[2], NULL, (void*)Tc1ThreadProc, (int*)3);
while(1){}
return ;
}
it's supposed to print "Tc1ThreadProc: my thread id (%d), arg is (%d)\n" 3 times but it prints only "Hii" which is because of the call to foo().
How do I fix this?
You pass pointer to function "TestCase1" as argument to "thread_create", but inside "thread_create" you don't use it at all:
thread_create(&tid1, NULL, (void*)TestCase1, 0);
You're calling "clone" syscall only with pointer to "foo" function.
From inside "thread_create" your "TestCase1" pointer is named "start_routine", so you need to call similar "clone" syscall, but instead pointer to "foo" you should pass pointer to "TestCase1". Something like that:
pid = clone( start_routine, (char*) stack + STACK_SIZE, SIGCHLD | CLONE_VM | CLONE_SIGHAND | CLONE_FS | CLONE_FILES, 0);
I have a project about marriage operations. In this program, a thread called registrar uses marriage function. In this marriage operations, we have brides and grooms. Marriage function does decrease bride count and groom count one by one. But i have a problem while i want to decrease these count.
MAIN.c
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<semaphore.h>
#include "bride.h"
#include "groom.h"
pthread_t groomThread;
pthread_t brideThread;
sem_t registrarSemaphore;
pthread_mutex_t lock;
int *groomCount = 14;
int *brideCount = 20;
int *availableRegistrar;
void createBride(int *brideCount) {
pthread_create(&brideThread, NULL, &increaseBrideCount, (void *) brideCount);
}
void createGroom(int *groomCount) {
pthread_create(&groomThread, NULL, &increaseGroomCount, (void *) groomCount);
}
void deleteGroom(int *groomCount) {
pthread_create(&groomThread, NULL, &decreaseGroomCount, (void *) groomCount);
}
void deleteBride(int *brideCount) {
pthread_create(&brideThread, NULL, &decreaseBrideCount, (void *) brideCount);
}
void marriage() {
sem_init(®istrarSemaphore, 0, 2);
while (1) {
sem_getvalue(®istrarSemaphore, &availableRegistrar);
printf("\nAvailable Registrar Number = %d\n", availableRegistrar);
printf("bride %d\n", brideCount);
printf("groom %d\n", groomCount);
if (brideCount > 0 && groomCount > 0) {
sem_wait(®istrarSemaphore);
sem_getvalue(®istrarSemaphore, &availableRegistrar);
printf("Available Registrar %d \n", availableRegistrar);
printf("Marriage Bride %d and Groom %d \n", brideCount, groomCount);
pthread_mutex_lock(&lock);
deleteBride(brideCount);
pthread_mutex_unlock(&lock);
//pthread_join(brideThread, &brideCount);
pthread_mutex_lock(&lock);
deleteGroom(groomCount);
pthread_mutex_unlock(&lock);
//pthread_join(groomThread, &groomCount);
printf("Exiting critical region...\n\n");
/* END CRITICAL REGION */
sem_post(®istrarSemaphore);
}
int random = rand() % 100;
if (random % 7 > 4) {
printf("Bride Created\n");
pthread_mutex_lock(&lock);
createBride(brideCount);
//pthread_join(brideThread, &brideCount);
pthread_mutex_unlock(&lock);
}
if (random % 7 < 2) {
printf("Groom Created\n");
pthread_mutex_lock(&lock);
createGroom(groomCount);
//pthread_join(groomThread, &groomCount);
pthread_mutex_unlock(&lock);
}
pthread_join(brideThread, &brideCount);
pthread_join(groomThread, &groomCount);
for (int i = 0; i < 100000000; i++);
printf("------------------------------");
}
}
int main(void) {
marriage();
}
In pthread_create part, there are some functions as you can see. It defined in .h part. For example in bride.h, there are 2 functions about bride.
BRIDE.H
#ifndef BRIDE_H
#define BRIDE_H
void* increaseBrideCount(void * bride);
void* decreaseBrideCount(void * bride);
#endif
BRIDE.C
#include <pthread.h>
#include "bride.h"
void* increaseBrideCount(void *bride){
int brideCount = (int)bride;
brideCount++;
pthread_exit(brideCount);
}
void* decreaseBrideCount(void* bride){
int brideCount = (int)bride;
brideCount--;
pthread_exit(brideCount);
}
While im creating new bride,i cant send the new value of bride to the function. For example :
I have 20 brides and 14 grooms at first.
I have 2 available registrar
Marraige does.
Bride count = 19, groom count = 13
Then, i want to create new bride.
It count goes to = 1 :( Im trying to make it 20 again.
If you can help, i would be very happy. Thank you
Semaphores are used to make sure only one of the threads that might make a particular change does so at a time. The things being changed in this case are the bride and groom counts, so you need to "protect" them using semaphores. You even seem to have created semaphores for this purpose (brideSemaphore and groomSemaphore); you just need to use them.
By the way: if you use a semaphore only in a single thread, you're wasting your time (as seems to be the case w/ your registrarSemaphore) in marriage()). Either it needs to be used elsewhere as well, or not at all.
I'm having trouble debugging the following program I wrote. The idea is to have two seperate threads; one thread executes a 5 second countdown while the other waits for key input from the user. Whichever thread completes first should cancel the sibling thread and exit the program. However, the following code just hangs.
Any help would be appreciated, but I would be most grateful for an explanation as to the problem.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For sleep()
#define NUM_THREADS 2
// The stuct to be passed as an argument to the countdown routine
typedef struct countdown_struct {
pthread_t *thread;
signed int num_secs;
} CountdownStruct;
// Struct for passing to the input wait routine
typedef struct wait_struct {
pthread_t *thread;
int *key;
} WaitStruct;
// Countdown routine; simply acts as a timer counting down
void * countdown(void *args)
{
CountdownStruct *cd_str = (CountdownStruct *)args;
signed int secs = cd_str->num_secs;
printf("Will use default setting in %d seconds...", secs);
while (secs >= 0)
{
sleep(1);
secs -= 1;
printf("Will use default setting in %d seconds...", secs);
}
// Cancel the other struct
pthread_cancel(*(cd_str->thread));
return NULL;
}
// Waits for the user to pass input through the tty
void * wait_for_input(void *args)
{
WaitStruct *wait_str = (WaitStruct *) args;
int c = 0;
do {
c = getchar();
} while (!(c == '1' || c == '2'));
*(wait_str->key) = c;
// Cancel the other thread
pthread_cancel(*(wait_str->thread));
return NULL;
}
int main(int argc, char **argv)
{
pthread_t wait_thread;
pthread_t countdown_thread;
pthread_attr_t attr;
int key=0;
long numMillis=5000;
int rc=0;
int status=0;
// Create the structs to be passe as paramaters to both routines
CountdownStruct *cd_str = (CountdownStruct *) malloc(sizeof(CountdownStruct));
if (cd_str == NULL)
{
printf("Couldn't create the countdown struct. Aborting...");
return -1;
}
cd_str->thread = &wait_thread;
cd_str->num_secs = 5;
WaitStruct *wait_str = (WaitStruct *) malloc(sizeof(WaitStruct));
if (wait_str == NULL)
{
printf("Couldn't create the iput wait struct. Aborting...");
return -1;
}
wait_str->thread = &countdown_thread;
wait_str->key = &key;
// Create the joinable attribute
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Create both threads
rc = pthread_create(&countdown_thread, &attr, countdown, (void *) cd_str);
if (rc) { printf("Error with the thread creation!"); exit(-1); }
rc = pthread_create(&wait_thread, &attr, wait_for_input, (void *) wait_str);
if (rc) { printf("Error with the thread creation!"); exit(-1); }
// Destroy the pthread_attribute
pthread_attr_destroy(&attr);
// now join on the threads and wait for main
pthread_join(wait_thread, NULL);
pthread_join(countdown_thread, NULL);
// Call pthread_exit
pthread_exit(NULL);
// Free the function structs
free(cd_str);
free(wait_str);
}
Getchar is not required to be a cancellation point. Select and pselect are. Even if you want to continue to use a countdown thread you could still provide a cancellation point in the opposing thread by use of select.
I had reasonable behavior with the following modified wait_for_input()
// Waits for the user to pass input through the tty
void * wait_for_input(void *args)
{
WaitStruct *wait_str = (WaitStruct *) args;
int c = 0;
fd_set readFds;
int numFds=0;
FD_ZERO(&readFds);
do {
struct timeval timeout={.tv_sec=8,.tv_usec=0};
/* select here is primarily to serve as a cancellation
* point. Though there is a possible race condition
* still between getchar() getting called right as the
* the timeout thread calls cancel.().
* Using the timeout option on select would at least
* cover that, but not done here while testing.
*******************************************************/
FD_ZERO(&readFds);
FD_SET(STDOUT_FILENO,&readFds);
numFds=select(STDOUT_FILENO+1,&readFds,NULL,NULL,&timeout);
if(numFds==0 )
{
/* must be timeout if no FD's selected */
break;
}
if(FD_ISSET(STDOUT_FILENO,&readFds))
{
printf("Only get here if key pressed\n");
c = getchar();
}
} while (!(c == '1' || c == '2'));
*(wait_str->key) = c;
// Cancel the other thread
pthread_cancel(*(wait_str->thread));
return NULL;
}
I'm doing parallel sorting using pthreads. Currently, I'm working with 4 threads and I have just started out , so right now no threads access same global locations. ( I have declared two variables globally, variables are arrays of large size and I'm making sure no two threads access the same index.)
Inside AASort , I have another function being called. This code works if I don't call any function within the AASort function.
unsigned int Numbers [N/4] [4];
unsigned int vMergedArray[NumProcs][64][4];
unsigned int v[NumProcs][2][4];
main()
{
/* Initialize array of thread structures */
threads = (pthread_t *) malloc(sizeof(pthread_t) * NumProcs);
assert(threads != NULL);
for(threadId=0; threadId < NumProcs; threadId++) {
ret = pthread_create(&threads[threadId], &attr, AASort, (void*) threadId);
assert(ret == 0);
}
for(threadId=0; threadId < NumProcs; threadId++) {
ret = pthread_join(threads[threadId], NULL);
assert(ret == 0);
}
}
I learn threads. I have read that thread terminates after it is out of a function (that is passed as parameter to pthread_create function).
So I create threads in the loop, they are executed and afterwards they are terminated.
(sorry for some long code)
But when I call a function pthread_create, new threads get the same ids. Why?
struct data {
FILE *f;
};
void *read_line_of_file(void *gdata) {
pthread_mutex_lock(&g_count_mutex); // only one thread can work with file,
//doing so we block other threads from accessing it
data *ldata = (data *) gdata;
char line[80];
int ret_val =fscanf(ldata->f,"%s",line);
pthread_mutex_unlock(&g_count_mutex); // allow other threads to access it
if (ret_val != EOF)
printf("%s %lu\n ", line, pthread_self());
// some time consuming operations, while they are being executed by one thread,
// other threads are not influenced by it (if there are executed on different cores)
volatile int a=8;
for (int i=0;i <10000;i++ )
for (int i=0;i <10000;i++ ) {
a=a/7+i;
}
if (ret_val == EOF) // here thread ends
pthread_exit((void *)1);
pthread_exit((void *)0);
}
int main() {
int kNumber_of_threads=3, val=0;
pthread_t threads[kNumber_of_threads];
int ret_val_from_thread=0;
data mydata;
mydata.f = fopen("data.txt","r");
if ( mydata.f == NULL) {
printf("file is not found\n");
return 0;
}
for( ; val != 1 ;) {
// THIS IS THAT PLACE, IDs are the same (according to the number of processes),
// I expected them to be changing..
for(int i=0; i<kNumber_of_threads; i++) {
pthread_create(&threads[i],NULL,read_line_of_file, &mydata);
}
for(int i=0; i<kNumber_of_threads; i++) {
pthread_join(threads[i], (void **) &ret_val_from_thread);
if (ret_val_from_thread != 0)
val = ret_val_from_thread;
}
printf(" next %d\n",val);
}
printf("work is finished\n");
fclose(mydata.f);
return 0;
}
as result, I see that id of threads are not being changed:
I wonder, are new threads really created?
Thanks in advance!
Thread IDs are only guaranteed to be different among currently running threads. If you destroy a thread and create a new one, it may well be created with a previously used thread ID.