0

您好,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它打印出空值一样。另一方面,我编译它时没有-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
4

2 回答 2

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(还添加了文件内容的直接输出 - 如果文件大小增长,应该删除它);现在这就是我的代码:

#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 同时从文件中读取时会发生某种冲突),但至少你会有一些东西可以继续使用。

我得到了这个输出:

一个

一个

某物

某物

某物

某物

进程号 4,字符 = A

进程号 3,字符 = A

进程号 2,字符 = T

进程号 1,字符 = T

tail:没有找到进程

于 2012-10-09T20:14:39.850 回答
0

您没有在 print_screen 中为 str 分配任何内存。fscanf 要求这样做。

尝试像这样的黑客:

void print_screen(int i){

char str[256];  /* plenty large enough for this example */
于 2012-10-10T01:07:42.690 回答