gpt4 book ai didi

c - 我的系统调用使用错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:59 24 4
gpt4 key购买 nike

我正在尝试通过分而治之来实现线性搜索,并将每个子问题交给我使用 fork 创建的新进程。

完整代码如下:

#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<unistd.h>


int PID ;

unsigned long long LinearSearchHelper(unsigned long long arr[] , unsigned long long low , unsigned long long high, unsigned long long key)
{
if(low>high) exit(0) ;
if(low==high)
exit(arr[low]==key ? low : 0 ) ;

unsigned long long mid = (low+high)/2 ;

int leftProcess = fork() ;
if(leftProcess==0){
exit(LinearSearchHelper(arr , low , mid , key)) ;
}

int rightProcess = fork() ;

if(rightProcess==0)
{
exit(LinearSearchHelper(arr , mid+1 ,high , key)) ;
}

if(leftProcess<0 || rightProcess<0){
printf("\nFAILED TO FORK PROCESS !" ) ;
exit(0) ;
}

unsigned long long stat1 , stat2 ;
waitpid(leftProcess , &stat1 , 0 ) ;
waitpid(rightProcess , &stat2 , 0 ) ;

unsigned long long l = WEXITSTATUS(stat1) ;
unsigned long long r = WEXITSTATUS(stat2) ;

exit(l!=0?l : r) ;
}


unsigned long long LinearSearch(unsigned long long arr[] , unsigned long long low , unsigned long long high, unsigned long long key)
{
int taskid = fork() ;
if(taskid<0){
printf("\nFAILED TO FORK PROCESS !" ) ;
}
int res ;
if(taskid==0){
exit(LinearSearchHelper(arr , low , high , key)) ;
}
else{
unsigned long long status ;
waitpid(taskid , &status , 0 ) ;
return WEXITSTATUS(status) ;
}
}



void fnGenRand(unsigned long long arr[] , unsigned long long n )
{
unsigned long long i =0 ;
for(i = 0 ; i < n ; i++) {
arr[i] = rand() %10000 ;
}
}



unsigned long long main(void)
{
PID = getpid() ;
unsigned long long size = 400000 ;
unsigned long long * arr = malloc(size * sizeof(unsigned long long)) ;
unsigned long long i =0 , n = 200 ;
struct timeval tv ;
double start , end ;
srand(time(NULL)) ;
FILE * fp = fopen("mergeplot.dat" ,"w") ;

unsigned long long key ;


for(i=100 ; i<300; i+=30)
{
n = i;

fnGenRand(arr , n ) ;

key = rand()%10000 ;

gettimeofday(&tv , NULL) ;
start = tv.tv_sec + (tv.tv_usec/1000000.0) ;
unsigned long long res = LinearSearch(arr , 0 , n-1 , key ) ;
gettimeofday(&tv , NULL) ;
end = tv.tv_sec + (tv.tv_usec/1000000.0) ;

fprintf(fp , "%d\t%1lf\n" , i , end-start) ;
}

fclose(fp) ;
}

问题是我想将当前数组的大小和对应的搜索时间写入文件“mergeplot.dat”。

但我面临的问题是 mergeplot.dat 文件中有很多重复的条目,我无法弄清楚哪里出了问题,为什么?

似乎有多个进程同时写入我的文件,因此出现了重复大小的条目。我 fork 并调用递归函数的方式有问题吗?

请解释我做错了什么。我是系统调用的新手。

最佳答案

您对 waitpid() 的论点是错误的。 waitpid() is declared as

#include <sys/wait.h>

pid_t waitpid(pid_t pid, int *stat_loc, int options);

请注意,第二个选项是 int *。您正在传递 waitpid() unsigned long long 的地址:

unsigned long long status ; 
waitpid(taskid , &status , 0 ) ;

那行不通。

您还可能对您的子进程可以返回的范围有疑问。进程只能提供0-255范围内的返回值。每the wait() man page :

WEXITSTATUS(wstatus)

returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should be employed only if WIFEXITED returned true.

关于c - 我的系统调用使用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51826459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com