标题: 一段进程 [打印本页]

作者: fucuhost    时间: 2018-11-2 07:27
标题: 一段进程
  1. #include <stdio.h>

  2. #include <unistd.h>

  3. #include <sys/ipc.h>

  4. #include <sys/shm.h>

  5. #include <sys/types.h>

  6. #include <string.h>


  7. #define BUF_SIZE 100


  8. int main()

  9. {

  10.         key_t key = ftok(".", 11);

  11.         int iShmID = shmget(key, BUF_SIZE, IPC_CREAT | 0666);

  12.         if (-1 == iShmID)

  13.         {

  14.                 return -1;

  15.         }

  16.         printf("shmget ok\r\n");

  17.        
  18. void *p = shmat(iShmID,NULL,0);

  19.         if (NULL == p)

  20.         {

  21.                 //delete

  22.                 return -1;

  23.         }

  24.         int i = 0;

  25.         char *p1 = (char *)p;

  26.         for (i=0;i<10;i++)

  27.         {

  28.          printf("%c ", p1[i]);

  29.         }

  30.         printf("\r\n");

  31.         shmdt(p);

  32.         printf("ok\r\n");

  33.         shmctl(iShmID,IPC_RMID, NULL);

  34.         return 0;

  35. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1