[英]reading characters line by line from a text file in c with multiprocessing
您好,我在阅读并尝试在多进程程序中使用fseek()
函数打印文件字符时遇到了null
字符问题。 这是我的简单代码,
#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
作为设置offset的参数。但是正如我提到的,它显示为null。 这是前景计划。
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
看看这个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;
}
它无法正常工作(我很确定当pthread同时从文件中读取文件时会发生某种冲突),但至少您会继续进行一些工作。
我得到以下输出:
一种
Ť
一种
Ť
某事
某事
某事
某事
进程4,字符= A
进程3,字符= A
进程2,字符= T
进程号1,字符= T
尾巴:未找到过程
您没有在print_screen中为str分配任何内存。 fscanf要求。
尝试类似的hack:
void print_screen(int i){
char str[256]; /* plenty large enough for this example */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.