i'm writing a C program in unix that take a number upto 5 and creates that amount of threads..if the use doesn't enter anything the program quits after 5 secs...and this is where i'm stuck T_T ..how do i do this ? time.h ? code of wat i've done so far.

#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>



void main()
{
	int i;
	int j;
	printf("enter the amount of threads ! max 5 \n");
	scanf("%d",&i);
while (1)
	{
if(i>5)
	{
		printf("please enter a nunmber less than 5\n");
		scanf("%d",&i);
	}
else
	{
			break;
	}
	
}

}

Do something of this sort for inducing delay

1. Create a thread to run in the background. Let this thread call function f1() when it wakes up.


int flag = 0;

int main()
{
// Create a thread to run in the background. Let this thread call f1()

int numberOfThreads = -1;
printf("Enter the number of threads\n");
scanf("%d",&numberOfThreads);
int flag = 1;

return 0;
}

void f1()
{
// Write this code. In addition to this also check if flag is set to 1. If flag is set to 1 then break out of the while loop. If flag is not set to 1 till the loop ends then exit the main program
}

here is what i have done..and when i try to compile this in linux i get the following error

assignment_2_v1.c: In function ‘main’:
assignment_2_v1.c:42:13: warning: comparison between pointer and integer
assignment_2_v1.c: In function ‘f1’:
assignment_2_v1.c:60:4: error: stray ‘\302’ in program
assignment_2_v1.c:60:4: error: stray ‘\244’ in program
assignment_2_v1.c:60:4: warning: passing argument 1 of ‘time’ makes pointer from integer without a cast
/usr/include/time.h:186:15: note: expected ‘time_t *’ but argument is of type ‘long int’
assignment_2_v1.c:63:11: warning: ‘return’ with a value, in function returning void

#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <time.h>


void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{  int numberOfThreads = -1;
   printf("enter the amount of threads ! max 5 \n");
   scanf("%d",&numberOfThreads);
   while (1)
	{
if(numberOfThreads>5)
	{
		printf("please enter a nunmber less than 5\n");
		scanf("%d",&numberOfThreads);
	}
else if(numberOfThreads == -1)
	{
		f1();
	}
else 
	{
			break;
	}
	}
   pthread_t threads[numberOfThreads];
   int rc;
   long t;
   for(t=0; t<&numberOfThreads; t++)
   {
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc)
	  {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         
      }
   }
   void f1()
   {
		time_t start;
	    time_t current;

	       time(&start);
	
	do{
	     time(¤t);
	}while(difftime(current,start) < 5.0);
	printf("No input entered program will now quit.\n");
	return 0;
	
   }
		pthread_exit(NULL);
}

>>for(t=0; t<&numberOfThreads; t++)

That is attempting to compare integer t with a pointer to numberOfThreads. Why the pointer? Remove the & pointer operator and it will be ok.

line 60: void functions can not return a value.

lines 63 and 64 are not inside any function. Format your code better and you would easily see that.

>>for(t=0; t<&numberOfThreads; t++)

That is attempting to compare integer t with a pointer to numberOfThreads. Why the pointer? Remove the & pointer operator and it will be ok.

>.< freaking typo ...lol didn't see that there :D ..i'm still having problem getting the user input time limit >.< ..

line 60: void functions can not return a value.

i can't seem to fix it (sorry n00b here T_T )..

lines 63 and 64 are not inside any function. Format your code better and you would easily see that.

fixed it !.

this is what i get now when i try to compile it

assignment_2_v1.c: In function ‘f1’:
assignment_2_v1.c:60:4: error: stray ‘\302’ in program
assignment_2_v1.c:60:4: error: stray ‘\244’ in program
assignment_2_v1.c:60:4: warning: passing argument 1 of ‘time’ makes pointer from integer without a cast
/usr/include/time.h:186:15: note: expected ‘time_t *’ but argument is of type ‘long int’

This is your function f1

void f1()
{
// Your code

return 0; // Functions which have a return type of void cannot return a value
}

>>assignment_2_v1.c:60:4: error: stray ‘\302’ in program

I have seen something like that before -- its caused by some unprintable character on a line. The way I fixed it was to delete the line and retype it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.