Pages

Tuesday, 5 February 2019

POSIX thread (pthread) System call program - ubuntu

The POSIX(Portable Operating system Interface) function is  to create a new thread within the same process used header file is
#include <pthread.h>

Program :
//POSIX Portable Operating System Interface called as pthread
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>

void *thread(void *arg);
int i,sum;

void main()
{
pthread_t tid;
printf("\n sum=%d",sum);
pthread_create(&tid,NULL,thread,NULL);
pthread_join(tid,NULL);
printf("\n sum=%d\n",sum);
}

void *thread(void *arg)
{
for(i=0;i<3;i++)
sum=sum+i;
pthread_exit(0);
}
 
Output: using this command we can execute the pthread system call programs
gcc pthread.c -o thread -lpthread
    

  • gcc is the compiler command.
  • thread.c is the name of c program source file.
  • -o is option to make object file.
  • thread is the name of object file.
  • -lpthread is option to execute pthread.h library file.
 
 

No comments:

Post a Comment