Ubuntu的信号量:分段故障(核心转储) [英] Ubuntu Semaphore: Segmentation fault (core dumped)

查看:251
本文介绍了Ubuntu的信号量:分段故障(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ubuntu现在学习信号灯的温度。教授只是把我们这个code和要求我们对其进行研究和观察。当我整理我得到的ctime警告(安培; sem_buf.sem_ctime)返回 INT ,而不是的char * ,但没有重大。当我运行它的输出就是:信号灯标识符:0分割故障(核心转储)。我作为什么地方出了错很迷茫,我不知道在这个code回事。一些帮助将非常AP preciated。

下面是code:

 的#include< SYS / types.h中>
#包括LT&; SYS / ipc.h>
#包括LT&; SYS /&sem.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&semaphore.h GT;
#定义NS 3
工会semun {
   INT VAL;
   结构为semid_ds * BUF;
   USHORT *阵列; //无符号短整型。
};INT主要(无效)
{
   INT sem_id,sem_value我;
   的key_t ipc_key;
   结构为semid_ds sem_buf;
   静态USHORT sem_array [NS] = {3,1,4};
   工会semun ARG;
   ipc_key = ftok(,S。); //创建密钥。
   / *创建信号量* /
   如果((sem_id = semget子(ipc_key,NS,IPC_CREAT | 0666))== -1){
      PERROR(了semget:IPC | 0666);
      出口(1);
   }
   的printf(旗语识别%d个\\ N,sem_id);
   / *设置ARG(工会)的存储位置为*地址/
   / *返回的semid_ds值* /
   arg.buf =安培; sem_buf;
   如果(了semctl(sem_id,0,IPC_STAT,ARG)== -1){
      PERROR(了semctl:IPC_STAT);
      出口(2);
   }
   的printf(创建%S的ctime(安培; sem_buf.sem_ctime));
   / *设置ARG(工会)来初始化向量*的地址/
   arg.array = sem_array;
   如果(了semctl(sem_id,0,SETALL,ARG)== -1){
      PERROR(了semctl:SETALL);
      出口(3);
   }
   对于(i = 0; I< NS; ++ I){
      如果((sem_value =了semctl(sem_id,I,GETVAL,0))== - 1){
         PERROR(了semctl:GETVAL);
         出口(4);
      }
      的printf(旗语%d个有%d \\ n的值,我,sem_value);
   }
   / *删除信号量* /
   如果(和semctl(sem_id,0,IPC_RMID,0)== - 1){
      PERROR(了semctl:IPC_RMID);
      出口(5);
   }
}


解决方案

您需要包括 time.h中编译器识别的ctime 功能。该警告是因为编译器不知道的ctime 是一个函数,返回一个的char * 。默认情况下GCC假设未知函数返回一个 INT

I am learning semaphores in C using Ubuntu right now. The professor just throw us this code and ask us to study it and observe. When I compiled I get a warning that ctime(&sem_buf.sem_ctime) returns an int, not a char * but nothing major. When I run it the output is just: Semaphore identifier: 0 Segmentation fault (core dumped). I am very confused as of what went wrong and I have no idea what is going on in this code. Some help would be very much appreciated.

Here is the code:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h> 
# define NS 3
union semun { 
   int val; 
   struct semid_ds *buf; 
   ushort *array; // Unsigned short integer.
}; 

int main(void) 
{ 
   int sem_id, sem_value, i; 
   key_t ipc_key; 
   struct semid_ds sem_buf; 
   static ushort sem_array[NS] = {3, 1, 4}; 
   union semun arg; 
   ipc_key = ftok(".", 'S'); // Creating the key.
   /* Create semaphore */ 
   if ((sem_id = semget(ipc_key, NS, IPC_CREAT | 0666)) == -1) { 
      perror ("semget: IPC | 0666"); 
      exit(1); 
   } 
   printf ("Semaphore identifier %d\n", sem_id); 
   /* Set arg (the union) to the address of the storage location for */ 
   /* returned semid_ds value */ 
   arg.buf = &sem_buf; 
   if (semctl(sem_id, 0, IPC_STAT, arg) == -1) { 
      perror ("semctl: IPC_STAT"); 
      exit(2); 
   } 
   printf ("Create %s", ctime(&sem_buf.sem_ctime)); 
   /* Set arg (the union) to the address of the initializing vector */ 
   arg.array = sem_array; 
   if (semctl(sem_id, 0, SETALL, arg) == -1) { 
      perror("semctl: SETALL"); 
      exit(3); 
   } 
   for (i=0; i<NS; ++i) { 
      if ((sem_value = semctl(sem_id, i, GETVAL, 0)) == -1) { 
         perror("semctl : GETVAL"); 
         exit(4); 
      } 
      printf ("Semaphore %d has value of %d\n",i, sem_value); 
   } 
   /*remove semaphore */ 
   if (semctl(sem_id, 0, IPC_RMID, 0) == -1) { 
      perror ("semctl: IPC_RMID"); 
      exit(5); 
   } 
} 

解决方案

You need to include time.h to the compiler recognize ctime function. The warning is because the compiler don't know ctime is a function and that returns an char*. By default GCC assumes the unknown function returns an int.

这篇关于Ubuntu的信号量:分段故障(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