Pthread with Semaphore <posix>

아래 Mutex로 동기화 했던걸 Semaphore로 변형 시킨거 위에껀 이름있는 세마포어??? 어래껀 익명 세미포어
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>
#include <string>
#include <pthread.h>
#include <fcntl.h> //for   O_CREAT
#include <semaphore.h>
 
using namespace std;
sem_t *mysem;
 
struct arg
{
    int start;
    int end;
    int time;
    string name;
};
 
 
void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;
 
    for(int i= temp->start; i < temp->end;i++)
    {
        sem_wait(mysem);
        cout << temp->name <<" : " << i << endl;
        sem_post(mysem);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;
 
    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;
 
    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
    sem_unlink("mysem"); //세마포어 삭제
    if((mysem = sem_open("mysem", O_CREAT, 0777, 1)) == NULL)
// /dev/shm에 해당이름의 세마포어 파일로 생성됨.
// O_CREAT : 파일이 존재하면 따로 생성하지 않음 , 0777 퍼미션
    {
        perror("Sem Open Error");
        return 1;
    }
    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);
 
    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);
 
    return 0;
}
</semaphore.h></fcntl.h></pthread.h></string></cstdlib></iostream>
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdlib>
#include <string>
#include <pthread.h>
#include <semaphore.h>
 
using namespace std;
sem_t mysem;
 
struct arg
{
    int start;
    int end;
    int time;
    string name;
};
 
 
void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;
 
    for(int i= temp->start; i < temp->end;i++)
    {
        sem_wait(&mysem);
        cout << temp->name <<" : " << i << endl;
        sem_post(&mysem);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;
 
    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;
 
    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
     
    if(sem_init(&mysem,0,1) ==-1)
    {
        cerr <<"Error";
        exit(0);
    }
 
 
    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);
 
    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);
 
    return 0;
}
</semaphore.h></pthread.h></string></cstdlib></iostream>

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Posix Thread 예제 소스  (0) 2010.03.15
자바 Tar 묶음 파일 만들기  (0) 2010.02.27
자바 - 정규표현식 예제입니다.<혼자보기용>  (0) 2010.02.27