Can';看不到程序';在c中使用fork和pipe后,在终端中输出

Can';看不到程序';在c中使用fork和pipe后,在终端中输出,c,process,pipe,signals,fork,C,Process,Pipe,Signals,Fork,我应该写一个程序,创建两个进程,用一个管道连接它们,在给定的时间后结束这两个进程并终止。 其中一个程序将写入管道,另一个程序将从中读取并打印到标准输出。 首先调用读取进程,然后将pid传递给第二个进程,以便它将SIGUSR1信号发送给第一个进程,以告诉它读取。 由于某些原因,我从未在第一个进程的终端看到输出, 此外,它甚至不打印行:“尝试执行1\n”,这是我为打印的进程调用“execlp”的地方。 以下是3个程序的代码: 主要节目: #define STDERR 2 #define STDOUT

我应该写一个程序,创建两个进程,用一个管道连接它们,在给定的时间后结束这两个进程并终止。 其中一个程序将写入管道,另一个程序将从中读取并打印到标准输出。 首先调用读取进程,然后将pid传递给第二个进程,以便它将SIGUSR1信号发送给第一个进程,以告诉它读取。 由于某些原因,我从未在第一个进程的终端看到输出, 此外,它甚至不打印行:“尝试执行1\n”,这是我为打印的进程调用“execlp”的地方。 以下是3个程序的代码:

主要节目:

#define STDERR 2
#define STDOUT 1
#define STDIN 0
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
void alarmHandler(int sig);
void systemError();

char * intToString(int num , char number[4]);

static pid_t processId1, processId2;

int main(int argc, char ** argv){
    pid_t pid1, pid2;
    sigset_t block_mask1;
    struct sigaction exitSig;
    sigfillset(&block_mask1);
    exitSig.sa_handler = alarmHandler;
    exitSig.sa_mask = block_mask1;
    exitSig.sa_flags  = 0;
    sigaction(SIGALRM, &exitSig, NULL);
    if (argc < 2){
            systemError();
    } else {
        int x = atoi(argv[1]);
        alarm(x);
    }
    int fields[2];
    if (pipe(fields)){
        systemError();
    }

    if ((pid1 = fork()) == 0){
        printf("trying to exec1\n");
        close(STDIN);
        dup(fields[0]);
        close(fields[0]);
        close(fields[1]);
        if(execlp("./ex2_inp", "./ex2_inp", NULL)){
            systemError();
        }
    } else {
        processId1 = pid1;
        if ((pid2 = fork()) == 0){
            char number[350];
            printf("trying to exec2\n");
            close(STDOUT);
            dup(fields[1]);
            close(fields[0]);
            close(fields[1]);
            char * pidString = intToString(processId1, number);
            if(execlp("./ex2_upd","./ex2_upd",pidString, NULL)){
                systemError();
            }
        } else{
            processId2 = pid2;
        }
    }

    close(fields[0]);
    close(fields[1]);
    pause();
    return 1;
}

/***********************
 * handler for alarm signal
 *************************/
void alarmHandler(int sig){
    kill(processId2, SIGINT);
    kill(processId1, SIGINT);
    exit(1);
}

/***********************
 * turn pid to string
 *************************/
char * intToString(int num , char number[350]){
    sprintf(number, "%d", num);
    return number;
}
#定义标准2
#定义标准输出1
#定义标准0
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效报警处理程序(int sig);
void systemError();
char*intToString(int num,char number[4]);
静态pid_t processId1、processId2;
int main(int argc,字符**argv){
pid_t pid1,pid2;
sigset_t block_mask1;
结构sigaction exitSig;
sigfillset(&block_mask1);
exitSig.sa_handler=alarmHandler;
exitSig.sa_mask=block_mask1;
exitSig.sa_标志=0;
sigaction(SIGALRM和exitSig,NULL);
如果(argc<2){
systemError();
}否则{
intx=atoi(argv[1]);
警报(x);
}
int字段[2];
if(管道(场)){
systemError();
}
if((pid1=fork())==0){
printf(“正在尝试执行1\n”);
关闭(标准输入);
dup(字段[0]);
关闭(字段[0]);
关闭(字段[1]);
if(execlp(“./ex2_inp”,”/ex2_inp,NULL)){
systemError();
}
}否则{
processId1=pid1;
if((pid2=fork())==0){
字符数[350];
printf(“尝试执行2\n”);
关闭(标准输出);
dup(字段[1]);
关闭(字段[0]);
关闭(字段[1]);
char*pidString=intToString(processId1,number);
if(execlp(“./ex2_-upd”,”/ex2_-upd),pidString,NULL)){
systemError();
}
}否则{
processId2=pid2;
}
}
关闭(字段[0]);
关闭(字段[1]);
暂停();
返回1;
}
/***********************
*报警信号处理机
*************************/
无效报警处理程序(int sig){
kill(processId2,SIGINT);
kill(processId1,SIGINT);
出口(1);
}
/***********************
*将pid设置为字符串
*************************/
char*intToString(int num,字符编号[350]){
sprintf(数字,“%d”,num);
返回号码;
}
ex2_inp:

#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
void exitHandler(int sig);
void printHandler(int sig);
int main(int argc, char * argv[]){

    sigset_t block_mask1, block_mask2;
    struct sigaction exitSig, print;
    sigfillset(&block_mask1);
    sigfillset(&block_mask2);
    exitSig.sa_handler = exitHandler;
    print.sa_handler = printHandler;
    print.sa_mask = block_mask2;
    exitSig.sa_mask = block_mask1;
    exitSig.sa_flags  = 0;
    print.sa_flags = 0;
    sigaction(SIGINT, &exitSig, NULL);
    sigaction(SIGUSR1, &print, NULL);
    pause();
    return 1;
}

