Operating System/System Programming(Arif Butt)

Lec23 Multi Threaded Programming

Tony Lim 2021. 7. 15. 22:39

Temporal Multi-threading  = Only one thread of instruction can execute in any given pipeline stage at a time

Simultaneous Multi-threading (SMT/HT)  =  More than one thread of insturction can execute in any given pipeline stage at a time (super scalar architecture)

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h> 
struct mystruct{
   char character;
   int count; 
};
void * f1(void *);

int main(){
   pthread_t tid1, tid2;
   struct mystruct t1_args;
   struct mystruct t2_args;
//create a child thread to print 1000 Xs
   t1_args.character = 'X';
   t1_args.count = 1000;
   pthread_create(&tid1, NULL, f1, (void*)&t1_args); 
//create a child thread to print 800 Os
   t2_args.character = 'O';
   t2_args.count = 800;
   pthread_create(&tid2, NULL, f1, (void*)&t2_args);
//wait for the child threads to terminate
   pthread_join(tid1, NULL);
   pthread_join(tid2, NULL);

   printf("\nBye Bye from main thread.\n");
   return 0;
}

void * f1(void * args){
   struct mystruct p = *(struct mystruct*)args;
   for (int i = 0; i < p.count; i++)
      putc(p.character,stdout);
   pthread_exit(NULL);
}

need to link with pthread library when compiling  "gcc ~ -lpthread"

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void* f1(void * arg);
int main(int argc, char* argv[]){
   if(argc != 3){
      printf("Invalid number of arguments. Must pass two file names.\n");
      exit(1);
   }
   pthread_t tid1, tid2;
   void *rv1, *rv2;
   pthread_create(&tid1, NULL, f1, (void*)argv[1]);
   pthread_create(&tid2, NULL, f1, (void*)argv[2]);
   pthread_join(tid1, &rv1);
   pthread_join(tid2, &rv2);
   int count1 = *((int*)rv1);
   int count2 = *((int*)rv2);
   printf("Characters in %s: %d\n", argv[1], count1);
   printf("Characters in %s: %d\n", argv[2], count2);
   return 0;
}
void* f1(void* args){
   char* filename = (char*)args;
   int *result = (int*)malloc(sizeof(int));
   *result = 0;
   char ch;
   int fd = open(filename, O_RDONLY);
   while((read(fd, &ch, 1)) != 0){ 
	   (*result)++;
   }
   close(fd);
   pthread_exit((void*)result);
}

f1 function return result and inside pthread_join function , second argument will receive the output.

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>  //exit
#include <math.h>
#include <unistd.h>

// Value depend on System core 
#define CORE 4 
#define MAX_THREAD 4 
// Maximum matrix size 
#define MAX 4 
int thread_no = 0;
// Maximum threads is equal to total core of system 
pthread_t thread[MAX_THREAD]; 
int mat_A[MAX][MAX], mat_B[MAX][MAX], sum[MAX][MAX]; 

// Addition of a Matrix 
void* addition(void* arg) { 
	int i, j, num = thread_no++;
	//int core = *((int*)arg); 
	// Each thread computes 1/4th of matrix addition 
	for (i = num * MAX / 4; i < (num + 1) * MAX / 4; i++) { 
		for (j = 0; j < MAX; j++) { 
			// Compute Sum Row wise 
			sum[i][j] = mat_A[i][j] + mat_B[i][j]; 
		} 
	} 
} 


// Driver Code 
int main() 
{ 

	int i, j, step = 0; 
	// Generating random values in mat_A and mat_B 
	for (i = 0; i < MAX; i++) { 

		for (j = 0; j < MAX; j++) { 

			mat_A[i][j] = rand() % 10; 
			mat_B[i][j] = rand() % 10; 

		} 

	} 


	// Displaying mat_A 
	fprintf(stderr,"\nMatrix A:\n"); 
	for (i = 0; i < MAX; i++) { 
		for (j = 0; j < MAX; j++) { 
			fprintf(stderr,"%d ", mat_A[i][j]); 
		} 
		fprintf(stderr,"\n"); 
	} 

	// Displaying mat_B 
	fprintf(stderr,"\nMatrix B:\n"); 
	for (i = 0; i < MAX; i++) { 
		for (j = 0; j < MAX; j++) { 
			fprintf(stderr,"%d ", mat_B[i][j]); 
		} 
		fprintf(stderr,"\n"); 
	} 

	// Creating threads equal 
	// to core size and compute matrix row 
	for (i = 0; i < MAX_THREAD; i++) { 
		pthread_create(&thread[i], NULL, &addition, NULL); 
	//	pthread_create(&thread[i + CORE], NULL, &subtraction, (void*)step); 
		step++; 
	} 

	// Waiting for join threads after compute 
	for (i = 0; i < MAX_THREAD; i++) { 
		pthread_join(thread[i], NULL); 
	} 

	// Display Addition of mat_A and mat_B 
	printf("\n Sum of Matrix A and B:\n"); 
	for (i = 0; i < MAX; i++) { 
		for (j = 0; j < MAX; j++) { 
			fprintf(stderr,"%d ", sum[i][j]); 
		} 
		fprintf(stderr,"\n"); 
	} 
	return 0; 
}