Hi Guys,

I am working on developing a simulation program that tracks multiple vehicles entered by the user and copy the current location of that vehicle in the specified text file.

For example,
If the user inputs 3 then the program must generate 3 text files for each vehicle and then using the simulation program locations are saved in the text file.

The problem:
I created the simulation program for one vehicle that successfully saves the location of that vehicle but I want the program to simulate multiple vehicles simultaneously using threads.

So each vehicle is considered as a thread and it has it's own text file.

Hope it's clear:)

The code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>

int main()
{
	FILE *file;
	int v = 0;
	int i;
	int x, y;
	int mode ;
	srand ( time(NULL) );

	printf("===============================================\n");
	printf("====Simulation Program For Testing Tracking====\n");
	printf("===============================================\n");

	printf("Please enter the number of vehicles you want to track: \n");
	scanf("%d",&v);
	printf("The %d vehicle(s) are being simulated...\n",v);
	
	int index,select=0;
	printf("Please Choose Which vehicle to Track From the List Below:\n");
	for(index=1;index<=v;index++)
	{
		printf("%d. vehicle%d.\n", index,index);
	}
	printf("Enter the number of the vehicle only: ");
	scanf("%d",&select);

	int place=select;

	int trial=0;
	int Xcor[50];
	int Ycor[50];
	Xcor[0]=Ycor[0]=0;

	for(i=1; i<=50; i++)
	{
		if(i<=24){
			Xcor[i]=Xcor[i-1] + 2;
			Ycor[i]=Ycor[i-1] + 2;
		}
		else
		{
			Xcor[i]=Xcor[i-1] - 1;
			Ycor[i]=Ycor[i-1] - 1;
		}
	}
		
		pid_t pid;
		pid = fork();
		if(pid == -1) //Checking for error in forking "Spawn"
		printf("Bad Fork...Try Again\n");
	
		else if(pid == 0)
		{

		while (trial!=10)
	   	{
		 mode = rand() % 50;
		 x=Xcor[mode];
		 y=Ycor[mode];
		 file = fopen("1.txt","w");
		 
		 if (!file) {
    		 	printf("Error in opening file");
 		 }
		 fprintf(file,"XLocation=%d&YLocation=%d",x,y);
  		 printf("The Location of vehicle%d is: ( %d , %d )\n",place,x,y);
		 fclose(file);
		 sleep(1);
		 trial++;
	        }	
		}

	return 0;
}

Any help is appreciated.

Well, i have not used pthreads much.

pid = fork();

This does not create a thread. It creates a child process.

You maytake a look at This

Thanks myk45 for the reply

I know that I should create threads and join them but I don't know how to make it work I tried but It doesn't work.

Any help guys will be appreciated.

>>I tried but It doesn't work.

Could you paste the code here? It would be easier to help when we know where/what the error is.

Here is my try with threads so far...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>

int x[50];
int y[50];

void *run(void *i)
{
      int trial = (int)i;
      int mode;
      int Xcor[50];
      int Ycor[50];
      while (trial!=10)
      {
       mode = rand() % 50;
       x[trial]=Xcor[mode];
       y[trial]=Ycor[mode];
      }
      pthread_exit(NULL);
}

int main()
{
	FILE *file;
	int v = 0;
	int i;
	int x, y;
	int mode ;
	srand ( time(NULL) );

	printf("===============================================\n");
	printf("====Simulation Program For Testing Tracking====\n");
	printf("===============================================\n");

	printf("Please enter the number of vehicles you want to track: \n");
	scanf("%d",&v);
	printf("The %d vehicle(s) are being simulated...\n",v);
	
	pthread_t main[v];

	for(i = 0; i < v; i++)
      	{
            pthread_create(&main[i], NULL, run, (void *)i);
      	}

	
	int trial=0;
	int Xcor[50];
	int Ycor[50];
	Xcor[0]=Ycor[0]=0;

	for(i=1; i<=50; i++)
	{
		if(i<=24){
			Xcor[i]=Xcor[i-1] + 2;
			Ycor[i]=Ycor[i-1] + 2;
		}
		else
		{
			Xcor[i]=Xcor[i-1] - 1;
			Ycor[i]=Ycor[i-1] - 1;
		}
	}

	for(i=0; i < v; i++)
	{
		pthread_join(main[i], NULL);
	}

		int index;
		for(index=0; index<trial; index++)
	   	{
		 file = fopen("1.txt","w");
		 if (!file) {
    		 	printf("Error in opening file");
 		 }
		 fprintf(file,"XLocation=%d&YLocation=%d",x,y);
		 fclose(file);
		 sleep(1);
		 trial++;
	        }	
		
	pthread_exit(NULL);

}

It runs and I don't get any errors but the files are not produced.

Well, these are the changes to be made:

1) in run():

trial++;

needs to be added into the while loop.

2)

for(index=0; index<trial; index++)

Here, trial is always 0. Its value is not being changed. So, the loop was not entered.

Just a point...Is it a good idea to call your thread array main[];

Also..

int x[50];
int y[50];

void *run(void *i)
{
      int trial = (int)i;
      int mode;
      int Xcor[50];
      int Ycor[50];
      while (trial!=10)
      {
       mode = rand() % 50;
       x[trial]=Xcor[mode];
       y[trial]=Ycor[mode];
      }
      pthread_exit(NULL);
}

Your accessing the global resources

int x[50];
int y[50];

from more than one thread?

Hi Guys again and thanks for the replies,

after I made the the changes the text file is generated but it has very strange readings

[IMG]http://img146.imageshack.us/img146/450/81966933.png[/IMG]

And one more thing how I could let the user choose the number of vehicles to simulate.

For example if the user chose 3 vehicles how could I make the program generates 3 text files for each vehicle.

>>after I made the the changes the text file is generated but it has very strange readings

Could you paste the modified code here?

>>For example if the user chose 3 vehicles how could I make the program generates 3 text files for each vehicle.

Well, i suppose you could have an array of FILE pointers, and open new files when needed.

This was very helpfull to me, Thanks Everybody

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>

int x[50];
int y[50];

void *run(void *i)
{
      int trial = (int)i;
      int mode;
      int Xcor[50];
      int Ycor[50];
      while (trial!=10)
      {
       mode = rand() % 50;
       x[trial]=Xcor[mode];
       y[trial]=Ycor[mode];
       trial++;
      }
      pthread_exit(NULL);
}

int main()
{
	FILE *file;
	int v = 0;
	int i;
	int x, y;
	int mode ;
	srand ( time(NULL) );

	printf("===============================================\n");
	printf("====Simulation Program For Testing Tracking====\n");
	printf("===============================================\n");

	printf("Please enter the number of vehicles you want to track: \n");
	scanf("%d",&v);
	printf("The %d vehicle(s) are being simulated...\n",v);
	
	pthread_t main[v];

	for(i = 0; i < v; i++)
      	{
            pthread_create(&main[i], NULL, run, (void *)i);
      	}

	
	int trial=10;
	int Xcor[50];
	int Ycor[50];
	Xcor[0]=Ycor[0]=0;

	for(i=1; i<=50; i++)
	{
		if(i<=24){
			Xcor[i]=Xcor[i-1] + 2;
			Ycor[i]=Ycor[i-1] + 2;
		}
		else
		{
			Xcor[i]=Xcor[i-1] - 1;
			Ycor[i]=Ycor[i-1] - 1;
		}
	}

	for(i=0; i < v; i++)
	{
		pthread_join(main[i], NULL);
	}

		int index;
		for(index=0; index<trial; index++)
	   	{
		 file = fopen("1.txt","w");
		 if (!file) {
    		 	printf("Error in opening file");
 		 }
		 fprintf(file,"XLocation=%d&YLocation=%d",x,y);
		 fclose(file);
		 sleep(1);
		 trial++;
	        }	
		
	pthread_exit(NULL);

}

and could you please elaborate more on how to make multiple text files using threads.
Thanks myk45

1) Remove this(line 88). The loop never ends in your case.

trial++;

2) >>could you please elaborate more on how to make multiple text files using threads.

Well, you mean create multiple files right?

You can try something like this:

char *str[3] = {"1.txt", 
                "2.txt", 
                "3.txt"}; // give more names if needed 

FILE *fptr[10]; // array of FILE pointers.

int index; x = 0, y = 0;
for(i = 0; index < trial; i++) // here, make sure trial <= Maximum number of files you open.
{
     fptr[index] = fopen(str[i], "w");  // Or, prompt the user for filenames.
     if ( !fptr[index] ) {
    	   printf("Error in opening file"); // perror would be a better option.
     }
     fprintf(fptr[index], "XLocation = %d & YLocation = %d", x, y);
     fclose(fptr[index]);
}
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.