Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*/
- |pthread example
- |Name : slmaan albeady
- |3.1 Programming with pthreads
- /*/
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define RED "\x1B[31m"
- #define CYN "\x1B[36m"
- #define RESET "\x1B[0m"
- void *thread_1(void * arg){
- sleep(5);
- printf(RED"Hello,I am thread 1\n"RESET);
- return NULL;
- }
- void *thread_2(void * arg){
- sleep(5);
- printf(RED"Hello,I am thread 2\n"RESET);
- return NULL;
- }
- int main(){
- pthread_t i1 , i2 ;
- pthread_create( &i1 , NULL, thread_1 , NULL);
- pthread_create( &i2 , NULL, thread_2 , NULL);
- pthread_join( i1 , NULL);
- pthread_join( i2 , NULL);
- printf(CYN"Hello,I am main process\n"RESET);
- }
- /*
- salbeady@penguin:~/os$ gcc -o test pthread_example.c -lpthread
- salbeady@penguin:~/os$ ./test
- Hello,I am thread 2
- Hello,I am thread 1
- Hello,I am main process
- salbeady@penguin:~/os$
- */
Advertisement
Add Comment
Please, Sign In to add comment