尝试实现信号处理程序,但收到警告:

assignment from incompatible pointer type [enabled by default]
   act.sa_sigaction = sigChldHandler;

... 我的教授也指出。
char * argv[3];
argv[0]= GUESSER_PROGNAME;
argv[2]= NULL;

这会为3个字符的指针变量分配内存,但它们不会特别指向任何地方。
\文件1
#include "assign2Headers.h"
pid_t answererPid;
pid_t guesserPid;

int shouldRun = 1;

void  sigAlrmHandler(int sig)
{
    kill(answererPid,TIME_OVER_SIGNAL);
    kill(guesserPid,TIME_OVER_SIGNAL);
    shouldRun=0;
}

void  sigChldHandler(int sig)
{
wait(NULL);
shouldRun=0;
}

int main(void){


  struct sigaction act;
  memset(&act, '\0', sizeof(struct sigaction));

  act.sa_handler = sigAlrmHandler;
  sigaction(SIGALRM, &act, NULL);

  act.sa_sigaction = sighldHandler;
  sigaction(SIGCHLD, &act, NULL);


char line[LINE_LEN];
char * argv[3];
argv[0]= GUESSER_PROGNAME;
argv[2]= NULL;
answererPid = fork();

if(answererPid == 0){

        execl(ANSWERER_PROGNAME,ANSWERER_PROGNAME,(char*)NULL);

    }

else{
        sleep(1);
        snprintf(line,LINE_LEN,"%d",answererPid);
        guesserPid=fork();
        if(guesserPid==0)
        {
            execl(GUESSER_PROGNAME,GUESSER_PROGNAME,argv[0],line,(char*)NULL);
        }
        else
        { alarm(NUM_SECONDS);
            while(shouldRun)
                sleep(1);
            sleep(1);
            sleep(1);
            printf("launcher finished\n");
            return (EXIT_SUCCESS);

        }

    }

}

\文件2
#include "assign2Headers.h"

int shouldRun = 1;

void timeoverhandler(int sig)
{   sleep(1);
    printf("\nOh no!  The time is up!\n");
    printf("guesser finished\n");
    shouldRun=0;
    exit(EXIT_SUCCESS);
}

void winsignalhandler(int sig)
{
    printf("\nCongratulations!  You found it!\n");
    shouldRun=0;
        signal(WIN_SIGNAL,winsignalhandler);
}

void correctsignalhandler(int sig)
{
    printf("Yay!  That was right!\n");
    signal(CORRECT_SIGNAL,correctsignalhandler);
}

void incorrectsignalhandler(int sig)
{
    printf("Oops!  That was wrong.  Please restart from the beginning.\n"
"\nRe-starting from the beginning:\n");
    signal(INCORRECT_SIGNAL,incorrectsignalhandler);
}

int main(int argc,char* argv[])
{

    pid_t answererPid=atoi(argv[1]);
    signal(TIME_OVER_SIGNAL,timeoverhandler);
    signal(WIN_SIGNAL,winsignalhandler);
    signal(CORRECT_SIGNAL,correctsignalhandler);
    signal(INCORRECT_SIGNAL,incorrectsignalhandler);

    while(shouldRun)
    {   int guess;
        printf("What would you like your next guess to be: 0 or 1? ");
        scanf("%d",&guess);
        if(guess==0)
            kill(answererPid,ZERO_SIGNAL);
        if(guess==1)
            kill(answererPid,ONE_SIGNAL);
        sleep(2);

    }


    printf("guesser finished\n");
    return (EXIT_SUCCESS);


}

\文件3
//---       Common standard header files                ---//

#include    <stdlib.h>
#include    <stdio.h>
#include    <string.h>
#include    <signal.h>
#include    <sys/types.h>
#include    <sys/wait.h>
#include    <unistd.h>

////---     Common constants:                   ---//

