gpt4 book ai didi

c - 处理与消息队列和信号的同步

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:30 27 4
gpt4 key购买 nike

我必须创建三个流程:
阅读1+3+5+12这样的表达式
检查表达式语法是否正确
添加数字并显示它们
进程之间的数据使用管道机制共享。进程同步使用消息队列和信号。我还应该能够通过控制台手动向每个进程发送信号。
我遇到的问题是所有进程似乎都是随机运行的。为什么,这里怎么了?它应该有用…
这是正确编译的全部代码:

   #include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZEBUFF 256

// Signal handling for each process
void h_sig1(int signo);
void h_sig2(int signo);
void h_sig3(int signo);
void h_S4(int signo);

// Processes functions
void process1(void);
void process2(void);
void process3(void);

// helper functions
bool isInteger(double val);

static pid_t P1, P2, P3; // PIDs for each process

int P; // parent PID
int pfd12[2], pfd23[2]; // Pipes between processes 1-2 and 2-3
int providePIDY1[2], providePIDY2[2]; // provide all PIDs to 1/2 process

// message in message queue
typedef struct
{
long type;
int sigNum;
} message;

int queue_ID;

int main(void)
{
P = getpid();

// Message queue created
if ((queue_ID = msgget(IPC_PRIVATE,IPC_CREAT|0666)) < 0)
{
printf("msgget\n");
return 1;
}

if((P1 = fork()) == 0)
{
P1 = getpid();
process1();
}
else if((P2 = fork()) == 0)
{
P2 = getpid();
process2();
}
else if((P3 = fork()) == 0)
{
P3 = getpid();
process3();
}
else
{ // Sending signals from parent through operator

// Sending PIDs to process 1
close(providePIDY1[0]);
write(providePIDY1[1], &P, sizeof(int));
write(providePIDY1[1], &P1, sizeof(int));
write(providePIDY1[1], &P2, sizeof(int));
write(providePIDY1[1], &P3, sizeof(int));
close(providePIDY1[1]);

// Sending PIDs to process 2
close(providePIDY2[0]);
write(providePIDY2[1], &P, sizeof(int));
write(providePIDY2[1], &P1, sizeof(int));
write(providePIDY2[1], &P2, sizeof(int));
write(providePIDY2[1], &P3, sizeof(int));
close(providePIDY2[1]);

printf("\nProgram options:\n");
printf("Send signal - 's'(send)\n");
printf("Display processes PIDs 'p'(pids)\n");
printf("Quit program - 'q'(quit)\n");

char choice, choice2, choice3;

while(1)
{
choice = getchar();

if(choice == 's')
{
printf("Which process is receiving the signal - 1, 2, or 3?: ");
choice2 = getchar();
choice2 = getchar();
printf("\n");

if((choice2 < 1) && (choice2 > 3))
{
printf("No such process!");
continue;
}

printf("What signal to send?:\n");
printf("1-S1(end execution)\n2-S2(pause execution)\n3-S3(renew execution)?\n ");
printf("Choice: ");
choice3 = getchar();
choice3 = getchar();
switch(choice2)
{
case '1': //nie można przechwycić sygnałów SIGKILL oraz SIGSTOP (zabicia oraz zatrzymania)
if(choice3 == '1') { kill(0,SIGCONT); kill(P1,SIGUSR1); }
if(choice3 == '2') kill(P1,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
case '2':
if(choice3 == '1') { kill(0,SIGCONT); kill(P2,SIGUSR1); }
if(choice3 == '2') kill(P2,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
case '3':
if(choice3 == '1') { kill(0,SIGCONT); kill(P3,SIGUSR1); }
if(choice3 == '2') kill(P3,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
default: printf("No such operation!!! \n\n");
}
}

if(choice == 'p')
{
// do something
}

if(choice == 'q')
{
// do something
}
}

}
}

void process1(void)
{
// Receiving PIDs
close(providePIDY1[1]);
read(providePIDY1[0], &P, sizeof(int));
read(providePIDY1[0], &P1, sizeof(int));
read(providePIDY1[0], &P2, sizeof(int));
read(providePIDY1[0], &P3, sizeof(int));
close(providePIDY1[0]);

struct sigaction act1;

act1.sa_handler = h_sig1;
sigemptyset(&act1.sa_mask);
act1.sa_flags = 0;
sigaction(SIGUSR1, &act1, 0);
sigaction(SIGTSTP, &act1, 0);
sigaction(SIGALRM, &act1, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);

// do something
}

void process2(void)
{
close(providePIDY2[1]);
read(providePIDY2[0], &P, sizeof(int));
read(providePIDY2[0], &P1, sizeof(int));
read(providePIDY2[0], &P2, sizeof(int));
read(providePIDY2[0], &P3, sizeof(int));
close(providePIDY2[0]);

struct sigaction act2;
act2.sa_handler = h_sig2;
sigemptyset(&act2.sa_mask);
act2.sa_flags = 0;
sigaction(SIGUSR1, &act2, 0);
sigaction(SIGTSTP, &act2, 0);
sigaction(SIGALRM, &act2, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);

// do something
}

void process3(void)
{
struct sigaction act3;

act3.sa_handler = h_sig3;
sigemptyset(&act3.sa_mask);
act3.sa_flags = 0;
sigaction(SIGUSR1, &act3, 0);
sigaction(SIGTSTP, &act3, 0);
sigaction(SIGALRM, &act3, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);

// do something
}

void h_sig1(int signo)
{
message msg;

msg.type = P2;
msg.sigNum = signo;

kill(P2, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P3;
kill(P3, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
}

if(signo == SIGTSTP)
{
}

if(signo == SIGALRM)
{
}
}

void h_sig2(int signo)
{
message msg;

msg.type = P1;
msg.sigNum = signo;

kill(P1, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P3;
kill(P3, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
}

if(signo == SIGTSTP)
{
}

if(signo == SIGALRM)
{
}
}

void h_sig3(int signo)
{
message msg;

msg.type = P1;
msg.sigNum = signo;

kill(P1, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P2;
kill(P2, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
}

if(signo == SIGTSTP)
{
}

if(signo == SIGALRM)
{
}
}

void h_S4(int signo)
{
int res;
message msg;

printf("\nProcess with PID=%d received signal S4", getpid());

if(signo == SIGINT)
{
res = msgrcv(queue_ID, &msg, sizeof(msg.sigNum), msg.type, 0);

if(res >= 0)
{
if(msg.sigNum == SIGUSR1)
{

}
if(msg.sigNum == SIGTSTP)
{

}
if(msg.sigNum == SIGALRM)
{
}
}
}
}

要编译的长版本:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZEBUFF 200

// Signal handling for each process
void h_sig1(int signo);
void h_sig2(int signo);
void h_sig3(int signo);

void h_S4(int signo); // signal handling for the 4th signal

// Processes functions
void process1(void);
void process2(void);
void process3(void);

// helper functions
bool isInteger(double val);

static pid_t P1, P2, P3; // PIDs for each process

int P; // parent PID
int pfd12[2], pfd23[2]; // Pipes between processes 1-2 and 2-3
int providePIDY1[2], providePIDY2[2]; // provide all PIDs to 1/2 process

// message in message queue
typedef struct
{
long type;
int sigNum;
} message;

int queue_ID;

int main(void)
{
P = getpid();

if (pipe(pfd12) == -1)
{
perror("pipe failed");
exit(1);
}
if (pipe(pfd23) == -1)
{
perror("pipe failed");
exit(1);
}

// Message queue created
if ((queue_ID = msgget(IPC_PRIVATE,IPC_CREAT|0666)) < 0)
{
printf("msgget\n");
return 1;
}

if (pipe(providePIDY1) == -1)
{
perror("pipe failed");
exit(1);
}
if (pipe(providePIDY2) == -1)
{
perror("pipe failed");
exit(1);
}

if((P1 = fork()) == 0)
{
P1 = getpid();
process1();
}
else if(P1 < 0)
{
perror("fork failed");
exit(2);
}
else if((P2 = fork()) == 0)
{
P2 = getpid();
process2();
}
else if(P2 < 0)
{
perror("fork failed");
exit(2);
}
else if((P3 = fork()) == 0)
{
P3 = getpid();
process3();
}
else if(P3 < 0)
{
perror("fork failed");
exit(2);
}
else
{ // Sending signals from parent through operator

// Sending PIDs to process 1
close(providePIDY1[0]);
write(providePIDY1[1], &P, sizeof(int));
write(providePIDY1[1], &P1, sizeof(int));
write(providePIDY1[1], &P2, sizeof(int));
write(providePIDY1[1], &P3, sizeof(int));
close(providePIDY1[1]);

// Sending PIDs to process 2
close(providePIDY2[0]);
write(providePIDY2[1], &P, sizeof(int));
write(providePIDY2[1], &P1, sizeof(int));
write(providePIDY2[1], &P2, sizeof(int));
write(providePIDY2[1], &P3, sizeof(int));
close(providePIDY2[1]);

printf("\nProgram options:\n");
printf("Send signal - 's'(send)\n");
printf("Display processes PIDs 'p'(pids)\n");
printf("Quit program - 'q'(quit)\n");

char choice, choice2, choice3;

while(1)
{
choice = getchar();

if(choice == 's')
{
printf("Which process is receiving the signal - 1, 2, or 3?: ");
choice2 = getchar();
choice2 = getchar();
printf("\n");

if((choice2 < 1) && (choice2 > 3))
{
printf("No such process!");
continue;
}

printf("What signal to send?:\n");
printf("1-S1(end execution)\n2-S2(pause execution)\n3-S3(renew execution)?\n ");
printf("Choice: ");
choice3 = getchar();
choice3 = getchar();
switch(choice2)
{
case '1': //nie można przechwycić sygnałów SIGKILL oraz SIGSTOP (zabicia oraz zatrzymania)
if(choice3 == '1') { kill(0,SIGCONT); kill(P1,SIGUSR1); }
if(choice3 == '2') kill(P1,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
case '2':
if(choice3 == '1') { kill(0,SIGCONT); kill(P2,SIGUSR1); }
if(choice3 == '2') kill(P2,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
case '3':
if(choice3 == '1') { kill(0,SIGCONT); kill(P3,SIGUSR1); }
if(choice3 == '2') kill(P3,SIGTSTP);
if(choice3 == '3') { kill(0,SIGCONT); kill(P3,SIGALRM); }
break;
default: printf("No such operation!!! \n\n");
}
}

if(choice == 'p')
{
printf("\n<Processes PIDs:>\n");
printf("P(initial process)=%d\n",P);
printf("P1(process 1)=%d\n",P1);
printf("P2(process 2)=%d\n",P2);
printf("P3(process 3)=%d\n\n",P3);
}

if(choice == 'q')
{
printf("\n<Quitting program>\n");
msgctl(queue_ID, IPC_RMID, 0);
kill(0, SIGKILL);
}
}

}
}

void process1(void)
{
int dataSize;
char buff[SIZEBUFF];

// Receiving PIDs
close(providePIDY1[1]);
read(providePIDY1[0], &P, sizeof(int));
read(providePIDY1[0], &P1, sizeof(int));
read(providePIDY1[0], &P2, sizeof(int));
read(providePIDY1[0], &P3, sizeof(int));
close(providePIDY1[0]);

printf("\n<Process 1 execution in progress>\n");

/*SIGACTION*/

struct sigaction act1;

act1.sa_handler = h_sig1;
sigemptyset(&act1.sa_mask);
act1.sa_flags = 0;
sigaction(SIGUSR1, &act1, 0);
sigaction(SIGTSTP, &act1, 0);
sigaction(SIGALRM, &act1, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);


close(pfd12[0]);
while(1)
{
printf("Provide expr: ");
scanf("%s", buff);
if(strcmp(buff, "0") == 0)
break;

dataSize = strlen(buff) + 1; // plus NULL
if(dataSize > 0)
write(pfd12[1], &dataSize, sizeof(int));
write(pfd12[1], &buff, sizeof(char)*dataSize);
}

dataSize = 0; // info that there's no more data
write(pfd12[1], &dataSize, sizeof(int));

close(pfd12[1]);

printf("\n---Process 1 finished execution---\n");

exit(0);
}

void process2(void)
{
int dataSize;
char buff[SIZEBUFF];
char *token, *end;
int number;
const char delim[2] = "+";

//Odebranie pidow
close(providePIDY2[1]);
read(providePIDY2[0], &P, sizeof(int));
read(providePIDY2[0], &P1, sizeof(int));
read(providePIDY2[0], &P2, sizeof(int));
read(providePIDY2[0], &P3, sizeof(int));
close(providePIDY2[0]);

printf("\n<Process 2 execution in progress>\n");

/*SIGACTION*/
struct sigaction act2;
act2.sa_handler = h_sig2;
sigemptyset(&act2.sa_mask);
act2.sa_flags = 0;
sigaction(SIGUSR1, &act2, 0);
sigaction(SIGTSTP, &act2, 0);
sigaction(SIGALRM, &act2, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);

close(pfd12[1]);

read(pfd12[0], &dataSize, sizeof(int));
if(dataSize > 0)
{
sleep(3);

read(pfd12[0], buff, dataSize);
token = strtok(buff, delim);
while( token != NULL )
{
number = strtol(token, &end, 0);
if(!isInteger(number))
break;
}
}

close(pfd12[0]);

// Sending result to process 3
close(pfd23[0]);
write(pfd23[1], &buff, sizeof(int));
close(pfd23[1]);

printf("\n---Process 2 finished execution---\n");
}

void process3(void)
{
int sum = 0;
char buff[SIZEBUFF];
char* token, *end;
int number;
const char delim[2] = "+";

/*SIGACTION*/
struct sigaction act3;

act3.sa_handler = h_sig3;
sigemptyset(&act3.sa_mask);
act3.sa_flags = 0;
sigaction(SIGUSR1, &act3, 0);
sigaction(SIGTSTP, &act3, 0);
sigaction(SIGALRM, &act3, 0);

struct sigaction act;
act.sa_handler = h_S4;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);

printf("\n<Process 3 execution in progress>");
close(pfd23[1]);

read(pfd23[0], &buff, sizeof(int));

token = strtok(buff, delim);

while( token != NULL )
{
number = strtol(token, &end, 0);
sum += number;
}

printf("Sum = %d\n", sum);

close(pfd23[0]);

printf("\n---Process 3 finished execution ---\n");

printf("\n---Program finished execution---\n");
kill(getppid(),SIGKILL);

}

/*******************************************************************************************************/
/****************************************SIGNAL HANDLING (S1-S3)***********************************************/
/*******************************************************************************************************/
void h_sig1(int signo)
{
message msg;

msg.type = P2;
msg.sigNum = signo;

kill(P2, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P3;
kill(P3, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
printf("\nProcess 1 received signal S1\n");
printf("Terminating parent process!\n");
kill(getppid(), SIGKILL);
printf("Terminating process 1!\n");
kill(getpid(), SIGKILL);
}

if(signo == SIGTSTP)
{
printf("\nProcess 1 received signal S2\n");
printf("Pausing process 1!\n");
kill(getpid(), SIGSTOP);
}

if(signo == SIGALRM)
{
printf("\nProcess 1 received signal S3\n");
printf("Renewing execution of process 1!\n");
kill(getpid(), SIGCONT);
}
}

void h_sig2(int signo)
{
message msg;

msg.type = P1;
msg.sigNum = signo;

kill(P1, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P3;
kill(P3, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
printf("\nProcess 2 received signal S1\n");
printf("Terminating parent process!\n");
kill(getppid(), SIGKILL);
printf("Terminating process 2!\n");
kill(getpid(), SIGKILL);
}

if(signo == SIGTSTP)
{
printf("\nProcess 2 received signal S2\n");
printf("Pausing process 2!\n");
kill(getpid(), SIGSTOP);
}

if(signo == SIGALRM)
{
printf("\nProcess 2 received signal S3\n");
printf("Renewing execution of process 2!\n");
kill(getpid(), SIGCONT);
}
}

void h_sig3(int signo)
{
message msg;

msg.type = P1;
msg.sigNum = signo;

kill(P1, SIGINT);

// send type of receiving signal to message queue
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

msg.type = P2;
kill(P2, SIGINT);
msgsnd(queue_ID, &msg, sizeof(msg.sigNum), 0);

if(signo == SIGUSR1)
{
printf("\nProcess 3 received signal S1\n");
printf("Terminating parent process!\n");
kill(getppid(), SIGKILL);
printf("Terminating process 3!\n");
kill(getpid(), SIGKILL);
}

if(signo == SIGTSTP)
{
printf("\nProcess 3 received signal S2\n");
printf("Pausing process 3!\n");
kill(getpid(), SIGSTOP);
}

if(signo == SIGALRM)
{
printf("\nProcess 3 received signal S3\n");
printf("Renewing execution of process 3!\n");
kill(getpid(), SIGCONT);
}
}

/*******************************************************************************************************/
/****************************************Handling S4 signal***********************************/
/*******************************************************************************************************/

void h_S4(int signo)
{
int res;
message msg;

printf("\nProcess with PID=%d received signal S4", getpid());

if(signo == SIGINT)
{
res = msgrcv(queue_ID, &msg, sizeof(msg.sigNum), msg.type, 0);

if(res >= 0)
{
if(msg.sigNum == SIGUSR1)
{
printf("Terminating process\n");
kill(getpid(),SIGKILL);

}
if(msg.sigNum == SIGTSTP)
{
printf("Pausing process\n");
kill(getpid(),SIGSTOP);

}
if(msg.sigNum == SIGALRM)
{
printf("Renewing process\n");
kill(getpid(),SIGCONT);

}
}
}
}

bool isInteger(double val)
{
int truncated = (int)val;
return (val == truncated);
}

最佳答案

源代码分析
存在一致性问题:

static pid_t P1, P2, P3; // PIDs for each process

int P; // parent PID

为什么是 Pint而不是 pid_t?为什么它是全局的而不是 static?为什么是 P而不是 P0?整批应该是一个数组吗?
static pid_t P[4];

(使用数组会有好处!)
在多次调用的函数中应该有重复:
// Sending PIDs to process 1
close(providePIDY1[0]);
write(providePIDY1[1], &P, sizeof(int));
write(providePIDY1[1], &P1, sizeof(int));
write(providePIDY1[1], &P2, sizeof(int));
write(providePIDY1[1], &P3, sizeof(int));
close(providePIDY1[1]);

// Sending PIDs to process 2
close(providePIDY2[0]);
write(providePIDY2[1], &P, sizeof(int));
write(providePIDY2[1], &P1, sizeof(int));
write(providePIDY2[1], &P2, sizeof(int));
write(providePIDY2[1], &P3, sizeof(int));
close(providePIDY2[1]);

注意,也有重复,因为 P值不是数组。还有一个可能的可移植性责任; pid_t不必与 int大小相同。
检查输入有问题:
choice = getchar();

if(choice == 's')

因为 choice是一个 char,所以如果您费心测试eof,您可能会得到错误的处理。您还可以在输入中保留一个换行符(至少),并且不要跳过输入中的前导空格。你最好读一行数据( fgets()或posix
readline())然后使用 if (sscanf(buffer, " %c", &choice) != 1) { …handle error… }获取字符。
您的下一个输入块很好奇:
printf("Which process is receiving the signal - 1, 2, or 3?: ");
choice2 = getchar();
choice2 = getchar();
printf("\n");

if((choice2 < 1) && (choice2 > 3))

第一个输入读取换行符(假设没有尾随空格等),第二个得到 '1''2''3'。但是,您要测试输入值是否同时小于1和大于3,并且在两个条件都为真的宇宙中没有已知值( NaN值是未知值)。你真的想要这样的东西:
if (choice2 < '1' || choice2 > '3')

在您确定要发送哪个信号(或多或少)之后,您将有另一个重复的代码块,因为您使用了 P1etc而不是数组 P
子进程中有大量重复的代码,例如读取进程号的代码。这些也应该在函数中。信号处理设置可能也应该在一个函数中,尽管我没有花太多时间检查不同处理函数之间的差异和相似性。
重大问题
你说代码应该处理算术表达式。您有关于信号处理的主程序循环读取选项,但似乎也有 process1()尝试读取表达式。这是个坏消息;不确定哪个进程能够读取任何给定的输入。
回到小事情上来
你有:
dataSize = strlen(buff) + 1; // plus NULL
if(dataSize > 0)
write(pfd12[1], &dataSize, sizeof(int));
write(pfd12[1], &buff, sizeof(char)*dataSize);

测试有点无意义, strlen()可以返回的最小值是 0,因此 dataSize中的最小值是 1,因此条件将始终为真。(理论上,我想,您可以输入太多的数据,以至于 size_t返回的 strlen()溢出了 int dataSize,但是您没有分配足够的空间来解决实际的问题—您的代码在此之前会有其他问题。)
process2()中,这段代码很奇怪:
    token = strtok(buff, delim);
while( token != NULL )
{
number = strtol(token, &end, 0);
if(!isInteger(number))
break;
}

使用 int number;扫描字符串时,在任何情况下 strtol()都不会是非整数。您有溢出的风险(例如,在64位unix系统上, sizeof(int) != sizeof(long))。有可能无法解释浮点值的剩余部分(因为 .不是整数的有效部分)。你需要重新编写代码。
信号处理代码中有很多重复;应该对其进行重构,以便您需要更少的函数。从长远来看,这会更容易理解。当结果接近单个程序中代码的克隆时,copy'n'paste'n'edit是一种非常糟糕的编程方法。
我不清楚你展示的两个版本之间有什么不同,我没有仔细研究过。您应该看看如何创建一个mcve( How to create a Minimal, Complete, and Verifiable Example?)或sscce( Short, Self-Contained, Correct Example)—两个名称和链接,用于相同的基本思想。我不确定这两个版本的代码是否都符合mcve的要求;这两个版本都过分了。只需提供可编译的代码。
编译后
我已经在Mac上编译了第二段代码(保存在名为 procsync.c的文件中),运行MacOSX10.10.3和GCC5.1.0,并使用命令行:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror procsync.c -o procsync
$

令我相当惊讶的是,在这些非常严格的选项下编译的代码没有任何抱怨——这是我很少在代码中看到的。
祝贺你!
(但还有其他问题需要担心。)

关于c - 处理与消息队列和信号的同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30824374/

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