NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
int num = 0;
#define COUNT_DONE 10

pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;


// print even numbers
void *printEvenNums()
{
for(;;) {
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
if ( num % 2 == 0 ) {
pthread_cond_wait( &condition_var, &count_mutex );
}
num++;
printf("%d ",num);
pthread_cond_signal( &condition_var );
if( num >= COUNT_DONE ) {
pthread_mutex_unlock( &count_mutex );
return(NULL);
}
pthread_mutex_unlock( &count_mutex );
}
}


// Print odd numbers
void *printOddNums()
{
for(;;) {
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
if ( num % 2 != 0 ) {
pthread_cond_wait( &condition_var, &count_mutex );
}
num++;
printf("%d ",num);
pthread_cond_signal( &condition_var );
if ( num >= COUNT_DONE ) {
pthread_mutex_unlock( &count_mutex );
return(NULL);
}
pthread_mutex_unlock( &count_mutex );
}
}


int main()
{
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, &printOddNums, NULL);
pthread_create( &thread2, NULL, &printEvenNums, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);

}







#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NTHREADS 4
#define ARRAYSIZE 100
#define ITERATIONS ARRAYSIZE / NTHREADS

double sum=0.0;
double a[ARRAYSIZE];
pthread_mutex_t sum_mutex;


void *do_work(void *tid)
{
int i, start, *mytid, end;
double mysum=0.0;

/* Initialize my part of the global array and keep local sum */
mytid = (int *) tid;
start = (*mytid * ITERATIONS);
end = start + ITERATIONS;
printf ("n n[Thread %5d] Doing iterations t%10d to t %10d",*mytid,start,end-1);
for (i=start; i < end ; i++) {
a[i] = i * 1.0;
mysum = mysum + a[i];
}

/* Lock the mutex and update the global sum, then exit */
pthread_mutex_lock (&sum_mutex);
sum = sum + mysum;
pthread_mutex_unlock (&sum_mutex);
pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
int i, start, tids[NTHREADS];
pthread_t threads[NTHREADS];
pthread_attr_t attr;

/* Pthreads setup: initialize mutex and explicitly create threads in a
joinable state (for portability). Pass each thread its loop offset */
pthread_mutex_init(&sum_mutex, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i=0; i<NTHREADS; i++) {
tids[i] = i;
pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
}

/* Wait for all threads to complete then print global sum */
for (i=0; i<NTHREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("n n[MAIN] Done. Sum= %e", sum);

sum=0.0;
/* for (i=0;i<ARRAYSIZE;i++){
a[i] = i*1.0;
sum = sum + a[i]; }
printf("n[MAIN] Check Sum= %e",sum);
*/
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&sum_mutex);
pthread_exit (NULL);
}








#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

// size of array
#define MAX 16

// maximum number of threads
#define MAX_THREAD 4


int a[] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 };
int sum[4] = { 0 };
int part = 0;

void* sum_array(void* arg)
{

// Each thread computes sum of 1/4th of array
int thread_part = part++;

for (int i = thread_part * (MAX / 4); i < (thread_part + 1) * (MAX / 4); i++)
sum[thread_part] += a[i];
}

// Driver Code
int main()
{

pthread_t threads[MAX_THREAD];

// Creating 4 threads
for (int i = 0; i < MAX_THREAD; i++)
pthread_create(&threads[i], NULL, sum_array, (void*)NULL);

// joining 4 threads i.e. waiting for all 4 threads to complete
for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);

// adding sum of all 4 parts
int total_sum = 0;
for (int i = 0; i < MAX_THREAD; i++)
total_sum += sum[i];

printf("n sum is %i ", total_sum) ;

return 0;
}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.