Files
C-exp-collection/exp1/task61.c

53 lines
1.0 KiB
C
Raw Normal View History

2026-05-14 09:27:48 +08:00
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *wokerT1(void *vargp)
{
for(int i=0; i<5; i++)
{
printf("My name is Lvjinzhong\n");
int sleep_time = rand() % 5 + 1;
sleep(sleep_time);
}
return NULL;
}
void *wokerT2(void *vargp)
{
for(int i=0; i<5; i++)
{
printf("My student number is 2024414290124\n");
int sleep_time = rand() % 5 + 1;
sleep(sleep_time);
}
return NULL;
}
void *wokerT3(void *vargp)
{
for(int i=0; i<5; i++)
{
time_t t = time(NULL);
2026-05-16 21:52:40 +08:00
printf("Current time %s",ctime(&t));
2026-05-14 09:27:48 +08:00
int sleep_time = rand() % 5 + 1;
sleep(sleep_time);
}
return NULL;
}
int main()
{
pthread_t t1,t2,t3;
pthread_create(&t1, NULL, wokerT1, NULL);
pthread_create(&t2, NULL, wokerT2, NULL);
pthread_create(&t3, NULL, wokerT3, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
return 0;
}