카테고리 없음
[운영체제] 쓰레드 생성
달의요정루나
2022. 6. 27. 11:10
1. 두 개의 자식 쓰레드를 생성한 후, 메인 쓰레드는 'M', 첫 번째 자식 쓰레드는 'A', 두 번째 자식 쓰레드는 'B'를 각각 10,000번씩 출력하고 종료하는 프로그램을 작성하시오.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define MAX 10000
void* child1(void*tid){
printf("A");
}
void* child2(void*tid){
printf("B");
}
int main(){
pthread_t threads[MAX];
for(int i=1;i<=MAX;i++){
pthread_create(&threads[i],NULL,child1,NULL);
printf("M");
}
for(int i=1; i<=MAX; i++){
pthread_create(&threads[i],NULL,child2,NULL);
printf("M");
}
return 0;
}
2. 두 개의 자식 쓰레드를 생성한 후, 첫 번째 자식 쓰레드는 'A', 두 번째 자식 쓰레드는 'B'를 각각 10,000번씩 출력하고 종료하며, 메인 쓰레드는 두 자식 쓰레드가 모두 종료한 후 "My children have gone." 메시지를 출력하고 종료하는 프로그램을 작성하시오.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define MAX 10000
void* child1 (void*tid){
printf("A");
}
void* child2(void*tid){
printf("B");
}
int main(){
pthread_t threads[MAX];
for(int i=1;i<=MAX;i++){
pthread_create(&threads[i],NULL,child1,NULL);
printf("M");
}
for(int i=1; i<=MAX; i++){
pthread_create(&threads[i],NULL,child2,NULL);
printf("M");
}
for(int i = 1; i<MAX ;i++){
pthread_join(threads[i],NULL);
}
printf(“My children have gone\n”);
return 0;
}