C Unix-从子块排序

C Unix-从子块排序,c,linux,pipe,system-calls,C,Linux,Pipe,System Calls,当我试图完成一个练习时,我发现了一些我无法理解的问题。 第一个: 我正在尝试使用system(字符串)对文件进行排序,其中字符串包含:/usr/bin/sort-n-o%s%s%s是我通过管道从父级读取的路径,打印字符串时我看到它是正确的。如果我删除-o%s,一切似乎都正常。如果我加上它,程序在排序上会被阻塞。 代码如下: str=strcat(dir, str); printf(" %s \n", str); sprintf(ssys, "/usr/bin/sort -n -

当我试图完成一个练习时,我发现了一些我无法理解的问题。 第一个: 我正在尝试使用system(字符串)对文件进行排序,其中字符串包含:/usr/bin/sort-n-o%s%s%s是我通过管道从父级读取的路径,打印字符串时我看到它是正确的。如果我删除-o%s,一切似乎都正常。如果我加上它,程序在排序上会被阻塞。 代码如下:

str=strcat(dir, str);
printf("    %s       \n", str);
sprintf(ssys, "/usr/bin/sort -n -o %s %s", str, str);
printf("     1111    \n");
system(ssys);
printf("     2222   \n");
输出为:

/home/dio/Desktop/operation System PoliTo/Lab6/ex2/text/1.txt

1111

正如您所看到的,它没有到达第三个printf,但就在我按下CTRL+c终止进程时,即使文件没有排序。我不明白为什么,因为如果我运行它并用ps检查,我会运行排序进程。它似乎在等待父进程完成,但它无论如何都不会排序

这是全部代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>

static void sig_handler (int);

static void sig_handler (int signo)
{
        if (signo == SIGPIPE)
                exit(0);
}

int main (int argc, char **argv)
{

        signal(SIGPIPE, sig_handler);

        if (argc<3)
        {
                printf("Too little arguments");
                return 1;
        }

        int c=atoi(argv[1]);
        int *pids=malloc(sizeof(int)*c);
        int i=0, num_char_str, id, line_sort, request_pipe[c][2], data_pipe[c][2], answer_pipe[c][2];
        pid_t pid;
        char cR, cW;
        char *dir=malloc(strlen(argv[2])+1);
        char *str=malloc(strlen(argv[2])*10);
        char *ssys=malloc(strlen(argv[2])*10);
        FILE* fpp;
        FILE* fp;

        strcpy(dir, argv[2]);
        sprintf(str, "ls %s > list.txt",  dir);
        system(str);
        for(i=0; i<c; i++)
        {
                if (pipe(request_pipe[i]) == -1)
                        return 2;
                if (pipe(data_pipe[i]) == -1)
                        return 2;
                if (pipe(answer_pipe[i]) == -1)
                        return 2;
        }
        for(i=0; i<c; i++)
        {
                pid=fork();
                if(pid>0)
                        pids[i]=pid;
                if (pid == -1)
                {
                        printf("error forking");
                        return 3;
                }
                if (pid==0)
                        break;
        }
    if (pid>0) /// parent
        {
                for(i=0; i<c; i++)
                {
                        close(answer_pipe[i][1]);  /// close answer pipe on writing
                        close(request_pipe[i][1]);  /// close request pipe on writing
                        close(data_pipe[i][0]);  /// close data pipe on reading
                }
                fp=fopen("list.txt", "r");
                if (fp==NULL)
                        return 3;
                i=0;
                while (fgets(str, sizeof(str), fp)!=NULL)
                {
                        /// wait for ask
                        read(request_pipe[i][0], &cR, 1);
                        /// sending first the number of characters to read
                        /// and then the file name
                        num_char_str=(strlen(str) +1);
                        write(data_pipe[i][1], &num_char_str, sizeof(int));
                        write(data_pipe[i][1], str, num_char_str);
                        /// read the answer
                        read(answer_pipe[i][0], &id, sizeof(int));
                        //pids[i]=id;
                        read(answer_pipe[i][0], &line_sort, sizeof(int));
                        if (i==c-1)
                                i=-1;
                        i++;
                }
                /// sending termination signal to children
                for (i=0; i<c; i++)
                        kill(pids[i], SIGPIPE);
                /// producing all_sorted.txt
                strcpy(str, "touch all_sorted.txt");
                system(str);
                sprintf(str, "find %s -name \"*.txt\" -exec cat \"{}\"\";\" > all_sorted.txt", dir);
                system(str);
                /// sorting all_sorted

        }
        if (pid == 0) /// child
        {
                close(answer_pipe[i][0]);  /// close answer pipe on reading
                close(request_pipe[i][0]);  /// close request pipe on reading
                close(data_pipe[i][1]);  /// close data pipe on writing
                while (1)
                {
                        /// send request to parent
                        write(request_pipe[i][1], &cW, 1);
                        /// read length of string
                        read(data_pipe[i][0], &num_char_str, sizeof(int));
                        /// read the file name
                        read(data_pipe[i][0], str, num_char_str);
                        /// sorting
                        str=strcat(dir, str);
                        sprintf(ssys, "/usr/bin/sort -n -o %s %s", str, str);
printf("     1111    \n");
                        system(ssys);
printf("     2222   \n");
                        /// sending back data
                        id=getpid(); 
                        write(answer_pipe[i][1], &id, sizeof(int));
                        //sprintf(str, "wc -l %s/%s", dir, str);
                        //fpp=popen(str, "r");
                        //fscanf(fpp, "%d", &line_sort);
                        //pclose(fpp);
                        line_sort=1;
                        write(answer_pipe[i][1], &line_sort, sizeof(int));
                }
        }

        return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
静态无效信号处理器(int);
静态无效信号处理器(int signo)
{
if(signo==SIGPIPE)
出口(0);
}
int main(int argc,字符**argv)
{
信号(信号管、信号处理器);

如果(argc谢谢,我不知道
strace

顺便说一句,问题似乎是fgets读取的“\n”并给排序调用带来了一些问题

谢谢,我不知道
strace

顺便说一句,问题似乎是“\n”fgets读取并给排序调用带来一些问题

sort
可能正在等待EOF,或者正在通过管道传送到其中的流关闭…您应该检查您的
sort
似乎挂起的文件。它是一个常规文件吗?它很大吗?尝试在挂起过程中调用
strace-fp
,然后查看哪个调用它被卡住了。
sort
可能正在等待EOF,或者正在通过管道传送到其中的流被关闭……您应该检查您的
sort
似乎挂起的文件。它是常规文件吗?它很大吗?尝试在挂起过程中调用
strace-fp
,然后查看它挂起的调用。