当前位置:网站首页 > 技术博客 > 正文

c语言ftell函数的用法



 key_t ftok(const char *pathname, int proj_id);

 pathname参数:  必须是一个已经存在且程序可范围的文件。

 proj_id参数: 虽然定义为一个整数,其实实际只有8个bit位有效,即如果该参数大于255,则只有后8bit有效。 

 函数作用: convert a pathname and a project identifier to a System V IPC key, Key可用于msgget, semget, or shmget的key参数

示例:

    #define  SHMSIZE   256

    key_t  shmKey = ftok(“/tmp”, 128);
    if (-1 == shmKey)
    {
        printf("ERROR: ftok faield, %s. ", strerror(errno));
        exit(-1);
    }

    //创建共享内存

    int shmid = shmget(shmKey, SHMSIZE, 0666);
    if (-1 == shmid)
    {
        shmid = shmget(shmKey, SHMSIZE, IPC_CREAT | 0666);
        if (-1 == shmid)
        {
            shmid = shmget(shmKey, 0, 0666);
            if (shmid >= 0)
            {
                shmctl(shmid, IPC_RMID, NULL);
                shmid = shmget(shmKey, SHMSIZE, IPC_CREAT | 0666);
            }
            if (-1 == shmid)
            {
                printf("ERROR: call shmget failed, %s. ", strerror(errno));
                exit(-1);
            }
       }
   

     //链接共享内存

     char *shmptr  = (char *)shmat(shmid, (char *) 0, 0666);
     if (shmptr  == (char *)(-1))
      {
          printf("Call shmat()  failed, %s. ", strerror(errno));
          exit(-1);
      }

     //拆卸共享内存
     if ((shmctl(shmid, IPC_RMID, 0) < 0))
     {
        printf("shmctl error:%s ", strerror(errno));
        return -1;
     }

 

版权声明


相关文章:

  • c语言中memset函数的用法2025-04-26 10:01:04
  • 最大似然估计求解步骤2025-04-26 10:01:04
  • sql编程实例2025-04-26 10:01:04
  • 文件对比工具 beyond compare2025-04-26 10:01:04
  • idea打包jar包含依赖2025-04-26 10:01:04
  • 备忘录功能介绍2025-04-26 10:01:04
  • linux查看硬盘分区表2025-04-26 10:01:04
  • 王道计算机组成原理视频2025-04-26 10:01:04
  • 指针网络科技有限公司2025-04-26 10:01:04
  • ldap服务端口2025-04-26 10:01:04