#define     ZERO_SIGNAL     SIGUSR1
#define     ONE_SIGNAL      SIGUSR2
#define     CORRECT_SIGNAL      SIGUSR1
#define     INCORRECT_SIGNAL    SIGUSR2
#define     WIN_SIGNAL      SIGINT
#define     TIME_OVER_SIGNAL    SIGTERM

#define     GUESSER_PROGNAME    "guesser"
#define     ANSWERER_PROGNAME   "answerer"

#define     LINE_LEN        256
#define     NUM_SECONDS     30

\文件4
//---       Inclusion of header files               ---//

#include    "assign2Headers.h"

//---       Definition of constants:                ---//

#define     PATTERN_LEN 4

//---       Definition of global vars:              ---//

int     answer;
int     numCorrect  = 0;
int     shouldRun   = 1;


//---       Definition of global fncs:              ---//

void        timeUpHandler   (int        sig
                )
{
  shouldRun = 0;
}


void        guessHandler    (int        sig,
                 siginfo_t* infoPtr,
                 void*      dataPtr
                )
{
  int   toSendBack;
  int   userBit     = (sig == ONE_SIGNAL);
  int   correctBit  = ((answer >> numCorrect) & 0x1);
  int   isCorrect   = (correctBit == userBit);

  printf("position %d: userBit %d, correctBit %d\n",
     numCorrect,userBit,correctBit
    );

  if  (isCorrect)
  {
    numCorrect++;

    if  (numCorrect >= PATTERN_LEN)
      toSendBack = WIN_SIGNAL;
    else
      toSendBack = CORRECT_SIGNAL;
  }
  else
  {
    numCorrect  = 0;
    toSendBack  = INCORRECT_SIGNAL;
  }

  kill(infoPtr->si_pid,toSendBack);
}


int     main        (int    argc,
                 char*  argv[]
                )
{
  //  I.  Application validity check:

  //  II.  Run program:
  //  II.A.  Initialize random number generator and choice:
  srand(getpid());

  answer    = rand() % (1 << PATTERN_LEN);

printf("(The answer is %d)\n",answer);

  //  II.B.  Install signal handlers:
  struct sigaction  act;

  memset(&act,'\0',sizeof(act));
  act.sa_handler    = timeUpHandler;
  sigaction(TIME_OVER_SIGNAL,&act,NULL);

  act.sa_flags      = SA_SIGINFO;
  act.sa_sigaction  = guessHandler;
  sigaction(ZERO_SIGNAL,&act,NULL);
  sigaction(ONE_SIGNAL,&act,NULL);

  //  II.C.  Hand out while game still active:
  while  ( (numCorrect < PATTERN_LEN)  &&  shouldRun )
    sleep(1);

  //  III.  Finished, return answer:
  printf("answerer finished\n");
  return(EXIT_SUCCESS);
}

我该如何消除这个警告,他是什么意思请有人帮帮我。

最佳答案

我猜你在Linux上工作。从sigaction手册页:

   struct sigaction {
       void     (*sa_handler)(int);
       void     (*sa_sigaction)(int, siginfo_t *, void *);
       sigset_t   sa_mask;
       int        sa_flags;
       void     (*sa_restorer)(void);
   };

原型void sigChldHandler(int sig)sa_sigaction不匹配。因此发出警告。
你可以用
sa_handler设置信号处理函数或指定SIG_IGN/SIG_DFL操作。
sa_sigaction设置信号处理函数-如果用户需要访问更多详细信息,如用户上下文和结构(发送方进程详细信息、信号类型等)。为此,需要在siginfo中设置SA_SIGINFO
对于您的情况,设置sa_flags可能就足够了。
sa_handler中:
struct sigaction act;
act.sa_handler = sigChldHandler;
act.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &act, NULL);

也,
对每个要设置的信号使用单独的main()结构,除非需要相同的处理程序。
不要混合使用。坚持一个也许,sigaction是新的,它提供了比sigaction()更多的功能。

关于c - 来自不兼容指针类型的分配-如何解决?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42128543/

10-11 17:57