Академический Документы
Профессиональный Документы
Культура Документы
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
int main()
{
int res;
pthread_t a_thread;
int main() {
int res;
pthread_t a_thread;
void *thread_result;
if ( res != 0 ) {
perror("Thread Creation Failed");
exit(EXIT_FAILURE);
}
if ( res != 0 ) {
perror("Thread Join Failed");
exit(EXIT_FAILURE);
}
int main() {
int res;
pthread_t a_thread;
void *thread_result;
if ( res != 0) {
perror("Thread Creation Failed");
exit(EXIT_FAILURE);
}
sleep(3);
printf("Cancelling Thread.....\n");
res = pthread_cancel(a_thread);
if ( res !=0 ) {
perror("Thread Cancellation Failed");
exit(EXIT_FAILURE);
}
if (res !=0 ) {
perror("Thread Join Failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
if ( res !=0 ) {
perror("Thread pthread_setcancelstate Failed");
exit(EXIT_FAILURE);
}
if ( res !=0 ) {
perrror("Thread pthread_setcanceltype Failed");
exit(EXIT_FAILURE);
}
pthread_exit(0);
}
// Mutex
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
int main() {
int res;
pthread_t a_thread;
void *thread_result;
pthread_mutex_lock(&work_mutex);
printf(" Input Some Text. Enter 'end' To Finish \n ");
while(!time_to_exit) {
fgets(work_area, WORK_SIZE, stdin);
pthread_mutex_unlock(&work_mutex);
while(1) {
pthread_mutex_unlock(&work_mutex);
if(work_area[0] != '\0') {
pthread_mutex_unlock(&work_mutex);
sleep(1);
}
else {
break;
}
}
}
pthread_mutex_unlock(&work_mutex);
printf(" \n Waiting For Thread To Finish.... \n");
res = pthread_join(a_thread, &thread_result);
if ( res != 0) {
perror(" Thread Join Failed");
exit(EXIT_FAILURE);
}
printf(" Thread Joined \n");
pthread_mutex_destroy(&work_mutex);
exit(EXIT_SUCCESS);
}
time_to_exit = 1;
work_area[0] = '\0';
pthread_mutex_unlock(&work_mutex);
pthread_exit(0);
}
// Parallel Reduction
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main() {
int n, j, i;
int p, l, a[100];
l = log10(n);
for(i=0;i<=((n/2)-1);i++) {
for(j=0;j<l;j++) {
p = pow(2,j);
if (( i%p == 0) && (2i + p) < n)) {
a[2i] = a[2j] + a[2i+p];
}
}
}
}
// Prefix Sum
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main() {
int n, j, i;
int p, l, a[100];
l = log10(n);
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main() {
int parent[100], child[100], sibling[100];
int succ[100], pos[100], preorder[100];
int i, j, n, k, l;
l = log10(2(n-1));
for(i=0; i<n; i++){
for( j=0; j<n; j++) {
//Put the edges into a linked list
if ( parent[i] == j) {
if ( sibling[i] != null)
succ[(i,j)] = (j, parent[i]);
else if ( parent[j] != null)
succ[(i,j)] = (j, parent[j]);
else {
succ[(i,j)] = (i, j);
preorder[j] = 1;
}
}
else {
if ( child[i] != null )
succ[(i,j)] = (j, child[j]);
else
succ[(i,j)] = (i,j);
}
if ( parent[i] == j)
pos[(i,j)] = 0;
else
pos[(i,j)] = 1;
for( k=1; k<=l ; k++)
{
pos[(i,j)] = pos[(i,j)] + pos[succ[(i,j)];
succ[(i,j)] = succ[succ[(i,j)]];
}
if ( i == parent[j] )
preorder[j] = n + 1 - pos[(i,j)];
}
}
// Parallel Matrix Using Row/Column Oriented Algorithm
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main() {
int i, j, k, n, c, p;
int a[100], b[100], t;
int main() {
int a[100], left_child[100], right_child[100], parent[100];
int i, root;