Skip to content

Instantly share code, notes, and snippets.

@insipx
Created May 15, 2017 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insipx/e3b242fb5de13a2dc9e728134dc67ae8 to your computer and use it in GitHub Desktop.
Save insipx/e3b242fb5de13a2dc9e728134dc67ae8 to your computer and use it in GitHub Desktop.
//File name: a.c
/*
quick description:
-takes in input string from user
-passes the array of characters to 10 ten threads
-threads are responsible for 1/10th of the string and must increment ASCII table called count
-main prints the sum of each character
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define thread_count 10
#define ASCIIs 127 //ASCII characters from 0 to 127
#define atmost 1000
char letters[atmost+1]; //extra location for '\0'. atmost must be divisible by thread_count
int count[ASCIIs], h; //h holds the length of the segment
pthread_mutex_t mutex[ASCIIs];
typedef struct {
int* array;
} arg_struct;
void *task(void* rank);
int main(){
int extraspaces;
int goodletterlength;
int testletterlength;
pthread_t thread_handles[thread_count];
int i, thread;
for(i=0;i<ASCIIs;i++){
count[i] = 0;
}
printf("Enter a line not larger than 100 characters.\n");
gets(letters); //Read a line into array letters. This function adds '\0' to the end of the string
goodletterlength = strlen(letters);
if(strlen(letters) % thread_count != 0){
extraspaces = 0;
testletterlength = strlen(letters);
while (testletterlength % thread_count != 0) {
testletterlength++;
extraspaces++;
}
i=0;
while(i < extraspaces){
strcat(letters, " ");
i++;
}
// for(i =0 ;i < testletterlength;i++){
// printf("%c,",letters[i]);
// }
}
// for(i =0 ;i < testletterlength;i++){
// printf("%c,",letters[i]);
// }
/*
- Assign count[i]’s by 0
- prompt the user to enter a line.
- Read the line. The C function gets(letters) reads a line into the array: letters.
- Add spaces to the end of the line to make its length divisible by thread_count.
The C function: strlen(letters) returns the length of the string.
The C function: strcat(letters, “ “ adds a single space to the end of the string.
initialize mutex[i] for i=0,..., ASCIIs.
Create thread_count threads. Each of them calls the function: task. Note that change the value to different numbers like 2, 5, 10, 20,..., 50 to test your program.
Join the threads and destroy all the mutex[i].
print the array count like I did in the serial program.
*/
for( i =0 ; i < ASCIIs; i++){ //for each segment
pthread_mutex_init(&mutex[i], NULL);
}
for( i =0 ; i < thread_count; i++){ //10
// pthread_create(thread_handles, NULL, &task, &i);
pthread_create(&thread_handles[i], NULL, &task, (void *)&i);
//need to send rank,
}
for( i = 0; i < thread_count; i++){
pthread_join(thread_handles[i], NULL);
}
//need to destroy each mutex individually
for (i = 0; i < ASCIIs; i++) {
pthread_mutex_destroy(&mutex[i]);
}
//free(thread_handles); made it an array
for(i = 33; i < ASCIIs; i++){
if(count[i] != 0)
printf("Number of %c is: %d\n", i, count[i]);
}
return 0;
}
/*
pthread args:
thread -
attr - NULL
start routine - function to run
arg - passed by ref as pointer
*/
void *task(void* rank){
/*
Each thread executes this function for a segment of the line. It is important no two threads try
to change the content of count[index] at the same time. Note that it is ok if one thread changes
count[m] and another thread changes count[n], where the value of m and n are different.
*/
printf("%d\n", *(int*)(rank));
//long thisrank;
//thisrank = (long) rank;
//printf("%li,",thisrank);
// for(i = 0; i < strlen(letters); i++){
// index = (int)letters[i]; //references value in standard ASCII table
// (count[index])++;
// }
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment