从具有多处理功能的c中的文本文件中逐行读取字符

时间:2012-10-09 19:55:42

标签: c file-io multiprocessing fseek

嗨我在阅读时面临null字符问题,并尝试在多进程程序中使用fseek()函数打印文件的字符。这是我的简单代码,

#include <stdio.h>     /* basic I/O routines.   */
#include <unistd.h>    /* define fork(), etc.   */
#include <sys/types.h> /* define pid_t, etc.    */
#include <sys/wait.h>  /* define wait(), etc.   */
#include <signal.h>    /* define signal(), etc. */
#include <pthread.h>
#include <time.h>

void print_screen(int i);
int counter=0;

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

    FILE* fptr;
    fptr = fopen("sample.txt","w");

    int counter = atoi(argv[1]);
    int i,k;
    int temp;
    pid_t child_pid;
    int child_status;
    char array[counter];

    srand ( time(NULL) );

    for(i=0; i<counter; i++){
        temp = rand()%4;

        if( temp==0 ) {

            fprintf(fptr,"A\n");
            array[i]='A';
            }
        else if( temp==1 ) {

            fprintf(fptr,"C\n");
            array[i]='C';
            }
        else if( temp==2 ) {

            fprintf(fptr,"G\n");
            array[i]='G';
            }
        else if( temp==3 ) {

            fprintf(fptr,"T\n");
            array[i]='T';
            }
    }
    fclose(fptr);

                for(i=1; i<=counter; i++){

                    child_pid = fork();

                       switch(child_pid) {

                        case -1:
                            printf("Error occured with fork()\n");
                            exit(1);

                        case 0: 
                            print_screen(i); /* Child Process */
                            exit(0);
                             }
            }

    wait(&child_status);
    execl("/usr/bin/killall","killall","tail",(char *) 0);
    return 0;
}

void print_screen(int i){

    char* str;
    FILE* fptr;
    fptr=fopen("sample.txt","r");

        fseek(fptr,i,SEEK_SET);
        fscanf(fptr,"%s",str);
        printf("Process Number %d, Character = %s\n",i,str);
        sleep(1);

    fclose(fptr);
    return;
    }

假设我在命令行输入./sample 10,程序将在sample.txt中打印10个字符,然后创建10个子进程,每个进程都会尝试选择一个字符并打印到屏幕上。 ,你可以看到我发送i作为参数来设置偏移量。但正如我所提到的,它打印为空。这是该计划的前景。

Process Number 7, Character = (null)
Process Number 6, Character = (null)
Process Number 5, Character = (null)
Process Number 3, Character = (null)
Process Number 8, Character = (null)
Process Number 4, Character = (null)
Process Number 9, Character = (null)
Process Number 10, Character = (null)
Process Number 2, Character = (null)
Process Number 1, Character = (null)

并且txt文件是这样的。

G
A
A
T
G
C
C
A
A
T

如果你能提供帮助和感谢,我将不胜感激。

编辑:我意识到我编译像$ gcc sample.c -o sample -lpthread它打印出null。另一方面,我编译它没有-lpthread它打印字符但不正确,例如像这是文本文件。

T
G
G
T
G

和终端给出这样的输出。

Process Number 1, Character = G
Process Number 2, Character = G
Process Number 3, Character = G
Process Number 4, Character = G
Process Number 5, Character = T

2 个答案:

答案 0 :(得分:0)

看看这个http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html

你可以知道fork()生成一个调用进程的副本,并且这个副本(几乎)与原始副本相同 - 它们只在child = 1中的fork()中有所不同

尝试(告诉我它是否正常工作,我无法测试它)。

            for(i=1; i<=counter; i++){

                child_pid = fork();

                   switch(child_pid) {

                    case -1:
                        printf("Error occured with fork()\n");
                        exit(1);
                    case 0: 
                        print_screen(i); /* Child Process */
                        exit(0);
                    default: 
                        printf("...\n");
                         }
        }

编辑:适合我

编辑:好吧我做了一些测试,应该使用if而不是case(还添加了文件内容的dirext输出 - 如果文件大小增长则应该删除);现在这就是我所拥有的代码:

#include <stdio.h>     /* basic I/O routines.   */
#include <unistd.h>    /* define fork(), etc.   */
#include <sys/types.h> /* define pid_t, etc.    */
#include <sys/wait.h>  /* define wait(), etc.   */
#include <signal.h>    /* define signal(), etc. */
#include <pthread.h>
#include <time.h>

void print_screen(int i);
int counter=0;

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

FILE* fptr;
fptr = fopen("sample.txt","w");

int counter = atoi(argv[1]);
int i,k;
int temp;
pid_t child_pid;
int child_status;
char array[counter];

srand ( time(NULL) );

for(i=0; i<counter; i++){
    temp = rand()%4;

    if( temp==0 ) {

        fprintf(fptr,"A\n");
        printf("A\n");
        array[i]='A';
        }
    else if( temp==1 ) {

        fprintf(fptr,"C\n");
        printf("C\n");
        array[i]='C';
        }
    else if( temp==2 ) {
        fprintf(fptr,"G\n");
        printf("G\n");
        array[i]='G';
        }
    else if( temp==3 ) {
        fprintf(fptr,"T\n");
        printf("T\n");
        array[i]='T';
        }
}
fclose(fptr);
            for(i=1; i<=counter; i++){

                child_pid = fork();

                    if (child_pid == -1){
                        printf("Error occured with fork()\n"); exit(-1);
                    }
                    else if (child_pid == 0){
                         print_screen(i); exit(0);
                    }
                    else{ printf("something \n"); }
        }

wait(&child_status);
execl("/usr/bin/killall","killall","tail",(char *) 0);
return 0;
}

void print_screen(int i){

char* str;
FILE* fptr;
fptr=fopen("sample.txt","r");

    fseek(fptr,i,SEEK_SET);
    fscanf(fptr,"%s",str);
    printf("Process Number %d, Character = %s\n",i,str);
    sleep(1);

fclose(fptr);
return;
}

它无法正常工作(我很确定当pthreads同时从文件中读取时会发生某种冲突),但至少你会有一些东西可以继续使用。

我得到了这个输出:

A

Ť

A

Ť

某事

某事

某事

某事

流程编号4,字符= A

流程编号3,字符= A

流程编号2,字符= T

进程编号1,字符= T

tail:没有找到进程

答案 1 :(得分:0)

您没有在打印屏幕中为字符串分配任何内存。 scanf要求。

试试黑客:

void print_screen(int i){

char str[256];  /* plenty large enough for this example */