Threading in c and initgraph - c

This is a program I did but the problem I am facing is that its not giving any output. When I try to run one thread its running perfectly else it doesn't. I tried to output it directly in the terminal which is working perfectly but when I try to output in the window using initgraph its not working.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<graphics.h>
struct myran
{
int x;
int y;
};
void *print_message_function( void *ptr );
main()
{
int gd=DETECT,gm=VGAMAX;
initgraph(&gd,&gm,NULL);
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
struct myran a,b,c,d;
a.x=10;
a.y=10;
b.x=20;
b.y=20;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) &a);
if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) &b);
if(iret2)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
exit(EXIT_FAILURE);
}
printf("pthread_create() for thread 1 returns: %d\n",iret1);
//printf("pthread_create() for thread 2 returns: %d\n",iret2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
while(!kbhit())
;
closegraph();
exit(EXIT_SUCCESS);
}
void *print_message_function( void *ptr )
{
struct myran *ss;
ss=(struct myran *)ptr;
int `enter code here`x1,y1,x,y;
x1=ss->x;
y1=ss->y;
int i=0;
for(i=0;i<5;i++)
printf("%d \n", x1);
}

Related

why the order of my output doesn't change even if i change my pthread_join() place

why do I get the same output order when pthread_join( thread3, NULL); comes before pthread_join( thread1, NULL);
and when:
pthread_join( thread1, NULL); comes before pthread_join( thread3, NULL);
I know that pthread_join() waits for the thread to terminate and it worked on thread 2 but why it doesn't work here like I waited for thread 3 to terminate but it still terminates after thread 1 terminate.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void * print_message_function( void *ptr );
void * do_something_else( );
void * a_new_function();
int main()
{
pthread_t thread1, thread2,thread3,thread4;
char *message1 = "Thread 1 is running ";
char *message2 = "Thread 2";
int iret1, iret2,iret3;
void * (*f_ptr)();
f_ptr = do_something_else;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, f_ptr/*do_something_else*/, NULL);
pthread_join( thread2, NULL);
iret3 = pthread_create( &thread3, NULL,a_new_function , NULL);
pthread_join( thread3, NULL); //--------------here-------------------------------
pthread_join( thread1, NULL); //---------------here-------------------------------
return 0;
}
void * print_message_function( void *ptr )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", (char *)ptr);
}
void * a_new_function(){
int a;
int b;
int c;
a=5;b=7;
c=b+a;
printf ("the value of c =%d\n",c);
return 0;
}
void * do_something_else( )
{
int i;
for(int i=0; i < 1; i++)
printf("%s \n", "this is do_something_else function");
}
the output in both ways is:
**this is do_something_else function
Thread 1 is running
the value of c =12**
Pthread_join does not affect the scheduling of the thread that you are joining with; really it is just a way to cleanup resources and retrieve exit status.
In your example, the only dependency is that thread 2 will complete before thread 3, since you do not create thread 3 until you have joined thread 2.
This has no effect on thread 1; and the order of the final joins is irrelevant.
"Waiting for X" is just waiting until X happens. "Waiting for Y" is just waiting until Y happens. Until either X or Y happens, there is no difference between waiting for X and waiting for Y. It's not clear why you think it makes any difference which thread you wait for. Waiting has no effect on the things that you are waiting for. It just waits for them to finish.

Segmentation Fault on multithreaded Producer Consumer C Program

// This is the multi-threaded code for a producer Consumer program ,it runs //successfully completion of both the threads , but receives a Segmentation error just //before thread join , I am not really able to figure it out
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
void* writer_function ();
void* reader_function ();
char buffer[9];
sem_t empty_buffers;
sem_t full_buffers;
int main()
{
pthread_t thread1, thread2;
//const char *message1 = "Thread 1";
//const char *message2 = "Thread 2";
int iret1, iret2;
sem_init(&empty_buffers,0,8);
sem_init(&full_buffers,0,0);
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL,writer_function, NULL);
if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}
iret2 = pthread_create( &thread2, NULL, reader_function(), NULL);
if(iret2)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
exit(EXIT_FAILURE);
}
// Runs Successfully ,and segmentation fault here
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("pthread_create() for thread 1 returns: %d\n",iret1);
printf("pthread_create() for thread 2 returns: %d\n",iret2);
sem_destroy(&empty_buffers);
sem_destroy(&full_buffers);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
printf("This marks the end and the threads to be joined \n ");
//exit(EXIT_SUCCESS);
return 0;
}
void* writer_function ()
{
int i ;
for (i = 0 ; i < 40 ; i++){
char c = (i + 66);
sem_wait(&empty_buffers);
buffer[i%8] = c;
sem_post(&full_buffers);
printf(" WRITER:the letter Written is %c\n", c);
}
}
void* reader_function ()
{
int i ;
for (i = 0 ; i < 40 ; i++){
sem_wait(&full_buffers);
char c = buffer[i%8];
sem_post(&empty_buffers);
printf("READER:the letter Received is %c\n", c);
}
}
change:
iret2 = pthread_create( &thread2, NULL, reader_function(), NULL);
to
iret2 = pthread_create( &thread2, NULL, reader_function, NULL);
add return NULL; for both functions reader_function, writer_function after loop and change their header from writer_/reader_function() to writer_/reader_function(void *args)

