Skip to content

Commit

Permalink
adding source files
Browse files Browse the repository at this point in the history
  • Loading branch information
manasesjesus committed Oct 10, 2017
1 parent 99912f1 commit da854dd
Show file tree
Hide file tree
Showing 4 changed files with 767 additions and 0 deletions.
150 changes: 150 additions & 0 deletions holamigos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* An “advanced” implementation of the classic “Hello, World” program
* using the main Pthreads functions to create, identify and synchronize
* the threads.
*
* It creates four threads by default, and optionally the number of
* threads to be created can be passed as a command-line parameter when
* executing the program.
*
* Compilation
* gcc -Wall -lpthread -o holamigos holamigos.c
*
* Execution
* ./holamigos [number_of_threads]
*
*
* File: holamigos.c Author: Manases Galindo
* Date: 09.02.2017
*/



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 4 /* default number of threads */

/* Shared global variables. All threads can access them. */
int nthreads; /* number of threads */


void *say_hello (void *);


int main (int argc, char **argv)
{
int i, /* loop variable */
*thr_num; /* array of thread numbers */
pthread_t *thr_ids; /* array of thread ids */

switch (argc) /* check command line arguments */
{
case 1:
/* Number of threads not specified, using the default value */
nthreads = NUM_THREADS;
break;

case 2:
/* Using the specified number of threads */
nthreads = atoi (argv[1]);
if (nthreads < 1)
{
fprintf (stderr, "Error: wrong number of threads.\n"
"Usage:\n"
" %s [number_of_threads]\n"
"number_of_threads should be > 0\n"
"Using default number of threads (%d).\n",
argv[0], NUM_THREADS);
nthreads = NUM_THREADS;
}
break;

default:
fprintf (stderr, "Error: wrong number of parameters.\n"
"Usage:\n"
" %s [number_of_threads]\n",
argv[0]);
exit (EXIT_FAILURE);
}

/* allocate memory for all dynamic data structures */
thr_ids = (pthread_t *) malloc (nthreads * sizeof (pthread_t));
thr_num = (int *) malloc (nthreads * sizeof (int));

/* Validate that memory was successfully allocated */
if ((thr_num == NULL) || (thr_ids == NULL))
{
fprintf (stderr, "File: %s, line %d: Can't allocate memory.",
__FILE__, __LINE__);
exit (EXIT_FAILURE);
}

printf ("\nHola amigos! I'm the main thread\n");
for (i = 0; i < nthreads; i++) /* Create the threads */
{
thr_num[i] = i + 1; /* Thread number */
pthread_create (&thr_ids[i], NULL, say_hello, &thr_num[i]);
}

/* Using join to syncronize the threads */
for (i = 0; i < nthreads; i++)
{
pthread_join (thr_ids[i], NULL);
}

/* Deallocate any memory or resources associated */
free (thr_num);
free (thr_ids);

return EXIT_SUCCESS;
}


/* say_hello runs as a thread and will print a random hello message
* to stdout (e.g. the screen)
*
* Input: arg pointer to current thread number
* Return value: none
*
*/
void *say_hello (void *arg)
{
int thr_index,
rdm_message;

/* Get the index number of current thread */
thr_index = *( ( int* )arg );

/* Select a random message to be displayed by the thread */
srand (time(NULL));
rdm_message = rand() % 4;
switch (rdm_message)
{
case 0:
printf ("Hola amigos! I'm thread %d of %d. My ID is %d \n",
thr_index, nthreads, (int) pthread_self());
break;

case 1:
printf ("Aloha honua! I'm thread %d of %d. My ID is %d \n",
thr_index, nthreads, (int) pthread_self());
break;

case 2:
printf ("Hello peers! I'm thread %d of %d. My ID is %d \n",
thr_index, nthreads, (int) pthread_self());
break;

case 3:
printf ("Hallo Leute! I'm thread %d of %d. My ID is %d \n",
thr_index, nthreads, (int) pthread_self());
break;
default:
/* Default case, for the compiler not to complain */
printf ("Konichiwa! I have no father. I was created by The Force!");
break;
}

pthread_exit (EXIT_SUCCESS); /* Terminate the thread */
}
133 changes: 133 additions & 0 deletions pythagoras.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* The Pythagoras' theorem states the relation among the three sides of
* a right-angled triangle, where the sum of the areas of the two
* squares on the sides (a, b) equals the area of the square on the
* hypotenuse (c), and it can be represented with the equation
* a^2 + b^2 = c^2
*
* This program uses two threads; each one to calculate the areas of the
* two squares on the sides. Initially, the hypotenuse value is set to
* zero. When one thread has made its calculation, it sums it to the
* hypotenuse, therefore it has to be treated as a critical section.
* It uses mutex to protect the shared data. To compile it may be
* necessary to add the option -lm to link the math.h library.
*
* Compilation
* gcc -lm -Wall -lpthread -o pythagoras pythagoras.c
*
* Execution
* ./pythagoras <side_a> <side_b>
*
*
* File: pythagoras.c Author: Manases Galindo
* Date: 09.02.2017
*/



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>

