summaryrefslogtreecommitdiff
path: root/lib/libc_r/TEST/test_sleep.c
blob: 47b476219f796e6773adb28c3e248844c8fecfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* ==== test_switch.c ============================================================
 * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
 *
 * Description : Test context switch functionality.
 *
 *  1.00 93/08/04 proven
 *      -Started coding this file.
 */

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

const char buf[] = "abcdefghijklimnopqrstuvwxyz";
int fd = 1;

void* new_thread(void* arg)
{
	int i;

	for (i = 0; i < 10; i++) {
		write(fd, buf + (long) arg, 1);
		sleep(1);
	}
	return NULL;
}

int
main()
{
	pthread_t thread[2];
	int count = sizeof thread/sizeof thread[0];
	long i;

	printf("Going to sleep\n");
	sleep(3);
	printf("Done sleeping\n");

	for(i = 0; i < count; i++) {
		if (pthread_create(&thread[i], NULL, new_thread, (void *) i)) {
			printf("error creating new thread %ld\n", i);
		}
	}

	for (i = 0; i < count; i++)
		pthread_join(thread[i], NULL);

	return(0);
}