我有一个项目需要我使用信号和叉来读取文件的内容并将其写回标准输出(低级 I/O)。我应该在父进程和子进程之间拆分读写工作,我应该先启动父进程来读取和写入文件的内容。父进程完成后,我应该向子进程发送信号 SIGUSR1,以便它可以开始读取和写入下一个数据块。我的读写部分运行正常,但信号和处理部分有问题。当我运行我的程序时,它会显示正确的输出,但它永远不会终止。有人可以检查我的代码有什么问题吗?
这是我到目前为止得到的:
#define _XOPEN_SOURCE 600
#include <limits.h> /* For LONG_MAX */
#include <fcntl.h> /*For open()*/
#include <unistd.h> /*For close(), read(), write(), nice()*/
#include <stdlib.h> /*For exit()*/
#include <stdio.h> /*For fprintf(),perror()*/
#include <signal.h> /*For signal()*/
#include <sys/types.h> /* For pid_t */
#include <sys/wait.h> /* For wait() */
void my_handler(int);
void displayUsage(FILE *, const char *);
int main(int argc, char **argv) {
char c[BUFSIZ]; /* To hold the string that has been read from the file */
int fd; /* file descriptor */
int n;
long int nBytes; /* To hold the number of bytes to read */
pid_t pid;
int child_status;
/* Display usage if user use this process wrong */
if(argc != 3){
displayUsage(stderr, argv[0]);
exit(1);
}
/* Convert argv[1] to long - number of bytes to read */
nBytes = strtol(argv[1], NULL, 10);
if((nBytes <= 0) || (nBytes > INT_MAX)){
fprintf(stderr, "Bufferize %s is not a positive integer\n",
argv[1]);
exit(2);
}
/* Open a file */
if((fd = open(argv[2],O_RDONLY,0)) < 0){
perror(argv[2]);
exit(3);
}
/* Attempt to register a signal handler */
if(signal(SIGUSR1, my_handler) == SIG_ERR){
perror("Could not set a handler for SIGUSR1");
exit(4);
}
/* lowering the process priority */
if(nice(40) < 0){
perror("Cannot lower the priority");
exit(5);
}
/* Create a new process */
pid = fork();
if(pid < 0){
exit(6);
}
/* Allocating memory space for string/data to be read from the file
if(!(c = malloc(nBytes))){
perror("ERROR");
exit(4);
}*/
if(pid == 0){
pause();
/* I'm the child */
printf("Child's turn!\n");
/* Read The File */
while((n = read(fd, c, nBytes)) != 0){
/* Write content of 'c' to the stdout */
if(write(STDOUT_FILENO, c, n) < 0){
perror("Write Error!\n");
exit(7);
}
}
kill(pid,SIGUSR1);
}
else{
/* I'm the parent */
printf("Parent's turn!\n");
/* Read The File */
while((n = read(fd, c, nBytes)) != 0){
/* Write content of 'c' to the stdout */
if(write(STDOUT_FILENO, c, n) < 0){
perror("Write Error!\n");
exit(7);
}
}
/* Reap the child */
wait(&child_status);
kill(pid, SIGUSR1);
}
pause();
/* Close the file */
if(close(fd) < 0){
perror(argv[2]);
exit(8);
}
exit(0);
}
void displayUsage(FILE *fp, const char *arg){
fprintf(fp, "Usage: %s n filename\n", arg);
fprintf(fp, "where\n");
fprintf(fp,
"\tn\tNumber of bytes to read and write in each \"block\".\n");
fprintf(fp,"\tfilename\tName of file to be catted.\n");
}
void my_handler(int sig){
/* Re-registering the handler */
if(signal(SIGUSR1, my_handler) == SIG_ERR){
perror("Could not set a handler for SIGUSR1");
exit(4);
}
/*if(isParent == 1){
printf("Parent's turn!\n");
}
else{
printf("Child's turn!\n");
}*/
}