#define NUM_THREADS 2 /* default number of threads */

/* Shared global variables. All threads can access them. */
float hypotenuse;
pthread_mutex_t mutexsum;


void *square_side (void *);


int main (int argc, char **argv)
{
int i; /* loop variable */
float sides[2]; /* right-angled triangle sides */
pthread_t *thr_ids; /* array of thread ids */

switch (argc) /* check command line arguments */
{
case 3:
/* Get the values of the right-angled triangle sides */
sides[0] = atof (argv[1]);
sides[1] = atof (argv[2]);
if ((sides[0] < 1) || (sides[1] < 1))
{
fprintf (stderr, "Error: wrong values for triangle sides.\n"
"Usage:\n"
" %s <side_a> <side_b>\n"
"values of sizes should be > 0\n",
argv[0]);
exit (EXIT_FAILURE);
}
break;

default:
fprintf (stderr, "Error: wrong number of parameters.\n"
"Usage:\n"
" %s <side_a> <side_b>\n",
argv[0]);
exit (EXIT_FAILURE);
}

/* allocate memory for all dynamic data structures */
thr_ids = (pthread_t *) malloc (NUM_THREADS * sizeof (pthread_t));

/* Validate that memory was successfully allocated */
if (thr_ids == NULL)
{
fprintf (stderr, "File: %s, line %d: Can't allocate memory.",
__FILE__, __LINE__);
exit (EXIT_FAILURE);
}

printf ("\nPythagoras' theorem | a^2 + b^2 = c^2 \n");
hypotenuse = 0;

/* Initialize the mutex to protect share data (hypotenuse) */
pthread_mutex_init (&mutexsum, NULL);

/* Create the threads and calculate the squares on the sides */
pthread_create (&thr_ids[0], NULL, square_side, &sides[0]);
pthread_create (&thr_ids[1], NULL, square_side, &sides[1]);

/* Using join to syncronize the threads */
for (i = 0; i < NUM_THREADS; i++)
{
pthread_join (thr_ids[i], NULL);
}

printf ("Hypotenuse is %.2f\n", sqrt(hypotenuse));

/* Deallocate any memory or resources associated */
pthread_mutex_destroy (&mutexsum);
free (thr_ids);

return EXIT_SUCCESS;
}


/* square_side runs as a thread and calculates the areas of the
* square on the side, then sums the value to the hypotenuse.
* It uses a mutex to protect the hypotenuse and avoid a race
* conditiong within the threads.
*
* Input: arg pointer to triangle side value
* Return value: none
*
*/
void *square_side (void *arg)
{
float side;

/* Get the value of the triangle side and print the square */
side = *( ( float* )arg );
printf ("%.2f^2 = %.2f\n", side, side * side);

/* Mutex lock/unlock to safely update the value of hypotenuse */
pthread_mutex_lock (&mutexsum);
hypotenuse += side * side;
pthread_mutex_unlock (&mutexsum);

pthread_exit (EXIT_SUCCESS); /* Terminate the thread */
}
Loading

0 comments on commit da854dd

Please sign in to comment.