【发布时间】:2017-05-07 22:23:30
【问题描述】:
您好,我想用 C 语言程序解决这个问题。
"编写一个 C 程序,其中一个进程 F 创建一个子进程 C。 子进程 C 等待用户输入密码,如果正确则向父进程发送信号 SIGUSR1,如果 3 次尝试后密码仍然不正确,则向父进程发送 SIGUSR2 信号并终止;如果它收到来自父亲的 SIGUSR1 信号,则必须停止查看“超时”消息。
他的父亲在 30 秒后(如果它没有收到孩子的任何信号)必须向孩子发送信号 SIGUSR1 并以 exit(1) 结束;如果它接收到 SIGUSR1 信号必须以 exit(0) 结束;如果它收到信号 SIGUSR2 必须以 exit (2) 结束。"
我正在尝试解决它,但我卡住了。这就是我所做的:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
void fatherprocess(int mysignal){
if (mysignal == SIGUSR1) {
printf("ACCESS GRANTED!\n");
exit(0);
}
if (mysignal == SIGUSR2){
printf("ACCESS DISCARDED! More than 3 tentatives!\n");
exit(2);
}
}
void childprocess(int mysignal){
if (mysignal == SIGUSR1) {
printf("TIMEOUT\n");
exit(1);
}
}
int main(int argc, char *argcv[]){
int fatherpid, childpid;
char enteredpassword[], password[] = "test";
int i =0;
unsigned int time_to_sleep = 30;
fatherpid = getpid();
childpid = fork();
if (childpid == 0) {
printf("Child Process waiting for a password\n");
while (1){
if (i < 3) {
printf("Enter Password: ");
scanf("%s", enteredpassword);
if (enteredpassword == password)
signal(SIGUSR1, fatherprocess);
} else {
signal(SIGUSR2, fatherprocess);
exit(1);
}
i++;
}
} else {
printf("Father Process\n");
while(time_to_sleep){
time_to_sleep = sleep(time_to_sleep);
signal(SIGUSR1, childprocess);
}
}
return 0;
}
我以这种方式编辑了我的程序:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
void fatherprocess(int mysignal, int fatherpid){
if (mysignal == SIGUSR1) {
printf("ACCESS GRANTED!\n");
kill(fatherpid, SIGUSR1);
exit(0);
}
if (mysignal == SIGUSR2){
printf("ACCESS DISCARDED! More than 3 tentatives!\n");
kill(fatherpid, SIGUSR2);
exit(2);
}
}
void childprocess(int mysignal, int childpid){
if (mysignal == SIGUSR1) {
printf("TIMEOUT\n");
kill(childpid, SIGUSR1);
exit(1);
}
}
int main(int argc, char *argcv[]){
int fatherpid, childpid;
char enteredpassword[] = "test", password[] = "test";
int i =0;
unsigned int time_to_sleep = 30;
fatherpid = getpid();
childpid = fork();
if (childpid == 0) {
printf("Child Process waiting for a password\n");
while (1){
if (i < 3) {
printf("Enter Password: ");
scanf("%s", enteredpassword);
if (strcmp(enteredpassword, password) == 0)
fatherprocess(SIGUSR1, fatherpid);
} else {
fatherprocess(SIGUSR2, fatherpid);
exit(1);
}
i++;
}
} else {
printf("Father Process\n");
while(time_to_sleep){
time_to_sleep = sleep(time_to_sleep);
childprocess(SIGUSR1, childpid);
}
}
return 0;
}
现在效果很好,但我不知道我是否尊重练习文本。
【问题讨论】:
-
这是完整的代码吗?你还没有安装信号处理程序。
-
它不完整,因为我不知道如何设置信号处理程序:(你能帮我吗?
-
我需要使用 kill 系统调用而不是信号吗?
-
是的,你需要使用
kill()系统调用。您还需要使用sigaction()(首选)或signal()(不太首选)来处理信号。由于您没有调用sigaction()或signal(),因此您的代码显然不正确。 -
如您所见,我尝试在第一个代码块中调用 signal(),但似乎它从未调用过那些处理程序,我不知道为什么。因此,唯一有效的策略是在第二个区块中,但正如你所说,它并不完全正确。
标签: c signals fork system-calls