簡體   English   中英

通過另一個程序讀取文件並執行操作,並使用系統調用將 output 保存在單獨的文件中

[英]read a file and perform operation through another program and save the output in a separate file using system calls

  //  This is my code for reading a file from command line arguments and storing it in another file.//
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <sys/types.h>
    #include <fcntl.h>
    
    #include <unistd.h> //for system calls such as dup(),pipe(),etc...
    
    #include <sys/wait.h>
    
    #define COUNT_PROGRAM "b"
    #define CONVERT_PROGRAM "c"
    
    int main(int argc, char *argv[])
    {
        if (argc != 3)
        {
            fprintf(stderr,"%s","ERROR : argument count not satisfied!\n");
            exit(1);
        }
    
        /* It is important to check all system calls (open, creat, dup, etc.) for a return value < 0, 
        particularly -1 because such a return value means an error has occurred. */
    
        int fd_in = open(argv[1], O_RDONLY);
        if (fd_in < 0) 
        {
            fprintf(stderr,"%s", "ERROR : file to be read does not exist!\n");
            exit(1);
        }
        
     int fd_out = creat(argv[2], 0644); /* mode = permissions, here rw-r--r-- */
        if (fd_out < 0)
        {
            fprintf(stderr,"%s", "ERROR : file could not be created!\n");
            exit(1);
        }
        
        
        if(dup(fd_in) < 0)//dup fd_in to 3
            fprintf(stderr , "ERROR assigning STREAM 3 to fd_in");     
        if(dup(fd_out) < 0)//dup fd_in to 4
            fprintf(stderr , "ERROR assigning STREAM 4 to fd_out");
    
        //dup fd_in to 0
        close(0);
        dup(fd_in);
        close(3);
    
        //dup fd_out to 1
        close(1);
        dup(fd_out);
        close(4);
    
        int fd[2];
    
        pipe(fd);   
    
        pid_t pid_child_1, pid_child_2;
        int status;
    
        if ((pid_child_1 = fork()) != 0)
        {      
            if ((pid_child_2 = fork()) != 0) 
            { 
                close(fd[0]);         
                close(fd[1]);  
                wait(&status);
                wait(&status);          
                // fprintf(stderr , "\nstatus of child_1 = %d",wait(&status));           
                // fprintf(stderr , "\nstatus of child_2 = %d",wait(&status));       
            } 
            else 
            {
                // close(fd[1]);
                // dup(1);       
                dup2(fd[1],1);  
                close(fd[0]);
                execl( CONVERT_PROGRAM, CONVERT_PROGRAM, (char*) NULL);
            }    
        } 
            
        else 
        {
            // close(fd[0]);
            // dup(0);
            dup2(fd[0],0);
            close(fd[1]);
            execl( COUNT_PROGRAM , COUNT_PROGRAM ,(char*) NULL);  
        }
    
    
    }

編譯應該包含 output 的文本文件后為空。 這些程序單獨運行良好。

這里我在運行 strace 命令后添加 strace 結果。 這里它在屏幕上打印 7 而文本文件中沒有 output。

  read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\35\2\0\0\0\0\0"..., 8                                                                             32) = 832
strace: Process 6018 attached
strace: Process 6019 attached
[pid  6019] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\35\2\0\0\                                                                             0\0\0"..., 832) = 832
[pid  6018] read(4, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\35\2\0\0\                                                                             0\0\0"..., 832) = 832
[pid  6019] read(0, "hello my name is himanshu KAUSHI"..., 4096) = 35
[pid  6019] read(0, "", 4096)           = 0
[pid  6019] write(1, "HELLO MY NAME IS HIMANSHU kaushi"..., 35) = 35
[pid  6019] +++ exited with 0 +++
[pid  6018] read(0, "HELLO MY NAME IS HIMANSHU kaushi"..., 4096) = 35
[pid  6017] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=6019, si_u                                                                             id=1062, si_status=0, si_utime=0, si_stime=0} ---
[pid  6018] read(0, "", 4096)           = 0
[pid  6018] write(2, "\n7", 2
7)          = 2
[pid  6018] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=6018, si_uid=1062, si_                                                                             status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

我在第一個 dup2(f_dsc[1],3) 命令中更改了標准表描述符,並且在文本文件中得到了 output,但隨后我的第一個程序停止運行。

#include<stdio.h>
#include<ctype.h>

int main()
{
        char c;
        int count=0;

                while(1)
                {
                        c=getchar();
                        if(c==EOF)
                        {
                                break;
                        }
                        if(!isalpha(c))
                        {
                                count++;
                        }
                }
                fprintf(stderr,"\n%d",count);


}

這是我的簡單 b 程序。

#include<stdio.h>
#include<ctype.h>
int main()
{
        char c;
        int count=0;
        while(1)
        {
                c=getchar();
                if(c==EOF)
                {
                        break;
                }
                if(islower(c))
                {
                        c=toupper(c);
                }
                else
                {
                        c=tolower(c);
                }

                putchar(c);
        }
        return 0;
}

這是我的簡單 c 程序。

當我嘗試您的程序時,它的正確執行等效於

c <a.txt | b >b.txt

因此,讓我們考慮一下您的設置可能有什么不同。 雖然你寫

    /* It is important to check all system calls (open, creat, dup, etc.) for a return value < 0, 
    particularly -1, because such a return value means an error has occurred. */

您不檢查execl調用的返回值(或者只是將perror("…");放在它們之后以查看它們是否失敗)。 也許bc是一個沒有第一行的腳本

#!/bin/sh

shell調用腳本時,您可以不用這樣一行(我猜您的意思是當您大喊這些程序單獨運行時),但在使用execl時則不行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM