I have a problem, I need to write a small c program that prints out a random numbers given by three processes that are printing only one number by themselfes 20 times.
output should be something like 0122102021012021120...
and something finishing from the parent process.
I only get outputs like:
00000000000000000000ready11111111111111111111readyready22222222222222222222readyreadyready
and I don't know what to do - it seems that I didn't understand the ground logic behind that fork() system ;)
my code is:
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4
5 void printfXtimes(int a, int b){
6 int i;
7 for(i=0;i<b;i++){
8 printf("%i",a);
9 sleep(1);
10 }
11 }
12
13 void main(){
14 for(int kid = 0; kid < 3; ++kid) {
15 int pid = fork();
16 if(pid < 0){
17 exit(EXIT_FAILURE);
18 }else if (pid > 0){
19 /* Parent process */
20 printf("ready");
21 }else{
22 /* Child process */
23 printfXtimes(kid,20);
24 exit(EXIT_SUCCESS);
25 }
26 for (int kid = 0; kid < 3; ++kid) {
27 int status;
28 pid_t pid = wait(&status);
29 }
30
31 }
32 }
whats wrong here? :/ The task is considered "easy"... I don't get it...