2016-09-18 84 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