linux共享記憶體存放字串和數字

2021-10-20 13:28:13 字數 2282 閱讀 6354

參考資料

read me : 在傳輸的時候沒有進行strcpy,而是進行了直接相等,於是沒有能夠將字串賦予正確的值

**1:寫入的是char*,取出的是int

#include #include #include #include #include #include #include #include #include using namespace std;

#define err_exit(m) \

do while (0)

void sem_v(int semid)

;//預設阻塞

int ret = semop(semid, &sops, 1);

if (ret == -1)

}void sem_p(int semid)

;//預設阻塞

int ret = semop(semid, &sops, 1);

if (ret == -1)

}int main()

int shemid = shmget(1234, 1024, 0);//allocates a system v shared memory segment

if (shemid == -1)

int* addr = (int*)shmat(shemid, null, 0);

//從共享記憶體取資料

sem_p(semid);

int p = *addr;

cout << "get value =" << p << endl;

}//寫資料進入到共享記憶體

int semid2 = semget(1235, 0, 0);// get a system v semaphore set identifier

if (semid2 == -1)

int shemid2 = shmget(1235, 1024, 0);//allocates a system v shared memory segment

if (shemid2 == -1)

char* shmptr2 = (char*)shmat(shemid2, null, 0);

//shmptr2 = "nuhao";

strcpy(shmptr2, "nuhao");

sleep(5);

sem_v(semid2);

return 0;

}

**2:取出char*,寫入int

#include #include #include #include #include #include #include #include using namespace std;

#define err_exit(m) \

do while (0)

void sem_v(int semid)

;//預設阻塞

int ret = semop(semid, &sops, 1);

if (ret == -1)

}void sem_p(int semid)

;//預設阻塞

int ret = semop(semid, &sops, 1);

if (ret == -1)

}int main()

int shemid = shmget(1235, 1024, 0);//allocates a system v shared memory segment

if (shemid == -1)

char* addr = (char*)shmat(shemid, null, 0);

//從共享記憶體取資料

sem_p(semid);

//char p = *addr;

cout << "get value =" << addr << endl;

}//寫資料傳送給前置伺服器

int semid2 = semget(1234, 0, 0);// get a system v semaphore set identifier

if (semid2 == -1)

int shemid2 = shmget(1234, 1024, 0);//allocates a system v shared memory segment

if (shemid2 == -1)

int* addr2 = (int*)shmat(shemid2, null, 0);

*addr2 = 110;

sleep(5);

sem_v(semid2);

return 0;

}

字串在常量池中如何存放

字串在常量池中如何存放 string s1 hello s1 zhangsan string s2 zhangsan string s3 new string zhangsan string s4 new string zhangsan string s5 s1 zhangsan 上面的這些字串,字...

codeup函式 字串逆序存放

題目描述 寫乙個函式將乙個字串按反序存放。在主函式中輸入乙個字串,通過呼叫該函式,得到該字串按反序存放後的字串,並輸出。輸入 一行字串。輸出 輸入字串反序存放後的字串。單獨佔一行。樣例輸入 copy abcd 樣例輸出 copy dcba include include 函式陣列作為引數,卻不允許作...

《C和指標》 字串操作補 字串記憶體操作

1.對於 string 0 它其實等價於 string 0 string 2.對於 strcpy函式,如果要複製的字串比老字串短,由於新字串是以nul 即 0 結尾,所以老字串最後剩餘的幾個字元也會被有效地刪除。如 char message original message strcpy messa...