Louie NRT Story

[RaspberryPi] Chatting Program using Shared Memory Flag 본문

에너지

[RaspberryPi] Chatting Program using Shared Memory Flag

hyeok0724.kim@gmail.com 2019. 5. 6. 21:26
반응형

[EcoServerBySharedMemory.c]

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
    
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
 
#define SHARED_MEMORY_KEY 1111
#define MEMORY_SIZE 1024
 
#define READ_CLIENT_FLAG 0
#define READ_SERVER_FLAG 1
#define PRINT_CLIENT_FLAG 2
 
int main()
{
    int shmid;
    char *buffer;
    char *string;
    
    //make space that shared-memory
    shmid = shmget((key_t)SHARED_MEMORY_KEY, MEMORY_SIZE, 0666|IPC_CREAT);
    
    if(shmid == -1)
    {
        perror("shmget failed: ");
        exit(0);
    }
    
    //attach shared memory
    buffer = shmat(shmid, (void *)00);
    
    if(buffer == (void *)-1)
    {
        perror("shmat failed:");
        exit(0);
    }
    string = buffer + 1;
    
    buffer[0= READ_CLIENT_FLAG;
    
    while(1)
    {
        if(buffer[0== READ_SERVER_FLAG)
        {
            puts(string);
            strcat(string"by server");
            buffer[0= PRINT_CLIENT_FLAG;
        }
        sleep(1);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

[EcoClientBySharedMemory.c]

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
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
 
#define SHARED_MEMORY_KEY 1111
#define MEMORY_SIZE 1024
 
#define READ_CLIENT_FLAG 0
#define READ_SERVER_FLAG 1
#define PRINT_CLIENT_FLAG 2
 
int main()
{
    int shmid;
    char *buffer;
    char *string;
    
    //make space that shared-memory
    shmid = shmget((key_t)SHARED_MEMORY_KEY, MEMORY_SIZE, 0666 | IPC_CREAT);
    if(shmid == -1)
    {
        perror("shmget failed: ");
        exit(0);
    }
    
    //attach shared memory
    buffer = shmat(shmid, NULL0);
    if(buffer == (void *)-1)
    {
        perror("shmat failed: ");
        exit(0);
    }
    
    buffer[0= READ_CLIENT_FLAG;
    string = buffer + 1;
    
    while(1)
    {
        if(buffer[0== READ_CLIENT_FLAG)
        {
            printf("message : ");
            scanf("%s"string);
            
            buffer[0= READ_SERVER_FLAG;
        }
        else if(buffer[0== PRINT_CLIENT_FLAG)
        {
            puts(string);
            buffer[0= READ_CLIENT_FLAG;
        }
        sleep(1);
    }
}    
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs
반응형
Comments