51 lines
1.0 KiB
C
51 lines
1.0 KiB
C
|
|
#include <pthread.h>
|
||
|
|
#include <time.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
void *workerT1(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 *workerT2(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 *workerT3(void *vargp)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < 5; i++) {
|
||
|
|
time_t t = time(NULL);
|
||
|
|
printf("Current time %s", ctime(&t));
|
||
|
|
int sleep_time = rand() % 5 + 1;
|
||
|
|
sleep(sleep_time);
|
||
|
|
}
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main()
|
||
|
|
{
|
||
|
|
srand(time(NULL));
|
||
|
|
pthread_t t1, t2, t3;
|
||
|
|
pthread_create(&t1, NULL, workerT1, NULL);
|
||
|
|
pthread_create(&t2, NULL, workerT2, NULL);
|
||
|
|
pthread_create(&t3, NULL, workerT3, NULL);
|
||
|
|
|
||
|
|
pthread_join(t1, NULL);
|
||
|
|
pthread_join(t2, NULL);
|
||
|
|
pthread_join(t3, NULL);
|
||
|
|
return 0;
|
||
|
|
}
|