Category: C++ Codes
Matrix Multiplication – Using Multicore Programming (C++)
/* Write a program for Matrix Multiplication using multicore programming. */ #include<iostream> #include<pthread.h> using namespace std; int MAT1[10][10],MAT2[10][10],MAT3[10][10]; int r1=3,c1=3,r2=3,c2=3; void *multi(void *p) { int i=0; i= (int &)p; for(int j=0;j<3;j++) { //MAT3[i][j]=0; for(int k=0;k<3;k++) { MAT3[i][j]=MAT3[i][j]+(MAT1[i][k]*MAT2[k][j]); } } } void read_mat(int MAT[10][10], int r, int c) { cout<<"Enter the elements in matrix...: "<<endl; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<"Enter the elements for row and column here: "<<endl; cin>>MAT[i][j]; } } } void print_mat(int MAT[10][10], int r, int c) { cout<<"\n"; for(int i=0;i<r;i++) { cout<<"\n"; for(int j=0;j<c;j++) { cout<<MAT[i][j]<<"\t"; } cout<<"\n"; } } main() { pthread_t tid[3]; read_mat(MAT1,r1,c1); read_mat(MAT2,r2,c2); ... Read morePassing command line arguement to thread functions (C++)
Write object oriented program using C++ for passing command line arguement to thread functions and using command line arguement to determine number of threads to be created. Use multicore programming.Multicore Programming – Producer Consumer problem solved by threads (C++)
#include <iostream> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> using namespace std; pthread_mutex_t status; sem_t empty,full; int buffer[50],x,item; void initialization() { sem_init(&empty,1,0); sem_init(&full,1,50); pthread_mutex_init(&status,NULL); x=0; } void* producer(void *para) { item=rand()%100; sem_wait(&full); //Producer waits if buffer is full pthread_mutex_lock(&status); //Lock the status, so that only one thread can access buffer at a time buffer[x]=item; //producer enters the no. in the buffer cout<<"\nProducer has producer : "<<buffer[x]; x++; pthread_mutex_unlock(&status); //Unlocks the buffer sem_post(&empty); } void* consumer(void *para) { sem_wait(&empty);//if buffer is empty,consumer waits pthread_mutex_lock(&status);//Lock the status, so that only one thread can access buffer at a time x--; item=buffer[x];//Consumer consumes an element ... Read moreMulticore Programming – using posix_spawn() to create processes (C++)
/* Using multicore programming implement posix_spawn() function to create a process. */ /*Execution: oct@CCOMPL08-10:~$ g++ templ.cpp -o mat.o oct@CCOMPL08-10:~$ g++ multicore.cpp -lpthread oct@CCOMPL08-10:~$ ./a.out */ #include<iostream> #include<spawn.h> #include<sys/types.h> #include<sys/wait.h> using namespace std; int main(int argc, char *argv[], char *env[]){ pid_t pid, pid1; int status, status1; status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid); status1 = posix_spawn(&pid1, "mat.o", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status1 == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid1); return 0; } View Article Page DownloadStandard Template Library – Lists (C++)
Google standard template library to use list container and using C++ implement following member functions of list class: empty, insert,merge,reverse,sortBubble Sort – using Function Template (C++)