void exitHandler(int sig){
    printf("exiting1!\n");
    close(1);
    exit(1);
}

void printHandler(int sig){
    char * buffer[80];
    read(1, buffer, 80);
    printf("%s", buffer);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效出口(int sig);
无效打印处理程序(int sig);
int main(int argc,char*argv[]){
sigset_t块_mask1,块_mask2;
结构SIGATION exitSig,打印;
sigfillset(&block_mask1);
sigfillset(&block_mask2);
exitSig.sa_handler=exitHandler;
print.sa_handler=printHandler;
print.sa_mask=block_mask2;
exitSig.sa_mask=block_mask1;
exitSig.sa_标志=0;
print.sa_flags=0;
sigation(SIGINT和exitSig,NULL);
sigaction(SIGUSR1,&print,NULL);
暂停();
返回1;
}
无效出入口(内部信号){
printf(“退出1!\n”);
关闭(1);
出口(1);
}
无效打印处理程序(int sig){
字符*缓冲区[80];
读取(1,缓冲器,80);
printf(“%s”,缓冲区);
}
ex2_upd:

#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
void exitHandler(int sig);

int main(int argc, char * argv[]){
    sigset_t block_mask1;
    struct sigaction exitSig;
    sigfillset(&block_mask1);
    exitSig.sa_handler = exitHandler;
    exitSig.sa_mask = block_mask1;
    exitSig.sa_flags  = 0;
    sigaction(SIGINT, &exitSig, NULL);
    printf("2's message\n");
    kill(atoi(argv[1]), SIGUSR1);
    pause();
    return 1;
}

void exitHandler(int sig){
    printf("exiting2!\n");
    close(0);
    exit(1);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效出口(int sig);
int main(int argc,char*argv[]){
sigset_t block_mask1;
结构sigaction exitSig;
sigfillset(&block_mask1);
exitSig.sa_handler=exitHandler;
exitSig.sa_mask=block_mask1;
exitSig.sa_标志=0;
sigation(SIGINT和exitSig,NULL);
printf(“2的消息\n”);
kill(atoi(argv[1]),SIGUSR1);
暂停();
返回1;
}
无效出入口(内部信号){
printf(“退出2!\n”);
关闭(0);
出口(1);
}
谢谢

ex2\u upd.c:

#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
void exitHandler(int sig);

int main(int argc, char * argv[]){
    sigset_t block_mask1;
    struct sigaction exitSig;
    sigfillset(&block_mask1);
    exitSig.sa_handler = exitHandler;
    exitSig.sa_mask = block_mask1;
    exitSig.sa_flags  = 0;
    sigaction(SIGINT, &exitSig, NULL);
    printf("2's message\n");
    kill(atoi(argv[1]), SIGUSR1);
    sleep(1); /* This was pause - causing ex2_inp read() to wait forever, since read() on pipe needs to either fill buffer or END_OF_FILE, unless we make the filedescriptor in the read-end non-blocking via fcntl()  */
    return 1;
}

void exitHandler(int sig){
    printf("exiting2!\n");
    close(0);
    exit(1);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效出口(int sig);
int main(int argc,char*argv[]){
sigset_t block_mask1;
结构sigaction exitSig;
sigfillset(&block_mask1);
exitSig.sa_handler=exitHandler;
exitSig.sa_mask=block_mask1;
exitSig.sa_标志=0;
sigation(SIGINT和exitSig,NULL);
printf(“2的消息\n”);
kill(atoi(argv[1]),SIGUSR1);
sleep(1);/*这是暂停-导致ex2_inp read()永远等待,因为管道上的read()需要填充缓冲区或_文件的END_,除非我们通过fcntl()使读取端的filedescriptor非阻塞*/
返回1;
}
无效出入口(内部信号){
printf(“退出2!\n”);
关闭(0);
出口(1);
}
ex2_inp.c:

#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
void exitHandler(int sig);
void printHandler(int sig);
int main(int argc, char * argv[]){

    sigset_t block_mask1, block_mask2;
    struct sigaction exitSig, print;
    sigfillset(&block_mask1);
    sigfillset(&block_mask2);
    exitSig.sa_handler = exitHandler;
    print.sa_handler = printHandler;
    print.sa_mask = block_mask2;
    exitSig.sa_mask = block_mask1;
    exitSig.sa_flags  = 0;
    print.sa_flags = 0;
    sigaction(SIGINT, &exitSig, NULL);
    sigaction(SIGUSR1, &print, NULL);
    pause();
    return 1;
}

void exitHandler(int sig){
    printf("exiting1!\n");
    close(1);
    exit(1);
}

void printHandler(int sig){
    char buffer[80]; /* removed * */
    read(0, buffer, 80);   /* stdin is fd=0, not 1 */
    printf("-> %s <-\n", buffer); /* added \n, forces new-line */
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效出口(int sig);
无效打印处理程序(int sig);
int main(int argc,char*argv[]){
sigset_t块_mask1,块_mask2;
结构SIGATION exitSig,打印;
sigfillset(&block_mask1);
sigfillset(&block_mask2);
exitSig.sa_handler=exitHandler;
print.sa_handler=printHandler;
print.sa_mask=block_mask2;
exitSig.sa_mask=block_mask1;
exitSig.sa_标志=0;
print.sa_flags=0;
sigation(SIGINT和exitSig,NULL);
sigaction(SIGUSR1,&print,NULL);
暂停();
返回1;
}
无效出入口(内部信号){
printf(“退出1!\n”);
关闭(1);
出口(1);
}
无效打印处理程序(int sig){
字符缓冲区[80];/*已删除**/
读取(0,缓冲区,80);/*stdin是fd=0,不是1*/

printf(“->%s尝试使用
fflush(stdout);
在调用
printf
之后和调用
close
之前使用