0% found this document useful (0 votes)
11 views2 pages

Ex - No 6 (Threading)

The document contains a C program that demonstrates the creation and management of threads using the pthread library. It creates two threads that execute a function, incrementing a counter and printing messages indicating the start and finish of each job. The program also includes error handling for thread creation and waits for both threads to complete before exiting.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Ex - No 6 (Threading)

The document contains a C program that demonstrates the creation and management of threads using the pthread library. It creates two threads that execute a function, incrementing a counter and printing messages indicating the start and finish of each job. The program also includes error handling for thread creation and waits for both threads to complete before exiting.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6.

Design, Develop and Implement Threading and synchronized applications


Program:

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

pthread_t tid[2];
int counter;

void* trythis(void* arg)


{
unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);

for (i = 0; i < (0xFFFFFFFF); i++)


;
printf("\n Job %d has finished\n", counter);

return NULL;
}

int main(void)
{
int i = 0;
int error;

while (i < 2) {
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}

pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);

return 0;
}
Output :
cc -lpthread file.c

Job 1 has started


Job 2 has started
Job 2 has finished
Job 2 has finished

You might also like