程序 在函数中未退出 红色的地方未执行。
本帖最后由 liyiqi 于 2021-11-14 09:41 编辑#include <unistd.h>
#include <iostream>
#include <string>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
pid_t childPid_1;
pid_t childPid_2;
int i = 1;
bool j = true;
/*处理信号函数*/
void cap(int signum);
void process_cap1 (int signum);
void process_cap2 (int signum);
int main(int argc,char *arg[])
{
int pfd;
char buf;
string send1 = "I send you ";
string send2 = " time";
string send;
/*创建匿名管道*/
if(pipe(pfd) == -1){
cout << "创建失败" << endl;
} else {
cout << "创建管道成功" << endl;
}
childPid_1 = fork ();
if (childPid_1 < 0){
cout << "创建失败" << endl;
} else {
if (childPid_1 == 0){
//忽略SIGINT
signal (SIGINT,SIG_IGN);
cout << "创建子进程——1成功" ;
childPid_1 = getpid ();
cout << "ppid"
<< getppid()
<< "pid"
<< getpid()
<< endl;
while (i){
/*执行子进程-1操作**/
send = send1+to_string(i);
send += send2;
const char* s = send.c_str();
i++;
write(pfd,s,1024);
/* 每发送一次 休眠一秒*/
sleep(1);
//捕捉父进程的信号,退出循环
if (signal(SIGUSR1,process_cap1) == SIG_ERR){
cout << "error";
}
}
cout << "Child Process 1 is killed by Parent" <<
endl;
exit(0);
} else {
//sleep(5);
/*执行父进程创建子进程-2操作*/
childPid_2 = fork();
if (childPid_2 < 0){
cout <<"创建失败" << endl;
} else if (childPid_2 == 0){
//忽略SIGINT
signal (SIGINT,SIG_IGN);
cout << "创建子进程——2成功" ;
childPid_2 = getpid();
cout << "ppid"
<< getppid()
<< "pid"
<< getpid()
<< endl;
/*执行子进程-2操作**/
while (j){
read(pfd,buf,1024);
cout << buf << endl;
//捕捉父进程信号
if (signal (SIGUSR2,process_cap2) == SIG_ERR){
cout << "error";
}
if (j == false){
break;
}
}
cout << "Child Process 2 is killed by Parent!" <<endl;
exit (0);
} else {
/*执行来自用户的操作 ctrl+c */
signal (SIGINT,cap);
while (true);
}
}
}
}
void cap (int signum)
{
cout << getpid()
<< "进程"
<< "捕捉到SIGINT信号"
<< endl;
kill (childPid_1,SIGUSR1);
waitpid (childPid_1,NULL,0);
kill (childPid_2,SIGUSR2);
waitpid (childPid_2,NULL,0);
cout << "Parent Process is killed";
signal(SIGINT,SIG_DFL);
}
void process_cap1 (int signum){
cout << getpid()
<< "进程"
<< "捕捉到SIGUSR1信号"
<< endl;
//将 i 设置为负数 退出while
i = -1;
}
void process_cap2 (int signum){
cout << j;
j = false;
cout << getpid()
<< "进程"
<< "捕捉到SIGUSR2信号"
<< endl;
cout << j;
return ;
} 是最后一个函数的后两条语句
没有标上,不好意思
重新发一下代码,用代码格式发,像这样
你要标注代码的话,用图片的形式标
人造人 发表于 2021-11-13 23:43
不太会用这个论坛
liyiqi 发表于 2021-11-13 23:56
不太会用这个论坛
本帖最后由 liyiqi 于 2021-11-14 00:03 编辑
#include <unistd.h>
#include <iostream>
#include <string>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
pid_t childPid_1;
pid_t childPid_2;
int i = 1;
bool j = true;
/*处理信号函数*/
void cap(int signum);
void process_cap1 (int signum);
void process_cap2 (int signum);
int main(int argc,char *arg[])
{
int pfd;
char buf;
string send1 = "I send you ";
string send2 = " time";
string send;
/*创建匿名管道*/
if(pipe(pfd) == -1){
cout << "创建失败" << endl;
} else {
cout << "创建管道成功" << endl;
}
childPid_1 = fork ();
if (childPid_1 < 0){
cout << "创建失败" << endl;
} else {
if (childPid_1 == 0){
//忽略SIGINT
signal (SIGINT,SIG_IGN);
cout << "创建子进程——1成功" ;
childPid_1 = getpid ();
cout << "ppid"
<< getppid()
<< "pid"
<< getpid()
<< endl;
while (i){
/*执行子进程-1操作**/
send = send1+to_string(i);
send += send2;
const char* s = send.c_str();
i++;
write(pfd,s,1024);
/* 每发送一次 休眠一秒*/
sleep(1);
//捕捉父进程的信号,退出循环
if (signal(SIGUSR1,process_cap1) == SIG_ERR){
cout << "error";
}
}
cout << "Child Process 1 is killed by Parent" <<
endl;
exit(0);
} else {
//sleep(5);
/*执行父进程创建子进程-2操作*/
childPid_2 = fork();
if (childPid_2 < 0){
cout <<"创建失败" << endl;
} else if (childPid_2 == 0){
//忽略SIGINT
signal (SIGINT,SIG_IGN);
cout << "创建子进程——2成功" ;
childPid_2 = getpid();
cout << "ppid"
<< getppid()
<< "pid"
<< getpid()
<< endl;
/*执行子进程-2操作**/
while (j){
read(pfd,buf,1024);
cout << buf << endl;
//捕捉父进程信号
if (signal (SIGUSR2,process_cap2) == SIG_ERR){
cout << "error";
}
if (j == false){
break;
}
}
cout << "Child Process 2 is killed by Parent!" <<endl;
exit (0);
} else {
/*执行来自用户的操作 ctrl+c */
signal (SIGINT,cap);
while (true);
}
}
}
}
void cap (int signum)
{
cout << getpid()
<< "进程"
<< "捕捉到SIGINT信号"
<< endl;
kill (childPid_1,SIGUSR1);
waitpid (childPid_1,NULL,0);
kill (childPid_2,SIGUSR2);
waitpid (childPid_2,NULL,0);
cout << "Parent Process is killed";
signal(SIGINT,SIG_DFL);
}
void process_cap1 (int signum){
cout << getpid()
<< "进程"
<< "捕捉到SIGUSR1信号"
<< endl;
//将 i 设置为负数 退出while
i = -1;
}
void process_cap2 (int signum){
cout << j;
j = false;
cout << getpid()
<< "进程"
<< "捕捉到SIGUSR2信号"
<< endl;
cout << j;
return ;
} 帮顶 {:10_254:} 回复别人要点 "回复" 按钮,不然别人是收不到通知的,除非别人主动点进来。
#include <iostream>
#include <signal.h>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
pid_t childPid_1;
pid_t childPid_2;
int i = 1;
bool j = true;
/*处理信号函数*/
void cap(int signum);
void process_cap1(int signum);
void process_cap2(int signum);
int main(int argc, char *arg[]) {
int pfd;
char buf;
string send1 = "I send you ";
string send2 = " time";
string send;
/*创建匿名管道*/
if(pipe(pfd) == -1) {
cout << "创建失败" << endl;
} else {
cout << "创建管道成功" << endl;
}
childPid_1 = fork();
if(childPid_1 < 0) {
cout << "创建失败" << endl;
} else {
if(childPid_1 == 0) {
//忽略SIGINT
signal(SIGINT, SIG_IGN);
// //捕捉父进程的信号,退出循环
//捕捉父进程的信号
if(signal(SIGUSR1, process_cap1) == SIG_ERR) {
cout << "error";
}
cout << "创建子进程——1成功";
childPid_1 = getpid();
cout << "ppid" << getppid() << "pid" << getpid() << endl;
while(i) {
/* 每发送一次 休眠一秒*/
sleep(1);
/*执行子进程-1操作**/
send = send1 + to_string(i);
send += send2;
//const char *s = send.c_str();
i++;
//write(pfd, s, 1024);
write(pfd, send.c_str(), send.size());
/* 每发送一次 休眠一秒*/
//sleep(1);
}
cout << "Child Process 1 is killed by Parent" << endl;
exit(0);
} else {
// sleep(5);
/*执行父进程创建子进程-2操作*/
childPid_2 = fork();
if(childPid_2 < 0) {
cout << "创建失败" << endl;
} else if(childPid_2 == 0) {
//忽略SIGINT
signal(SIGINT, SIG_IGN);
//捕捉父进程信号
if(signal(SIGUSR2, process_cap2) == SIG_ERR) {
cout << "error";
}
cout << "创建子进程——2成功";
childPid_2 = getpid();
cout << "ppid" << getppid() << "pid" << getpid()
<< endl;
/*执行子进程-2操作**/
while(j) {
/*
read(pfd, buf, 1024);
cout << buf << endl;
*/
size_t nbytes = read(pfd, buf, 1024);
cout << std::string(buf, buf + nbytes) << endl;
sleep(1);
if(j == false) {
break;
}
}
cout << "Child Process 2 is killed by Parent!" << endl;
exit(0);
} else {
/*执行来自用户的操作 ctrl+c */
signal(SIGINT, cap);
while(true)
;
}
}
}
}
void cap(int signum) {
cout << getpid() << "进程"
<< "捕捉到SIGINT信号" << endl;
kill(childPid_1, SIGUSR1);
waitpid(childPid_1, NULL, 0);
kill(childPid_2, SIGUSR2);
waitpid(childPid_2, NULL, 0);
cout << "Parent Process is killed";
signal(SIGINT, SIG_DFL);
}
void process_cap1(int signum) {
cout << getpid() << "进程"
<< "捕捉到SIGUSR1信号" << endl;
//将 i 设置为负数 退出while
i = -1;
}
void process_cap2(int signum) {
cout << j;
j = false;
cout << getpid() << "进程"
<< "捕捉到SIGUSR2信号" << endl;
cout << j;
return;
}
{:10_254:} 路过 {:10_277:}
页:
[1]