これは c の単純な並列プログラムです。
コンパイルにはubuntuとgccを使用しています。
プログラムはプロセス数の入力を受け取り、同じ数の数を作成してユーザーに要求します。次に、各プロセスを使用して、各数値の階乗を計算します。
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
int factorial(int n)
{
int fact=1;
int i;
for(i=1 ; i<=n ; i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
int process_id;
int num_process;
int pid;
int i;
printf("\nEnter No. of Process : ");
scanf("%d",&num_process);
int num[num_process];
for ( i=1; i < num_process; i++ )
{
printf("\nEnter %d Number : ",i);
scanf("%d", &num[i]);
}
for ( i=0; i < num_process; i++ )
{
pid=fork();
if (pid == -1)
{
perror("Error forking");
return -1;
}
else if(pid > 0)
{
waitpid(-1, NULL, 0);
}
else
{
printf("\nFactorial of %d is : %d", num[i], factorial(num[i]));
exit(0);
}
}
return 0;
}
セグメンテーション違反について聞いたことがありませんが、誰かがそれが何を意味するのか説明できますか?