2016-09-18 3 views
0

여기에 문제는 내가 우리의 93 넣어되고 싶어. 모든 스레드에 의해 변수를 공유하고 싶습니다. 정적 변수와 마찬가지로 모든 개체에 공통적인데 모든 스레드에 공통된 변수가 필요합니다.Mmap을 사용하여 메모리를 공유하는 방법. 제발 내 코드를 수정하십시오

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/mman.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <unistd.h> 

static int *glob_var; 

int main(void) 
    { 
    glob_var = (int*)mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE, 
       MAP_SHARED | MAP_ANONYMOUS, -1, 0); 

    *glob_var = 1; 
    int ppid =fork(); 

    if (ppid == 0) { 
    *glob_var = 92; printf("%d\n", *glob_var); 

    } else if(ppid!=0){ 
    (*glob_var)++; /////I want a 93 over here??? 
     printf("%d\n", *glob_var); /////I want a 93 over here??? print 
     munmap(glob_var, sizeof *glob_var); 
    } 
    return 0; 
    } 
+0

변수는 모든 스레드간에 공유됩니다. 그런 일을하기 위해 아무 것도 할 필요가 없습니다. – xaxxon

+0

그것이 스레드와 프로세스의 차이점입니다. 프로세스는 각각 고유의 메모리를 가지며 스레드는 모두 동일한 메모리에서 실행됩니다. 전역 변수를 사용하거나 스레드간에 포인터를 전달하십시오. – Barmar

답변

0

두 프로세스 모두 업데이트 glob_var입니다. 이 공유 메모리에 대한 액세스를 조정해야합니다. 데이터 수정의 올바른 순서, 즉 92의 값을 먼저 지정해야합니다.

Semaphore

는 종종 상황에서 작업을 동기화하는 데 사용됩니다 :

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/mman.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <sys/ipc.h> 
#include <sys/sem.h> 
#include <unistd.h> 

// Binary semaphore implementation. Initial state 0 
union semun 
{ 
    int val; 
    struct semid_ds *buf; 
    unsigned short int *array; 
    struct seminfo *__buf; 
}; 

int binary_semaphore_allocation (key_t key, int sem_flags) 
{ 
    return semget (key, 1, sem_flags); 
} 

int binary_semaphore_deallocate (int semid) 
{ 
    union semun ignored_argument; 
    return semctl (semid, 1, IPC_RMID, ignored_argument); 
} 

int binary_semaphore_initialize (int semid) 
{ 
    union semun argument; 
    unsigned short values[1]; 
    values[0] = 0; 
    argument.array = values; 
    return semctl (semid, 0, SETALL, argument); 
} 

int binary_semaphore_wait (int semid) 
{ 
    struct sembuf operations[1]; 
    operations[0].sem_num = 0; 
    /* Decrement by 1. */ 
    operations[0].sem_op = -1; 
    operations[0].sem_flg = SEM_UNDO; 
    return semop (semid, operations, 1); 
} 

int binary_semaphore_post (int semid) 
{ 
    struct sembuf operations[1]; 
    operations[0].sem_num = 0; 
    /* Increment by 1. */ 
    operations[0].sem_op = 1; 
    operations[0].sem_flg = SEM_UNDO; 
    return semop (semid, operations, 1); 
} 

int main(void) 
{ 
    key_t ipc_key; 
    ipc_key = ftok(".", 'S'); 
    int sem_id; 

    glob_var = (int*)mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE, 
       MAP_SHARED | MAP_ANONYMOUS, -1, 0); 

    *glob_var = 1; 

    if ((sem_id=binary_semaphore_allocation(ipc_key, IPC_CREAT|IPC_EXCL)) != -1) 
    { 
    if (binary_semaphore_initialize(sem_id) == -1) 
    { 
     printf("Semaphore initialization failed"); 
     return 2; 
    } 
    } 

    int ppid = fork(); 
    if (ppid == 0) 
    { 
    if ((sem_id = binary_semaphore_allocation(ipc_key, 0)) == -1) 
    { 
     printf("Child process failed to open semaphore"); 
     return 3; 
    } 
    } 
    else 
    { 
    // Wait in parent process until child update glob_var 
    binary_semaphore_wait(sem_id); 
    } 

    if (ppid == 0) 
    { 
    *glob_var = 92; 
    printf("%d\n", *glob_var); 
    binary_semaphore_post(sem_id); 
    } 
    else if(ppid!=0) 
    { 
    (*glob_var)++; 
    printf("%d\n", *glob_var); 
    munmap(glob_var, sizeof *glob_var); 
    binary_semaphore_deallocate(sem_id); 
    } 

    return 0; 
} 

출력 :

92 
93 
관련 문제