-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfact.c
More file actions
61 lines (55 loc) · 1.1 KB
/
fact.c
File metadata and controls
61 lines (55 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
//#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
int main(int argc , char *argv[] )
{
pid_t pid;
if (argc != 2)
{
printf("arg missing or exceeding\n");
exit(0);
}
if ( atoi ( argv[1] ) <0 )
{
printf("negative number entered %d", atoi(argv[1]));
exit(0);
}
pid=fork();
if ( pid<0 )
{
printf("failed to create child\n");
exit(0);
}
else if ( pid==0 )
{
int ans = 0, i, j, k = 2, n;
n = atoi(argv[1]);
int arr[n],sum[n];
printf("%d",getpid());
arr[0] = 1;
for (i=1 ; i<n; i++)
{
arr[i] = arr[i-1]*k;
k++;
}
for (j=0; j<n; j++)
{
sum[j] = 0;
for (i=0; i<=j; i++)
{
printf(" %d ",arr[i]);
sum[j]+=arr[i];
}
printf("\n");
}
}
else
{
int p= wait(NULL);
printf("%d",p);
printf("Done\n");
}
}