Вы находитесь на странице: 1из 11

// Semaphore

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>

void *thread_function(void * arg);


sem_t bin_sem;

#define WORK_SIZE 1024


char work_area[WORK_SIZE];

int main()
{
int res;
pthread_t a_thread;

res = sem_init(&bin_sem, 0, 0);


if ( res != 0 ) {
perror(" Semaphore Initialization Failed ");
exit(EXIT_FAILURE);
}

res = pthread_create(&a_thread, NULL, thread_function, NULL);


if ( res !=0 ) {
perror(" Thread Creatio Failed ");
exit(EXIT_FAILURE);
}

printf(" \n Input Some Text. Enter 'end' to Finish \n");


while (( strncmp("end", work_area, 3) != 0)
{
fgets(work_area, WORK_SIZE, stdin);
sem_post(&bin_sem);
}

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");


sem_destroy(&bin_sem);
exit(EXIT_SUCCESS);
}

void *thread_function(void * arg) {


sem_wait(&bin_sem);
while(strncmp("end", workarea, 3) !=0 ) {
printf("You Input %d Charaters \n", strlen(wok_area)-1);
sem_wait(&bin_sem);
}
pthread_exit(NULL);
}
// Posix Thread Program
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>

void *thread_function(void *arg);

char message[] = "Hello World!";

int main() {
int res;
pthread_t a_thread;
void *thread_result;

res = pthread_create(&a_thread, NULL, thread_function, (void *)message);

if ( res != 0 ) {
perror("Thread Creation Failed");
exit(EXIT_FAILURE);
}

printf("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, It Returned %s\n", (char*)thread_result);


printf(" Message Is Now %s,\n",message);
exit(EXIT_FAILURE);
}

void *thread_function(void * arg) {


printf(" thread_function is running. Argument was %s \n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank You For The CPU Time");
}
// Thread Cancellation Program
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void *thread_function(&a_thread, NULL, thread_function, NULL);

int main() {
int res;
pthread_t a_thread;
void *thread_result;

res = pthread_create(&a_thread, NULL, thread_function, NULL);

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);
}

printf("Waiting For Thread To Finish.....\n");

res = pthread_join(a_thread, &thread_result);

if (res !=0 ) {
perror("Thread Join Failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

void *thread_functiion(void * arg) {


int i, res;

res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

if ( res !=0 ) {
perror("Thread pthread_setcancelstate Failed");
exit(EXIT_FAILURE);
}

res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);

if ( res !=0 ) {
perrror("Thread pthread_setcanceltype Failed");
exit(EXIT_FAILURE);
}

printf("Thread Function Is Running");


for(i = 0 ; i <10; i++) {
printf("Thread Is Still Running (%d)......\n", i);
sleep(1);
}

pthread_exit(0);
}
// Mutex

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>

void *thread_function(void *arg);


pthread_mutex_t work_mutex;

#define WORK_SIZE 1024


char work_area[WORK_SIZE];
int time_to_exit = 0;

int main() {
int res;
pthread_t a_thread;
void *thread_result;

res = pthread_mutex_init(&work_mutex, NULL);

if( res !=0 ) {


perror(" Mutex Initialization Failed ");
exit(EXIT_FAILURE);
}

res = pthread_create(&athread, NULL, thread_function, NULL);

if( res !=0 ) {


perror(" Thread Creation Failed ");
exit(EXIT_FAILURE);
}

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);
}

void *thread_function(void *arg) {


sleep(1);
pthread_mutex_lock(&work_mutex);

while(strncmp("end", workarea, 3) !=0 ) {


printf(" You input %d Characters\n", strlen(work_area)-1);
work_area[0] = '\0';
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
while ( work_area[0] == '\0' ) {
pthread_mutex_unlock(&work_mutex);
sleep(1);
pthread_mutex_lock(&work_mutex);
}
}

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];

printf(" Enter The Number:");


scanf("%d", &n);

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];

printf(" Enter The Number:");


scanf("%d", &n);

l = log10(n);

for(i=1; i<=(n-1); i++) {


for(j=0; j<(l-1); j++) {
p = pow(2,j);
if ( i - p) >= 0 ) {
a[i] = a[i] + a[i-p];
}
}
}
}
// Preorder Tree Traversal

#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;

printf("Enter The Number:");


scanf("%d", &n);

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;

printf("Enter The Number:");


scanf("%d", &n);

for( i=0; i<n; i++) {


for( j=i; j=i+n-1; j++) {
t = 0;
for( k=0; k<=(n-1); k++) {
t = t + a[k] * b[k];
}
c[ j%n] = t;
for( p=0; p<n; p++)
b[p] = successor(b[p]);
}}
// Parallel Quicksort
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
int a[100], left_child[100], right_child[100], parent[100];
int i, root;

for(i=0; i<100; i++) {


root = i;
parent[i] = root;
left_child[i] = right_child[i] = 100+1;
}

for(i=0; i<100; i++) {


while( i != root)
{
if(( a[i] < a[parent[i]] ) || (a[i] = a[parent[i]] && i<parent[i])) {
left_child[parent[i]] = i;
if ( i = left_child[parent[i]] ) {
break;
}
else {
parent[i] = left_child[parent[i]];
}
}
else {
right_child[parent[i]] = i;
if ( i = right_child[parent[i]] ) {
break;
}
else {
parent[i] = right_child[parent[i]];
}}}}}

Вам также может понравиться