gpt4 book ai didi

c - 如何修复编译项目时出现的警告

转载 作者:行者123 更新时间:2023-11-30 21:36:22 24 4
gpt4 key购买 nike

screen error

任务:需要开发一个多进程应用程序。原始进程是管理器,接受键盘输入并控制子进程。按“+”键,添加一个新进程,“-”-删除最后添加的进程,“q”-程序结束。每个子进程在永恒循环中逐个字符地显示其自己唯一的字符串。同时,字符串输出操作必须是原子的,即输出过程必须同步,这样屏幕上的行就不会混淆。信号/事件应该用作同步方法。我该如何纠正这些错误?

    #include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#define MAX_COUNT 10
/*
char strings[10][30] = {{"\r\n1) First process"}, {"\r\n2) Second process"}, {"\r\n3) Third process"}, {"\r\n4) Fourth process"}, {"\r\n5) Fifth process"} , {"\r\n6) Sixth process"}, {"\r\n7) Seventh process"},{"\r\n8) Eighth process"}, {"\r\n9) Ninth process"}, {"\r\n10) Tenth process"}};
*/

char strings[10][30] = {{"1) First process"}, {"2) Second process"}, {"3) Third process"}, {"4) Fourth process"}, {"5) Fifth process"} , {"6) Sixth process"}, {"7) Seventh process"},{"8) Eighth process"}, {"9) Ninth process"}, {"10) Tenth process"}};

int printFlag = 0;
int flagEnd = 1;

struct sigaction printSignal, endSignal;

void canPrint(int signo)
{
printFlag = 1;
}

void setEndFlag(int signo)
{
flagEnd = 1;
}

int main(void)
{
int currentNum = 0;
int processNumber = 0;

int flag = 0;

initscr();
clear();
noecho();
refresh();

printSignal.sa_handler = canPrint;
sigaction(SIGUSR1,&printSignal,NULL);

endSignal.sa_handler = setEndFlag;
sigaction(SIGUSR2,&endSignal,NULL);

char c = 0;
int i = 0;

pid_t cpid[MAX_COUNT];


while(c!='q')
{
c = getchar();

switch(c)
{
case '+':
if(processNumber < MAX_COUNT)
{
cpid[processNumber] = fork();
processNumber++;

switch(cpid[processNumber-1])
{
case 0:
flagEnd = 0;
while(!flagEnd)
{
usleep(10000);
if(printFlag)
{
for(i=0; i<strlen(strings[processNumber-1]); i++)
{
if(flagEnd)
return 0;

printf("%c",strings[processNumber-1][i]);
refresh();
usleep(70000);
}
refresh();
printFlag = 0;
kill(getppid(),SIGUSR2);
}
}
return 0;
break;

case -1:
printf("Error!\n");
break;

default:
break;
}
}
break;

case '-':
if(processNumber > 0)
{
kill(cpid[processNumber-1],SIGUSR2);
waitpid(cpid[processNumber-1],NULL,NULL);//???

processNumber--;

if (currentNum >= processNumber)
{
currentNum = 0;
flag = 1;
flagEnd = 1;
}

}
break;
}

if(flagEnd && processNumber>0)
{
flagEnd = 0;
if(currentNum >= processNumber - 1)
currentNum = 0;
else if(!flag) currentNum++;

flag = 0;

kill(cpid[currentNum],SIGUSR1);
}
refresh();
}


if(cpid[processNumber-1]!=0)
for(;processNumber>=0;processNumber--)
{
kill(cpid[processNumber-1],SIGUSR2);
waitpid(cpid[processNumber-1],NULL,NULL);//???
}

clear();
endwin();

return 0;
}

最佳答案

w.c:97:46: warning: passing argument 3 of ‘waitpid’ makes integer from pointer without a cast [-Wint-conversion]
waitpid(cpid[processNumber-1],NULL,NULL);//???

In file included from w.c:8:0:
/usr/include/arm-linux-gnueabihf/sys/wait.h:86:16: note: expected ‘int’ but argument is of type ‘void *’
extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options);

第三个参数必须是 intNULL 不是 int 而是指针 (((void*) 0 ) 正如您在警告中看到的那样),将其替换为 0,因此 waitpid(cpid[processNumber-1], NULL, 0);

关于c - 如何修复编译项目时出现的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com