Threads in c, what am I missing here?

I'm trying to create a thread, and I cannot figure out what I am doing wrong here. It's very basic, I just want to make sure I can get the thread created before I delve into what I'll be doing in the thread. Here's my code.
//prog.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
int threadCount =0; //Global variable to hold our thread counter
//this is the function that gets called when a thread is created
void *threadCreate(void* arg){
printf("Thread #%d has been created\n", threadCount);
threadCount++;
int param = (int)arg;
printf("We were sent: %d\n", param);
printf("Now the thread will die\n");
threadCount--;
pthread_exit(NULL);
}
//main
int main(int argc, char *argv[]){
pthread_t tid;
int numski = 50;
int res;
res = pthread_create(&tid, NULL, threadCreate, (void*)numski);
if (res){
printf("Error: pthread_create returned %d\n", res);
exit(EXIT_FAILURE);
}
return 0;
}
I am compiling using the following command:
gcc -Wall -pthread -std=c99 prog.c -o Prog
And when I try to run it, I get no output at all.
Main is exiting right away, and therefore your process is dying right away. End it with pthread_join to wait for them. Here is one example I googled, which contains the following example code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}

Threads 'wait for parent' in C

Could you please show me the way how I can print the parent first using a mutex...Thanks
void *print_message_function( void *ptr );
main() {
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
pthread_join( thread1, NULL);
printf("(Parent)Thread 1 returns: %d\n",iret1);
exit(0);
}
void *print_message_function( void *ptr ) {
char *message;
message = (char *) ptr;
printf("(Child)%s \n", message);
}
By acquiring a lock before creating a thread and then immediately trying to acquire that lock in your new thread you can guarantee that some code after the thread is created will execute before the thread can possibly have acquired the lock. Of course it would be simpler just to run the code before creating the thread in the first place.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *print_message_function( void *ptr );
static pthread_mutex_t mut;
int main() {
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
pthread_mutex_init(&mut, NULL);
pthread_mutex_lock(&mut);
/* lock before creating the thread */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
printf("Parent first\n");
/* realasing the lock gives the child a chance to acquire it */
pthread_mutex_unlock(&mut);
pthread_join( thread1, NULL);
/* NOTE: iret1 is the result of the create call, not what the thread itself returned */
printf("(Parent)Thread 1 returns: %d\n",iret1);
return 0;
}
void *print_message_function( void *ptr ) {
/* parent already has this lock, so this will always block until they release it */
pthread_mutex_lock(&mut);
char *message;
message = (char *) ptr;
printf("(Child)%s \n", message);
pthread_mutex_unlock(&mut);
return NULL;
}
#include <pthread.h>
static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;
void *print_message_function( void *ptr );
main() {
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
pthread_mutex_lock( &cs_mutex );
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
printf("(Parent)Thread 1 returns: %d\n",iret1);
pthread_mutex_unlock( &cs_mutex );
exit(0);
}
void *print_message_function( void *ptr ) {
pthread_mutex_lock( &cs_mutex );
char *message;
message = (char *) ptr;
printf("(Child)%s \n", message);
pthread_mutex_unlock( &cs_mutex );
}

Creating Dynamic Threads in C

How do we create multiple thread during runtime in C programming language?
If we need to create same amount of thread as specified by the user how do we do it?
On Linux use pthreads
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
int main(int argc, char *argv[])
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
Check _beginthread for Windows and posix threads for linux
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
void *print()
{
printf("\nThread Created");
}
int main()
{
int ch,i;
pthread_t *t;
printf("Enter the number of threads you want to create : ");
scanf("%d",&ch);
t=(pthread_t *)malloc(ch * sizeof(pthread_t ));
for(i=0;i<ch;i++)
{
pthread_create(&t[i],NULL,print,NULL); //Default Attributes
}
for(i=0;i<ch;i++)
{
pthread_join(t[i],NULL);
}
}
This is the most basic code for creating threads during runtime in Linux OS using pthread library.
pthreads

Resources