如何才能用C执行子进程 [英] How to execute child processes in order in C

查看:116
本文介绍了如何才能用C执行子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C code计算行字和字符在文件中。像厕所命令。在code低于和正常工作。

I have written a code in C to count lines words and characters in a file. Like the wc command. The code is below and works fine.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

char* concat(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);
strcpy(result, s1);
strcat(result, s2);
return result;
} 

int countLines(char *f){
 FILE *file = fopen(f, "r");
int count = 0;
char ch;
while ((ch = fgetc(file)) != EOF){
   if (ch == '\n')
       count++;
}
return count;
}

int countWords(char *f){
char buffer[1];
FILE *file = fopen(f, "r");
  int countW = 0;
  enum states { WSP, WRD };
  int state = WSP;
char last = ' '; 
 while (read(fileno(file),buffer,1) == 1 )
 {
 if ( buffer[0]== ' ' || buffer[0] == '\t' || buffer[0]=='\n' )
 {
    state = WSP;
 }
 else 
 {
    if ( state == WSP )
    {
       countW++;
    }
    state = WRD;
 }
  last = buffer[0];
}
return countW;
}

int countChars(char *f){
 FILE *file = fopen(f, "r");
int chars = 0;
char ch;
while ((ch = fgetc(file))){
     if (ch == EOF) break;
  chars++;
}
return chars;
}

int main(int argc, char** argv)
{
 int lineCount = 0;
 int wordCount = 0;
 int charCount = 0;
 int n = 3;
 int i,status;
 int pids[3];
 char *theprogram = argv[0];
 char *thefile = argv[1];
 if ( argc !=2 )
 {
     printf( "Help: %s filename\n", argv[0]);
 }
 else{
     FILE *file = fopen( argv[1], "r");

   if(file == 0){
         char *sub = concat(theprogram, ": ");
         char *middle = concat(sub, thefile); 
         perror(middle);
   }
   else{
         for (i = 0; i < n; i++) {
             pids[i] = fork();
             if ( pids[i] < 0) { 
                perror("fork"); 
                exit(-1); 
            } else if (pids[i] == 0) { 
                if (i==0){
                        lineCount = countLines(argv[1]);
                        printf("This is child proccess %d, and the number of lines is %d\n", i+1, lineCount);
                        exit(0);
                    }
                    else if (i==1){ 
                        wordCount = countWords(argv[1]);
                        printf("This is child proccess %d, and the number of words is %d\n", i+1, wordCount);
                        exit(0);
                    }
                    else if (i==2){
                        charCount += countChars(argv[1]);
                        printf("This is child proccess %d, and the number of characters is %d\n", i+1, charCount);
                        exit(0);
                    }
            } 
          }
            for(i = 0; i < n; i++){
                wait(NULL);
            }
            return 0;      
            }
 } 
}

我的问题是这样的:

The problem i have is this:

输出并不总是按顺序是这样的:

The output is not always in order, like this:

//GOOD
This is child proccess 1, and the number of lines is 5
This is child proccess 2, and the number of words is 5
This is child proccess 3, and the number of characters is 159

//BAD
This is child proccess 1, and the number of lines is 5
This is child proccess 3, and the number of characters is 159
This is child proccess 2, and the number of words is 5

第三处理前的第二有时结束。我怎么能prevent呢?

The third process ends before the second sometimes. How can i prevent this?

推荐答案

快速和肮脏的方法:

        } else if (pids[i] == 0) { 
            usleep(i*1000);  /* wait 1ms for child 1, 2ms for child 2,... */
            if (i==0){
                    lineCount = countLines(argv[1]);
                    printf("This is child proccess %d, and the number of lines is %d\n", i+1, lineCount);
                    exit(0);
                }
                else if (i==1){ 
                    wordCount = countWords(argv[1]);
                    printf("This is child proccess %d, and the number of words is %d\n", i+1, wordCount);
                    exit(0);
                }
                else if (i==2){
                    charCount += countChars(argv[1]);
                    printf("This is child proccess %d, and the number of characters is %d\n", i+1, charCount);
                    exit(0);
                }
        } 

另一种方式是通过让父进程创建三个Unix套接字,并使用选择()或类似的东西,听他们。蔡尔兹将连接并写入套接字,退出,然后,父进程将打印在正确的顺序收到的信息。

Another way would be by having the parent process to create three UNIX sockets and listen to all of them using select() or something like that. Childs would connect and write to the socket, exit, and then, parent process would print the information received in proper order.

这篇关于如何才能用C执行子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