
24/04/2005, 08:48
|
| | Fecha de Ingreso: diciembre-2003
Mensajes: 190
Antigüedad: 21 años, 3 meses Puntos: 0 | |
alguien sabe q hace esto? alguien sabe que hace este programa?
me estoy volviendo loco
#include <stdio.h>
#include <stdlib.h> // for rand()
#include <sys/types.h>
#include <unistd.h> // for getpid() & usleep()
#include <pthread.h>
#define MAX_THREADS 1000
char buffer[64];
void *collision(void *arg)
{
int i;
int id = *(int *)arg;
for (i = 0; i < sizeof(buffer) -1; i++)
{
buffer[i] = 'A' + id % ('Z' - 'A');
usleep(rand() % 9);
}
// printf("Hello world from node %d (thread id = 0x%x)\n", id, pthread_self());
return NULL;
}
int main(int argc, char* argv[])
{
int n, i;
pthread_t *threads;
int *ids;
if (argc != 2)
{
printf ("Usage: %s n\n where n is no. of threads\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
if ((n < 1) || (n > MAX_THREADS))
{
printf ("The no of thread should be between 1 and %d.\n", MAX_THREADS);
return 1;
}
threads = (pthread_t *)malloc(n * sizeof(*threads));
ids = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) // Start up threads
{
ids[i] = i;
pthread_create(&threads[i], NULL, collision, (void *)(ids + i));
}
for (i = 0; i < n; i++) // Synchronize the completion of each thread.
pthread_join(threads[i], NULL);
free(ids);
printf ("buffer = %s\n", buffer);
return 0;
}
muchas gracias |