-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path1_odd-even.c
50 lines (44 loc) · 1.18 KB
/
1_odd-even.c
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
/*C Program to create 2 process one parent process must bring odd numbers up to limit n.
At the same time child process must bring even numbers up to n. Also print the process id*/
#include <stdio.h>
#include <unistd.h>
void main()
{
int limit;
printf("Enter the limit :- ");
scanf("%d",&limit);
//fork() system call returns a process ID, pid_t data type representing process ID
pid_t pid;
pid=fork();//creates a child process
//return value of fork() = 0 -> Child process
//return value of fork() > 0 -> Parent process
//return value of fork() < 0 -> Error!
if (pid==0)//child process
{
printf("--------------\n");
printf("Child process\n");
printf("--------------\n");
printf("Even numbers upto limit %d :-\n",limit);
for (int i = 0; i <= limit; ++i)
{
if(i%2==0)
printf("%d ",i);
}
printf("\n");
printf("Process ID of Child process : %d\n",getpid());
}
else
{
printf("--------------\n");
printf("Parent process\n");
printf("--------------\n");
printf("Odd numbers upto limit %d \n",limit);
for (int i = 0; i <= limit; ++i)
{
if(i%2!=0)
printf("%d ",i);
}
printf("\n");
printf("Process ID of Parent process : %d\n",getpid());
}
}