Linux共享記憶體示例,使用訊號量同步

2021-07-08 09:21:48 字數 4894 閱讀 9598

1、shm_test1.c

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define sem_key 100

#define shm_key 101

#define total_sem 10

#define unix_domain "/tmp/unix_sock"

typedef

struct shared_use shared_use_t;

static

unsigned

char ucast_pkt = ;

static

int sem_id = 0;

static

int com_fd = 0;

static

void *shm = null;

/* 獲取訊號量資源 */

static

int semaphore_v(void)

return0;}

/* 釋放訊號量資源 */

static

int semaphore_p(void)

return0;}

/* 初始化訊號量 */

static

int semaphore_init(void)

sem_union.val = 1;

if (semctl(sem_id, 0, setval, sem_union) == -1)

return0;}

/* 初始化共享記憶體 */

static

int sharemmy_init(void)

/* 將本程序的虛擬記憶體對映到共享記憶體 */

shm = shmat(shmid, (void *)0, 0);

if (shm == (void *)-1)

return shmid;

}static

int sharemmy_destroy(int shmid, void *shm)

/* 刪除建立的共享記憶體塊 */

rv = shmctl(shmid, ipc_rmid, 0);

if (rv == -1)

return0;}

static

int domain_server(void)

memset(&srv_un, 0, sizeof(srv_un));

srv_un.sun_family = af_unix;

strncpy(srv_un.sun_path, unix_domain, sizeof(srv_un.sun_path) - 1);

unlink(unix_domain);

rv = bind(listen_fd, (struct sockaddr *)&srv_un, sizeof(srv_un));

if (rv < 0)

rv = listen(listen_fd, 1);

if (rv < 0)

len = sizeof(clt_un);

while (1)

break;

}return0;}

static

void print_cmd_help(void)

static

int shmwrite(void *shm)

shared = (shared_use_t *)shm;

shared->pktlen = sizeof(ucast_pkt);

memcpy(shared->pkt, ucast_pkt, sizeof(ucast_pkt));

rv = semaphore_v();

if (rv < 0)

return0;}

int main()

shmid = sharemmy_init();

if (shmid == -1)

rv = domain_server();

if (rv < 0)

printf("server init success!\n");

printf("\n");

while (1) else

if (strstr(cmd, "send") != null || strstr(cmd, "send") != null)

rv = send(com_fd, cmd, sizeof(cmd), 0);

if (rv < 0)

} else

if (strstr(cmd, "quit") != null || strstr(cmd, "quit") != null)

break;

} else

printf("\n");

}usleep(1000);

rv = sharemmy_destroy(shmid, shm);

if (rv < 0)

return

1;}

2、shm_test2.c

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define sem_key 100

#define print_times 10

#define shm_key 101

#define unix_domain "/tmp/unix_sock"

typedef

struct shared_use shared_use_t;

static

int sem_id = 0;

static

int connect_fd = 0;

static

void *shm = null;

static

int semaphore_v()

return0;}

static

int semaphore_p()

return0;}

static

int semaphore_init()

return0;}

static

int sharemmy_init(void)

shm = shmat(shmid, (void *)0, 0);

if (shm == (void *)-1)

return shmid;

}static

int sharemmy_destroy(int shmid, void *shm)

return0;}

static

int domain_client(void)

memset(&srv_un, 0, sizeof(srv_un));

srv_un.sun_family = af_unix;

strncpy(srv_un.sun_path, unix_domain, sizeof(srv_un.sun_path) - 1);

rv = connect(connect_fd, (struct sockaddr *)&srv_un, sizeof(srv_un));

if (rv < 0)

return0;}

static

int shmread(shared_use_t *shared)

shared->pktlen = ((shared_use_t *)shm)->pktlen;

memcpy(shared->pkt, ((shared_use_t *)shm)->pkt, shared->pktlen);

rv = semaphore_v();

if (rv < 0)

return0;}

static

void print_pkt(shared_use_t shared_pkt)

}printf("********** end **********\n");

printf("\n");

}int main()

shmid = sharemmy_init();

if (shmid == -1)

usleep(10*1000);

rv = domain_client();

if (rv < 0)

printf("client init success.\n");

while (1)

if (strstr(cmd, "send") != null || strstr(cmd, "send") != null)

print_pkt(shared_pkt);

continue;

} else

if (strstr(cmd, "quit") != null || strstr(cmd, "quit") != null) else

}rv = sharemmy_destroy(shmid, shm);

if (rv < 0)

close(connect_fd);

return

1;}

以上兩個程式實現的功能:shm_test1接收使用者輸入命令,如果是send則將向共享記憶體區域寫入乙個報文,並傳送socket訊息通知程序shm_test2從共享記憶體中讀出報文並列印出來。

CUDA共享記憶體的使用示例

cuda共享記憶體使用示例如下 參考教材 gpu高效能程式設計cuda實戰 p54 p65 1 include 2 include 3 include 4 include 5 include 6 include 78 using namespace std 910 define imin a,b a...

linux共享記憶體與訊號量的使用

1 使用共享記憶體在兩個程序中傳值 2 使用訊號量做同步控制。include include include include include include include static int init semvalue int static void del semvalue int stati...

Linux 使用共享記憶體

1.共享記憶體與訊息佇列的區別 訊息佇列在實現訊息的收發時,首先由傳送程序從程序空間將資料複製到核心分配的資料緩衝區中,接受程序再從核心的緩衝區複製到程序的虛擬位址空間 共享記憶體是將核心分配的共享儲存區域對映到程序的位址空間實現的,沒有資料的複製過程,共享記憶體的訪問速度要比訊息佇列快 2.共享記...