任务:有必要开发一个多进程应用程序。原始进程为管理器,接受键盘输入并控制子进程。通过按“ +”键,将添加一个新进程,“-”-删除最后添加的进程,“ 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;
}
答案 0 :(得分:1)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
第三个参数必须为 int , NULL 不是 int ,而是指针(如您所见,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);
在您的警告中),将其替换为0,因此((void*) 0)