A small instance of the go language shared memory operation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >wrapper.c</strong> #include <stdlib.h> #include <string.h> #include <sys/shm.h> #include    <sys/types.h>int My_shm_open (char* filename, int open_flag) {int shm_id;    key_t key;    Key = Ftok (filename, 0x03);    if (key = =-1) {return-1; } if (open_flag) shm_id = Shmget (key, 4096, ipc_creat|    IPC_EXCL|0600);    else shm_id = shmget (key, 0, 0);    if (shm_id = =-1) {return-1; } return shm_id;}    int my_shm_update (int shm_id, char* content) {char* addr; addr = (char*) Shmat (shm_id, NULL, 0);    if (addr = = (char*)-1) {return-1;    } if (strlen (content) > 4095) return-1;    strcpy (addr, content);    SHMDT (addr); return 0;}    int my_shm_close (int shm_id) {shmctl (shm_id, Ipc_rmid, NULL); return 0;}    char* my_shm_read (char* filename) {int shm_id;    char* addr;    char* s;    shm_id = my_shm_open (filename, 0);    if (shm_id = =-1) return NULL;    Addr = (char*) shmat (shm_id, NULL, 0);    if (addr = = (char*)-1) {return NULL;    } s = (char*) malloc (strlen (addr) + 1);    strcpy (S, addr);    SHMDT (addr); return s;}


<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >reader.go</strong>package main//#include <stdlib.h>//#include "wrapper.c" import "C" import "unsafe" Import "FMT" func read (filename string) string {    f: = c.cstring (filename)    defer c.free (unsafe. Pointer (f))    s: = C.my_shm_read (f)    defer c.free (unsafe. Pointer (s))    return c.gostring (s)}func main () {    fmt. Println (Read ("tmp"))}

<strong style= "margin:0px; padding:0px; border:0px; font-size:15px; Font-weight:bold; Color:rgb (34, 34, 34); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; Font-style:normal; Font-variant:normal; Letter-spacing:normal; line-height:19px; Orphans:auto; Text-align:left; text-indent:0px; Text-transform:none; White-space:normal; Widows:auto; word-spacing:0px; -webkit-text-stroke-width:0px; Background-color:rgb (255, 255, 255); " >writter.go</strong>package main//#include <stdlib.h>//#include "wrapper.c" import "C" import "unsafe" Import ("Log" "Time") type errorstring struct {s string}func (e *errorstring) Error () string {return E.s}fun C Open (file string) (int, error) {f: = c.cstring (file) defer c.free (unsafe. Pointer (f)) R: = Int (C.my_shm_open (f, C.int (1))) if r = =-1 {return 0, &errorstring{"error"}} RET Urn R, nil}func update (shm_id int, content string) error {c: = c.cstring (content) defer C. Free (unsafe.    Pointer (c)) R: = Int (C.my_shm_update (C.int (shm_id), c)) if r = =-1 {return &errorstring{"update Error"} } return Nil}func close (shm_id int) error {C.my_shm_close (C.int (shm_id)) return Nil}func main () {ID, err : = Open ("/tmp") if err! = Nil {log. Fatal (ERR)} defer close (id) Err = update (ID, "Hello World") if err! = Nil {log. Fatal (ERR)} time. Sleep (1E9 * 100)}
This is my stackoverflower on the question others answer, or not too understand, keep.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.