Sample C routine with POSIX functions
This C routine
creates multiple threads by using POSIX functions.
#pragma longname
#define _POSIX_SOURCE
#define _OPEN_THREADS
#pragma runopts (POSIX(ON))
#include <leawi.h>
#include <types.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
void * CEPSXT1(void *);
main()
{
pthread_t CEPSXT1_pid[3];
int status[2], i, j[2], rc, count=0;
fprintf(stderr,"\n Creating two threads................\n");
fflush(stderr);
for(i=0; i<2; i++)
{
j[i] = i+1;
rc = pthread_create(&CEPSXT1_pid[i], NULL, &CEPSXT1, &j[i])
if (rc)
{
fprintf(stderr, "Thread creation unsuccessful;Error: %d",errno);
fprintf(stderr, "pthread_create() returns %d ",rc);
fflush(stderr);
exit(-1);
}
else
fprintf(stderr,"Thread %d created\n",j[i]);
}
for(i=0; i<2; i++)
{
j[i] = i+1;
if (!(rc = pthread_join(CEPSXT1_pid[i],(void*) &status[i])))
{
if (status[i] == 1)
count++;
}
else
{
fprintf(stderr,"pthread_join failed for thread %d\n",j[i]);
fflush(stderr);
exit(-1);
}
} if (count == 2)
fprintf(stderr,"\n***** SUCCESS *****\n");
else
fprintf(stderr,"\n***** ERROR *****\n");
fflush(stderr);
pthread_exit(0);
}
void * CEPSXT1(void *arg)
{
int status=0, success=0;
div_t ans;
char path = '/';
int i, rc;
i = *((int *)arg);
fprintf(stderr,"\n Call POSIX access() function in Thread %d",i);
fflush(stderr);
if (access(path, F_OK) == 0)
fprintf(stderr,"\nPOSIX access() function succeeds in Thread %d\n",i);
else
fprintf(stderr, "Error generated by call to access() is %d", errno);
fflush(stderr);
status=1;
fprintf(stderr,"***** Thread %d completed *****\n", i);
fflush(stderr);
pthread_exit( (void*) status);
}