메시지큐

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h> 
#include <sys/msg.h> 
#include <sys/stat.h> 
 
struct msgbuf //muti
{
    // 이부분은 고정!!!!
    long msgtype;      
    // 아래 부분은 모두 변경 가능
    long long int total;
};
 
struct msgbuf1 //single
{
    // 이부분은 고정!!!!
    long msgtype;      
    // 아래 부분은 모두 변경 가능
    long long int total;
};
 
 
int main(int argc,char **argv)
{
 
    clock_t start, end;
    double result;
    start=clock();
 
 
    if(argc!=3){printf("error"); exit(1);}
    int num1,num2;
    num1=atoi(argv[1]);
    num2=atoi(argv[2]);
 
    key_t key_id;
    int i;
    struct msgbuf mybuf;
    pid_t pid;
    struct msgbuf1 mybuf1;
    if(num2==1)
    {
        mybuf1.msgtype=3;
    }
    else if(num2==2)
    {
        mybuf.msgtype=4;
    }
    else{
        printf("error");
        return -1;
    }
 
 
 
//    msgctl(131073, IPC_RMID, NULL);
 
 
    // 1. Message Queue 할당.
    key_id = msgget((key_t)1234, IPC_CREAT|0666);
    if (key_id == -1)
    {
        perror("msgget error : ");
        return 0;
    }
    // 2. 할당된 ID 확인
    printf("Key is %d\n", key_id);
 
    i = 0;
 
 
    memset(&mybuf, 0sizeof(struct msgbuf));
    memset(&mybuf1, 0sizeof(struct msgbuf1));
 
    int status;
    long long int c_num=0;
    if (num2==2) {
        // 3. 메시지를 전송한다. 
        printf("메시지 전송 ==4\n");
        mybuf.msgtype=4;
 
        pid=fork();
 
        if(pid>0){//부모
 
            printf("parent %d\n",num1);
            for(int i=0;i<num1/2;i++)
            {
                mybuf.total+=i;
            }
 
            printf("parent test\n");
 
            if (msgsnd( key_id, (void *)&mybuf, sizeof(struct msgbuf), IPC_NOWAIT) == -1)
            {
                perror("msgsnd error : ");
                return 0;
            }
        }
 
        else if(pid==0)//자식
        {
 
 
            printf("child %d\n",num1/2);
 
 
            for(int j=num1/2;j<=num1;j++)
            {
                c_num+=j;
            }
            if (msgrcv( key_id, (void *)&mybuf, sizeof(struct msgbuf), 4, MSG_NOERROR)==-1)
            {
                perror("msgrcv error : ");
                return 0;
            }
 
            mybuf.total+=c_num;
            printf("msgtype==4\n%lld\n",mybuf.total);
 
            end=clock();
            result=(double)(end-start);
            printf("time:%f\n",result/CLOCKS_PER_SEC);
 
            exit(1);
        }
 
    } else { // single
        printf("msg test else\n");
        mybuf1.msgtype = 3;
 
        printf("num1 == %d\n",num1);
        for(int i=0;i<=num1;i++)
        {
            mybuf1.total+=i;
        }
        // 3. 메시지를 전송한다. 
        if (msgsnd( key_id, (void *)&mybuf1, sizeof(struct msgbuf1), IPC_NOWAIT) == -1)
        {
            perror("msgsnd error : ");
            return 0;
        } 
    }
 
 
 
    if (num2 == 1) {
        printf("if test ==3\n");
 
        if (msgrcv(key_id, (void *)&mybuf1, sizeof(struct msgbuf1), 3, MSG_NOERROR)==-1)
        {
            perror("msgrcv error : ");
            return 0;
        }
        printf("msgtype==3\n%lld\n",mybuf1.total);
 
        end=clock();
        result=(double)(end-start);
        printf("time:%f\n",result/CLOCKS_PER_SEC);
    } 
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

 

공유메모리

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h> 
#include <sys/msg.h> 
#include <sys/stat.h> 
#include <sys/shm.h>
 
int main(int argc,char **argv)
{
 
    if(argc!=3){printf("error"); exit(1);}
    int num1,num2;
    num1=atoi(argv[1]);
    num2=atoi(argv[2]);
 
 
    clock_t start, end;
    double result;
    start=clock();
 
    long long int shmid;
    int pid;
    long long int *cal_num;
    void *shared_memory=(void*)0;
    shmid=shmget((key_t)1234,sizeof(long long int),0666|IPC_CREAT);
 
    int status;
 
    if(shmid==-1){
        perror("shmget failed:");
        return -1;
    }
 
    shared_memory=shmat(shmid,(void*)0,0);
    if(shared_memory==(void*)-1){
        perror("shmat failed:");
        return -1;
    }
 
    cal_num=(long long int *)shared_memory;
 
    if(num2==1)
    {
 
        long long int sig_total=0;
        for(int i=0;i<=num1;i++)
        {
            sig_total+=i;
        }
 
        printf("%lld\n",sig_total);
 
        end=clock();
        result=(double)(end-start);
        printf("time:%f\n",result/CLOCKS_PER_SEC);
 
    }
    else if(num2==2)
    {
        pid=fork();
 
        if(pid==0){
            shmid=shmget((key_t)1234,sizeof(long long int),0);
            if(shmid==-1){
                perror("shmget failed:");
                return -1;
            }
 
            shared_memory=shmat(shmid,(void*)0,0666|IPC_CREAT);
            if(shared_memory==(void*)-1){
                perror("shmat failed:");
                return -1;
            }
            cal_num=(long long int *)shared_memory;
            *cal_num=0;
            for(int i=0;i<num1/2;i++)
            {
                *cal_num+=i;
            }
 
 
 
        }
        else if(pid>0){
            wait(&status);
            for(int j=num1/2;j<=num1;j++)
            {
                *cal_num+=j;
            }
 
            printf("%lld\n",*cal_num);
 
 
            end=clock();
            result=(double)(end-start);
            printf("time:%f\n",result/CLOCKS_PER_SEC);
 
        }
    }
    else{
        printf("error");
        return -1;
    }
 
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

+ Recent posts