#include pthread.h
#include stdio.h
static int seq[50];
void *runner(void *); /* the thread */
int main(int argc, char *argv[])
{
int y;
pthread_t tid[3]; /* the thread identifier */
pthread_attr_t attr; /* set of attributes for the thread */
if(argc!=2)
{
fprintf(stderr,"you need to enter two arguments");
exit(-1);
}
else
{
int a = atoi(argv[1]);
if(a>0)
{
y=a;
}
else
{
fprintf(stderr,"you need to enter a valid number greater than zero");
exit(-1);
}
}
/* get the default attributes */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
/* create three threads */
pthread_create(&tid, NULL, runner,(void *)y);
/* now wait for the thread to exit */
sleep(2);
printf( " Am in main process, The sequence is ");
int j=0,val=0;
for( j = 0; j < 40; j++)
{
val=seq[j];
if(val>=1)
printf( " %d ", seq[j]);
}
pthread_join(tid, NULL);
pthread_exit(0);
}
/**
* The thread will begin control in this function
*/
void *runner(void *param)
{
int count=0;
int y= (int *) param;
// printf(" Am in runner");
while(y!=1)
{
if(y%2==0)
y = y/2;
else
y= ((3*y)+1);
// printf(" %d ", y);
seq[count] = y;
count++;
}
pthread_exit(0);
}
I am getting the following errors
bonus.c: In function ‘main’:
bonus.c:91: warning: cast to pointer from integer of different size
bonus.c:91: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
bonus.c:123: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast
bonus.c: In function ‘runner’:
bonus.c:157: warning: comparison between pointer and integer
bonus.c:159: error: invalid operands to binary %
bonus.c:161: error: invalid operands to binary /
bonus.c:165: error: invalid operands to binary *
bonus.c:171: warning: assignment makes integer from pointer without a cast
==>
and
==>